AVR定时计数器0-1-2的CTC模式彻底应用(共7页).doc
精选优质文档-倾情为你奉上/*定时计数器0,1,2的CTC模式彻底应用*FileName : CTC.cAuthor : 沧海麒麟Date : 2011.05.28Version : 1.0Clock Frequency :8 MHZDescription : 适用于ATmega16(L)AVR的3个定时计数器CTC模式 查询与中断的基本应用*/#include <iom16v.h> /常用头文件,如有需要可以查库文件include#include <avrdef.h>#include <macros.h>#include <string.h>#include <math.h>/#include "delay.c"/#include "LCM1602.c"#define uint unsigned int#define uchar unsigned char/*T/C0*/void Timer0CTC_Init(uchar temp);void Timer0CTC_NquiryMode(uchar temp);/T/C0查询方式函数void Timer0CTC_InterruptMode(void);/T/C0中断方式函数/*T/C2*/void Timer2CTC_Init(uchar temp);void Timer2CTC_NquiryMode(uchar temp);/T/C2查询方式函数void Timer2CTC_InterruptMode(void);/T/C2中断方式函数/*T/C1*/void Timer1CTC_4_Init(uchar tempH,uchar tempL);/T/C1波形产生模式:CTC4void Timer1CTC_12_Init(uchar tempH,uchar tempL);/T/C1波形产生模式:CTC12void Timer1CTC_A_NquiryMode(uchar tempH,uchar tempL);/T/C1比较匹配A查询方式函数void Timer1CTC_B_NquiryMode(uchar tempH,uchar tempL);/T/C1比较匹配B查询方式函数void Timer1CTC_InterruptMode(uchar tempH,uchar tempL);/T/C1中断方式函数void main(void) /CTC产生1000HZ的频率 /Timer0CTC_Init(124); /Timer2CTC_Init(124); /Timer1CTC_4_Init(0,124); Timer1CTC_12_Init(0,124); while (1);/*定时计数器的函数*/*与8位T/C0相关的寄存器1:T/C0计数寄存器TCNT0,输出比较寄存器OCR0,定时计数器中断屏蔽寄存器TIMSK 定时计数器中断标志寄存器TIFR,T/C0控制寄存器TCCR02:每计数一次需要的时间是(1/Xtal)*N us 在此程序中Xtal=8,N定义为与分频系数N:1,8,64,256,1024 1 : TCCR0|=0x01;/0:无时钟,系统不工作 8 : TCCR0|=0x02; 64 : TCCR0|=0x03; 256 : TCCR0|=0x04; 1024: TCCR0|=0x05; /0x06:时钟由T0引脚输入,下降沿触发 0x07:时钟由T0引脚输入,上升沿触发 */*/void Timer0CTC_Init(uchar temp)DDRB |= 0X08;TCNT0 = 0x00; TCCR0 = 0x08|0x10|0x03; /CTC设置|OC0触发方式的设置|预分频设置OCR0 = temp; /Foc0 = Fclk/(2N(1+OCR0)/*/void Timer0CTC_NquiryMode(uchar temp)/T/C0查询方式函数while(!(TIFR&0x02); TIFR=0x02;OCR0 = temp;/*/void Timer0CTC_InterruptMode(void)/T/C0中断方式函数 CLI(); /disable all interrupts TCCR0 = 0x00; /stop TCNT0 = 0x00; /set count/temp=6,N=64:2000us;计数250次 TCCR0 = 0x08|0x10|0x03; /CTC设置|OC0触发方式的设置|预分频设置 MCUCR = 0x00; GICR = 0x00; TIMSK = 0x02; /timer interrupt sources SEI(); /re-enable interrupts /SREG|=BIT(7);/开启总中断#pragma interrupt_handler timer0_comp_isr:iv_TIM0_COMP /20void timer0_comp_isr(void) /compare occured TCNT0=OCR0/*/*与8位T/C2相关的寄存器1:T/C2计数寄存器TCNT0,输出比较寄存器OCR2,定时计数器中断屏蔽寄存器TIMSK 定时计数器中断标志寄存器TIFR,T/C2控制寄存器TCCR22:每计数一次需要的时间是(1/Xtal)*N us 在此程序中Xtal=8,N定义为与分频系数N:1,8,64,256,1024 1 : TCCR2|=0x01;/0:无时钟,系统不工作 8 : TCCR2|=0x02; 32 : TCCR2|=0x03; 64 : TCCR2|=0x04; 128 : TCCR2|=0x05; 256 : TCCR2|=0x06; 1024: TCCR2|=0x07;*/*/void Timer2CTC_Init(uchar temp)DDRD |= 0X80;TCNT2 = 0x00; TCCR2 = 0x08|0x10|0x04; /CTC设置|OC2触发方式的设置|预分频设置OCR2 = temp; /Foc2 = Fclk/(2N(1+OCR2)/*/void Timer2CTC_NquiryMode(uchar temp)/T/C0查询方式函数while(!(TIFR&0x80); TIFR=0x80;OCR0 = temp;/*/void Timer2CTC_InterruptMode(void)/T/C0中断方式函数 CLI(); /disable all interrupts TCCR2 = 0x00; /stop TCNT2 = 0x00; /set count/temp=6,N=64:2000us;计数250次 TCCR2 = 0x08|0x10|0x04; /CTC设置|OC2触发方式的设置|预分频设置 MCUCR = 0x00; GICR = 0x00; TIMSK = 0x80; /timer interrupt sources SEI(); /re-enable interrupts /SREG|=BIT(7);/开启总中断#pragma interrupt_handler timer2_comp_isr:iv_TIM2_COMP /4void timer2_comp_isr(void) /compare occured TCNT2=OCR2/*/*与16位T/C1相关的寄存器1:T/C1计数寄存器TCNT1H和TCNT1L,输出比较寄存器OCR1A(H/L)和OCR1B(H/L), 定时计数器中断屏蔽寄存器TIMSK,定时计数器中断标志寄存器TIFR, T/C1控制寄存器TCCR1A和TCCR1B2:每计数一次需要的时间是(1/Xtal)*N us 在此程序中Xtal=8,N定义为与分频系数N:1,8,64,256,1024 /1 : TCCR1B=0x01;/0:无时钟,系统不工作 /8 : TCCR1B=0x02; /64 : TCCR1B=0x03; /256 : TCCR1B=0x04; /1024: TCCR1B=0x05; /0x06:时钟由T1引脚输入,下降沿驱动 0x07:时钟由T1引脚输入,上升沿驱动 */*/void Timer1CTC_4_Init(uchar tempH,uchar tempL)DDRD |= 0x30;TCNT1H = 0x00;TCNT1L = 0x00;TCCR1A = 0x50; /OC1A&OC1B的触发方式设置 TCCR1B = (0 << WGM13)|(1 << WGM12)|0x03;/(1<< WGM13)|(1 << WGM12)CTC设置|预分频设置OCR1AH = tempH; /Foc0 = Fclk/(2N(1+ICR1)OCR1AL = tempL;/*/void Timer1CTC_12_Init(uchar tempH,uchar tempL)DDRD |= 0x30;TCNT1H = 0x00;TCNT1L = 0x00;TCCR1A = 0x50; /OC1A&OC1B的触发方式设置 TCCR1B = (1 << WGM13)|(1 << WGM12)|0x03;/(0 << WGM13)|(1 << WGM12)CTC设置|预分频设置ICR1H = tempH; /Foc0 = Fclk/(2N(1+ICR1)ICR1L = tempL;/*/void Timer1CTC_A_NquiryMode(uchar tempH,uchar tempL)/T/C1比较匹配A查询方式函数while(!(TIFR&0x10); TIFR = 0x10;OCR1AH = tempH; /Foc0 = Fclk/(2N(1+OCRnA)OCR1AL = tempL;/*/void Timer1CTC_B_NquiryMode(uchar tempH,uchar tempL)/T/C1比较匹配B查询方式函数while(!(TIFR&0x08); TIFR = 0x08;OCR1BH = tempH; /Foc0 = Fclk/(2N(1+OCRnB)OCR1BL = tempL;/*/void Timer1CTC_InterruptMode(uchar tempH,uchar tempL)/T/C1中断方式函数 CLI(); /disable all interrupts TCNT1H = 0x00; TCNT1L = 0x00; TCCR1A = 0x50; /OC1A&OC1B的触发方式设置 TCCR1B = (1 << WGM13)|(1 << WGM12)|0x03;/(0 << WGM13)|(1 << WGM12)CTC设置|预分频设置 OCR1AH = tempH; /Foc0 = Fclk/(2N(1+OCRnA) OCR1AL = tempL; MCUCR = 0x00; GICR = 0x00; TIMSK = 0x18; /timer interrupt sources SEI(); /re-enable interrupts /SREG|=BIT(7);/开启总中断/*/#pragma interrupt_handler timer1_compa_isr:iv_TIM1_COMPA /7void timer1_compa_isr(void) /compare occured TCNT1=OCR1A#pragma interrupt_handler timer1_compb_isr:iv_TIM1_COMPB /8void timer1_compb_isr(void) /compare occured TCNT1=OCR1B专心-专注-专业