⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eclock.c

📁 我自己写的LCD1602 时钟程序
💻 C
字号:
 #include <reg51.h>
 #include <intrins.h>
 #define DataBus P2
 sbit E=P1^0;
 sbit RS=P1^2;
 sbit RW=P1^1;
 sbit BF=P2^7;
 static unsigned char data Cycle;//To produce 1s delay by using 50ms delay, as a static type, Cycle could be checked at any time.
 unsigned char code Table1[]={0x20,0x4d,0x20,0x20,0x20,0x44,0x20,0x20,0x48,0x20,0x20,0x4d,0x20,0x20,0x53,0x20}; // show the clock's lable
 unsigned char code Month[]={0x4a,0x61,0x6e,0x20}; // You can change here manually to go to next month
 struct  Clock {
 unsigned char DateH,DateL;
 unsigned char HourH,HourL;
 unsigned char MinH,MinL;
 unsigned char SecH,SecL;
 }data Time;  // specify the struct type variable for easily and briefly reading

// the main function 
void main (void)
{
 // Function declarations
 void initialization (void);
 void BFcheck(void); // busy flag check function
 void Delay5ms (void);
 void Delay2ms (void);//interval for lcd internal operation
 void Delay50ms(void);
 void Delay1s(void);
 void IRwrite ( unsigned char );//write the instruction register
 void DDRAMwrite ( unsigned char );//write the diplay data ram
 void Displayline1(void);
 void DisplaySec(void);
 void DisplayMin(void);
 void DisplayHour(void);
 void DisplayDate(void);
 void DisplayMonth(void);

 unsigned char Typeconver(unsigned char);
 // initial value for adjusting to the standard time
 Time.DateH=1;
 Time.DateL=3;// change here for initial date value setting
 Time.HourH=Time.HourL=0; //change here for hour value adujsting 
 Time.MinH=Time.MinL=0;
 Time.SecH=Time.SecL=0;

 Delay5ms();
 Delay5ms(); // To meet the SMC1602A LCM initialization requirement
 initialization();


 while(1)
 {
  Displayline1();
  DisplayMonth();
  DisplayDate();
  DisplayHour();
  DisplayMin();
  DisplaySec();
  while(Cycle)
  _nop_();  // hold the display to prevent flicker in second display process
  Cycle=20;
 }
}


void initialization ( void )
{
 unsigned char data delaycnt;
 for(delaycnt=0;delaycnt<3;delaycnt++)
 {
  Delay5ms();
  DataBus=0x38;
  RS=0;
  RW=0;
  E=0;
  Delay2ms( );
  E=1;
 }
 IRwrite(0x38); // set the display mode:2-line display and 5*7 dot mode
 IRwrite(0x0c); // open the display and forbiden the cursor flash
 IRwrite(0x06); // set the address counter's update mode AC++
 IRwrite(0x01); // clear the display including AC and RAM
 }
 

//write the instruction register
void IRwrite (unsigned char temptdata)
{
 DataBus=temptdata;
 RS=0;
 RW=0;
 E=0;
 Delay2ms( ); // set the interval for lcd internal operation ( take the lcm datasheet for reference 
 E=1;
 BFcheck( );
}

//write the diplay data ram
void DDRAMwrite (unsigned char tempt)
{
 DataBus=tempt;
 RS=1;
 RW=0;
 E=0;
 Delay2ms( ); // set the interval for lcd internal operation ( take the lcm datasheet for reference 
 E=1;
 BFcheck( );
}

// To meet the lcm initialization requirements' delay time
void Delay5ms ( void)
{
 TMOD=0x01;
 EA=1;
 ET0=1;
 TH0=0xec;
 TL0=0x77;
 TR0=1;
 while (!TF0);
 TF0=0;
}

//check the busy flag status
void BFcheck (void)
{
 BF=1;
 RS=0;
 RW=1;
 E=0;
 Delay2ms();
 E=1;          // Starts to read the BF status
 while (BF);
 RW=0;
}

//lcm internal operation time
void Delay2ms(void)
{
 TMOD=0x10;
 EA=1;
 ET1=1;
 TH1=0xf8;
 TL1=0x2f;
 TR1=1;
 while (!TF1);
 TF1=0;
}

//display line 1
void Displayline1(void)
{
 unsigned char data cnt1;
 unsigned char *Pointer1;
 Pointer1=&Table1[0];
 IRwrite(0x80); //set the line 1 AC value at the end of the first line
 for(cnt1=16;cnt1>0;cnt1--)
 {
  DDRAMwrite(*Pointer1++);
 }
}

