单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:1585|回复:2
打印上一主题下一主题

单片机定时器LCD1602显示简易电子钟代码Proteus仿真图

[复制链接]
跳转到指定楼层
楼主
ID:1057281发表于 2022-12-13 10:49|只看该作者|只看大图回帖奖励
这是一个简单的电子时钟设计,利用LCD1602显示,均为原创,请大家指正
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include<reg51.h>
  2. //#include<lcd1602.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. uchar int_time;
  6. uchar second;
  7. uchar minute;
  8. uchar hour;

  9. void write_data(uchar date);
  10. void write_com(uchar com);
  11. void init1602();
  12. void write_string(uchar *pp, uint n);
  13. void write_sfm(uchar add,uchar date);

  14. uchar code date[]="wwj 2604200321";
  15. uchar code time[]=" TIME  23:59:55 ";
  16. uchar second=55,minute=59,hour=23;

  17. void clock_init(){

  18.      uchar i,j;
  19.      for(i=0;i<16;i++){
  20.          write_data(date[i]);
  21.      }

  22.      write_com(0x80+0x40);
  23.      for(j=0;j<16;j++){
  24.          write_data(time[j]);
  25.      }
  26. }

  27. void clock_write(uint s, uint m, uint h){
  28.      write_sfm(0x47,h);
  29.      write_sfm(0x4a,m);
  30.      write_sfm(0x4d,s);
  31. }
  32. void main(){
  33.      init1602();
  34.      clock_init();
  35.      TMOD=0x01;
  36.      EA=1;
  37.      ET0=1;
  38.      TH0=(65536-46483)/256;
  39.      TL0=(65536- 46483)%256;
  40.      TR0=1;
  41.      int_time=0;
  42.      second=55;
  43.      minute=59;
  44.      hour=23;

  45.      while(1){
  46.          clock_write(second,minute,hour);
  47.      }
  48. }
  49. void T0_interserve(void) interrupt 1 using 1{
  50.      int_time++;
  51.      if(int_time==20){
  52.          int_time=0;
  53.          second++;
  54.      }
  55.      if(second==60){
  56.          second=0;
  57.          minute++;
  58.      }
  59.      if(minute==60){
  60.          minute=0;
  61.          hour ++;
  62.      }
  63.      if(hour==24){
  64.          hour=0;
  65.      }

  66.      TH0=(65536-46083)/256;
  67.      TL0=(65536- 46083)%256;
  68. }

  69. //#ifndef LCD_CHAR_1602_2011_5_9
  70. #define LCD_CHAR_1602_2011_5_9
  71. #define uchar unsigned char
  72. #define uint unsigned int
  73. sbit Icdrs = P2^0;
  74. sbit Icdrw= P2^1;
  75. sbit Icden= P2^2;

  76. void delay(uint z){
  77.      uint x,y;
  78.      for(x=z;x>0;x--){
  79.          for(y=110;y>0;y--);
  80.      }
  81. }

  82. void write_com(uchar com){
  83.      Icdrw=0;
  84.      Icdrs=0;
  85.      P3=com;
  86.      delay(5);
  87.      Icden=1;
  88.      delay(5);
  89.      Icden=0;
  90. }

  91. void write_data(uchar date){
  92.      Icdrw=0;
  93.      Icdrs=1;
  94.      P3=date;
  95.      delay(5);
  96.      Icden=1;
  97.      delay(5);
  98.      Icden=0;
  99. }

  100. void init1602(){
  101.      Icdrw=0;
  102.      Icden=0;
  103.      write_com(0x3C);
  104.      write_com(0x0c);
  105.      write_com(0x06);
  106.      write_com(0x01);
  107.      write_com(0x80);
  108. }

  109. void write_string(uchar *pp, uint n){
  110.      int i;
  111.      for(i=0;i<n;i++){
  112.          write_data(pp[i]);
  113.      }
  114. }

  115. void write_sfm(uchar add,uchar date){
  116.      uchar shi,ge;
  117.      shi=date/10;
  118.      ge=date%10;
  119.      write_com(0x80+add);
  120.      write_data(0x30+shi);
  121.      write_data(0x30+ge);
  122. }
  123. //#endif
复制代码

Keil代码与Proteus8.13仿真下载:
LCD.7z(35.31 KB, 下载次数: 53)

评分

黑币 +50
收起理由
+ 50
共享资料的黑币奖励!

查看全部评分

板凳
ID:884711发表于 2024-5-5 09:38|只看该作者
路过学习,谢谢分享!
沙发
ID:1103226发表于 2023-12-8 08:54|只看该作者
楼主,代码全吗?

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网