找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

單片機(jī)+DS1302+LCD2004A顯示問題 一直卡在00-01-01 00:00:80

[復(fù)制鏈接]
ID:680973 發(fā)表于 2020-1-4 13:56 | 顯示全部樓層 |閱讀模式
在仿真中DS1302配合lcd沒有問題,下載到實(shí)物時(shí)間就跑不動了一直卡在00-01-01 00:00:80。有大佬幫忙看看嗎

LCD2004A.c:
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <string.h>
  4. #include "lcd2004.h"

  5. /*外部接口函數(shù)在lcd2004.h中聲明*/

  6. /*****************內(nèi)部函數(shù)******************/
  7. static void delay1msForLcd2004(void) ;
  8. static UB8 lcd2004ReadStatus(void) ;
  9. static void lcd2004CheckBusy(void) ;

  10. /**********************************************/


  11. /******************************************************
  12. Function        :delay1msForLcd2004
  13. Input                :N/A
  14. Output                :N/A
  15. Return                :N/A
  16. Description        :N/A
  17. Note                :crystal frequency is 11.0592MHZ
  18. ******************************************************/
  19. static void delay1msForLcd2004(void)
  20. {
  21.         unsigned char i, j;

  22.         _nop_();
  23.         _nop_();
  24.         i = 12;
  25.         j = 168;
  26.         do
  27.         {
  28.                 while (--j);
  29.         } while (--i);        
  30.         
  31. }

  32. /******************************************************
  33. Function        :lcd2004ReadStatus
  34. Input                :N/A
  35. Output                :N/A
  36. Return                :lcd2004 status value
  37. Description        :N/A
  38. Note                :N/A
  39. ******************************************************/
  40. static UB8 lcd2004ReadStatus(void)
  41. {
  42.         UB8 statusCode ;

  43.         lcd2004_en_bit = LCD2004_DISABLE ;
  44.         
  45.         lcd2004_rs_bit = LCD2004_COMMAND_OPERATION ;
  46.         lcd2004_rw_bit = LCD2004_READ_OPERATION ;

  47.         LCD2004_DATA_PORT = 0xff ;
  48.         delay1msForLcd2004() ;
  49.         lcd2004_en_bit = LCD2004_ENABLE ;
  50.         delay1msForLcd2004() ;
  51.         statusCode = LCD2004_DATA_PORT ;
  52.         
  53.         lcd2004_en_bit = LCD2004_DISABLE ;

  54.         return statusCode ;
  55. }

  56. /******************************************************
  57. Function        :lcd2004CheckBusy
  58. Input                :N/A
  59. Output                :N/A
  60. Return                :N/A
  61. Description        :check lcd2004 busy or free,if busy,wait !
  62. Note                :when LCD2004_DATA_PORT[7]==1,lcd2004 is busy,
  63.                                   LCD2004_DATA_PORT[7]==0,lcd2004 is free.
  64.                                  
  65.                         補(bǔ)充:這里的“10”是經(jīng)過測試的,測試中,利用串口打印
  66.                         可知,“清除屏幕顯示”命令“l(fā)cd2004CleanAll()”用時(shí)最長,
  67.                         此命令下i值為1,這里設(shè)置i為10,足夠大了
  68. ******************************************************/
  69. static void lcd2004CheckBusy(void)
  70. {
  71.         UW16 i=0 ;
  72.         do{
  73.                 i++;
  74.         }while( (lcd2004ReadStatus() & 0x80) && (i<10));
  75. }

  76. /******************************************************
  77. Function        :lcd2004WriteCommand
  78. Input                :command code
  79. Output                :N/A
  80. Return                :N/A
  81. Description        :write command to lcd2004
  82. Note                :N/A
  83. ******************************************************/
  84. void lcd2004WriteCommand(UB8 commandCode)
  85. {
  86.         
  87.         lcd2004CheckBusy();
  88.         
  89.         lcd2004_en_bit = LCD2004_DISABLE ;
  90.         
  91.         lcd2004_rs_bit = LCD2004_COMMAND_OPERATION ;/*command*/
  92.         lcd2004_rw_bit = LCD2004_WRITE_OPERATION ;        /*write*/

  93.         LCD2004_DATA_PORT = commandCode ;
  94.         //delay1msForLcd2004();
  95.         lcd2004_en_bit = LCD2004_ENABLE ;
  96.         //delay1msForLcd2004();

  97.         lcd2004_en_bit = LCD2004_DISABLE ;        
  98. }


  99. /******************************************************
  100. Function        :lcd2004WriteData
  101. Input                :data code
  102. Output                :N/A
  103. Return                :void
  104. Description        :write data to lcd2004
  105. Note                :N/A
  106. ******************************************************/
  107. void lcd2004WriteData(UB8 dataCode)
  108. {        
  109.         
  110.         lcd2004CheckBusy() ;

  111.         lcd2004_en_bit = LCD2004_DISABLE ;
  112.         
  113.         lcd2004_rs_bit = LCD2004_DATA_OPERATION ;        /*data*/
  114.         lcd2004_rw_bit = LCD2004_WRITE_OPERATION ;        /*write*/

  115.         LCD2004_DATA_PORT = dataCode;
  116.         //delay1msForLcd2004();
  117.         lcd2004_en_bit = LCD2004_ENABLE ;
  118.         //delay1msForLcd2004();

  119.         lcd2004_en_bit = LCD2004_DISABLE ;           
  120. }


  121. /******************************************************
  122. Function        :lcd2004CleanAll
  123. Input                :N/A
  124. Output                :N/A
  125. Return                :N/A
  126. Description        :clean lcd2004 display
  127. Note                :清除屏幕顯示,光標(biāo)歸位(左上角),地址計(jì)數(shù)器AC設(shè)為0
  128. ******************************************************/
  129. void lcd2004CleanAll(void)
  130. {
  131.         lcd2004WriteCommand(LCD2004_CLEAN_ALL_DISPALY);
  132. }

  133. /******************************************************
  134. Function        :lcd2004CursorHoming
  135. Input                :N/A
  136. Output                :N/A
  137. Return                :N/A
  138. Description        :curosr homing to the 0x80
  139. Note                :光標(biāo)歸位,當(dāng)屏幕移動顯示時(shí),lcd2004顯示所有數(shù)據(jù)后,
  140.                          調(diào)用此函數(shù),屏幕顯示的所有東西都會歸位。光標(biāo)在第一
  141.                          個位置(0x80)。
  142. ******************************************************/
  143. void lcd2004CursorHoming(void)
  144. {
  145.         lcd2004WriteCommand(LCD2004_CURSOR_RETURN_TO_ORIGIN);
  146. }

  147. /******************************************************
  148. Function        :lcd2004Init
  149. Input                :N/A
  150. Output                :N/A
  151. Return                :N/A
  152. Description        :N/A
  153. Note                :N/A
  154. ******************************************************/
  155. void lcd2004Init(void)
  156. {
  157.         lcd2004_en_bit = LCD2004_DISABLE ;
  158.         
  159.         lcd2004CleanAll();
  160.         lcd2004WriteCommand(LCD2004_DEFAULT_DISPALY_MODE);
  161.         lcd2004WriteCommand(LCD2004_DEFAULT_DISPLAY_AND_CURSOR_MODE);
  162.         lcd2004WriteCommand(LCD2004_DEFAULT_POINT_AND_POINT_ADDRESS_MODE);

  163.         /*可忽略,在lcd2004CleanAll()中隱含了該功能*/
  164.         lcd2004WriteCommand(LCD2004_ROW0_ADDRESS_START);
  165.         
  166.         lcd2004_en_bit = LCD2004_DISABLE ;
  167. }

  168. /******************************************************
  169. Function        :lcd2004AddressWriteByte
  170. Input                :行位置
  171.                          列位置
  172.                          數(shù)據(jù)
  173. Output                :N/A
  174. Return                :N/A
  175. Description        :N/A
  176. Note                :N/A
  177. ******************************************************/
  178. void lcd2004AddressWriteByte(UB8 row,UB8 column,UB8 dataCode)
  179. {
  180.         
  181.         if(row == LCD2004_ROW0)
  182.         {
  183.                 lcd2004WriteCommand(LCD2004_ROW0_ADDRESS_START+column) ;
  184.         }
  185.         else if(row == LCD2004_ROW1)
  186.         {
  187.                 lcd2004WriteCommand(LCD2004_ROW1_ADDRESS_START+column) ;
  188.                
  189.         }
  190.         else if(row == LCD2004_ROW2)
  191.         {
  192.                 lcd2004WriteCommand(LCD2004_ROW2_ADDRESS_START+column) ;
  193.         }
  194.         else if(row == LCD2004_ROW3)
  195.         {
  196.                 lcd2004WriteCommand(LCD2004_ROW3_ADDRESS_START+column) ;
  197.         }
  198.         
  199.         lcd2004WriteData(dataCode);
  200. }

  201. /******************************************************
  202. Function        :lcd2004AddressWriteString
  203. Input                :首個字符顯示的行,列地址,字符串
  204. Output                :N/A
  205. Return                :N/A
  206. Description        :N/A
  207. Note                :這里使用的是strlen,而不是sizeof
  208. ******************************************************/
  209. void lcd2004AddressWriteString(UB8 row,UB8 column,UB8 *stringCode)
  210. {
  211.         UB8 length = strlen(stringCode) ;
  212.         
  213.         if(row == LCD2004_ROW0)
  214.         {
  215.                 lcd2004WriteCommand(LCD2004_ROW0_ADDRESS_START+column) ;
  216.         }
  217.         else if(row == LCD2004_ROW1)
  218.         {
  219.                 lcd2004WriteCommand(LCD2004_ROW1_ADDRESS_START+column) ;
  220.                
  221.         }
  222.         if(row == LCD2004_ROW2)
  223.         {
  224.                 lcd2004WriteCommand(LCD2004_ROW2_ADDRESS_START+column) ;
  225.         }
  226.         else if(row == LCD2004_ROW3)
  227.         {
  228.                 lcd2004WriteCommand(LCD2004_ROW3_ADDRESS_START+column) ;
  229.                
  230.         }
  231.         while(length--)
  232.         {
  233.                 lcd2004WriteData(*stringCode);
  234.                 stringCode++;
  235.         }
  236. }

  237. [b]DS1302.C:[/b]

  238. #include <reg52.h>  
  239. #include <stdlib.h>  
  240. #include "common.h"  
  241. #include "lcd2004.h"  
  242. #include <intrins.h>
  243. #include "ds1302.h"

  244. UB8 ds[]={0x30,0x30,0x22,0x08,0x02,0x05,0x19};
  245. UB8 shijian[7];
  246. UB8 ds_date[]={"00-00-00 00:00:00"};

  247. void timezh()
  248. {
  249.         dsreadtime();
  250.         time(shijian[6],ds_date+0);
  251.         time(shijian[4],ds_date+3);
  252.         time(shijian[3],ds_date+6);
  253.         
  254.         time(shijian[2],ds_date+9);
  255.         time(shijian[1],ds_date+12);
  256.         time(shijian[0],ds_date+15);
  257.         
  258. }
  259. void time(UB8 d,UB8 *a)
  260. {
  261.         a[0]=d/10+'0';
  262.         a[1]=d%10+'0';
  263. }
  264. void dsreadtime()
  265. {
  266.         UB8 i;
  267.         UB8 add=0x81;
  268.         dswrite(0x8e,0x00);
  269.         for(i=0;i<7;i++)
  270.         {
  271.                 shijian[i]=dsread(add);
  272.                 add=add+2;
  273.         }
  274.         dswrite(0x8e,0x80);        
  275. }

  276. char dsread(UB8 add)
  277. {
  278.         UB8 i,temp,dat1,dat2;
  279.         CE=0;
  280.         SCLK=0;
  281.         CE=1;
  282.         dswritebyte(add);
  283.         for(i=0;i<8;i++)
  284.         {
  285.                 SCLK=0;
  286.                 temp>>=1;
  287.                 if(IO)
  288.                 {
  289.                         temp|=0x80;
  290.                 }
  291.                 SCLK=1;
  292.         }
  293.         IO=0;

  294.         dat1=temp/16;
  295.         dat2=temp%16;
  296.         temp=dat1*10+dat2;
  297.         
  298.         return temp;
  299. }

  300. void dswritebyte(UB8 dat)
  301. {
  302.         UB8 i;
  303.         for(i=0;i<8;i++)
  304.         {
  305.                 SCLK=0;
  306.                 IO=dat&0X01;
  307.                 SCLK=1;
  308.                 dat>>=1;
  309.         }
  310. }

  311. void dswrite(UB8 add,UB8 dat)
  312. {
  313.         UB8 num;
  314.         CE=0;
  315.         SCLK=0;
  316.         CE=1;
  317.         dswritebyte(add);
  318.         num=(dat/10<<4)|(dat%10);
  319.         dswritebyte(num);
  320.         CE=0;
  321. }
  322. void dsinit()
  323. {
  324.         UB8 i,add=0x80;
  325.         dswrite(0x8e,0x00);
  326.         for(i=0;i<7;i++)
  327.         {
  328.                 dswrite(add,ds[i]);
  329.                 add+=2;
  330.         }               
  331.         dswrite(0x8e,0x80);        
  332. }


  333. [b]主程序:[/b]
  334. #include <reg52.h>  
  335. #include <stdlib.h>  
  336. #include "common.h"  
  337. #include "lcd2004.h"  
  338. #include "ds1302.h"
  339. #include "i2c.h"
  340. void lnit_timer2(void) ;

  341. void main(void)  
  342. {   
  343.         UW16 i;
  344.     lcd2004Init();
  345.         lnit_timer2();
  346. //        eeprom_rw();
  347. //        rs_time();
  348.     while(1)
  349.         {
  350.         
  351.     for(i=0 ; i<17; i++)  
  352.     {  
  353.                
  354.         lcd2004AddressWriteByte(LCD2004_ROW0,i,ds_date[i]);  
  355.     }
  356. //          for(i=0 ; i<17; i++)  
  357. //    {  
  358. //               
  359. //        lcd2004AddressWriteByte(LCD2004_ROW1,i,sds_date[i]);  
  360. //    }
  361.     lcd2004AddressWriteString(LCD2004_ROW1,0,"1.Add member");  
  362.     lcd2004AddressWriteString(LCD2004_ROW2,0,"2.Delete member");  
  363.     lcd2004AddressWriteString(LCD2004_ROW3,0,"3.View record");  
  364.         };     
  365. }
  366. void Timer2() interrupt 5  //中斷
  367. {
  368.         UB8 t;
  369.         TF2=0;
  370.         t++;
  371.         if(t==5)    //間隔200ms(50ms*4)
  372.         {
  373.                 t=0;
  374.                 timezh();
  375.         }
  376. }
  377. void lnit_timer2(void) //定時(shí)器2初始化
  378. {
  379.         RCAP2H=0x3c;
  380.         RCAP2L=0xb0;
  381.         TR2=1;
  382.         ET2=1;
  383.         EA=1;
  384. }

復(fù)制代碼
全部資料51hei下載地址:
DS1302 加 LCD2004a.7z (51.18 KB, 下載次數(shù): 16)

回復(fù)

使用道具 舉報(bào)

ID:155507 發(fā)表于 2020-1-5 07:45 | 顯示全部樓層
仿真能行實(shí)物不行多數(shù)是硬件搭建有錯漏。仿真中AT89c52和ds1302不接電源、晶振和復(fù)位電路照樣運(yùn)轉(zhuǎn)正常,系統(tǒng)默認(rèn)其存在。而實(shí)物實(shí)驗(yàn)缺少一樣也不行。

把時(shí)鐘芯片ds1302換了試試 或換一個晶振試試。
回復(fù)

使用道具 舉報(bào)

ID:680973 發(fā)表于 2020-1-6 16:59 | 顯示全部樓層
angmall 發(fā)表于 2020-1-5 07:45
仿真能行實(shí)物不行多數(shù)是硬件搭建有錯漏。仿真中AT89c52和ds1302不接電源、晶振和復(fù)位電路照樣運(yùn)轉(zhuǎn)正常,系 ...

好的,我試試謝謝dalao
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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