100000秒以内记数器。
本例程是用外部中断,按一下开始记数,再按一下停止,再按一下清零。
以下是Proteus的运行截图:
以下是Studio6.2的程序的截图:
程序清单:
/*
* GccApplication5.c
*
* Created: 2014-10-29 20:19:55
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
const uint8_t SEG_CODE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uint8_t Digits_Buffer[] = {0,0,0,0,0,0};
void Show_Count_ON_DSY()
{
uint8_t i;
for(i=0;i<=5;i++)
{
PORTC = 0x00;
PORTA = ~_BV(5-i);
PORTC = SEG_CODE[Digits_Buffer[i]];
if(i==1)PORTC |= 0x80;
_delay_ms(3);
}
}
int main(void)
{
DDRA = 0xFF;PORTA = 0xFF;
DDRC = 0xFF;PORTC = 0xFF;
DDRD = 0x00;PORTD = 0xFF;
MCUCR = 0x02;
GICR = 0x40;
TCNT1 = 65536 - F_CPU/8*0.1;
TIMSK = _BV(TOIE1);
sei();
while(1) Show_Count_ON_DSY();
}
ISR(TIMER1_OVF_vect)
{
uint8_t i;
TCNT1 = 65536-F_CPU/8*0.1;
Digits_Buffer[0]++;
for(i=0;i<=5;i++)
{
if(Digits_Buffer[i] == 10)
{
Digits_Buffer[i]=0;
if(i!=5) Digits_Buffer[i+1]++;
}
else break;
}
}
ISR(INT0_vect)
{
uint8_t i;
static uint8_t KeyOperation = 0;
if(++KeyOperation==3)KeyOperation=0;
switch(KeyOperation)
{
case 0:TCCR1B = 0x00;
for(i=0;i<6;i++)Digits_Buffer[i] = 0;
break;
case 1:TCCR1B = 0x02;
break;
case 2:TCCR1B = 0x00;
break;
}
}
|