感谢楼主帮助,刚修改了一下代码,实现了我需要的功能。
需要实现的目标:在液晶上以二行的形式显示出时钟,按下k1 后将当前时间存入ds1302的RAM中,时钟正常显示。选择一个按键k2,
按下 k2 后显示按下 k1 的时间,在选择一个按键k3,按下k3 后 ,恢复显示当前时间。
我使用的是普中STC89C52的板子。
直接附代码,希望能帮到和我遇到同样问题的人。没写注释,抱歉~~~~
/*****************************************************************************************/
/****************************************main.c********************************************/
/*****************************************************************************************/
#include "reg52.h"
#include "DS1302.h"
#include "LCD1602.h"
sbit K1 = P3^1;
sbit K2 = P3^0;
sbit K3 = P3^2;
uchar TIMEtemp1[7];
uchar TIMEtemp2[7];
uchar code RAMreadaddr[7] = {0xc1,0xc3,0xc5,0xc7,0xc9,0xcb,0xcd};
uchar code RAMwriteaddr[7] = {0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc};
void LCD1602display();
void delay10ms(void);
void keypress();
void main()
{
LCD1602Init();
DS1302Init();
while(1)
{
DS1302readtime();
LCD1602display();
keypress();
}
}
void LCD1602display()
{
LCDwriteorder(0x80);
LCDwritedata('2');
LCDwritedata('0');
LCDwritedata('0'+TIME[6]/16); //year
LCDwritedata('0'+(TIME[6]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[4]/16); //month
LCDwritedata('0'+(TIME[4]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[3]/16); //day
LCDwritedata('0'+(TIME[3]&0x0f));
LCDwriteorder(0x8D);
LCDwritedata('0'+(TIME[5]&0x07));//week
LCDwriteorder(0x80+0x40);
LCDwritedata('0'+TIME[2]/16); //hour
LCDwritedata('0'+(TIME[2]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[1]/16); //minute
LCDwritedata('0'+(TIME[1]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[0]/16); //second
LCDwritedata('0'+(TIME[0]&0x0f));
}
void delay10ms(void) //10 ms 0 mission
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void keypress()
{
uchar n;
if(K1==0)
{
delay10ms();
if(K1==0)
{
for (n=0; n<7; n++)//读取七个字节的时钟信号:分秒时日月周年
{
TIMEtemp1[n] = DS1302read(RTCreadaddr[n]);
}
DS1302write(0x8E,0x00);
for(n=0;n<7;n++)
{
DS1302write(RAMwriteaddr[n],TIMEtemp1[n]);
}
DS1302write(0x8E,0x80);
}
while(!K1);
}
if(K2==0)
{
delay10ms();
if(K2==0)
{
for(n=0;n<7;n++)
{
TIMEtemp2[n]=DS1302read(RAMreadaddr[n]);
}
while(1)
{
LCDwriteorder(0x80);
LCDwritedata('2');
LCDwritedata('0');
LCDwritedata('0'+TIMEtemp2[6]/16); //year
LCDwritedata('0'+(TIMEtemp2[6]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[4]/16); //month
LCDwritedata('0'+(TIMEtemp2[4]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[3]/16); //day
LCDwritedata('0'+(TIMEtemp2[3]&0x0f));
LCDwriteorder(0x8D);
LCDwritedata('0'+(TIMEtemp2[5]&0x07));//week
LCDwriteorder(0x80+0x40);
LCDwritedata('0'+TIMEtemp2[2]/16); //hour
LCDwritedata('0'+(TIMEtemp2[2]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[1]/16); //minute
LCDwritedata('0'+(TIMEtemp2[1]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[0]/16); //second
LCDwritedata('0'+(TIMEtemp2[0]&0x0f));
if(K3==0)
{
delay10ms();
if(K3==0)
{
LCD1602display();
break;
}
}
}
}
}
}
/*****************************************************************************************/
/****************************************DS1302.h*****************************************/
/*****************************************************************************************/
#define __DS1302_H_
#include "reg52.h"
#include "intrins.h"
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
sbit DSIO = P3^4;
sbit RST = P3^5;
sbit SCLK = P3^6;
void DS1302write(uchar addr,uchar dat);
uchar DS1302read(uchar addr);
void DS1302Init();
void DS1302readtime();
extern uchar TIME[7];
extern uchar code RTCreadaddr[7];
extern uchar code RTCwriteaddr[7];
#endif
/*****************************************************************************************/
/****************************************DS1302.c*****************************************/
/*****************************************************************************************/
#include "DS1302.h"
uchar code RTCreadaddr[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar code RTCwriteaddr[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar TIME[7]={0,0,0,0x14,0x02,0x04,0x19};// 00:00:00 14-2-19
void DS1302write(uchar addr,uchar dat)
{
uchar n;
RST = 0; //CE
_nop_();
SCLK = 0 ;
_nop_();
RST = 1;
_nop_();
for(n=0;n<8;n++) //address
{
DSIO = addr & 0x01; //低位开始传送
addr >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
for(n=0;n<8;n++) //order
{
DSIO = dat & 0x01;
dat >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;
_nop_();
}
uchar DS1302read(uchar addr)
{
uchar n,dat,dat1;
RST = 0;
_nop_();
SCLK = 0;
_nop_();
RST = 1;
_nop_();
for(n=0;n<8;n++)
{
DSIO = addr & 0x01;
addr >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
_nop_();
for(n=0;n<8;n++)
{
dat1 = DSIO;
dat = (dat>>1)|(dat1<<7);
//data1读入数据只有2个值,0x01和0x00,只看0x01那么<<7后变成0x80,
//再看dat假如dat=0x80,>>1后变成0x40, 0x40. /0x80=1100 0000B.
//简单说就是一位一位读入串行数据。
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;
_nop_();
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return dat;
}
void DS1302Init()
{
uchar n;
DS1302write(0x8E,0x00); //禁止写保护
for(n=0;n<7;n++)
{
DS1302write(RTCwriteaddr[n],TIME[n]);
}
DS1302write(0x8E,0x80); //开启写保护
}
void DS1302readtime()
{
uchar n;
for(n=0;n<7;n++)
{
TIME[n] = DS1302read(RTCreadaddr[n]);
}
}
/*****************************************************************************************/
/****************************************LCD1602.h*****************************************/
/*****************************************************************************************/
#ifndef __LCD_H_
#define __LCD_H_
#include "reg52.h"
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#define LCD1602_DATAPINS P0
sbit LCD1602_E = P2^7;
sbit LCD1602_RW = P2^5;
sbit LCD1602_RS = P2^6;
void LCD1602_delay1ms(uint c);
void LCDwriteorder(uchar order);
void LCDwritedata(uchar dat);
void LCD1602Init();
#endif
/*****************************************************************************************/
/****************************************LCD1602.c*****************************************/
/*****************************************************************************************/
#include "LCD1602.h"
void LCD1602_delay1ms(uint c) //1ms , 0 mission
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void LCDwriteorder(uchar order)
{
LCD1602_E = 0; //power open
LCD1602_RS = 0; //send order
LCD1602_RW = 0; //choose write
LCD1602_DATAPINS = order;
LCD1602_delay1ms(1);
LCD1602_E = 1;
LCD1602_delay1ms(5);
LCD1602_E = 0;
}
void LCDwritedata(uchar dat)
{
LCD1602_E = 0;
LCD1602_RS = 1; //send data
LCD1602_RW = 0;
LCD1602_DATAPINS = dat;
LCD1602_delay1ms(1);
LCD1602_E = 1;
LCD1602_delay1ms(5);
LCD1602_E = 0;
}
void LCD1602Init()
{
LCDwriteorder(0x38); //display open
LCDwriteorder(0x0c); // no flash sign
LCDwriteorder(0x06); // one date one point
LCDwriteorder(0x01); // rush screen
LCDwriteorder(0x80); // set point start space
}
/*****************************************************************************************/
/****end********end*********end******end*****end****end*****end********end**********end****/
/*****************************************************************************************/
再次感谢楼主,哈哈哈哈哈哈哈哈~~~~~~~
|