打印

一个关于单片机的问题,请高手帮帮忙!

[复制链接]
1437|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
lzf156965364|  楼主 | 2009-4-20 17:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcd_rs=P2^0;
sbit lcd_rw=P2^1;
sbit lcd_en=P2^2;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
uchar code table[]="Date:2009-12-31";
uchar code table1[]="Time: 23:59:52";
uchar code table3[]={31,29,31,30,31,30,31,31,30,31,30,31};
void write_com(uchar);
void write_data(uchar);
void write_dat1(uchar,uint);
void write_dat2(uchar,uchar);
void write_dat3(uchar,uchar);
void delay(uint);
void state();
void keyscan();
void clock();
uint hou,min,sec,tt,day,mon,year,keynum;
void main()
{sec=52;
min=59;
hou=23;
day=31;
mon=12;
year=2009;
state();
while(1)
{clock();
keyscan();}
}
void mc() interrupt 1
{TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
}
void write_com(uchar com)
{lcd_rs=0;
lcd_rw=0;
lcd_en=0;
P1=com;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
void write_data(uchar date)
{lcd_rs=1;
lcd_rw=0;
lcd_en=0;
P1=date;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
void write_dat1(uchar add1,uint dat1)
{uint qian,bai,shi,ge;
qian=2;
bai=0;
shi=(dat1%100)/10;
ge=dat1/10;
write_com(0x80+add1);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_dat2(uchar add2,uchar dat2)
{uchar shi,ge;
shi=dat2/10;
ge=dat2%10;
write_com(0x80+0x40+add2);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_dat3(uchar add3,uchar dat3)
{uchar shi,ge;
shi=dat3/10;
ge=dat3%10;
write_com(0x80+add3);
write_data(0x30+shi);
write_data(0x30+ge);
}
void state()
{uchar num;
lcd_en=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{write_data(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<14;num++)
{write_data(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void clock()
{if(tt==20)//定时1秒
{tt=0;
sec++;    
if(sec==60)
{sec=0;
min++;
if(min==60)
{
    min=0;
    hou++;
    if(hou==24)
    {hou=0;
    day++;                            if(day>table3[mon-1])
{day=1;
mon++;
    if(mon==13)
     mon=1;
    year++;
    if(year==2100)
    {
       year=2000;
    }
    write_dat1(5,year);
    }
    write_dat3(10,mon);
    }
    write_dat3(13,day);                
}
write_dat2(6,hou);
}
write_dat2(9,min);
}
write_dat2(12,sec);
}    
}
void delay(uint z)
{
    uint x,y;
    for(x=z;x>0;x--)
        for(y=110;y>0;y--);
}
void keyscan()
{
    if(key1==0)
    {
        delay(5);
        if(key1==0)
        {
            keynum++;
            while(!key1);
            if(keynum==1)
            {
                TR0=0;
                write_com(0x80+0x40+12);
                write_com(0x0f);
            }
            if(keynum==2)
            {
                write_com(0x80+0x40+9);
            }
            if(keynum==3)
            {
                write_com(0x80+0x40+6);
            }
            if(keynum==4)
            {
                write_com(0x80+13);
            }
            if(keynum==5)
            {
                write_com(0x80+10);
            }
            if(keynum==6)
            {
                write_com(0x80+5);
            }
            if(keynum==7)
            {
                keynum=0;
                write_com(0x0c);
                TR0=1;
            }
        }
    }
    if(keynum!=0)
    {
        if(key2==0)
        {
            delay(5);
            if(key2==0)
            {
                while(!key2);
                switch(keynum)
                {
                case 1:sec++;
                if(sec==60)sec=0;
                write_dat2(12,sec);
                write_com(0x80+0x40+12);break;
                case 2:min++;
                if(min==60)min=0;
                write_dat2(9,min);
                write_com(0x80+0x40+9);break;
                case 3:hou++;
                if(hou==24)hou=0;
                write_dat2(6,hou);
                     write_com(0x80+0x40+6);break;
                case 4:day++;
                                     if(day==table3[mon-1])day=1;
                write_dat3(13,day);
                write_com(0x80+13);
                break;
                case 5:mon++;
                if(mon==13)mon=1;
                write_dat3(10,mon);
                write_com(0x80+10);break;
                case 6:year++;
                if(year==2100)year=2000;
                write_dat1(5,year);
                write_com(0x80+5);break;
                }
            }
        }
        if(key3==0)
        {
            delay(5);
            if(key3==0)
            {
                while(!key3);
                switch(keynum)
                {
                case 1:sec--;
                if(sec==-1)sec=59;
                write_dat2(12,sec);
                write_com(0x80+0x40+12);break;
                case 2:min--;
                if(min==-1)min=59;
                write_dat2(9,min);
                write_com(0x80+0x40+9);break;
                case 3:hou--;
                if(hou==-1)hou=23;
                write_dat2(6,hou);
                write_com(0x80+0x40+6);break;
                case 4:day--;                        if(day==0)day=table3[mon-1];
                write_dat3(13,day);
                write_com(0x80+13);
                break;
                case 5:mon--;
                if(mon==0)mon=12;
                write_dat3(10,sec);
                write_com(0x80+10);break;
                case 6:year--;
                if(year==1999)year=2099;
                write_dat1(5,sec);
                write_com(0x80+5);break;
                }
            }
        }
    }
}
自己编写的一个时钟程序,在仿真的时候,在调节日的时候递加到最大只有30,但是从1递减的时候却又会出现31;在调节月份的时候递减,就会出现错误的月份;在调节年的时候,无论是递加还是递减都出现错误的年数。其他都是好的,请高手帮忙解决一下!

相关帖子

沙发
mohanwei| | 2009-4-20 21:12 | 只看该作者

别的不说,你先尝试自己加上注释,就知道问题所在了

使用特权

评论回复
板凳
ningling_21| | 2009-4-21 07:51 | 只看该作者

太乱了

使用特权

评论回复
地板
vrgood| | 2009-4-21 09:36 | 只看该作者

应该是这样吧?

应该是这样吧?
 修改为:if(day==(table3[mon-1]+1))day=1;

使用特权

评论回复
5
red1986| | 2009-4-21 21:02 | 只看该作者

代码太长估计没有几个人会耐心看完

如题,建议去GOOGLE下万年历,单片机,很多资料很现成的代码

使用特权

评论回复
6
木字四点水| | 2009-4-22 11:07 | 只看该作者

yun,太长

使用特权

评论回复
7
ajb110| | 2009-4-22 13:31 | 只看该作者

给你发个时间调节程序,希望对你有帮助

SysTimer.t_sec ++;

if(SysTimer.t_sec == 60)
{
  SysTimer.t_sec = 0;
  SysTimer.t_min ++;
  if(SysTimer.t_min == 60)
  {
    //declare the printer test timer
    SysTimer.t_min = 0;
    SysTimer.t_hour ++;
    if(SysTimer.t_hour == 24)
    {
      SysTimer.t_hour = 0;
      switch (SysTimer.t_month)
      {
    case    1:
    case    3:
    case    5:
    case    7:
    case    8:
    case    10:
    case    12:
        if(SysTimer.t_day >= 31)
        {
              SysTimer.t_day = 1;
         if(SysTimer.t_month == 12)
         {
        SysTimer.t_month = 1;
        SysTimer.t_year ++;
        if(SysTimer.t_year==100)SysTimer.t_year=0;
              }
        else
              SysTimer.t_month ++;
        }
      else
        SysTimer.t_day ++;
      break;

     case    2:
      if((SysTimer.t_year % 4 == 0 && SysTimer.t_year % 100 != 0) || SysTimer.t_year % 400 ==0)
       {
            if(SysTimer.t_day >= 29)
         {
            SysTimer.t_day = 1;
            SysTimer.t_month ++;
          }
        else
           SysTimer.t_day ++;
    }
    else
    {
        if(SysTimer.t_day >= 28)
        {
           SysTimer.t_day = 1;
              SysTimer.t_month ++;
       }
       else
          SysTimer.t_day ++;
     }
       break;

       case    4:
       case    6:
       case    9:
       case    11:
         if(SysTimer.t_day >= 30)
    {
         SysTimer.t_day = 1;
       SysTimer.t_month ++;
    }
         else
          SysTimer.t_day ++;
       break;
    }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

10

帖子

0

粉丝