void DisplayMonth(void)
{
 unsigned char data cnt2;
 unsigned char *Pointer2;
 Pointer2=&Month[0]; // set the line 2 AC value
 IRwrite(0xc0);
 for(cnt2=4;cnt2>0;cnt2--)
 {
  DDRAMwrite(*Pointer2++);
 }
}

unsigned char Typeconver(unsigned char tempt) // ASCII code to hex type conversion
{
 unsigned char data resulthex;
 unsigned char data base=0x30;
 resulthex=base+tempt;
 return resulthex;
}



void DisplayDate(void)
{
 
 if(Time.DateL<=9)
 {
  DDRAMwrite(Typeconver(Time.DateH));//display the higher bit of Date
  DDRAMwrite(Typeconver(Time.DateL));//display the lower bit of Date
  DDRAMwrite(0x20); // display a space to keep away from Hour
 }
 else
 {
  Time.DateL=0;
  Time.DateH++;
  DDRAMwrite(Typeconver(Time.DateH));//display the higher bit of Date
  DDRAMwrite(Typeconver(Time.DateL));//display the lower bit of Date
  DDRAMwrite(0x20);
  }
 }



 void DisplayHour(void)
 {
  if((Time.HourH==2)&&(Time.HourL==4))
  {Time.DateL++;
   Time.HourL=0;
   Time.HourH=0;
   DDRAMwrite(Typeconver(Time.HourH));//display the higher bit of hour
   DDRAMwrite(Typeconver(Time.HourL));//display the lower bit of hour
   DDRAMwrite(0x3a); //display the ':'between the Hour and Minute
   Cycle=0;
   }
  else if(Time.HourL>9)
  {Time.HourH++;
   Time.HourL=0;
   DDRAMwrite(Typeconver(Time.HourH));//display the higher bit of hour
   DDRAMwrite(Typeconver(Time.HourL));//display the lower bit of hour
   DDRAMwrite(0x3a); //display the ':'between the Hour and Minute
  }
  else 
  {
   DDRAMwrite(Typeconver(Time.HourH));//display the higher bit of hour
   DDRAMwrite(Typeconver(Time.HourL));//display the lower bit of hour
   DDRAMwrite(0x3a); //display the ':'between the Hour and Minute
  }
 }
   

 void DisplayMin(void)
 {
  if(Time.MinH==6)
  {Time.HourL++;
   Time.MinH=0;
   Time.MinL=0;
   DDRAMwrite(Typeconver(Time.MinH));//display the higher bit of minute
   DDRAMwrite(Typeconver(Time.MinL));//display the lower bit of minute
   DDRAMwrite(0x3a); // display '-' between Min and Second
   Cycle=0;
   }
  else if (Time.MinL>9)
  {Time.MinH++;
   Time.MinL=0;
   DDRAMwrite(Typeconver(Time.MinH));//display the higher bit of minute
   DDRAMwrite(Typeconver(Time.MinL));//display the lower bit of minute
   DDRAMwrite(0x3a); // display '-' between Min and Second
   }
  else 
  {
   DDRAMwrite(Typeconver(Time.MinH));//display the higher bit of minute
   DDRAMwrite(Typeconver(Time.MinL));//display the lower bit of minute
   DDRAMwrite(0x3a); // display '-' between Min and Second
   }
  }


  void DisplaySec(void)
  {

   if (Time.SecH==6)
   {Time.MinL++;
    Time.SecH=0;
	Time.SecL=0;
    DDRAMwrite(Typeconver(Time.SecH));//display the higher bit of second
	DDRAMwrite(Typeconver(Time.SecL));//display the lower bit of second
	DDRAMwrite(0x20); // to display a space
	Cycle=0;
   }

   else if (Time.SecL>9)
   {
    Time.SecH++;
	Time.SecL=0;
    DDRAMwrite(Typeconver(Time.SecH));//display the higher bit of second
	DDRAMwrite(Typeconver(Time.SecL));//display the lower bit of second
	DDRAMwrite(0x20); // to display a space
    Cycle=0;
   }
   
   else 
   {
    DDRAMwrite(Typeconver(Time.SecH));//display the higher bit of second
	DDRAMwrite(Typeconver(Time.SecL));//display the lower bit of second
	DDRAMwrite(0x20); // to display a space
	Time.SecL++;
	Delay1s();
   }
 }




// the origin of the clock

 void Delay50ms(void)
 {
  TMOD=0x01;
  EA=1;
  ET0=1;
  TH0=0x3c;
  TL0=0xaf;
  TR0=1;
  while (!TF0);
  TF0=0;
  }

 void Delay1s(void)
 {
  for(Cycle=19;Cycle>0;Cycle--)
  {
   Delay50ms();
   }
  }
 














  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -