#include
<
iom8v.h
>
#include < macros.h >
#define LED_BIT_SELECT PORTC
#define LED_BIT_DATA PORTD
/**/ /**********************
* PC0~3 ------- G1~4 *
* PD0~7 ------- a~h *
* PB0 ---------- K1 *
* PB1 ---------- K2 *
///
利用计数溢出中断做的简单秒表
K1开始、暂停
k2复位
G1~4为数码管选位
a~h为数码管数据
**********************/
UINT16 LED_TEXT[ 24 ] = ... {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71,
0x73, 0x38, 0x76, 0x3E, 0x50, 0x08, 0x40, 0x00} ;
void Device_Init( void )
... {
Port_Init();
//stop errant interrupts until set up
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void Port_Init( void )
... {
DDRB = 0x00;
DDRD = 0xFF;
DDRC = 0xFF;
PORTD = 0x00;
PORTC = 0x0F;
PORTB = 0xFF;
}
void Show_Led(UINT16 wNum)
... {
UINT8 chBit;
for(chBit = 0x08; chBit > 0; chBit >>= 1)
...{
LED_BIT_DATA = ~LED_TEXT[wNum % 10];
LED_BIT_SELECT = chBit;
wNum /= 10;
if(wNum == 0)
...{
return;
}
}
}
void timer0_init( void )
... {
TCCR0 = 0x00; //stop
TCNT0 = 0x83; //set count
TCCR0 = 0x03; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
UINT16 g_Time_Count;
void Count_Increase( void ) // 时间增加一个单位
... {
g_Time_Count++;
if(g_Time_Count > 9999) //限制最大数额
...{
g_Time_Count = 0;
}
}
void Count_Clear( void ) // 计时器复位
... {
timer0_init(); //计数器复位
g_Time_Count = 0; //时间累计复位
}
void Pause_Or_Start( void ) // 暂停、继续
... {
static UINT8 state = 0; //保存先前的功能数据,决定本次功能是继续还是暂停
state = !state; //置反
if (state) //如果是暂停
...{
CLI(); //使所有中断失效
}
else //如果是继续
...{
SEI(); //重新处理中断
}
}
void timer0_ovf_isr( void ) // 计数中断程序
... {
static UINT16 wCount = 0;
TCNT0 = 0x83; //reload counter value
if(wCount >= 1000) //当时间为1s时
...{
wCount = 0; //复位
Count_Increase(); //时间累计数加一
}
wCount++;
}
void main()
... {
g_Time_Count = 0; //初始化
Device_Init();
while(1)
...{
if (KEY_DOWN1) //如果暂停、继续键被按下
...{
Delay_MS(10); //延迟以消除抖动
if (KEY_DOWN1)
...{
Pause_Or_Start(); //执行开始暂停功能
while(KEY_DOWN1)
...{}
}
}
if(KEY_DOWN2) //如果复位键被按下
...{
Delay_MS(10);
if (KEY_DOWN2)
...{
Count_Clear(); //计时累计清零
while(KEY_DOWN2)
...{}
}
}
Show_Led(g_Time_Count); //显示累计时间
}
}
#include < macros.h >
#define LED_BIT_SELECT PORTC
#define LED_BIT_DATA PORTD
/**/ /**********************
* PC0~3 ------- G1~4 *
* PD0~7 ------- a~h *
* PB0 ---------- K1 *
* PB1 ---------- K2 *
///
利用计数溢出中断做的简单秒表
K1开始、暂停
k2复位
G1~4为数码管选位
a~h为数码管数据
**********************/
UINT16 LED_TEXT[ 24 ] = ... {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71,
0x73, 0x38, 0x76, 0x3E, 0x50, 0x08, 0x40, 0x00} ;
void Device_Init( void )
... {
Port_Init();
//stop errant interrupts until set up
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void Port_Init( void )
... {
DDRB = 0x00;
DDRD = 0xFF;
DDRC = 0xFF;
PORTD = 0x00;
PORTC = 0x0F;
PORTB = 0xFF;
}
void Show_Led(UINT16 wNum)
... {
UINT8 chBit;
for(chBit = 0x08; chBit > 0; chBit >>= 1)
...{
LED_BIT_DATA = ~LED_TEXT[wNum % 10];
LED_BIT_SELECT = chBit;
wNum /= 10;
if(wNum == 0)
...{
return;
}
}
}
void timer0_init( void )
... {
TCCR0 = 0x00; //stop
TCNT0 = 0x83; //set count
TCCR0 = 0x03; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
UINT16 g_Time_Count;
void Count_Increase( void ) // 时间增加一个单位
... {
g_Time_Count++;
if(g_Time_Count > 9999) //限制最大数额
...{
g_Time_Count = 0;
}
}
void Count_Clear( void ) // 计时器复位
... {
timer0_init(); //计数器复位
g_Time_Count = 0; //时间累计复位
}
void Pause_Or_Start( void ) // 暂停、继续
... {
static UINT8 state = 0; //保存先前的功能数据,决定本次功能是继续还是暂停
state = !state; //置反
if (state) //如果是暂停
...{
CLI(); //使所有中断失效
}
else //如果是继续
...{
SEI(); //重新处理中断
}
}
void timer0_ovf_isr( void ) // 计数中断程序
... {
static UINT16 wCount = 0;
TCNT0 = 0x83; //reload counter value
if(wCount >= 1000) //当时间为1s时
...{
wCount = 0; //复位
Count_Increase(); //时间累计数加一
}
wCount++;
}
void main()
... {
g_Time_Count = 0; //初始化
Device_Init();
while(1)
...{
if (KEY_DOWN1) //如果暂停、继续键被按下
...{
Delay_MS(10); //延迟以消除抖动
if (KEY_DOWN1)
...{
Pause_Or_Start(); //执行开始暂停功能
while(KEY_DOWN1)
...{}
}
}
if(KEY_DOWN2) //如果复位键被按下
...{
Delay_MS(10);
if (KEY_DOWN2)
...{
Count_Clear(); //计时累计清零
while(KEY_DOWN2)
...{}
}
}
Show_Led(g_Time_Count); //显示累计时间
}
}