📄 main._c
字号:
// ICC-AVR application builder : 2007-5-28 22:03:35
// Target : M16
// Crystal: 7.3728Mhz
// 连接:接好电源和晶振的跳线
// 7断数码管的ABCD接VCC,a~dp接PA0~PA7
#include <iom16v.h>
#include <macros.h>
const unsigned char seg7_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//0~F and "shut"
const volatile unsigned char i; //在中断中会变化的变量,加volatile限定词定义
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER1 initialize - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Sec
// actual value: 1.000Sec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x8F; //setup
TCNT1L = 0x81;
OCR1AH = 0x70;
OCR1AL = 0x7F;
OCR1BH = 0x70;
OCR1BL = 0x7F;
ICR1H = 0x70;
ICR1L = 0x7F;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIMER1_OVF
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x8F; //reload counter high value
TCNT1L = 0x81; //reload counter low value
i++; //在文件开始的定义,volatile unsigned char i;
if(i==10) i=0;
}
//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;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources ,这里允许了timer1溢出中断。
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices(); //初始化,包含了定时器的初始化
while(1)
{
PORTA=seg7_data[i]; //一直刷新显示计时
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -