找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 958|回复: 0
收起左侧

单片机电子钟仿真出现问题 是我的问题还是仿真的问题

[复制链接]
ID:893765 发表于 2021-5-6 09:41 | 显示全部楼层 |阅读模式
本来是长按KSET可以设置时间 KINC、KDEC分别对修改对象加/减1 长按KSET结束设置 但仿真时一按KSET屏幕就灭了 并且钟的显示时间没有改变 是我的问题还是仿真的问题 代码如下麻烦大佬们了
电子钟.jpg

单片机源程序如下:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define KSET 0x60
#define KSET_LONG 0x61
#define KINC 0x50
#define KDEC 0x30

sbit SPK=P3^6;

uchar code MAX[2]={24,60};
uchar code segtab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x8c,0xff};
uchar dbuf[4]={0,0,0,0};
uint count;

uchar time[3]={0,0,0};
uchar ala[2]={0,0};
uchar state;

bit update;
uchar flash=00;
uchar disp_on=0xff;

void delay(void)
{   uchar i;
    for(i=0;i<200;i++);
}

void hexbcd(uchar*tp)
{   dbuf[3]=tp[0]/10;
    dbuf[2]=tp[0]%10|0x80;
    dbuf[1]=tp[1]/10;
    dbuf[0]=tp[1]%10;
}
void disp(void)
{    static uchar cn=0;
     uchar n,bsel;
     bsel=0x01;
     for(n=0;n<4;n++)
     {   P2=bsel&disp_on|0xf0;
         P0=segtab[dbuf[n]&0x7f];
         if((dbuf[n]&0x80)!=0)
            P0=P0&0x7f;
         bsel=_crol_(bsel,1);
         delay();
         P0=0xff;
     }
     cn++;
     if(cn==100)
     {   disp_on=disp_on^flash;
         dbuf[2]=dbuf[2]^0x80;
         cn=0;
     }
}

void time0(void)interrupt 1 using 1
{   static uint n=0,m=0;
    count++;
    if(count==4000)
    {    count=0;
         time[2]++;
         if(time[2]==60)
         {    time[2]=0;
              time[1]++;
              if(state!=1)update=1;
              if(time[1]==60)
              {    time[1]=0;
                   time[0]++;
                   if(time[0]==24)
                   time[0]=0;
              }
         }
         if((state==0)&&(time[2]=0))
         {    if((time[0]==ala[0])&&(time[1]==ala[1]))
              {    flash=0x0f;
                   disp_on=0xff;
                   state=2;
                   dbuf[2]=dbuf[2]|0x80;
              }

         }
    }
    if(state==0x02)
    {    if(count%2==0)
         {    n++;
              if(n<500)
                 SPK=~SPK;
              else
                 SPK=1;
              if(n==2000)
              {   n=0;
                  m++;
                  if(m==60)
                  {   state=0;
                      m=0;
                      flash=0x00;
                      disp_on=0xff;   
                  }
             }
        }
    }
}

uchar getkey(void)
{   uchar key;
    uchar t;
    disp();
    if((key=P2&0x70)==0x70)return 0xff;
    for(t=0;t<5;t++)disp();
    if((key=P2&0x70)==0x70)return 0xff;
    while((P2&0x70)!=0x70)
    {    disp();
         if(t<250)t++;
    }
  if((t>200)&&(key==0x60))return 0x61;
  return key;
}
void set_time(uchar*tp)
{   uchar p=0;
    uchar k;
    state=1;
    flash=0x0c;
    while(1)
    {   hexbcd(tp);
        k=getkey;
        if(k==KSET)
        {   p++;
            p=p&0x01;
            disp_on=0xff;
            if(p==0x00)
                 flash=0x0c;
            else
                 flash=0x03;   
        }
      else if(k==KINC)
           {   tp[p]++;
               if(tp[p]==MAX[p])tp[p]=0;
           }
      else if(k==KDEC)
           {   tp[p]--;
               if(tp[p]==0xff)tp[p]=MAX[p]-1;
           }
      else if(k==KSET_LONG)
              break;   
    }
    hexbcd(time);
    flash=0x00;
    disp_on=0xff;
    state=0;
}
void main(void)
{
     uchar k;
     state=0;
     count=0;
     flash=0x00;
     disp_on=0xff;
     time[0]=12;
     time[1]=25;
     time[2]=55;
     ala[0]=12;
     ala[1]=26;
     hexbcd(time);
     TMOD=0x02;
     TH0=-250;
     TL0=-250;
     ET0=1;
     TR0=1;
     EA=1;
     while(1)
     {   disp();
         k=getkey();
         if(state==0x02)
         {    if(k!=0xff)
              {   state=0x00;
                  flash=0x00;
                  disp_on=0xff;
              }
         }
         else if(k==KSET_LONG)
              {
                 set_time(ala);
              }
         else if(k==KSET)
              {   TR0=0;
                  set_time(time);
                  TR0=1;
              }
         if(update==1)
         {    update=0;
              hexbcd(time);
         }
     }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表