找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 7784|回復(fù): 0
收起左側(cè)

LCD1602 LM016L+DS1302顯示時(shí)間(Proteus仿真+代碼)

[復(fù)制鏈接]
ID:469990 發(fā)表于 2019-3-12 09:02 | 顯示全部樓層 |閱讀模式
無(wú)按鍵操作
# 元器件
LCD1602的元器件是LM016L

# 文件介紹
除了未測(cè)試文件夾下的代碼不能使用,其它三份都可以在仿真圖上使用。
ds1302+lcd1602_1:摘抄自仿真圖鏈接中的代碼;
ds1302+lcd1602_2:是對(duì)仿真圖鏈接代碼的分解版本;
my1302:是修改過(guò)的版本,按照需要條件,可以加入別的項(xiàng)目直接調(diào)用;
未測(cè)試:忘了是從哪摘抄的了,沒有對(duì)應(yīng)得仿真圖,無(wú)法測(cè)試。

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png

單片機(jī)源程序如下:
  1. /*
  2.   時(shí)間:2019/01/30 11:47
  3. */

  4. /*
  5. 51單片機(jī):DS1302+LCD1602 Proteus 仿真程序。
  6. 功能:LCD1602時(shí)鐘與日期的顯示。


  7. 仿真結(jié)果:LCD1602顯示設(shè)定的時(shí)間與日期。

  8. */


  9. #include <reg52.h>

  10. #include "ds1302.h"


  11. /**********LCD1602接口程序**********/


  12. #define LCD_PORT P1//液晶LCD1602數(shù)據(jù)
  13. sbit RS = P2^4;
  14. sbit RW = P2^5;
  15. sbit E= P2^6;

  16. bit ReadRTC_Flag; //讀DS1302標(biāo)志。1為讀 0為不讀。

  17. unsigned char time_buf1[7] = {34,2,16,23,59,50,7}; // 年月日時(shí)分秒周 2014-02-16 23:59:50 7周
  18. unsigned char time_buf[7] ;


  19. char data str1[16]="Date: ? ?";
  20. char data str2[16]="Time: ? ?";


  21. void InitTIMER0(void);//inital timer0

  22. // LCD1602函數(shù)聲明
  23. void Delay_1ms(unsigned char i);
  24. void Delay_10us(unsigned char i);
  25. void Write_Cmd(unsigned char cmd);
  26. void Write_Dat(unsigned char dat);
  27. void Addr_x_y(unsigned char x,bit y);
  28. void Show_Char(unsigned char x,bit y,unsigned char p);
  29. void Show_String(unsigned char x,bit y,char *ptr);
  30. void LCD_Init(void);


  31. void main(void)
  32. {
  33.         LCD_Init();
  34.         DS1302_Init();
  35.         DS1302_Write_Time();
  36.         InitTIMER0();
  37.         //P2=0xff; ? //51默認(rèn)為輸入
  38.         while(1)
  39.         {
  40.                 if(ReadRTC_Flag==1)
  41.                 {
  42.                         ReadRTC_Flag=0;
  43.                         DS1302_Read_Time();
  44.                 }

  45.                 str1[5]=time_buf1[0]/10 + '0'; //年 數(shù)據(jù)的轉(zhuǎn)換,
  46.                 str1[6]=time_buf1[0]%10 + '0'; //因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
  47.                 str1[7]=0x2d; //加入"-"
  48.                 str1[8]=time_buf1[1]/10 + '0'; //月
  49.                 str1[9]=time_buf1[1]%10 + '0';
  50.                 str1[10]=0x2d;
  51.                 str1[11]=time_buf1[2]/10 + '0'; //日
  52.                 str1[12]=time_buf1[2]%10 + '0';
  53.                 str1[13]=0x20;
  54.                 str1[14]='W';
  55.                 str1[15]=time_buf1[6] + '0'; //周幾

  56.                 str2[5]=time_buf1[3]/10 + '0'; //時(shí) 數(shù)據(jù)的轉(zhuǎn)換,
  57.                 str2[6]=time_buf1[3]%10 + '0'; //因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
  58.                 str2[7]=0x3a;//加入"-"
  59.                 str2[8]=time_buf1[4]/10 + '0'; //分
  60.                 str2[9]=time_buf1[4]%10 + '0';
  61.                 str2[10]=0x3a;
  62.                 str2[11]=time_buf1[5]/10 + '0'; //秒
  63.                 str2[12]=time_buf1[5]%10 + '0';


  64.                 Show_String(0,0,str1);
  65.                 Show_String(0,1,str2);
  66.         }
  67. }






  68. /********************************/
  69. void Delay_1ms(unsigned char i) //最小延時(shí)1ms
  70. {
  71.         unsigned char j;
  72.         while(i--)
  73.                 ;
  74.         for(j=0;j<125;j++)
  75.                 ;
  76. }

  77. void Delay_10us(unsigned char i) //最小延時(shí)10us
  78. {
  79.         unsigned char j;
  80.         while(i--)
  81.         for(j=0;j<10; j++)
  82.                 ;
  83. }


  84. void Write_Cmd(unsigned char cmd)//寫指令
  85. {
  86.         Delay_10us(5);
  87.         E=0;
  88.         RS=0;
  89.         RW=0;
  90.         LCD_PORT=cmd;
  91.         Delay_10us(5); //>40us
  92.         E=1;
  93.         Delay_1ms(2); //>150us
  94.         E=0;
  95.         Delay_10us(4); //>25+10us?
  96. }


  97. void Write_Dat(unsigned char dat) //寫數(shù)據(jù)
  98. {
  99.         Delay_10us(5);
  100.         E=0;
  101.         RS=1;
  102.         RW=0;
  103.         LCD_PORT = dat;
  104.         Delay_10us(5);
  105.         E=1;
  106.         Delay_10us(5);
  107.         E=0;
  108.         Delay_10us(4);
  109. }




  110. void Addr_x_y(unsigned char x,bit y) //寫坐標(biāo),定位置
  111. {
  112.         unsigned char temp=0x80; //默認(rèn)最高位:D7為1 即以0x80開始。 ?
  113.         if(y) //y :0為第一行 ?1為第二行
  114.         {
  115.                 temp|=0x40;
  116.         }
  117.         temp|=x;
  118.         Write_Cmd(temp);
  119. }




  120. void Show_Char(unsigned char x,bit y,unsigned char p)
  121. //在指定位置顯示一個(gè)字符。
  122. {
  123.         Addr_x_y(x,y);
  124.         Write_Dat(p);
  125. }


  126. void Show_String(unsigned char x,bit y,char *ptr)
  127. {
  128.         unsigned char i;
  129.         for (i=0;i<16;i++)
  130.         Show_Char(x++,y,*(ptr+i));//循環(huán)顯示16個(gè)字符
  131. }




  132. void LCD_Init(void) //1602初始化代碼
  133. {
  134.         Delay_1ms(1500);
  135.         Write_Cmd(0x38);
  136.         Delay_1ms(5);
  137.         Write_Cmd(0x38);
  138.         Delay_1ms(5);
  139.         Write_Cmd(0x38);
  140.         Delay_1ms(5);
  141.         Write_Cmd(0x38);
  142.         Write_Cmd(0x08);
  143.         Write_Cmd(0x06);
  144.         Write_Cmd(0x0c);
  145.         Write_Cmd(0x01);
  146. }


  147. /*------------------------------------------------
  148. ? ? ? ? ? ?Timer0 初始化,開中斷,2ms定時(shí)
  149. ------------------------------------------------*/
  150. void InitTIMER0(void)
  151. {
  152.         TMOD|=0x01; //定時(shí)器設(shè)置 16位
  153.         TH0=(65536-2000)/256; //定時(shí)時(shí)間 ? 2ms
  154.         TL0=(65536-2000)%256;
  155.         EA=1;
  156.         ET0=1;
  157. ……………………

  158. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
LCD1602+DS1302-201903110942.zip (38.62 KB, 下載次數(shù): 102)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表