⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 avr单片机atmega8系列定时器的使用测试程序
💻 C
字号:
//ICC-AVR application builder : 2005-2-24 22:21:34
// Target : M8
// Crystal: 8.0000Mhz

#include <iom8v.h>
#include <macros.h>

int  i;

//  初始化timer0, 1  ms
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0xE1; //set count
 TCCR0 = 0x04; //start timer
}

//  timer0 中断
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
 TCNT0 = 0xE1;     //reload counter value
 if(++i == 1000){  //  1 秒
    i = 0;
    PORTB ^= 0x02; //  端口取反
 }
}

//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 DDRB  = 0x02;    //  设置 PB1 为输出
 timer0_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x01; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

//
void main(void)
{
 init_devices();
 //insert your functional code here...
 for(;;);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -