1
收藏
微博
微信
复制链接

51单片机带定时功能时钟问题?

提问于
2022-07-30 03:24

dcbe55045e7a156d5873407ada969c.jpg

问题描述:烧入程序之后,LED显示如图所示,望各位指教


#include
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99
,0x92,0x82,0xf8,0x80,0x90};//共阳数码管
uchar time,hour_now,min_now,sec_now,hour_a1,min_a1,sec_a1,key;
sbit beep=P3^7;
void delayms(uchar i)
{
while(i--);
}
void write(uchar num,chose)
{
uchar i;

for(i=0;i<2;i++)
    {
  P0=0xff;
     P2=~chose;//位选

     if(i==0)
       {
        P0=table[num%10];//段选
       }
     else
        P0=table[num/10];//段选
     
  chose=chose<<1;
     delayms(5000);
    }
}
void show()
{
P0=0xff;
P2=0xdb;
P0=0xbf;
delayms(5000);
}//-隔开时,分,秒显示
void init()
{
TMOD=0x01;
TH0=0x4c;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}
void display(uchar hour_now,uchar min_now,uchar sec_now)
{
  write(sec_now,0x01);
  write(min_now,0x08);
  write(hour_now,0x40);
  show();
}
void jianpanjiance()
{
uchar temp;
P1=0xfe;
temp=P1;
if(temp!=temp&0xf0)
   {  
    delayms(10000);
temp=P1;
if(temp!=temp&0xf0)
   {
    temp=P1;
    switch(temp)
    {
     case 0xee:
             key=1;
         break;
  case 0xde:
         key=2;
      break;
  case 0xbe:
         key=3;
      break;
  case 0x7e:
         key=4;
      break;
       }         
   }        
   }
while(temp!=0xf0)
        {
   temp=P1;
         temp=temp&0xf0;
        }

P1=0xfd;
temp=P1;
if(temp!=temp&0xf0)
   {  
    delayms(10000);
temp=P1;
if(temp!=temp&0xf0)
   {
    temp=P1;
    switch(temp)
    {
     case 0xed:
             key=5;
         break;
  case 0xdd:
         key=6;
      break;
  case 0xbd:
         key=7;
      break;
  case 0x7d:
         key=8;
      break;
       }         
   }        
   }
while(temp!=0xf0)
        {
   temp=P1;
         temp=temp&0xf0;
  }
P1=0xfb;
temp=P1;
if(temp!=temp&0xf0)
   {  
    delayms(10000);
temp=P1;
if(temp!=temp&0xf0)
   {
    temp=P1;
    switch(temp)
    {
     case 0xeb:
             key=9;
         break;
  case 0xdb:
         key=10;
      break;
  case 0xbb:
         key=11;
      break;
  case 0x7b:
         key=12;
      break;
       }         
   }        
   }
while(temp!=0xf0)
        {
   temp=P1;
         temp=temp&0xf0;
  }
P1=0xf7;
temp=P1;
if(temp!=temp&0xf0)
   {  
    delayms(10000);
temp=P1;
if(temp!=temp&0xf0)
   {
    temp=P1;
    switch(temp)
    {
     case 0xe7:
             key=13;
         break;
  case 0xd7:
         key=14;
      break;
  case 0xb7:
         key=15;
      break;
  case 0x77:
         key=16;
      break;
       }         
   }        
   }
while(temp!=0xf0)
        {
   temp=P1;
         temp=temp&0xf0;
  }
}


void display_key(uchar hour,uchar min,uchar sec,tkey)
{
uchar i=0,take_key;
uchar data timenum[]={0,0,0,0,0,0};
jianpanjiance();
if(key==tkey)
   {
    take_key=1;
    timenum[0]=hour/10;
timenum[1]=hour%10;
timenum[2]=min/10;
timenum[3]=min%10;
while(take_key==1)
{
  jianpanjiance();
  if(key<11)
    {
     i++;
  timenum[i-1]=key;
  key=17;
    }
  if(key==12)
    take_key=0;
  if(i==6)
    take_key=0;
  hour=timenum[0]*10+timenum[1];
  min=timenum[2]*10+timenum[3];
  sec=timenum[4]*10+timenum[5];
  display(hour,min,sec);
}
    if(sec>59)
   sec=0;
if(min>59)
   min=0;
if(hour>23)
   hour=0;
if(key==11)
   {
    sec_now=sec;
    min_now=min;
    hour_now=hour;
   }
if(key==13)
   {
    sec_a1=sec;
    min_a1=min;
    hour_a1=hour;
   }   
   }
}
void main()
{
hour_now=23;
min_now=37;
sec_now=50;
display(hour_now,min_now,sec_now);
init();
while(1)
{
   display(hour_now,min_now,sec_now);
   
   display_key(hour_now,min_now,sec_now,11);
  
   display_key(hour_a1,min_a1,sec_a1,13);
}
}
void T0_Time() interrupt 1
{
TH0=0x4c;
TL0=0x00;
time++;
if(time==20)
      {  
    time=0;
      if(hour_now==hour_a1&min_now==min_a1)
      beep=0;
    else
      beep=1;
   
       sec_now++;
    if(sec_now==60)
      {  
       sec_now=0;
       min_now++;
        if(min_now==60)
         {
          min_now=0;
          hour_now++;
          if(hour_now==24)
            {
             hour_now=0;
           }
        }
      }
   }
}

收藏 658 0 1
粉丝
0
关注
0
主题
10
工程文件上传上来 我看看
0

回答于 · 2022-07-30 10:50