#include <pic.h>//包含单片机内部资源预定义
#include <math.h>
unsigned char const sin_32[]={104,109,114,119,124,128,133,137,142,146,150,154,157,161,164,//15
167,169,172,174,176,177,178,179,180,180,180,180,179,178,176,175,173,171,168,165,//20
162,159,156,152,148,144,140,135,131,126,121,116,111,106,101,96,91,86,81,76,72,67,63,//23
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
41,44,48,52,56,60,65,69,74,79,84,89,94,99
};//浮点型精度高,换其他会出现尖角
void work0();
unsigned char set_pwm;
void main(void)
{
work0();
while(1)
{
}
}
void work0()//m=0.8;T=(PR2+1)*4*f*TIMR2分频
{
//CCPR1L=0X00;//设置CCP1,0%的脉宽输出
TRISC=0X00;//PORTC are outputs
CCP1CON=0x0C;//0000 1100,设置CCP1为PWM工作方式,且工作循环位的低两位为00
PR2=199;//设置PWM周期:(PR2+1)*4*Tosc*(TMR2预分频数1)=200u(5k)
T2CON=0x04;//Tc赋值=T*D=(PR2+1)*D=200*D
CCP1IE=1;
INTCON=0xC0;//GIE=1,开总中断,开启第一外设中断外围功能模块中断
TMR2IE=1;TMR2IF=0;
}
//interrupt//
void interrupt pic(void)
{
if(TMR2IF&TMR2IE)
{
set_pwm++;
TMR2IF=0;CCP1IF=0;
CCPR1L=sin_32[set_pwm];
if(set_pwm==99) set_pwm=0;
}
}