|
在仿真中DS1302配合lcd沒有問題,下載到實(shí)物時(shí)間就跑不動了一直卡在00-01-01 00:00:80。有大佬幫忙看看嗎
LCD2004A.c:
- #include <reg52.h>
- #include <intrins.h>
- #include <string.h>
- #include "lcd2004.h"
- /*外部接口函數(shù)在lcd2004.h中聲明*/
- /*****************內(nèi)部函數(shù)******************/
- static void delay1msForLcd2004(void) ;
- static UB8 lcd2004ReadStatus(void) ;
- static void lcd2004CheckBusy(void) ;
- /**********************************************/
- /******************************************************
- Function :delay1msForLcd2004
- Input :N/A
- Output :N/A
- Return :N/A
- Description :N/A
- Note :crystal frequency is 11.0592MHZ
- ******************************************************/
- static void delay1msForLcd2004(void)
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 12;
- j = 168;
- do
- {
- while (--j);
- } while (--i);
-
- }
- /******************************************************
- Function :lcd2004ReadStatus
- Input :N/A
- Output :N/A
- Return :lcd2004 status value
- Description :N/A
- Note :N/A
- ******************************************************/
- static UB8 lcd2004ReadStatus(void)
- {
- UB8 statusCode ;
- lcd2004_en_bit = LCD2004_DISABLE ;
-
- lcd2004_rs_bit = LCD2004_COMMAND_OPERATION ;
- lcd2004_rw_bit = LCD2004_READ_OPERATION ;
- LCD2004_DATA_PORT = 0xff ;
- delay1msForLcd2004() ;
- lcd2004_en_bit = LCD2004_ENABLE ;
- delay1msForLcd2004() ;
- statusCode = LCD2004_DATA_PORT ;
-
- lcd2004_en_bit = LCD2004_DISABLE ;
- return statusCode ;
- }
- /******************************************************
- Function :lcd2004CheckBusy
- Input :N/A
- Output :N/A
- Return :N/A
- Description :check lcd2004 busy or free,if busy,wait !
- Note :when LCD2004_DATA_PORT[7]==1,lcd2004 is busy,
- LCD2004_DATA_PORT[7]==0,lcd2004 is free.
-
- 補(bǔ)充:這里的“10”是經(jīng)過測試的,測試中,利用串口打印
- 可知,“清除屏幕顯示”命令“l(fā)cd2004CleanAll()”用時(shí)最長,
- 此命令下i值為1,這里設(shè)置i為10,足夠大了
- ******************************************************/
- static void lcd2004CheckBusy(void)
- {
- UW16 i=0 ;
- do{
- i++;
- }while( (lcd2004ReadStatus() & 0x80) && (i<10));
- }
- /******************************************************
- Function :lcd2004WriteCommand
- Input :command code
- Output :N/A
- Return :N/A
- Description :write command to lcd2004
- Note :N/A
- ******************************************************/
- void lcd2004WriteCommand(UB8 commandCode)
- {
-
- lcd2004CheckBusy();
-
- lcd2004_en_bit = LCD2004_DISABLE ;
-
- lcd2004_rs_bit = LCD2004_COMMAND_OPERATION ;/*command*/
- lcd2004_rw_bit = LCD2004_WRITE_OPERATION ; /*write*/
- LCD2004_DATA_PORT = commandCode ;
- //delay1msForLcd2004();
- lcd2004_en_bit = LCD2004_ENABLE ;
- //delay1msForLcd2004();
- lcd2004_en_bit = LCD2004_DISABLE ;
- }
- /******************************************************
- Function :lcd2004WriteData
- Input :data code
- Output :N/A
- Return :void
- Description :write data to lcd2004
- Note :N/A
- ******************************************************/
- void lcd2004WriteData(UB8 dataCode)
- {
-
- lcd2004CheckBusy() ;
- lcd2004_en_bit = LCD2004_DISABLE ;
-
- lcd2004_rs_bit = LCD2004_DATA_OPERATION ; /*data*/
- lcd2004_rw_bit = LCD2004_WRITE_OPERATION ; /*write*/
- LCD2004_DATA_PORT = dataCode;
- //delay1msForLcd2004();
- lcd2004_en_bit = LCD2004_ENABLE ;
- //delay1msForLcd2004();
- lcd2004_en_bit = LCD2004_DISABLE ;
- }
- /******************************************************
- Function :lcd2004CleanAll
- Input :N/A
- Output :N/A
- Return :N/A
- Description :clean lcd2004 display
- Note :清除屏幕顯示,光標(biāo)歸位(左上角),地址計(jì)數(shù)器AC設(shè)為0
- ******************************************************/
- void lcd2004CleanAll(void)
- {
- lcd2004WriteCommand(LCD2004_CLEAN_ALL_DISPALY);
- }
- /******************************************************
- Function :lcd2004CursorHoming
- Input :N/A
- Output :N/A
- Return :N/A
- Description :curosr homing to the 0x80
- Note :光標(biāo)歸位,當(dāng)屏幕移動顯示時(shí),lcd2004顯示所有數(shù)據(jù)后,
- 調(diào)用此函數(shù),屏幕顯示的所有東西都會歸位。光標(biāo)在第一
- 個位置(0x80)。
- ******************************************************/
- void lcd2004CursorHoming(void)
- {
- lcd2004WriteCommand(LCD2004_CURSOR_RETURN_TO_ORIGIN);
- }
- /******************************************************
- Function :lcd2004Init
- Input :N/A
- Output :N/A
- Return :N/A
- Description :N/A
- Note :N/A
- ******************************************************/
- void lcd2004Init(void)
- {
- lcd2004_en_bit = LCD2004_DISABLE ;
-
- lcd2004CleanAll();
- lcd2004WriteCommand(LCD2004_DEFAULT_DISPALY_MODE);
- lcd2004WriteCommand(LCD2004_DEFAULT_DISPLAY_AND_CURSOR_MODE);
- lcd2004WriteCommand(LCD2004_DEFAULT_POINT_AND_POINT_ADDRESS_MODE);
- /*可忽略,在lcd2004CleanAll()中隱含了該功能*/
- lcd2004WriteCommand(LCD2004_ROW0_ADDRESS_START);
-
- lcd2004_en_bit = LCD2004_DISABLE ;
- }
- /******************************************************
- Function :lcd2004AddressWriteByte
- Input :行位置
- 列位置
- 數(shù)據(jù)
- Output :N/A
- Return :N/A
- Description :N/A
- Note :N/A
- ******************************************************/
- void lcd2004AddressWriteByte(UB8 row,UB8 column,UB8 dataCode)
- {
-
- if(row == LCD2004_ROW0)
- {
- lcd2004WriteCommand(LCD2004_ROW0_ADDRESS_START+column) ;
- }
- else if(row == LCD2004_ROW1)
- {
- lcd2004WriteCommand(LCD2004_ROW1_ADDRESS_START+column) ;
-
- }
- else if(row == LCD2004_ROW2)
- {
- lcd2004WriteCommand(LCD2004_ROW2_ADDRESS_START+column) ;
- }
- else if(row == LCD2004_ROW3)
- {
- lcd2004WriteCommand(LCD2004_ROW3_ADDRESS_START+column) ;
- }
-
- lcd2004WriteData(dataCode);
- }
- /******************************************************
- Function :lcd2004AddressWriteString
- Input :首個字符顯示的行,列地址,字符串
- Output :N/A
- Return :N/A
- Description :N/A
- Note :這里使用的是strlen,而不是sizeof
- ******************************************************/
- void lcd2004AddressWriteString(UB8 row,UB8 column,UB8 *stringCode)
- {
- UB8 length = strlen(stringCode) ;
-
- if(row == LCD2004_ROW0)
- {
- lcd2004WriteCommand(LCD2004_ROW0_ADDRESS_START+column) ;
- }
- else if(row == LCD2004_ROW1)
- {
- lcd2004WriteCommand(LCD2004_ROW1_ADDRESS_START+column) ;
-
- }
- if(row == LCD2004_ROW2)
- {
- lcd2004WriteCommand(LCD2004_ROW2_ADDRESS_START+column) ;
- }
- else if(row == LCD2004_ROW3)
- {
- lcd2004WriteCommand(LCD2004_ROW3_ADDRESS_START+column) ;
-
- }
- while(length--)
- {
- lcd2004WriteData(*stringCode);
- stringCode++;
- }
- }
- [b]DS1302.C:[/b]
- #include <reg52.h>
- #include <stdlib.h>
- #include "common.h"
- #include "lcd2004.h"
- #include <intrins.h>
- #include "ds1302.h"
- UB8 ds[]={0x30,0x30,0x22,0x08,0x02,0x05,0x19};
- UB8 shijian[7];
- UB8 ds_date[]={"00-00-00 00:00:00"};
- void timezh()
- {
- dsreadtime();
- time(shijian[6],ds_date+0);
- time(shijian[4],ds_date+3);
- time(shijian[3],ds_date+6);
-
- time(shijian[2],ds_date+9);
- time(shijian[1],ds_date+12);
- time(shijian[0],ds_date+15);
-
- }
- void time(UB8 d,UB8 *a)
- {
- a[0]=d/10+'0';
- a[1]=d%10+'0';
- }
- void dsreadtime()
- {
- UB8 i;
- UB8 add=0x81;
- dswrite(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- shijian[i]=dsread(add);
- add=add+2;
- }
- dswrite(0x8e,0x80);
- }
- char dsread(UB8 add)
- {
- UB8 i,temp,dat1,dat2;
- CE=0;
- SCLK=0;
- CE=1;
- dswritebyte(add);
- for(i=0;i<8;i++)
- {
- SCLK=0;
- temp>>=1;
- if(IO)
- {
- temp|=0x80;
- }
- SCLK=1;
- }
- IO=0;
- dat1=temp/16;
- dat2=temp%16;
- temp=dat1*10+dat2;
-
- return temp;
- }
- void dswritebyte(UB8 dat)
- {
- UB8 i;
- for(i=0;i<8;i++)
- {
- SCLK=0;
- IO=dat&0X01;
- SCLK=1;
- dat>>=1;
- }
- }
- void dswrite(UB8 add,UB8 dat)
- {
- UB8 num;
- CE=0;
- SCLK=0;
- CE=1;
- dswritebyte(add);
- num=(dat/10<<4)|(dat%10);
- dswritebyte(num);
- CE=0;
- }
- void dsinit()
- {
- UB8 i,add=0x80;
- dswrite(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- dswrite(add,ds[i]);
- add+=2;
- }
- dswrite(0x8e,0x80);
- }
- [b]主程序:[/b]
- #include <reg52.h>
- #include <stdlib.h>
- #include "common.h"
- #include "lcd2004.h"
- #include "ds1302.h"
- #include "i2c.h"
- void lnit_timer2(void) ;
- void main(void)
- {
- UW16 i;
- lcd2004Init();
- lnit_timer2();
- // eeprom_rw();
- // rs_time();
- while(1)
- {
-
- for(i=0 ; i<17; i++)
- {
-
- lcd2004AddressWriteByte(LCD2004_ROW0,i,ds_date[i]);
- }
- // for(i=0 ; i<17; i++)
- // {
- //
- // lcd2004AddressWriteByte(LCD2004_ROW1,i,sds_date[i]);
- // }
- lcd2004AddressWriteString(LCD2004_ROW1,0,"1.Add member");
- lcd2004AddressWriteString(LCD2004_ROW2,0,"2.Delete member");
- lcd2004AddressWriteString(LCD2004_ROW3,0,"3.View record");
- };
- }
- void Timer2() interrupt 5 //中斷
- {
- UB8 t;
- TF2=0;
- t++;
- if(t==5) //間隔200ms(50ms*4)
- {
- t=0;
- timezh();
- }
- }
- void lnit_timer2(void) //定時(shí)器2初始化
- {
- RCAP2H=0x3c;
- RCAP2L=0xb0;
- TR2=1;
- ET2=1;
- EA=1;
- }
復(fù)制代碼 全部資料51hei下載地址:
DS1302 加 LCD2004a.7z
(51.18 KB, 下載次數(shù): 16)
2020-1-4 13:56 上傳
點(diǎn)擊文件名下載附件
|
|