极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11123|回复: 1

arduino+1602液晶实现简单电子时钟功能

[复制链接]
发表于 2013-10-26 19:02:08 | 显示全部楼层 |阅读模式

lantpark也曾做过类似的东西(http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1248),用到了http://www.arduino.cc/playground/uploads/Code/DateTime.zip,实现了能显示日期和时间的功能。这几天在学习时,突然想到仅用字符串也能简单的实现电子时钟,时间用delay来计算,做好后,整整跑了一个下午,发现时间的误差几乎没有,还是可以接受的。

lcd 1602 接线采用四线连接法参照弘毅的教程
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=78&reltid=955&pre_thread_id=0&pre_pos=1&ext=


代码如下:
  1.     int LCD1602_RS=12;   
  2.     int LCD1602_RW=11;   
  3.     int LCD1602_EN=10;   
  4.     int DB[] = { 6, 7, 8, 9};
  5.     char logtxt[]="Local Time";
  6.     char *sec[60]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};

  7.    
  8.     void LCD_Command_Write(int command)
  9.     {
  10.     int i,temp;
  11.     digitalWrite( LCD1602_RS,LOW);
  12.     digitalWrite( LCD1602_RW,LOW);
  13.     digitalWrite( LCD1602_EN,LOW);
  14.      
  15.     temp=command & 0xf0;
  16.     for (i=DB[0]; i <= 9; i++)
  17.     {
  18.        digitalWrite(i,temp & 0x80);
  19.        temp <<= 1;
  20.     }
  21.      
  22.     digitalWrite( LCD1602_EN,HIGH);
  23.     delayMicroseconds(1);
  24.     digitalWrite( LCD1602_EN,LOW);
  25.      
  26.     temp=(command & 0x0f)<<4;
  27.     for (i=DB[0]; i <= 9; i++)
  28.     {
  29.        digitalWrite(i,temp & 0x80);
  30.        temp <<= 1;
  31.     }
  32.      
  33.     digitalWrite( LCD1602_EN,HIGH);
  34.     delayMicroseconds(1);
  35.     digitalWrite( LCD1602_EN,LOW);
  36.     }
  37.      
  38.     void LCD_Data_Write(int dat)
  39.     {
  40.     int i=0,temp;
  41.     digitalWrite( LCD1602_RS,HIGH);
  42.     digitalWrite( LCD1602_RW,LOW);
  43.     digitalWrite( LCD1602_EN,LOW);
  44.      
  45.     temp=dat & 0xf0;
  46.     for (i=DB[0]; i <= 9; i++)
  47.     {
  48.        digitalWrite(i,temp & 0x80);
  49.        temp <<= 1;
  50.     }
  51.      
  52.     digitalWrite( LCD1602_EN,HIGH);
  53.     delayMicroseconds(1);
  54.     digitalWrite( LCD1602_EN,LOW);
  55.      
  56.     temp=(dat & 0x0f)<<4;
  57.     for (i=DB[0]; i <= 9; i++)
  58.     {
  59.        digitalWrite(i,temp & 0x80);
  60.        temp <<= 1;
  61.     }
  62.      
  63.     digitalWrite( LCD1602_EN,HIGH);
  64.     delayMicroseconds(1);
  65.     digitalWrite( LCD1602_EN,LOW);
  66.     }
  67.      
  68.     void LCD_SET_XY( int x, int y )
  69.     {
  70.       int address;
  71.       if (y ==0)    address = 0x80 + x;
  72.       else          address = 0xC0 + x;
  73.       LCD_Command_Write(address);
  74.     }
  75.      
  76.     void LCD_Write_Char( int x,int y,int dat)
  77.     {
  78.       LCD_SET_XY( x, y );
  79.       LCD_Data_Write(dat);
  80.     }
  81.      
  82.     void LCD_Write_String(int X,int Y,char *s)
  83.     {
  84.         LCD_SET_XY( X, Y );    //设置地址
  85.         while (*s)             //写字符串
  86.         {
  87.           LCD_Data_Write(*s);   
  88.           s ++;
  89.         }
  90.     }
  91.      
  92.     void setup (void)
  93.     {
  94.       int i = 0;
  95.       for (i=6; i <= 12; i++)
  96.        {
  97.          pinMode(i,OUTPUT);
  98.        }
  99.       delay(100);
  100.       LCD_Command_Write(0x28);//4线 2行 5x7
  101.       delay(50);
  102.       LCD_Command_Write(0x06);
  103.       delay(50);
  104.       LCD_Command_Write(0x0c);
  105.       delay(50);
  106.       LCD_Command_Write(0x80);
  107.       delay(50);
  108.       LCD_Command_Write(0x01);
  109.       delay(50);
  110.      
  111.     }
  112.      
  113.     void loop (void)
  114.     {


  115.        for(int index=0,minindex=59,hourindex=12;index<60;index++)   //这里设定的初始时间为12:59:00
  116.        {
  117.          LCD_Command_Write(0x01);
  118.          delay(50);
  119.          LCD_Write_String(3,0,logtxt);
  120.          LCD_Write_String(3,1,sec[hourindex]);
  121.          LCD_Write_Char(6,1,0x3a);
  122.          LCD_Write_String(7,1,sec[minindex]);
  123.          LCD_Write_Char(10,1,0x3a);
  124.          LCD_Write_String(11,1,sec[index]);

  125.          delay(950);
  126.          if (index==59)
  127.          {
  128.            index=-1;
  129.            minindex++;
  130.            if (minindex==60)
  131.            {
  132.              minindex=0;
  133.              hourindex++;
  134.              if (hourindex==24)
  135.              {
  136.                hourindex=0;
  137.              }
  138.            }

  139.          }
  140.        }
  141.      
  142.     }
复制代码


实现效果
回复

使用道具 举报

发表于 2013-11-5 23:15:28 | 显示全部楼层
感谢分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-13 22:59 , Processed in 0.036398 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表