基于51单片机的12864液晶时钟C语言程序

自己写的12864液晶时钟程序,经过验证可以使用,希望可以为初学者作为参考。

 

#include<reg52.h>
#include<math.h>
           
#define uint  unsigned int
#define uchar unsigned char
sbit K0=P2^4;
sbit K1=P2^5;
sbit K2=P2^6;
sbit K3=P2^7;  
uchar data CLOCK[4]={0,0,0,21};//存放时钟时间(依次是百分秒、秒、分和小时)
sbit rs = P3^0;  
sbit rw = P3^1;  
sbit  e = P3^2;
sbit rst=P3^4;
sbit psb=P3^3;
unsigned char i,j;
sbit busy=P1^7;
//sbit led=P1^0;

uchar code tab1[]=
{
"时间:           "
"百分秒:        "
"华中科技大学    "
"胡畅            "
};
void timer0(void) interrupt 1
{
 TH0=0xb8;TL0=0x00;//重装初值
 CLOCK[0]=CLOCK[0]+1;
 if(CLOCK[0]==100)
 {
  CLOCK[0]=0;
  CLOCK[1]+=1;
  if(CLOCK[1]==60)
  {
   CLOCK[1]=0;
   CLOCK[2]+=1;
   if(CLOCK[2]==60)
   {
    CLOCK[2]=0;
    CLOCK[3]+=1;
    if(CLOCK[3]==24)
    {CLOCK[3]=0;}
   }
  }
 }
}

void busy1(void)
{
  P1=0xff;
  rs=0;
  rw=1;
  e =1;
  while(busy==1);
  e =0;
}

void delay (uint us)   //delay time
{
  while(us--);
}
void delay1 (uint ms)
{
  uint i,j;
  for(i=0;i<ms;i++)
  for(j=0;j<15;j++)
  delay(1);
}
void write_dat(uchar dat)
{  busy1();
   rs=1;
   rw=0;
   P1=dat;
   e=1;
   delay(2);
   e=0;
  
}
void  write_zhiling(uchar com)
{  busy1();
  
   rs=0;
   rw=0;
   P1=com;
   e=1;
   delay(2);
   e=0;
}
void chushihua()
{ rst=1;
  psb=1;
   write_zhiling(0x30);
    write_zhiling(0x0c);
   write_zhiling(0x01);
   write_zhiling(0x06);
  
}
void display(void)

 uchar o;
 uchar un[11];
 un[0]=CLOCK[3]/10;//小时
 un[1]=CLOCK[3]%10;
 un[2]='-';
 un[3]=CLOCK[2]/10;//分钟
 un[4]=CLOCK[2]%10;
 un[5]='-';
 un[6]=CLOCK[1]/10;//秒钟
 un[7]=CLOCK[1]%10;
 un[8]=CLOCK[0]/10;//百分秒
 un[9]=CLOCK[0]%10;
 //转换成ASCII字符
 for(o=0;o<=9;o++)
 {
  un[o]+='0';
 }
 un[2]=45;
 un[5]=45;
 write_zhiling(0x30);
 write_zhiling(0x84);
 for(o=0;o<8;o++)
  write_dat(un[o]);
 write_zhiling(0x30);
 write_zhiling(0x94);
  write_dat(un[8]);
  write_dat (un[9]);
 write_zhiling(0x96);
  write_dat (0x02);
  write_dat (0x03);
}
//------------------清整个GDRAM空间------------------
void clrgdram()
{
    unsigned char x,y ;
    for(y=0;y<64;y++)
    for(x=0;x<16;x++)
    {
        write_zhiling(0x34);
        write_zhiling(y+0x80);
        //行地址
        write_zhiling(x+0x80);
        //列地址
        write_zhiling(0x30);
        write_dat(0x00);
        write_dat(0x00);
    }
}
void clear(void)
{
  write_zhiling (0x30);
  write_zhiling (0x01);
}

 void hanzi(uchar code *han)   //显示汉字
{
  uchar i,j;
  write_zhiling(0x30);
  write_zhiling (0x80);
  j=0;
  for (i=0;i<16;i++)
  write_dat(han[j*16+i]);   //写第一行
  write_zhiling (0x90);
  j=1;
  for (i=0;i<16;i++)   //写第二行
  write_dat(han[j*16+i]);
  write_zhiling (0x88);
    j=2;
  for (i=0;i<16;i++)
  write_dat(han[j*16+i]);   //写第3行
  write_zhiling (0x98);
  j=3;
  for (i=0;i<16;i++)   //写第4行
  write_dat(han[j*16+i]);
     

}
 unsigned char ReadByte(void)
{
    unsigned char byReturnValue ;
    busy1();
    P1=0xff ;
    rs=1 ;
    rw=1 ;
    e=0 ;
    e=1 ;
    byReturnValue=P1 ;
    e=0 ;
   
    return byReturnValue ;
}
void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color)    //画点程序
{
    unsigned char Row,Tier,Tier_bit ;
    unsigned char ReadOldH,ReadOldL ;
    write_zhiling(0x34);
    write_zhiling(0x36);
    Tier=X>>4 ;
    Tier_bit=X&0x0f ;
    if(Y<32)
    {
        Row=Y ;
    }
    else
    {
        Row=Y-32 ;
        Tier+=8 ;
    }
    write_zhiling(Row+0x80);
    write_zhiling(Tier+0x80);
    ReadByte();
    ReadOldH=ReadByte();
    ReadOldL=ReadByte();
    write_zhiling(Row+0x80);
    write_zhiling(Tier+0x80);
    if(Tier_bit<8)
    {
        switch(Color)
        {
            case 0 :
            ReadOldH&=(~(0x01<<(7-Tier_bit)));
            break ;
            case 1 :
            ReadOldH|=(0x01<<(7-Tier_bit));
            break ;
            case 2 :
            ReadOldH^=(0x01<<(7-Tier_bit));
            break ;
            default :
            break ;
        }
        write_dat(ReadOldH);
        write_dat(ReadOldL);
    }
    else
    {
        switch(Color)
        {
            case 0 :
            ReadOldL&=(~(0x01<<(15-Tier_bit)));
            break ;
            case 1 :
            ReadOldL|=(0x01<<(15-Tier_bit));
            break ;
            case 2 :
            ReadOldL^=(0x01<<(15-Tier_bit));
            break ;
            default :
            break ;
        }
        write_dat(ReadOldH);
        write_dat(ReadOldL);
    }
   write_zhiling(0x30);
}

void main()
{
    chushihua();
    hanzi(tab1);
    delay(80000);
    EA=1;
 ET0=1;
 TMOD=0x01;//T0方式1计时
 TH0=0xB8;
 TL0=0x00;
 P2=0xff;
 TR0=1;
 while(1)
 {
      if(K0==0)
       {delay(250);
         if(K0==0)
        TR0=0;}
        display();
      if(K1==0)
      { delay(250);
      if(K1==0)
       CLOCK[3]+=1; if(CLOCK[3]==24) CLOCK[3]=0; }
         display();
      if(K2==0)
      { delay(400);
      if(K2==0)
       CLOCK[2]+=1; if(CLOCK[2]==60) CLOCK[2]=0; }
           display();
      if(K3==0)
      {delay(400);
      if(K3==0) 
      TR0=1; break; }
     
   
    display();
   }
      display();
 
 } 
 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值