📄 定时器.c
字号:
//ICC-AVR application builder : 2007-8-31 21:15:43
// Target : M162
// Crystal: 8.0000Mhz
#include <iom162v.h>
#include <macros.h>
const led_table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x88,0x86,0xc7,0xc4,0x83,0x8b};// 0~~f
//定义LED的数据表,注意,只有0到9是正确的,A-F没有认真写。
unsigned char i;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF;
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
PORTE = 0xFF;
DDRE = 0x00;
}
//TIMER1 initialize - prescale:1024
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1mSec
// actual value: 0.896mSec (10.4%)
void timer1_init(void)
{
TCCR1B= 0x00; //stop
TCNT1H= 0xFF; //setup
TCNT1L= 0xF9;
OCR1AH= 0x00;
OCR1AL= 0x07;
OCR1BH= 0x00;
OCR1BL= 0x07;
TCCR1A= 0x00;
TCCR1B= 0x05; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:16
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H= 0xFF; //reload counter high value
TCNT1L= 0xF9; //reload counter low value
i++;
if(i==10) i=0; //等于十时,恢复零
PORTA=led_table[i]; //这里使LED变化
}
//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;
EMCUCR = 0x00;
TIMSK= 0x80; //设定允许定时器1的TOIE1 7
ETIMSK=0x00;
GICR= 0x00;
PCMSK1=0x00;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
while(1)
;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -