1602液晶显示时钟

lw209   2011-10-8 21:24 楼主
/*
设计者:李文
功能:
1602液晶显示时钟,并且可调。
*/
#include <reg52.h>//头文件
#include <intrins.h>
#define uchar unsigned char//宏定义
#define uint unsigned int
sbit wela=P2^7;//定义位类型
sbit dula=P2^6;
sbit rs =P3^5;
sbit en =P3^4;
sbit key1=P3^6;//功能键
sbit key2=P3^3;//按键加
sbit key3=P3^1;//按键减
sbit key4=P3^2;//清零
sbit beep=P2^3;
sbit P3_6=P3^7;
uchar miao ,shi,fen;//定义变量
uchar count=0,num;//定义变量
//uchar code table[]={{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};//定义数组
void delay(uint x)//延时1ms函数
{
  uchar i;
  while(x--)
  {for(i=0;i<120;i--);}
}
void com (uchar com)//1602的写指令
{
  rs=0;
  en=0;
  P0=com;
  delay(5);
  en=1;
  delay(5);
  en=0;
}
void dat(uchar dat)//1602的写数据
{
  rs=1;
  en=0;
  P0=dat;
  delay(5);
  en=1;
  delay(5);
  en=0;
  }
void write_sfm(uchar add,uchar num)//液晶显示一位两位的十进制数
{
uchar shi ,ge;
shi=num/10;//十位和个位的分离
ge=num%10;
com(0x80+0x40+add);//显示的位置
dat(0x30+shi);//显示的十位
dat(0x30+ge);//显示的个位
}
void write_sfmTIAN(uchar add,uchar num)//液晶显示一位两位的十进制数
{
uchar shi ,ge;
shi=num/10;//十位和个位的分离
ge=num%10;
com(0x80+add);//显示的位置
dat(0x30+shi);//显示的十位
dat(0x30+ge);//显示的个位
}
void write_sfmNIAN(uint num)//液晶显示一位四位的十进制数
{
uchar qian,bai,shi ,ge;
qian=num/1000;//
bai=num%1000/100;
shi=num%1000%100/10;//
ge=num%1000%100%10;
com(0x80);//显示的位置
dat(0x30+qian);//显示
dat(0x30+bai);//显示
dat(0x30+shi);//显示
dat(0x30+ge);//显示
}

void di ()//蜂鸣器
{
 beep=0;
 delay(100);
 beep=1;
 
}
void init ()//初始化函数
{
  uint k=2011;
 uchar yue=4,tian=10;
 dula=0;
 wela=0;
 en=0;
 fen=0;
 shi=0;
 miao=0;
 count=0;
//1602液晶的初始化
 com(0x38);//显示模式初始化
 com(0x0c);//开显示,光标不显示,光标不闪烁
 com(0x06);//指针加一
 com(0x01);//清屏
 
  com(0x80+4);//定位数据指针
  dat('-');//写数据
  delay(5);//延时
  com(0x80+7);//定位数据指针
  dat('-');//写数据
  delay(5); //延时

 write_sfmNIAN(k);
 write_sfmTIAN(5,yue);
 write_sfmTIAN(8,tian);
 com(0x80+0x40+6);//定位数据指针
  dat(':');//写数据
  delay(5);//延时
  com(0x80+0x40+9);//定位数据指针
  dat(':');//写数据
  delay(5); //延时
  write_sfm(10,miao);//秒的显示
  write_sfm(7,fen);//分的显示
  write_sfm(4,shi);//时的显示
 
  TMOD=0X01;//定时器0工作在方式1
  TH0=(65536-50000)/256;//设置初值
  TL0=(65536-50000)%256;//
  EA=1;//开总中断
  ET0=1;//开定时器0
  TR0=1;  //启动定时器0
}
void key()//按键函数
{
   P3_6=0;
   if(key1==0)
      {
     delay(5);
        if(key1==0)
          {
            num++;
            while(!key1);
            di();
            if(num==1)
               {
                   TR0=0;
                   com(0x80+0x40+10);
                   com(0x0f);
                }
              if(num==2)
                {          
                   com(0x80+0x40+7);
                   com(0x0f);
                   }
               if(num==3)
                {          
                   com(0x80+0x40+4);
                   com(0x0f);
                   }
               if(num==4)
                {          
                   com(0x80);
                   com(0x0f);
                   
                   }
                if(num==5)
                {          
                   com(0x80+5);
                   com(0x0f);
                   }
                 if(num==6)
                {          
                   com(0x80+8);
                   com(0x0f);
                   }
                 if(num==7)
                {          
                   com(0x80+11);
                   }
                if(num==8)
                {          
                   num=0;
                   com(0x0C);
                    TR0=1;
                   }
             }
          }
  
     if(num != 0)
       {
     if(key2==0)
        {
           delay(5);
           if(key2==0)
             {
              while(!key2);
                 di();
                 if(num==1)
                   {
                     miao++;
                     if(miao==60)
                      miao=0;
                      write_sfm(10,miao);//秒的显示
                      com(0x80+0x40+10);
                       }
                  if(num==2)
                   {
                     fen++;
                     if(fen==60)
                     fen=0;
                      write_sfm(7,fen);//分的显示
                     com(0x80+0x40+7);
                       }
                  if(num==3)
                    {
                     shi++;
                     if(shi==24)
                      shi=0;
                      write_sfm(4,shi);//时的显示
                      com(0x80+0x40+4);
                      }
               }
          }
 
       }
      if(num!=0)
       {
     if(key3==0)
        {
           delay(5);
           if(key3==0)
             {
              while(!key3);
                 di();
                 if(num==1)
                   {
                     miao--;
                     if(miao==-1)
                      miao=59;
                      write_sfm(10,miao);//秒的显示
                      com(0x80+0x40+10);
                       }
                  if(num==2)
                   {
                     fen--;
                     if(fen==-1)
                     fen=59;
                      write_sfm(7,fen);//分的显示
                     com(0x80+0x40+7);
                       }
                  if(num==3)
                   {
                     shi--;
                     if(shi==-1)
                      shi=23;
                      write_sfm(4,shi);//时的显示
                      com(0x80+0x40+4);
                       }
               }
          }
 
       }
 if(num != 0)
       {
     if(key4==0)
        {
           delay(5);
           if(key4==0)
             {
              while(!key4);
                 di();
                 if(num==1)
                   {
                      miao=0;
                      write_sfm(10,miao);//秒的显示
                      com(0x80+0x40+10);
                       }
                  if(num==2)
                   {
                     fen=0;
                      write_sfm(7,fen);//分的显示
                     com(0x80+0x40+7);
                       }
                  if(num==3)
                   {
                      shi=0;
                      write_sfm(4,shi);//时的显示
                      com(0x80+0x40+4);
                       }
               }
          }
 
       }

   }
void main()//主函数
{
  init();//初始化
  while(1)
    {
       key();
      }
}
void timer0() interrupt 1//中断服务程序
{
 
   TH0=(65536-50000)/256;//重新赋初值
   TL0=(65536-50000)%256;
   count++;
   if(count==20)//1秒到了
     {
       count=0;//清零
       miao++; //秒加一
       if(miao==60)//
         {
           miao=0;
           fen++;//分加一
          if(fen==60)//
           {
                 fen=0;
                 shi++;//时加一
                 if(shi==24)//
                   {
                   shi=0;
                    }
                 write_sfm(4,shi);//刷新时
                }
            
      write_sfm(7,fen);//刷新分
           }
  write_sfm(10,miao);//刷新秒
      }
    
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

回复评论 (3)

好。。。

点赞  2011-10-8 23:12
嗯嗯···
点赞  2012-8-15 17:20
你用定时器提供时间,而且还用的工作方式1,误差也太大了吧
点赞  2012-8-15 19:37
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复