掉电可保存时钟

#include 
#include "i2c.h"
#include "delay.h"
#include "1602.h"

 //?????,?????????,???????????????
#include

sbit RS = P2^4;   //???? 
sbit RW = P2^5;
sbit EN = P2^6;
sbit spk = P1^2;

#define RS_CLR RS=0 
#define RS_SET RS=1
#define RW_CLR RW=0 
#define RW_SET RW=1 
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
unsigned char shi,fen,miao,tt;
unsigned char table[]="CLOCK !";
unsigned char table1[]="00:00:00";
sbit key1=P3^7;
sbit key2=P3^6;
sbit key3=P3^5;
sbit key4=P3^4;
sbit key5=P3^3;

void DelayUs2x(unsigned char t)
{   
 while(--t);
}

void DelayMs(unsigned char t)
{

 while(t--);

 {
     //????1mS
     DelayUs2x(245);
     DelayUs2x(245);
 }
}

void delay(unsigned int t)
{
    unsigned char j,k;
    for(j = 0; j < 255; j++)
        for(k = 0; k < t; k++)
            ;   
}

 bit LCD_Check_Busy(void) 
 { 
 DataPort= 0xFF; 
 RS_CLR; 
 RW_SET; 
 EN_CLR; 
 _nop_(); 
 EN_SET;
 return (bit)(DataPort & 0x80);
 }

 void LCD_Write_Com(unsigned char com) 
 {  
// while(LCD_Check_Busy()); //????
 DelayMs(5);
 RS_CLR; 
 RW_CLR; 
 EN_SET; 
 DataPort= com; 
 _nop_(); 
 EN_CLR;
 }

 void LCD_Write_Data(unsigned char Data) 
 { 
 //while(LCD_Check_Busy()); //????
 DelayMs(5);
 RS_SET; 
 RW_CLR; 
 EN_SET; 
 DataPort= Data; 
 _nop_();
 EN_CLR;
 }

 void LCD_Clear(void) 
 { 
 LCD_Write_Com(0x01); 
 DelayMs(5);
 }

 void LCD_Init(void) 
 {
   unsigned char i;
   LCD_Write_Com(0x38);    /*??????*/ 
   DelayMs(5); 
   LCD_Write_Com(0x38); 
   DelayMs(5); 
   LCD_Write_Com(0x38); 
   DelayMs(5); 
   LCD_Write_Com(0x38);  
   LCD_Write_Com(0x08);    /*????*/ 
   LCD_Write_Com(0x01);    /*????*/ 
   LCD_Write_Com(0x06);    /*????????*/ 
   DelayMs(5); 
   LCD_Write_Com(0x0C);    /*????????*/
   shi=0;
   fen=0;
   miao=0;
   key1=1;
   key2=1;
   key3=1;
   key4=1;
   key5=1;
   LCD_Write_Com(0x80+5);
   for(i=0;i<7;i++)
   {
   LCD_Write_Data(table[i]);
   }
    LCD_Write_Com(0x80+0x44);
    for(i=0;i<8;i++)
   {
   LCD_Write_Data(table1[i]);
   }
   TMOD=0X01;
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
   EA=1;
   ET0=1;
   TR0=1;
   }

   void write_shifenmiao(unsigned char add,unsigned char Data)
   {
   unsigned char shi,ge;
   shi=Data/10;
   ge=Data%10;
   LCD_Write_Com(0x80+0x40+add);
   LCD_Write_Data(0x30+shi);
   LCD_Write_Data(0x30+ge);
   }

  void time0() interrupt 1
  {
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
   tt++;
   if(tt==20)
   {
   tt=0;
   miao++;
   if(miao==60)
   {
   miao=0;
   fen++;
   if(fen==60)
   {
    spk = 1;
    delay(500);
    spk = 0;

   fen=0;

   shi++;
   if(shi==24)
   {
   shi=0;
   }
   write_shifenmiao(4,shi);
   }
   write_shifenmiao(7,fen);
   }
   write_shifenmiao(10,miao);
   }
   }

   void keyscan()
   {
   if(key1==0)
   {
   DelayMs(5);
   if(key1==0)
   {
   TR0=0;
   shi++;
   if(shi==24)
   {
   shi=0;
   }
   write_shifenmiao(4,shi);
   DelayMs(200);
   TR0=1;
   }
   }
   if(key2==0)
   {
   DelayMs(5);
   if(key2==0)
   {
   TR0=0;
   shi--;
   if(shi>24)
   {
   shi=23;
   }
   write_shifenmiao(4,shi);
   DelayMs(200);
   TR0=1;
   }
   }
    if(key3==0)
   {
   DelayMs(5);
   if(key3==0)
   {
   TR0=0;
   fen++;
   if(fen==60)
   {
   fen=0;
   }
   write_shifenmiao(7,fen);
   DelayMs(200);
   TR0=1;
   }
   }         
    if(key4==0)
   {
   DelayMs(5);
   if(key4==0)
   {
   TR0=0;
   fen--;
   if(fen>24)
   {
   fen=59;
   }
   write_shifenmiao(7,fen);
   DelayMs(200);
   TR0=1;
   }
   }
    if(key5==0)
   {
   DelayMs(5);
   if(key5==0)
   {
   TR0=0;
   miao=0;
   write_shifenmiao(10,miao);
   DelayMs(200);
   TR0=1;
   }
   }
   }         

void main(void) 
{ 
LCD_Init(); 

while (1) 
 {  
 keyscan();
}
 }


你可能感兴趣的:(单片机)