×
单片机 > 单片机程序设计 > 详情

1602LCD液晶如何编写数字电子钟

发布时间:2020-06-19 发布时间:
|
    以前,学习单片机,都没有开发板,因此感觉编写数字电子钟的程序,是非常难的,简直就是不知道如何着手。 现在已经对单片机各种操作都逐渐熟悉了,因此刚刚就已经实现了自己关于数字电子钟的一点小小的设计思想
 
   由于时间关系,现在已经比较晚了,所以就暂时不编写按键功能,但是已经实现了动态计时功能了,明天再继续添加按键功能。
#include
#include"MyFuntion.h"  //自定义头文件
 
unsigned char year1=20; // 年1
unsigned char year2=12; // 年2
unsigned char month=1;   // 月
unsigned char day=22;    // 日
unsigned char hour=23;   // 时
unsigned char minute=12; // 分
unsigned char second=56; // 秒
unsigned char table1[10];
unsigned char table2[8];
unsigned char t; //定时器T0 发生中断次数
//unsigned char t1; //定时器T1 发生中断次数
/*
//独立按键P1口
sbit Key1=P3^0; //Key1 取消调时 恢复单片机调时之前的实际时间
sbit Key2=P3^1; //Key2 进入调整时间状态: 停止定时器T0,启动定时器T1.
sbit Key3=P3^2; //Key3 退出调整时间状态: 启动定时器T0,停止定时器T1.
sbit Key4=P3^3; //Key4 选择调整时间: 秒, 分, 时, 日, 月, 年
sbit Key5=P3^4; //Key5 调时: 递增  同时启动蜂鸣器
sbit Key6=P3^5; //Key6 Key6 调时: 递减  同时启动蜂鸣器
*/
 
void Init_Table_YMD()  //年月日
//进行对 时间的转换 以致于可以把时间发送到1602LCD显示
{
//把 year, month, day 转换为一个字符存储在数组table1
table1[0]=year1/10;
table1[1]=year1%10;
table1[2]=year2/10;
table1[3]=year2%10;
table1[4]='-';
 
    table1[5]=month/10;
table1[6]=month%10;
table1[7]='-';
 
table1[8]=day/10;
table1[9]=day%10;
}
void Init_Table_HMS() // 时分秒
{
//把 hour, minute, second 转换为一个字符存储在数组table2
table2[0]=hour/10;
table2[1]=hour%10;
table2[2]=':';
 
table2[3]=minute/10;
table2[4]=minute%10;
table2[5]=':';
 
table2[6]=second/10;
table2[7]=second%10;
}
 
void IncreaseTime() // 秒钟 递增
{
   second++;
 
   if(second==60)
   {
  second=0;
  minute++;
  if(minute==60)
  {
     minute=0;
 hour++;
 if(hour==24)
 {
    hour=0;
 }
  }
   }
 
   Init_Table_HMS(); //时分秒
   DisplayLCD_HMS(table2, 8); //LCD显示时间 时分秒
}
 
void main()
{
   TMOD=0X01;
   EA=1;
   ET0=1;
 //  ET1=1;
   TR0=1; //启动定时器T0
//   TR1=0; //停止定时器T1 即还没有启动定时器T1
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
 
   InitLCD(); //初始化LCD
   Init_Table_YMD(); // LCD 时间表 年月日
   Init_Table_HMS(); // LCD 时间表 时分秒
   DisplayLCD_YMD(table1, 10); //LCD显示时间 年月日
   DisplayLCD_HMS(table2, 8); //LCD显示时间 时分秒
   while(1)
   {
  if(t==20)
  {
    t=0;
IncreaseTime();
  }
   }
   
  // while(1);
 
void LCD_Timer0() interrupt 1 using 0
{
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
 
   t++;
}

关键字:1602LCD液晶  数字电子钟 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM基础知识八