PIC16F7X正弦程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//------------------------------------------------------------//spwm信号调制//------------------------------------------------------------#includepic16f7x.h#includepic.h//系统配置__CONFIG(HS&PWRTEN&BOREN&PROTECT&WDTEN);//打开看门狗,选择高速晶振,上电延时复位,掉电复位使能,代码保护//------------------------------------------------------------//指示灯宏定义#defineL1_RED_ONRB1=1;RB2=0;//L1红灯亮#defineL1_OFFRB1=0;RB2=0;#defineL2_RED_ONRB4=1;RB3=0;//L2红灯亮#defineL2_OFFRB4=0;RB3=0;#defineL3_RED_ONRB7=1;RB5=0;//L3红灯亮#defineL3_OFFRB7=0;RB5=0;#defineL1_GREE_ONRB2=1;RB1=0;//L1绿灯亮#defineL2_GREE_ONRB3=1;RB4=0;//L2绿灯亮#defineL3_GREE_ONRB5=1;RB7=0;//L3绿灯亮//-----------------------------------------------------------//模拟输入通道宏定义#defineAN0CHS2=0;CHS1=0;CHS0=0;//ADCON0寄存器定义,选择通道AN0#defineAN1CHS2=0;CHS1=0;CHS0=1;//ADCON0寄存器定义,选择通道AN1#defineAN2CHS2=0;CHS1=1;CHS0=0;//ADCON0寄存器定义,选择通道AN2//-----------------------------------------------------------//-----------------------------------------------------------voidadc_init(){//ADCS1:ADCS0:A/D转换时钟选择位//10Fosc/32外部晶振32分频ADCS1=1;//FOSC/32ADCS0=0;/*IFCPUISPIC16F7X7VCFG1=0;VCFG0=0;//vref=vdd-vss*///PCFG2:PCFG0:A/D端口配置控制位//000A端口0,1,2,3,5为A/D模拟输入PCFG0=0;PCFG1=0;PCFG2=0;//VREF=VDD-VSSADON=1;//打开ad模块ADIF=0;//清除ad中断标志ADIE=0;//打开ad模块中断}//-----------------------------------------------------------voidport_init()//端口初始化{//端口b设置/*TRISB0=0;TRISB1=0;TRISB2=0;TRISB3=0;TRISB4=0;TRISB5=0;//RB0||RB5输出TRISB6=1;//输入TRISB7=0;//RB7输出*/TRISB=0x40;//01000000RB6为输入,其他为输出PORTB=0;//输出0RBPU=0;//由端口的各个锁存值使能PORTB上拉INTE=0;//禁止RB0/INT外部中断RBIF=0;//RB7:RB4引脚的电平状态没有变化(RB端口电平变化中断标志位)RBIE=1;//允许RB端口电平变化中断//端口c设置TRISC=0X00;//PORTCareoutputsPORTC=0;//输出为0RC1=RC2=0;RC3=1;RC4=0;//timer0initT0CS=0;//选择内部指令时钟(可能为外部时钟4分频)T0IF=0;//TIMER0溢出标志位清零//T0IE=1;//在第一次ccp1比较后打开中断使能//timer1TMR1CS=0;//使用内部时钟(FOSC/4)}//----------------------------------------------------------//----------------------------------------------------------//软件延时子程序*/voidDELAY(){unsignedinti;for(i=2000;i0;i--);}//----------------------------------------------------------voidinit_start()//开始{L1_RED_ON;L2_RED_ON;L3_RED_ON;DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();L2_OFF;L3_OFF;L1_OFF;DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();L1_GREE_ON;L2_GREE_ON;L3_GREE_ON;DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();DELAY();L2_OFF;L3_OFF;L1_OFF;}//----------------------------------------------------------bitk,power;//关机及按键标志bank1unsignedchartm_sum;//系统实际变量bank1unsignedcharad_res_1,ad_res_0,ad_res_2;//ad转换结果寄存器bank1floatsin_am,sin_l,sin_d;//浮点数,幅值变量,临时变量,临时变量bitsin_up;//sin函数正负半周标志bittmr_s,tmr_d,crut_ie;//定时标志,定时标志变化标志,电流延时保护允许unsignedcharcrut_tmr;//电流延时时间变量unsignedcharpower_s;//电源动作延时变量bitpower_ie;//电源允许动作标志bitpower_up;//初次开机标志unsignedcharpower_up_s;//初次开机延时标志bitcrut_if;volue_if;//-----------AD1通道转换--------------------voidad_0(){AN0;//选择通道0DELAY();//延时,采样电容充电ADGO=1;//开器ADwhile(ADGO);//等待ad结束ad_res_0=ADRES;//结果转存到变量AD_RES_0}//-----------AD1通道转换--------------------voidad_1(){AN1;DELAY();ADGO=1;while(ADGO);ad_res_1=ADRES;if(ad_res_1132){sin_am-=0.005;if((ad_res_1-132)10)sin_am-=0.04;}elseif((ad_res_1=132)&&(ad_res_1=130))sin_am+=0;elseif(ad_res_1130){sin_am+=0.005;if((130-ad_res_1)10)sin_am+=0.04;}if(sin_am1.6)sin_am=1.6;}//------------AD2通道转换-------------------voidad_2(){AN2;DELAY();ADGO=1;while(ADGO);ad_res_2=ADRES;}//-------------------------------------------constunsignedcharsin_[]={40,50,73,85,100,113,127,141,157,170,180,189,196,200,203,204,//正半周204,//负半周203,200,195,188,179,169,157,144,129,113,96,78,59,39,15};unsignedcharsin_num;//SIN函数表查表变量//-------------------------------------------------------------------------------voidCCP_start(){CCPR2L=0X0;//设置CCP2,0%的脉宽输出CCPR1L=0X0;//设置CCP1,0%的脉宽输出TRISC=0X00;//PORTCareoutputsPR2=0Xff;//设置PWM的工作周期,16Mhz,PWM周期15.562khzCCP1M3=1;CCP1M2=1;//CCP1模块PWM模式CCP2M3=1;CCP2M2=1;//CCP2模块PWM模式sin_up=1;//正负半周SIN函数sin_num=0;//脉宽周期调整计数器sin_am=0;//sin函数的幅值sin_am=0.3000;//sin函数的幅值crut_ie=1;//电流慢保护允许}//-------------中断服务程序------------------------------------voidinterruptkey_ccp_timer(){if(TMR2IF&TMR2IE){TMR2IF=0;//定时器2中断服务函数if(sin_num==31){sin_num=0;sin_up=!sin_up;}sin_d=sin_[sin_num];sin_l=si

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功