#include <avr/io.h>
#include <avr/interrupt.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
0x7f,0x6f};
uchar count=0;
uchar flag=0;
uchar num=0;
void delay(uint ms)
{
uint x,y;
for(x=ms;x>0;x--)
for(y=150;y>0;y--);
}
void pwm()
{
TCCR2=0X6c;
OCR2=1;
}
void show(uchar j,uchar k)
{
PORTA|=(1<<3);
PORTB=table[j];
PORTA&=~(1<<3);
PORTB=0XFF;
PORTB&=~(1<<k);
PORTA|=(1<<4);
PORTA&=~(1<<4);
delay(1);
}
ISR(SIG_OVERFLOW1)
{
TCNT1=0xffff;
GICR=0x40;
MCUCR=0X01;
GIFR=0X40;
SREG|=1<<7;
}
ISR(SIG_INTERRUPT0)
{
if(flag==0)
TCNT1=0;
count++;
flag=1;
}
int main(void)
{
DDRA|=0X18;
DDRB=0XFF;
DDRD=0x80;
PORTD=0XFF;
TCCR1B=0x04;
TCNT1=0xffff;
tiMSK|=0X06;
SREG|=(1<<7);
pwm();
while(1)
{
if(count==2)
{
num=TCNT1;
SREG&=~(1<<7);
}
show(num/1000,0);
show(num/100%10,1);
show(num/10%10,2);
show(num%10,3);
}
}
mega16
一周热门 更多>