我用STM32 的PD 10.11.12 口分别接DS1302 的SCK,SDA.RST.初始化配置和底层代码如下,我想读出来时间,却读不出来,请各位高手指教!
/*
该程序暂时没有使用时间调整和备用电池部分,所以每次重新上电后可以看到时间恢复到初始值,
该程序用于验证DS1302的工作正常与否。
*/
\\\\\\不知道这样配置模式对不对,仿照 SPI 的\\\\\\
#define RTC_RST PDout(12) // 推挽输出 低电平有效
#define RTC_SDA PDout(11) // 复用 功能推挽输出 模式
#define RTC_SCK PDout(10) // 复用 功能推挽输出 模式
#include "stm32f10x_lib.h"
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "rtc.h"
#include "stdio.h"
unsigned char RTC_Content[7];
unsigned char l_tmpdate[7]={55,59,55,31,12,7,9};//秒分时日月周年
unsigned char l_tmpdisplay[8];
unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//共阴数码管 0-9 '-' '熄灭‘表
// unsigned char table1[]={0,1,2,3,4,5,6,7};
//显示位码表
void Rtc_Init(void)
{
RCC->APB2ENR|=1<<5; //使能PORTD 时钟
GPIOD->CRH&=0XFFF000FF;
GPIOD->CRH|=0X0003BB00; // PD 10. 11 复用功能推挽输出模式 PD 12 推挽输出
GPIOD->ODR|=0X1C00; // 写高
Write_Ds1302(0x8E,0x80); //加保护
}
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++) //循环8次 写入数据
{
RTC_SCK=0;
RTC_SDA=temp&0x01; //每次传输低字节
temp>>=1; //右移一位
RTC_SCK=1;
}
}
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RTC_RST=0;
__nop();
RTC_SCK=0;
__nop();
RTC_RST=1;
__nop(); //启动
Write_Ds1302_Byte(address); //发送地址
Write_Ds1302_Byte(dat); //发送数据
RTC_RST=0; //恢复
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RTC_RST=0;
__nop();
RTC_SCK=0;
__nop();
RTC_RST=1;
__nop();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循环8次 读取数据
{
if(RTC_SDA)
temp|=0x80; //每次传输低字节
RTC_SCK=0;
temp>>=1; //右移一位
RTC_SCK=1;
}
RTC_RST=0; __nop(); //以下为DS1302复位的稳定时间
RTC_RST=0;
RTC_SCK=0; __nop();
RTC_SCK=1; __nop();
RTC_SDA=0; __nop();
RTC_SDA=1; __nop();
return (temp); //返回
}
/****************************************************************************/
void Read_RTC(void) //读取 日历
{
unsigned char i,*p;
p=read_rtc_address; //地址传递
for(i=0;i<7;i++) //分7次读取 秒分时日月周年
{
RTC_Content=Read_Ds1302(*p);
p++;
}
TransByte(USART1,RTC_Content[0]); // 这一句我是想把 秒的变化,通过串口 反映出来,可是 都是 7F,不知道怎么回事??
}
/***********************************************************************/
void Set_RTC(void) //设定 日历
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++){ //BCD处理 0X85 0X89
tmp=l_tmpdate/10;
l_tmpdate=l_tmpdate%10;
l_tmpdate=l_tmpdate+tmp*16;
}
Write_Ds1302(0x8E,0X00); //去除写保护
p=write_rtc_address; //传地址
for(i=0;i<7;i++) //7次写入 秒分时日月周年
{
Write_Ds1302(*p,l_tmpdate);
p++;
}
Write_Ds1302(0x8E,0x80); //加保护
}
//////下面是主函数
int main()
{
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
uart1_init(72,9600); //串口初始化为9600
Rtc_Init();
Set_RTC();
while(1)
{
Read_RTC();
}
我是想看一下读出来的秒的变化,通过串口看的,不管时间再长都是7F,请各位高手指点,谢谢!
|