标题:
单片机+DS1302+LCD2004A显示问题 一直卡在00-01-01 00:00:80
[打印本页]
作者:
631945815
时间:
2020-1-4 13:56
标题:
单片机+DS1302+LCD2004A显示问题 一直卡在00-01-01 00:00:80
在仿真中DS1302配合lcd没有问题,下载到实物时间就跑不动了一直卡在00-01-01 00:00:80。有大佬帮忙看看吗
LCD2004A.c:
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#include "lcd2004.h"
/*外部接口函数在lcd2004.h中声明*/
/*****************内部函数******************/
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.
补充:这里的“10”是经过测试的,测试中,利用串口打印
可知,“清除屏幕显示”命令“lcd2004CleanAll()”用时最长,
此命令下i值为1,这里设置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 :清除屏幕显示,光标归位(左上角),地址计数器AC设为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 :光标归位,当屏幕移动显示时,lcd2004显示所有数据后,
调用此函数,屏幕显示的所有东西都会归位。光标在第一
个位置(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 :行位置
列位置
数据
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) //定时器2初始化
{
RCAP2H=0x3c;
RCAP2L=0xb0;
TR2=1;
ET2=1;
EA=1;
}
复制代码
全部资料51hei下载地址:
DS1302 加 LCD2004a.7z
(51.18 KB, 下载次数: 16)
2020-1-4 13:56 上传
点击文件名下载附件
作者:
angmall
时间:
2020-1-5 07:45
仿真能行实物不行多数是硬件搭建有错漏。仿真中AT89c52和ds1302不接电源、晶振和复位电路照样运转正常,系统默认其存在。而实物实验缺少一样也不行。
把时钟芯片ds1302换了试试 或换一个晶振试试。
作者:
631945815
时间:
2020-1-6 16:59
angmall 发表于 2020-1-5 07:45
仿真能行实物不行多数是硬件搭建有错漏。仿真中AT89c52和ds1302不接电源、晶振和复位电路照样运转正常,系 ...
好的,我试试谢谢dalao
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1