1602LCD设计的可调式电子钟无法显示时间
时间:10-02
整理:3721RD
点击:
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define DelayNOP(){_nop_();_nop_();_nop_();_nop_();}
- sbit K1 =P1^0;
- sbit K2 =P1^1;
- sbit K3 =P1^2;
- sbit K4 =P1^3;
- sbit SPK =P3^0;
- sbit LCD_RS =P2^0;
- sbit LCD_RW =P2^1;
- sbit LCD_EN =P2^2;
- bit LCD_Busy_Check();
- void LCD_Initialize();
- void LCD_Set_POS(uchar);
- void LCD_Write_Command(uchar);
- void LCD_Write_Data(uchar);
- void Display_String(uchar *,uchar);
- uchar code Str1[]=" Current Time ";
- uchar code Str2[]="Set New Time...";
- uchar HMS_String[]=" 12:30:00 ";
- bit Settime=0;
- bit Change_H_or_M=1;
- uchar MilliSecond, Hour=12, Minute=30, Second=0;
- void DelayX(uint x)
- {
- uchar i;
- while(x--) for(i=0;i<120;i++);
- }
- void Display_HMS(uchar h,m,s)
- {
- if (Settime) HMS_String[3]='>';
- else
- HMS_String[3]=' ';
- HMS_String[4]=h/10+'0';
- HMS_String[5]=h%10+'0';
- HMS_String[7]=m/10+'0';
- HMS_String[8]=m%10+'0';
- HMS_String[10]=s/10+'0';
- HMS_String[11]=s%10+'0';
- Display_String(HMS_String,0x40);
- }
- void Change_Time()
- {
- Settime=0;
- if(K1==0||K2==0||K3==0)
- {TR0=0;
- Display_String(Str2,0x00);
- Settime=1;
- }
- while(Settime)
- {
- if(K1==0)
- {
- while(K1==0);
- Change_H_or_M =!Change_H_or_M;
- }
- else if(K2==0)
- {
- while(K2==0);
- if(Change_H_or_M==1)
- { if(++Hour==24)Hour=0;}
- else
- {if(++Minute==60)Minute=0;}
- }
- else if (K3==0)
- {
- while(K3==0);
- if(Change_H_or_M==1)
- { if(--Hour==0xff)Hour=23;}
- else
- {if(--Minute==0xff)Minute=59;}
- }
- else if(K4==0)
- {
- while(K4==0);
- Display_String(Str1,0x00);
- Settime=0;
- TR0=1;
- }
- Display_HMS(Hour,Minute,Second);
- }
- }
- void Time0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- if(++MilliSecond==20)
- {
- MilliSecond=0;
- if(++Second==60)
- {
- Second=0;
- if(++Minute==60)
- {
- Minute=0;
- if(++Hour==24)
- {
- Hour=0;Minute=0;Second=0;
- }
- }
- }
- }
- }
- void main()
- {
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- IE=0x82;
- SPK=0;
- LCD_Initialize();
- Display_String(Str1,0x00);
- TR0=1;
- P1=0xFF;
- while(1);
- {
- Display_HMS(Hour,Minute,Second);
- DelayX(500);
- Change_Time();
- }
- }
- // 1602LCD显示驱动函数
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define DelayNOP( ){_nop_( ); _nop_( ); _nop_( ); _nop_( );}
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_EN = P2^2;
- bit LCD_Busy_Check( );
- void LCD_Initialize( );
- void LCD_Set_POS(uchar);
- void LCD_Write_Command(uchar);
- void LCD_Write_Date(uchar);
- // 延时
- void DelayMS(uint ms)
- {
- uchar t;
- while(ms--) for (t =0;t < 120;t++);
- }
- // LCD忙检查
- bit LCD_Busy_Check( )
- {
- bit Result;
- LCD_RS = 0; LCD_RW = 1; LCD_EN = 1;DelayNOP( );
- Result = (bit)(P0 & 0x80);
- LCD_EN = 0;
- return Result;
- }
- // 向LCD写指令
- void LCD_Write_Command(uchar cmd)
- {
- while(LCD_Busy_Check() );
- LCD_RS = 0; LCD_RW = 0; LCD_EN = 0;
- _nop_( ); _nop_( );
- P0 = cmd; DelayNOP( );
- LCD_EN = 1;DelayNOP( );
- LCD_EN = 0;
- }
- // 向LCD写数据
- void LCD_Write_Date(uchar str)
- {
- while(LCD_Busy_Check() );
- LCD_RS = 1; LCD_RW = 0; LCD_EN = 0;P0 = str; DelayNOP( );
- LCD_EN = 1; DelayNOP( );LCD_EN = 0;
- }
- // 初始化LCD
- void LCD_Initialize( )
- {
- DelayMS(5); LCD_Write_Command(0x38);
- DelayMS(5); LCD_Write_Command(0x0c);
- DelayMS(5); LCD_Write_Command(0x06);
- DelayMS(5); LCD_Write_Command(0x01);
- DelayMS(5);
- }
- // 设置显示位置
- void LCD_Set_POS(uchar Position)
- {
- LCD_Write_Command(Position | 0x80);
- }
- //显示函数,在LCD指定行上显示字符串
- void Display_String(uchar *str,uchar LineNO)
- {
- uchar k;
- LCD_Set_POS(LineNO);
- for (k = 0;k < 16;k++) LCD_Write_Date(str[k]);
- }
试试自己设定一个时间来显示,看看是显示函数的问题,还是数据格式转换的问题