📄 main._c
字号:
// ICC-AVR application builder : 2007-5-29 15:21:23
// Target : M16
// Crystal: 7.3728Mhz
// 用途:演示定时器的计数功能
// 作者:古欣 AVR 与虚拟仪器 www.avrvi.com
// 连接:接好电源和晶振的跳线
// 7断数码管的ABCD接VCC,a~dp接PA0~PA7
// PB0接一个独立按键
#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"
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x01; //PB0,是TIMER0的外部时钟输入脚(T0),需要设为输入,并且使能内部上拉
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:Falling edge
// WGM: Normal
// desired value: 1KHz
// actual value: Out of range
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x00; //set count
OCR0 = 0x0A; //set compare十进制的十,十次按键后匹配,进入这里
TCCR0 = 0x06; //start timer 时钟由T0 引脚输入,下降沿触发
}
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
//compare occured TCNT0=OCR0
//按下键OCR0次后,会进入本中断
TCNT0 = 0x00;
}
//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 = 0x02; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
while(1)
{
PORTA = seg7_data[TCNT0]; //一直显示TCNT0的值
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -