zoukankan      html  css  js  c++  java
  • PIC16F877A的50HZ正弦波

    View Code
     1 #include <pic.h>//包含单片机内部资源预定义
     2 #include <math.h>
     3 
     4 unsigned char const sin_32[]={104,109,114,119,124,128,133,137,142,146,150,154,157,161,164,//15
     5 167,169,172,174,176,177,178,179,180,180,180,180,179,178,176,175,173,171,168,165,//20
     6 162,159,156,152,148,144,140,135,131,126,121,116,111,106,101,96,91,86,81,76,72,67,63,//23
     7 58,54,50,46,43,39,36,33,31,28,26,24,23,22,21,20,20,20,20,21,22,24,25,27,29,32,35,38,//42
     8 41,44,48,52,56,60,65,69,74,79,84,89,94,99
     9 };//浮点型精度高,换其他会出现尖角
    10 
    11 void work0();
    12 unsigned char set_pwm;
    13 void main(void)
    14 {    
    15     work0();
    16     while(1)
    17     {
    18     }
    19 }
    20 
    21 void work0()//m=0.8;T=(PR2+1)*4*f*TIMR2分频
    22 {
    23     //CCPR1L=0X00;//设置CCP1,0%的脉宽输出
    24     TRISC=0X00;//PORTC are outputs
    25     CCP1CON=0x0C;//0000 1100,设置CCP1为PWM工作方式,且工作循环位的低两位为00
    26     PR2=199;//设置PWM周期:(PR2+1)*4*Tosc*(TMR2预分频数1)=200u(5k)
    27     T2CON=0x04;//Tc赋值=T*D=(PR2+1)*D=200*D
    28     CCP1IE=1;
    29     INTCON=0xC0;//GIE=1,开总中断,开启第一外设中断外围功能模块中断
    30     TMR2IE=1;TMR2IF=0;
    31 }
    32 
    33 //interrupt//
    34 void interrupt pic(void)
    35 {
    36     if(TMR2IF&TMR2IE)
    37     {  
    38         set_pwm++;
    39         TMR2IF=0;CCP1IF=0;
    40         CCPR1L=sin_32[set_pwm];
    41         if(set_pwm==99) set_pwm=0;
    42     }
    43 }
  • 相关阅读:
    线性表的相关操作
    jq实现登陆页面的拖拽功能
    js实现登陆页面的拖拽功能
    HTML5存储
    js函数声明
    js打字机效果实现
    js日期显示效果
    js实现倒计时效果
    js中如何去获取外部css样式
    面向对象的几种方法详解(后)
  • 原文地址:https://www.cnblogs.com/wangh0802PositiveANDupward/p/2633416.html
Copyright © 2011-2022 走看看