/*txt1.c*/
- #include <reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define DelayNOP(){_nop_();_nop_();_nop_();_nop_();}
- uchar Time_Buffer[]={0,0,0,0};
- uchar LCD_Display_Buffer[] = {"00:00:00:00"};
- void LCD_Initialize();
- void LCD_Set_POS(uchar);
- void LCD_Write_Data(uchar);
- void Display_String(uchar *,uchar);
- void Beep();
- sbit K1=P1^0;
- sbit K2=P1^1;
- sbit BEEP=P3^0;
- sbit LCD_RS=P2^0;
- sbit LCD_RW=P2^1;
- sbit LCD_EN=P2^2;
- char KeyCount=0;
- uchar code msg1[]= {"Second Watch 0 "};
- uchar code msg2[]= {"#### "};
- uchar code Prompts[][16]=
- {{"##1--> "},{"##1-->##2 "},{"##3--> "},{"##3-->4 "}};
- void Beep()
- {
- uchar i,j=70;
- for(i=0;i<180;i++)
- {while(--j);BEEP=~BEEP;}
- BEEP=0;}
- void DelayX(uint MS)
- {
- uchar i;
- while(MS--) for(i=0;i<120;i++);}
- void Show_Second()
- {
- uchar i;
- LCD_Set_POS(0X45); //设置 LCD显示起点
- for (i=3;i!=0xff;i--)
- {
- //将两位整数的1、100s, 秒,分,时转换为8位数字字符
- LCD_Display_Buffer[2*i+1]=Time_Buffer[i]/10+'0';
- LCD_Display_Buffer[2*i]=Time_Buffer[i]%10+'0';
- //在i=3,2,1,0时分别显示时,分,秒,1/100s
- LCD_Write_Data(LCD_Display_Buffer[2*i+1]);
- LCD_Write_Data(LCD_Display_Buffer[2*i]);
- LCD_Write_Data(':');
- }}
- //Time0中断
- void Time0() interrupt 1 using 0
- {
- TH0=-10000/256;
- TL0=-10000%256;
- Time_Buffer[0]++;
- if(Time_Buffer[0]==100)
- {
- Time_Buffer[0]=0; Time_Buffer[1]++;
- }
- if(Time_Buffer[1]==60)//秒
- {
- Time_Buffer[1]=0; Time_Buffer[2]++;
- }
- if(Time_Buffer[2]==60) //分
- {
- Time_Buffer[2]=0; Time_Buffer[3]++;
- }
- if(Time_Buffer[3]==24) //时
- Time_Buffer[3]=0;
- }
- void main()
- {
- uchar i;
- IE=0x82;
- TMOD=0x01;
- TH0=-10000/256;
- TL0=-10000%256;
- LCD_Initialize();
- Display_String(msg1,0x00);
- Display_String(msg2,0x40);
- while(1)
- {
- if(K1==0)
- {
- DelayX(100);
- i=++KeyCount;
- switch(i)
- {
- case 1:
- case 3: TR0=1;
- Display_String(Prompts[i-1],0);
- break;
- case 2:
- case 4: TR0=0;
- Display_String(Prompts[i-1],0);
- break;
- default:TR0=0;
- break;
- }
- while (K1==0);
- Beep();
- }
- else
- if(K2==0)
- {
- TR0=0;
- KeyCount=0;
- for(i=0;i<4;i++)
- Time_Buffer[i]=0;
- Display_String(msg1,0);
- Beep();
- DelayX(100);
- while(K2==0);
- }
- Show_Second();
- }
- }
- /*txt2.c*/
- #include <reg52.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_Write_Data(uchar);
- void LCD_Write_Command(uchar);
- void DelayMS(unit);
- void DelayMS(uint MS)
- {
- uchar t;
- while(MS--) for(t=0;t<120;t++);
- }
- 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;
- }
- 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;
- }
- void LCD_Write_Data(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;
- }
- 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);
- }
- void Display_String(uchar*str,uchar LineNo)
- {
- uchar k;
- LCD_Set_POS(LineNo);
- for(k=0;k<16;k++)
- LCD_Write_Data(str[k]);
- }
复制代码
|