您的位置 首页 系统

AVR单片机的TC0守时溢出例程

程序生成向导配置TC0,溢出中断,产生1ms20ms延时。定时公式:Time=PRE*(MAX-TCNT0+1)F_cpu单位S,其中,PRE为与分频数includeiom1

程序生成导游装备TC0,溢出中止,发生1ms/20ms延时。

守时公式:Time=PRE*(MAX-TCNT0+1)/F_cpu单位S ,其间,PRE为与分频数

#include
#include
void port_init(void)
{
PORTA = 0xFF;
DDRA= 0x00;
PORTB = 0xFF;
DDRB= 0xFF;
PORTC = 0xFF; //m103 output only
DDRC= 0x00;
PORTD = 0xFF;
DDRD= 0x00;
}
//TIMER0 initialize – prescale:8/256
// WGM: Normal
// desired value: 1mSec/20ms
// actual value:1.000mSec (0.0%)/19.968ms(0.16%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
//TCNT0 = 0x83; //set count T=PRE*(MAX-TCNT0+1)/F_cpu=8*(255-130)/1MHz=1ms
TCNT0 = 0xB2; //set count T=PRE*(MAX-TCNT0+1)/F_cpu=256*(255-178+1)/1MHz=19.968ms
OCR0= 0x7D;//set compare 程序中未运用
//TCCR0 = 0x02; //start timer8分频
TCCR0 = 0x04; //start timer256分频
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
//TCNT0 = 0x83; //reload counter value 重载TCNT0,使TC0重复从0x83-0xff计数
TCNT0 = 0xB2; //reload counter value 重载TCNT0,使TC0重复从0xB2-0xff计数
PORTB^=BIT(7)|BIT(6)|BIT(5);//翻转PB6/PB7口,完成两个LED灯的1ms距离亮灭
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR= 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here…
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/259557.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部