以前使用的T/C0 T/C1 T/C2均工作于定时方式,本例中T/C0工作于计数方式,其区别在于此前TCNTx的计数时钟由系统分频后提供。所提供的时钟具有固定频率(周期),本例中T/C0工作于计数方式,TCNT0的计数时钟不再由系统时钟分频繁提供,而是由来自T0(PB0)引脚的外部信号提供,主程序中TCCR0= 0x06,该行代码将TCCR0的低3位(CS02,CS01,CS00)设为110,它使得TCNT0的计数时钟来自于T0(PB0)引脚,且为下降沿触发.
Protus截图:
Studio截图:
程序:
/*
* GccApplication2.c
*
* Created: 2014-10-24 20:29:33
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define Clear_Key_DOWN() ((PINB &0x40) == 0x00)
const uint8_t SEG_CODE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7d,0x07,0x7F,0x6F};
void Show_Count_ON_DSY()
{
PORTD = 0xFF;
PORTC = SEG_CODE[TCNT0/10];
PORTD = 0xFE;
_delay_ms(2);
PORTD = 0xFF;
PORTC = SEG_CODE[TCNT0 %10];
PORTD = 0xFD;
_delay_ms(2);
}
int main(void)
{
DDRC = 0xFF;PORTD = 0xFF;
DDRD = 0xFF;PORTD = 0xFF;
DDRB = 0x00;PORTB = 0xFF;
TCCR0 = 0x06;
TCNT0 = 0x00;
while(1)
{
//TODO:: Please write your application code
if(Clear_Key_DOWN()) TCNT0 = 0;
if(TCNT0 >= 100) TCNT0 = 0;
Show_Count_ON_DSY();
}
}
|