代码:
#include
#define key_input PIND.7
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2
#define key_state_3 3
typedef unsigned char uchar;
typedef unsigned int uint;
const uchar led_7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f};
const uchar position[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar time[3];
uchar dis_buff[6];
uchar time_counter,key_stime_counter;
uchar posit;
bit point_on,time_1s_ok,key_stime_ok;
void display(void)
{
PORTD=0xff;
PORTC=led_7[dis_buff[posit]];
if(point_on&&(posit==2||posit==4))PORTC|=0X80;
PORTD=position[posit];
if(++posit>=6)posit=0;
}
interrupt [TIM0_COMP] void time0_comp_isr(void)
{
display();
if(++key_stime_counter>=5)
{
key_stime_counter=0;
key_stime_ok=1;
if(++time_counter>=100)
{
time_counter=0;
time_1s_ok=1;
}
}
}
void time_to_disbuffer(void)
{
uchar i,j=0;
for(i=0;i<=2;i++)
{
dis_buff[j++]=time%10;
dis_buff[j++]=time/10;
}
}
uchar read_key_n(void)
{
static uchar key_state=0,key_time=0;
uchar key_press,key_return=0;
key_press=key_input;
switch(key_state)
{
case key_state_0:
if(!key_press) key_state=key_state_1;
break;
case key_state_1:
if(!key_press)
{
key_state=key_state_2;
key_time=0;
}
else
key_state= key_state_0;
break;
case key_state_2:
if(key_press)
{
key_state= key_state_0;
key_return=1;
}
else if(++key_time>=100)
{
key_state= key_state_3;
key_time=0;
key_return=2;
}
break;
case key_state_3:
if(key_press) key_state= key_state_0;
else
{
if(++key_time>=50)
{
key_time=0;
key_return=2;
}
}
break;
}
return key_return;
}
void main(void)
{
PORTC=0;
DDRC=0XFF;
PORTD=0X3F;
DDRD=0X3F;
TCCR0=0X0B;
TCNT0=0;
OCR0=0X7C;
TIMSK=2;
time[2]=23;time[1]=58;time[0]=55;
posit=0;
time_to_disbuffer();
#asm("sei");
while (1)
{
if(time_1s_ok)
{
time_1s_ok=0;
point_on=~point_on;
}
if(key_stime_ok)
{
key_stime_ok=0;
switch(read_key_n())
{
case 1:
++time[0];
break;
case 2:
time[0]+=10;
break;
}
if(time[0]>=60)
{
time[0]-=60;
if(++time[1]>=60)
{
time[1]=0;
if(++time[2]>=24)
time[2]=0;
}
}
time_to_disbuffer();
}
}
}
短按秒加1,长按1秒没释放每隔0.5秒加10
仿真图:
文章评论(0条评论)
登录后参与讨论