本例使用T/C2定时器工作于异步模式,由PB6(TOSC1)与PB7(TOSC)外接32768Hz晶振提供时钟,通过设置异步状态可选择T/C2的时钟源,T/C2的时钟频率为32768Hz/64 = 512Hz
以下是Proteus截图:
以下是Studio6.2溢出中断:
程序清单:
/*
* GccApplication7.c
*
* Created: 2014-10-31 21:15:59
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
const uint8_t SEG_CODE[] = {0x3F,0x06,0x58,0x4F,0x66,0x6D,0x7d,0x07,0x7F,0x6F,0x00};
uint8_t Disp_Buffer[] = {0,0,0x40,0,0,0x40,0,0};
uint8_t Disp_Idx;
uint8_t Key_State = 0xFF;
uint8_t h,m,s;
void Increase_Hour()
{
if(++h >23) h = 0;
Disp_Buffer[0] = SEG_CODE[h/10];
Disp_Buffer[1] = SEG_CODE[h % 10];
}
void Increase_Minute()
{
if(++m > 59)
{
m = 0; Increase_Hour();
}
Disp_Buffer[3] = SEG_CODE[m/10];
Disp_Buffer[4] = SEG_CODE[m % 10];
}
void Increase_Second()
{
if(++s >59)
{
s = 0; Increase_Minute();
}
Disp_Buffer[6] = SEG_CODE[s/10];
Disp_Buffer[7] = SEG_CODE[s % 10];
}
int main(void)
{
DDRA = 0xFF; PORTA = 0xFF;
DDRD = 0xFF; PORTD = 0xFF;
DDRB = 0x00; PORTB = 0xFF;
TCCR0 = 0x03;
TCNT0 = 256 - F_CPU/64.0 * 0.004;
ASSR = 0x08;
TCCR2 = 0x04;
TCNT2 = 0;
TIMSK = _BV(TOIE2) | _BV(TOIE0);
h = 12; m=s=0;
Disp_Buffer[0] = SEG_CODE[h/10];
Disp_Buffer[1] = SEG_CODE[h%10];
Disp_Buffer[3] = SEG_CODE[m/10];
Disp_Buffer[4] = SEG_CODE[m %10];
Disp_Buffer[6] = SEG_CODE[s/10];
Disp_Buffer[7] = SEG_CODE[s%10];
sei();
while(1)
{
if(PINB ^Key_State)
{
_delay_ms(10);
if(PINB ^ Key_State)
{
Key_State = PINB;
if(!(Key_State&_BV(PB3)))
Increase_Hour();
else
if(!(Key_State&_BV(PB6)))
Increase_Minute();
}
}
}
}
ISR(TIMER0_OVF_vect)
{
static uint8_t Disp_Idx = 0;
TCNT0 = 256 - F_CPU/64.0*0.004;
PORTA = 0x00;
PORTA = Disp_Buffer[Disp_Idx];
PORTD= ~_BV(Disp_Idx);
Disp_Idx = (Disp_Idx+1)&0x07;
}
ISR(TIMER2_OVF_vect)
{
if(Disp_Buffer[2] == 0x40)
{
Disp_Buffer[2] = Disp_Buffer[5] = 0x00;
}
else
{
Disp_Buffer[2] = Disp_Buffer[5] = 0x40;
Increase_Second();
}
}
|