原先可以显示初始设定值25但在加入了计时器和中断之后只能显示00
#include
unsigned char Led_Disbuf[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char position[2]={0x80,0x40};
unsigned char time;
unsigned char dis_buff[2];
int time_counter;
unsigned char time_is_ok;
void main()
{
port_init();//端口定义
while(1)
{
keyscan();//按键
}
}
void port_init()//端口初始化
{
DDRA=0xFF;//LED OUTPUT
PORTA=0xFF;
DDRB=0xC0;//按键 INPUT
DDRC=0xFF;
PORTC=0xFF;
DDRD=0xFF;
PORTB=0x3F;
}
void keyscan()//第一次按键选择彩灯OR交通灯
{
if((PINB&0x3F)!=0x3F)
{
if((PINB&0x3F)==0x3E)
{
cai1();
}
if((PINB&0x3F)==0x3D)
{
cai2();
}
if((PINB&0x3F)==0x3B)
{
cai3();
}
if((PINB&0x3F)==0x37)//选择交通灯
{
jiao();
}
}
}
void cai1()
{
unsigned int p;
PORTC=0xFF;
delay();
PORTC=0xFE;
delay();
PORTC=0xFD;
delay();
PORTC=0xBF;
delay();
PORTC=0x7F;
delay();
PORTC=0xFF;
PORTA=0xFE;
delay();
for(p=0;p<7;p++)
{
PORTA=(PORTA<<1)|0x01;
delay();
}
PORTA=0xFF;
PORTC=0xFF;
}
void cai2()
{
unsigned int p;
PORTC=0xFF;
delay();
PORTC=0xFE;
delay();
PORTC=0xFC;
delay();
PORTC=0xBC;
delay();
PORTC=0x3C;
delay();
PORTA=0xFE;
delay();
for(p=0;p<7;p++)
{
PORTA=(PORTA<<1);
delay();
}
}
void cai3()
{
unsigned int p;
PORTC=0x3C;
PORTA=0x00;
delay();
PORTC=0x3D;
delay();
PORTC=0x3F;
delay();
PORTC=0x7F;
delay();
PORTC=0xFF;
delay();
PORTA=0x01;
delay();
for(p=0;p<7;p++)
{
PORTA=(PORTA<<1)|0x01;
delay();
}
}
void delay()
{
int i,j;
for(j=0;j<1000;j++)
for(i=0;i<100;i++);
}
void jiao()
{
time=25;
//T/C0 // F0C0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
// 0 0 0 0 1 0 1 1
// WGM01:WGM00=1:0 CTC; CLK=8MHZ CLK/64=125KHZ
TCCR0=0x0B;//0X1B--OC0(PB4)比较匹配时取反;
TCNT0=0x00;
OCR0=0x7C;//OCR0=124,(124+1)/125=1ms
TIMSK=0x02;//OCIE0=1T/C0 Output compare match interrupt enable
SREG=0x80;
time_to_disbuffer();//拆数
int_init();//中断定义
display();
while(1)
{
if(time_is_ok)
{
time_is_ok=0;
if(time<=15&time>10)
{
duan1();
}
if(time<=10&time>5)
{
duan2();
}
if(time<=5&time>0)
{
duan3();
}
time_to_disbuffer();
}
}
}
void display()
{
unsigned char posit=0;
PORTB=0x00;
while(1)
{
PORTD=Led_Disbuf[dis_buff[posit]];//显示数字
PORTB=position[posit] ;//显示位置
delay();
if(++posit>1) posit=0;
}
}
void time_to_disbuffer()
{
unsigned char j=0;
dis_buff[j++]=time%10;
dis_buff[j++]=time/10;
}
//T0比较中断服务程序
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
display();
if(++time_counter>=1000)
{
time_counter=0;
time_is_ok=1;
}
}
void duan1()
{
PORTA=0x7F;
PORTC=0x7F;
}
void duan2()
{
while(1)
{
PORTA=0x7F;
PORTC=0x7F;
delay();
PORTA=0xFF;
PORTC=0xFF;
delay();
}
}
void duan3()
{
PORTA=0xFE;
}
void int_init()
{
MCUCR=0x02;
GICR=0x40;
SREG=0x80;
}