如何使用PWM中的方式15?原程序如下: //ICC-AVR application builder : 2007-5-28 19:25:02 // Target : T2313 // Crystal: 7.3728Mhz
#include <iot2313v.h> #include <macros.h> #pragma ctask timer1_init,port_init,uart0_init,init_devices,interrupt_handler timer1_capt_isr
unsigned char icpl,icph,flag; void port_init(void) { PORTA = 0x00; DDRA = 0x00; PORTB = 0x00; DDRB = 0x10; PORTD = 0x00; DDRD = 0x3C; }
//TIMER1 initialize - prescale:1 // WGM: 15) PWM fast, TOP=OCRnA // desired value: 550uSec // actual value: 549.995uSec (0.0%)
void timer1_init(void) { TCCR1B = 0x00; //stop timer TCNT1H = 0x00; //set count value TCNT1L = 0x00; OCR1AH = 0xFF; //set compare value OCR1AL = 0xFF; OCR1BH = 0xF0; //set compare value OCR1BL = 0x29; TCCR1A = 0x23; TCCR1B = 0x99; //start Timer
}
#pragma interrupt_handler timer1_capt_isr:4 void timer1_capt_isr(void) { //timer 1 input capture event, read (int)value in ICR1 using; if(!flag) { icpl=ICR1L; //Read low byte first (important) icph=ICR1H; //Read high byte and shift into top byte flag=1; } }
/* #pragma interrupt_handler timer1_compb_isr:13 void timer1_compb_isr(void) { //compare occured TCNT1=OCR1A }
#pragma interrupt_handler timer1_ovf_isr:6 void timer1_ovf_isr(void) { //TIMER1 has overflowed TCNT1H = 0xF0; //reload counter high value TCNT1L = 0x2A; //reload counter low value } */
//UART0 initialize // desired baud rate: 19200 // actual: baud rate:19200 (0.0%) void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = 0x06; UBRRH = 0x00; //set baud rate upper UBRRL = 0x17; //set baud rate lower UCSRB = 0x18; //enable }
//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(); uart0_init();
MCUCR = 0x00; GIMSK = 0x00; TIMSK = 0x08; SEI(); //re-enable interrupts //all peripherals are now initialized }
// void main(void) { init_devices(); //insert your functional code here... while(1) { if(flag) { while(!(UCSRA&0x20)); UDR=icph; while(!(UCSRA&0x20)); UDR=icpl; flag=0; }
}
}
软件仿真调试时虽然设置的还是模式15,但是却变成模式9下工作,硬件运行时用示波器观察波形周期,也是在模式9下工作.OCRA和OCRB的高6位被舍弃一样,为什么? |