#include "Head.h"
void main (void)
{
OSCCON=0b11110000;
TRISD=0;
PORTD=0;
TRISC=0b10000000;
OPTION_REG=0b10001000;
INTCON=0b11100000;
PIE1=0b00100000;//开串口中断
CCP1CON=0b00001100;
PR2=255;
T2CON=0b00000100;// 最大脉冲宽度 4*(255+1)=1024
TXSTA=0b00100100; //SYNC=0;BRGH=1;
RCSTA=0b10010000;
BAUDCON=0b00001000; //BRG16=1;
SPBRGH=0;
SPBRG=207; //FOSC/[4(n+1)] (32000000/38400)/4-1=207
while(1)
{
Send();
}
}
//定时器中断程序
void interrupt TMR0INT(void)//4/32*256=32us
{
static unsigned char a=0,b=0,c=0;
static unsigned int pwm_duty=0;
const unsigned int pwm_1[PI_data]={0,13,25,37,50,62,74,85,96,107,118,127,137,146,154,162,
169,175,181,186,190,194,196,198,200,200,200,198,196,194,190,186,
181,175,169,162,154,146,137,127,118,107,96,85,74,62,50,37,
25,13};
const unsigned int pwm_2[PI_data]={0,31,63,94,124,155,184,213,241,268,294,319,342,364,385,405,
422,438,452,465,476,484,491,496,499,500,499,496,491,484,476,465,
452,438,422,405,385,364,342,319,294,268,241,213,184,155,124,94,
63,31};
const unsigned int pwm_3[PI_data]={0,63,125,187,249,309,368,426,482,536,588,637,685,729,771,809,
844,876,905,930,951,969,982,992,998,1000,998,992,982,969,951,930,
905,876,844,809,771,729,685,637,588,536,482,426,368,309,249,187,
125,63};
if(T0IF)
{
T0IF=0;
LATD6^=1;
if(++a==Pwm_frequency) //1000000/(Pwm_frequency*32*50)=频率HZ 1000000/(6*32*50)=104HZ
{
a=0;
if(++b==PI_data)b=0;
switch(c)
{
case 1: {pwm_duty=pwm_1;break;}
case 2: {pwm_duty=pwm_2;break;}
case 3: {pwm_duty=pwm_3;break;}
default: {pwm_duty=pwm_1;break;}
} CCP1CON=(CCP1CON&0xcf)|((pwm_duty&3)<<4);CCPR1L=pwm_duty>>2;
}
}
if(RCIF)
{
static unsigned char temp=0;
if(OERR==1){CREN=0;asm("nop");CREN=1;}
temp=RCREG;
switch(temp)
{
case 'a':{Pwm_frequency++;break;}//频率加
case 'b':{if(Pwm_frequency)Pwm_frequency--;break;}//频率减
case 'c':{c++;if(c>3)c=1;break;}//振动幅度
}
}
}