完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
各位大神们,小弟这有一spwm程序,忘大神注释这个程序功能的实现,万分感谢!
//ICC-AVR application builder : 2014/5/28 17:11:50 // Target : M8 // Crystal: 8.0000Mhz #include #include unsigned char i=0,k=0,out_date=0; /*unsigned char timer_long[40]= { 0x01,0x39, ;此段为测试输出,实际中不用 0x02,0x64, 0x03,0x84, 0x04,0x94, 0x05,0x86, 0x06,0x52, 0x06,0xf6, 0x07,0x6e, 0x07,0xb7, 0x07,0xd0, 0x07,0xd0, 0x07,0xb7, 0x07,0x6e, 0x06,0xf6, 0x06,0x52, 0x05,0x86, 0x04,0x94, 0x03,0x84, 0x02,0x64, 0x01,0x39 };*/ unsigned char timer_long[20]={ 0x01,0x37,//0x35, 0x02,0xc5,//0xc3, 0x03,0x7d,//0x7b, 0x03,0xb9,//0xb7, 0x03,0xdd,//0xdb, 0x03,0xdd,//0xdb, x03,0xb9,//0xb7, 0x03,0x7d,//0x7b, 0x02,0xc5,//0xc3, 0x01,0x37,//0x35, }; void port_init(void) { PORTB = 0xff; DDRB = 0xFF; PORTC = 0x00; //m103 output only DDRC = 0x00; PORTD = 0x00; DDRD = 0x00; } //TIMER1 initialize - prescale:1 // WGM: 0) Normal, TOP=0xFFFF // desired value: 0.125uSec // actual value: 0.125uSec (0.0%) void timer1_init(void) { TCCR1B = 0x00; //stop TCNT1H = 0xFF; //setup TCNT1L = 0xFF; OCR1AH = 0x00; OCR1AL = 0x01; OCR1BH = 0x00; OCR1BL = 0x01; ICR1H = 0x00; ICR1L = 0x01; TCCR1A = 0x00; TCCR1B = 0x01; //start Timer } #pragma interrupt_handler timer1_ovf_isr:9 void timer1_ovf_isr(void) { //TIMER1 has overflowed k++; if(k==2)k=0; if(k==1){ TCNT1H = 0xff-timer_long[2*i]; //reload counter high value TCNT1L = 0xff-timer_long[2*i+1]; //reload counter low value if(out_date==0) {PORTB =PORTB|0x01;PORTB =PORTB&0xfd;} else { PORTB =PORTB|0x02;PORTB =PORTB&0xfe;} i++; } else{ TCNT1H =0xfc+timer_long[2*i]; //reload counter high value TCNT1L =0xff+timer_long[2*i+1]; //reload counter low value //TCNT1L =0x19+timer_long[2*i+1]; //reload counter low value // TCNT1H =0xf8+timer_long[2*i]; //reload counter high value;16M // TCNT1L =0x2f+timer_long[2*i+1]; //reload counter low value //TCNT1H =0xe0+8*timer_long[2*i]; //reload counter high value // TCNT1L =0xbf+8*timer_long[2*i+1]; //reload counter low value if(out_date==0) {PORTB =PORTB&0xfe;PORTB =PORTB&0xfd;} else { PORTB =PORTB&0xfd;PORTB =PORTB&0xfe;} } if(i==10){i=0;out_date++;if(out_date==2){out_date=0;}; }; } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer1_init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x04; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } // void main(void) { init_devices(); //insert your functional code here... } |
|
相关推荐
2 个讨论
|
|
自己顶一下!..........
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
AVR Atmega16 Bootloader程序与上位机LabView程序
5122 浏览 6 评论
#include <ioavr.h>这个头文件我应该下什么编译器
7767 浏览 0 评论
3026 浏览 2 评论
3103 浏览 1 评论
10053 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 04:39 , Processed in 0.366539 second(s), Total 43, Slave 33 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号