完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include
#include #define SYSCLK 12000000 int temp; int fx=50; ***it p0out=P0^0; int N; int D=10000; int FBASE=700; int pwm_i; int c_temp; int pwm_angle; int FF; int valu; int PWM_HIGH; int PWM_LOW; float pwm_sin; float M=0.9; char s_temp; float code sinx[]= { 0, 175, 349, 523, 698, 872,1045,1219,1392,1564,1737,1908,2079,2250, 2419,2588,2756,2924,3090,3256,3420,3584,3746,3907,4067,4226,4384,4540, 4695,4848,5000,5150,5299,5446,5592,5736,5878,6018,6157,6293,6428,6561, 6691,6820,6947,7071,7193,7313,7431,7547,7660,7772,7880,7986,8090,8192, 8290,8387,8480,8572,8660,8746,8830,8910,8988,9063,9136,9205,9272,9336, 9397,9455,9511,9563,9613,9659,9703,9744,9782,9816,9848,9877,9903,9926, 9945,9962,9976,9986,9994,9999,10000 }; void SYSCLK_Init (void) { int i; // delay counter OSCXCN = 0x67; // start external oscillator with // 18.432MHz crystal for (i=0; i < 256; i++) ; // Wait for osc. to start up while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle OSCICN = 0x88; // select external oscillator as SYSCLK // source and enable missing clock // detector } void PCA_Init() { PCA0MD = 0x09; PCA0CPM0 = 0x4D; PCA0CPL0 = (0xff & (PWM_HIGH+valu)); // 初始化PCA比较值 PCA0CPH0 = (0xff & ((PWM_HIGH+valu) >> 8)); PCA0CN = 0x40;//允许PCA计数 } void Port_IO_Init() { P0MDOUT = 0x01; XBR0 = 0x18; XBR2 = 0x40; } void Interrupts_Init() { IE = 0x80; EIE1 = 0x08; EIP1 = 0x08; } void main (void) { WDTCN = 0xde; WDTCN = 0xad; FF=SYSCLK/FBASE; valu=65536-FF; N=FBASE/fx; PWM_HIGH=(float)FF/2; PWM_LOW=FF-PWM_HIGH; pwm_i=0; pwm_angle=0; pwm_sin=0; //pwm_sin为规则采样法时三角载波的底点处的正弦值 Port_IO_Init(); SYSCLK_Init (); Interrupts_Init(); PCA_Init(); while(1){ }; } void PCA_ISR (void) interrupt 9 { if (CCF0) { CCF0 = 0; // 清除比较标志 if (p0out) { // 处理上升沿 PCA0CPM0 = 0x4c; if(pwm_i==N) { pwm_i=0; } pwm_i++; pwm_angle=(pwm_i*180)/N; //规则采样,计算在每个三角波的顶点或者底点处的角度 if(pwm_angle<=90) //用查表法计算正弦波的值;根据正弦值区间不同分类讨论 { pwm_sin=sinx[pwm_angle]/(D);} // else if(pwm_angle<=180) {pwm_sin=sinx[180-pwm_angle]/(D);} // else if(pwm_angle<=270) //{ pwm_sin=-sinx[pwm_angle-180]/(D);} // // else //{pwm_sin=-sinx[360-pwm_angle]/(D);} PWM_HIGH=((float)FF*(1+M*pwm_sin))/2; c_temp = valu; c_temp +=PWM_HIGH; temp = c_temp ; PCA0CPL0 =temp; s_temp= (0xff & (temp >> 8)); PCA0CPH0 =s_temp; PCA0CPM0 = 0x4D; } else { PCA0CPM0 = 0x4C; PWM_LOW=FF-PWM_HIGH; temp = (PCA0CPH0 << 8) | PCA0CPL0; temp = PWM_LOW+valu; PCA0CPL0 =(0xff & temp); PCA0CPH0 = (0xff & (temp >> 8)); PCA0CPM0 = 0x4D; } } if (CF) { CF = 0; //valu=valu-0xe7; PCA0L=0xFF & (valu-4); PCA0H=(0xFF & (valu>>8)); } } 哪位大神能告诉我这些程序的作用,我是C8051单片机的初学者,希望向大家多多学习 |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
飞凌嵌入式ElfBoard EL 1板卡-i2c与从设备通讯编程示例之i2c-tools工具使用
652 浏览 0 评论
stc15f2k60s2利用串口传输字模存储到eeprom并进行点阵显示
696 浏览 1 评论
1264 浏览 0 评论
588 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-串口通讯编程示例之串口编写程序
1190 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11598 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-3 06:51 , Processed in 0.441484 second(s), Total 75, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号