本帖最后由 Nivans 于 2015-9-1 20:19 编辑
各位大神帮帮忙,目前已经调试了2天,程序完全按照时序图来做的。其他的地方都正常,但是上电初始化读取秒寄存器来判断是否断过电,之后就无法读出数据了,读出的数据都是零,数码管显示的都是0。无法查找原因。代码如下。
**************************************Main.c
#include "system.h"
#include "ds1302.h"
sbit LS138A=P2^2;
sbit LS138B=P2^3;
sbit LS138C=P2^4;
bit ReadRTC_Flag = 0;
uint8 l_tmpdisplay[8];
code uint8 table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//共阴数码管 0-9 '-' '熄灭‘表
cTime RealTime;
void InitTIMER0(void)
{
TMOD|=0x01;//定时器设置 16位
TH0=0xef;//初始化值
TL0=0xf0;
ET0=1;
TR0=1;
EA=1;
}
void main(void)
{
ds1302_Init();
InitTIMER0(); //初始化定时器0
while(1)
{
if(ReadRTC_Flag)
{
GetTime(&RealTime);
l_tmpdisplay[0]=RealTime.hour/16; //数据的转换,因我们采用数码管0~9的显示,将数据分开
l_tmpdisplay[1]=RealTime.hour&0x0f;
l_tmpdisplay[2]=10; //加入"-"
l_tmpdisplay[3]=RealTime.min/16;
l_tmpdisplay[4]=RealTime.min&0x0f;
l_tmpdisplay[5]=10;
l_tmpdisplay[6]=RealTime.sec/16;
l_tmpdisplay[7]=RealTime.sec&0x0f;
}
}
return;
}
void tim(void) interrupt 1 using 1
//中断,用于数码管扫描
{
static uint8 i,num;
TH0=0xf5;
TL0=0xe0;
P0=table[l_tmpdisplay[i]]; //查表法得到要显示数字的数码段
switch(i)
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;
}
i++;
if(i==8)
{
i=0;
num++;
if(10==num) //隔段时间读取1302的数据。时间间隔可以调整
{
ReadRTC_Flag=1; //使用标志位判断
num=0;
}
}
}
[code]***************************ds1302.c
#include "system.h"
#include "ds1302.h"
/*---------------------------------------------
/* DS1302 Read Byte
/*-------------------------------------------*/
uint8 ReadByte()
{
uint8 i,tmp = 0x00;
io = 1; //IO 置1
for(i = 0; i < 8 ; i ++)
{
if(io) tmp|=0x80;
sclk = 1 ;
sclk = 0;
tmp >>= 1;
}
return tmp;
}
/*---------------------------------------------
DS1302 WriteByte()
--------------------------------------------*/
void WriteByte(uint8 dat)
{
uint8 i;
for(i = 0; i < 8 ; i ++)
{
io = (bit)(dat & 0x01);
sclk = 0;
sclk = 1;
dat >>= 1;
}
}
/*---------------------------------------------
/* DS1302 SingleRead 单字节读
/*-------------------------------------------*/
uint8 SingleRead(uint8 addr)
{
uint8 dat;
cs = 0;
sclk = 0;
cs = 1;
WriteByte(addr);
dat = ReadByte();
sclk = 0;
cs = 0;
return(dat);
}
/*----------------------------------------------
/* DS1302 SingleWrite 单字节写
/---------------------------------------------*/
void SingleWrite(uint8 addr,uint8 dat)
{
cs = 0;
sclk = 0;
cs = 1;
WriteByte(addr);
WriteByte(dat);
sclk = 0;
cs = 0;
}
/*---------------------------------------------
/* DS1302 MultiRead 多字节读
/*-------------------------------------------*/
void MultiRead(uint8 *ptr)
{
uint8 i;
cs = 0;
sclk = 0;
cs = 1;
WriteByte(0xbf);
for(i = 0; i < 8 ; i ++)
{
ptr[i] = ReadByte();
}
sclk = 0;
cs = 0;
}
/*---------------------------------------------
/* DS1302 MultiWrite 多字节读
/*-------------------------------------------*/
void MultiWrite(uint8 *ptr)
{
uint8 i;
cs = 0;
sclk = 0;
cs = 1;
WriteByte(0xbe);
for(i = 0; i < 8 ; i ++)
{
WriteByte(ptr[i]);
}
sclk = 0;
cs = 0;
}
/*-----------------------------------------------
/* DS1302 GetTime
/*---------------------------------------------*/
void GetTime(cTime *p)
{
uint8 tmp[8];
MultiRead(tmp); //多字节读,传地址方式返回时间、日期
(*p).sec = tmp[0];
(*p).min = tmp[1];
(*p).hour = tmp[2];
(*p).date = tmp[3];
(*p).month = tmp[4];
(*p).week = tmp[5];
(*p).year = tmp[6];
}
/*-----------------------------------------------
/* DS1302 SetTime
/*---------------------------------------------*/
void SetTime(cTime *p)
{
uint8 tmp[8];
tmp[0] = (*p).sec;
tmp[1] = (*p).min;
tmp[2] = (*p).hour;
tmp[3] = (*p).date ;
tmp[4] = (*p).month;
tmp[5] = (*p).week ;
tmp[6] = (*p).year ;
tmp[7] = 0x00;
MultiWrite(tmp); //多字节写,传地址方式返回时间、日期
}
/*-----------------------------------------------
/* DS1302 INIT
/*---------------------------------------------*/
void ds1302_Init(void)
{
cTime code DefaultTime [] = {0x06,0x10,0x09,0x31,0x08,0x01,0x15};
if(SingleRead(0x81)&0x80)
{
SingleWrite(0x8e,0x00); //解除读写保护
SetTime(DefaultTime);
}
}
[/code]
*********************************DS1302.H
#ifndef _DS1302_H
#define _DS1302_H
typedef struct Timelist
{
uint8 sec; //秒
uint8 min; //分
uint8 hour; //时
uint8 date; //日
uint8 month; //月
uint8 week; //星期
uint8 year; //年
}cTime; //定义一个时间数据结构
/* 函数声明 */
void ds1302_Init(void);
void GetTime(cTime *);
/* 端口定义 */
sbit cs = P3^5; //片选使能
sbit sclk = P3^6; //时钟线
sbit io = P3^7; //串行数据端口
#endif
*****************************SYSTEM.H
#ifndef _STSTEM_H
#define _SYSTEM_H
#include <reg52.h> //8052头文件
#include <intrins.h>
/*-------------------------------------------------*/
/* 自定义类型 */
/*-------------------------------------------------*/
typedef signed char int8;
typedef signed int int16;
typedef signed long int32;
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
#endif
|