ds1302实时时钟串口发送,时间显示不动
时间:10-02
整理:3721RD
点击:
#include <ioCC2530.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
//#include "ds1302.h"
#include <string.h>
#define SCK P0_4
#define SDA P0_5
#define RST P0_6
//复位脚
#define RST_CLR RST=0//电平置低
#define RST_SET RST=1//电平置高
#define uint unsigned int
#define uchar unsigned char
//双向数据
#define IO_CLR SDA=0//电平置低
#define IO_SET SDA=1//电平置高
#define IO_R SDA //电平读取
//时钟信号
#define SCK_CLR SCK=0//时钟信号
#define SCK_SET SCK=1//电平置高
#define ds1302_sec_add 0x80 //秒数据地址
#define ds1302_min_add 0x82 //分数据地址
#define ds1302_hr_add 0x84 //时数据地址
#define ds1302_date_add 0x86 //日数据地址
#define ds1302_month_add 0x88 //月数据地址
#define ds1302_day_add 0x8a //星期数据地址
#define ds1302_year_add 0x8c //年数据地址
#define ds1302_control_add 0x8e //控制数据地址
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
//extern unsigned char time_buf1[8];//空年月日时分秒周
//extern unsigned char time_buf[8] ;//空年月日时分秒周
/*------------------------------------------------
向DS1302写入一字节数据
------------------------------------------------*/
void Ds1302_Write_Byte(unsigned char addr, unsigned char d);
/*------------------------------------------------
从DS1302读出一字节数据
------------------------------------------------*/
unsigned char Ds1302_Read_Byte(unsigned char addr) ;
/*------------------------------------------------
向DS1302写入时钟数据
------------------------------------------------*/
void Ds1302_Write_Time(void) ;
/*------------------------------------------------
从DS1302读出时钟数据
------------------------------------------------*/
void Ds1302_Read_Time(void) ;
/*------------------------------------------------
DS1302初始化
------------------------------------------------*/
void Ds1302_Init(void);
//void UartTX_Send_String(char *Data,int len);
void UartSend_String(char *Data,int len);
char time_buf2[16];
char TempData[8]; //存储显示值的全局变量
char time_buf1[8] = {20,21,6,5,17,18,00,6};//空年月日时分秒周
char time_buf[8] ; //空年月日时分秒周
void delay()
{
int i=100;
while(i--);
}
/*------------------------------------------------
向DS1302写入一字节数据
------------------------------------------------*/
void Ds1302_Write_Byte(unsigned char addr, unsigned char d)
{
unsigned char i;
RST_SET;
delay();
//写入目标地址:addr
addr = addr & 0xFE; //最低位置零
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
IO_SET;
delay();
}
else
{
IO_CLR;
delay();
}
SCK_SET;
delay();
SCK_CLR;
delay();
addr = addr >> 1;
}
//写入数据:d
for (i = 0; i < 8; i ++)
{
if (d & 0x01)
{
IO_SET;
delay();
}
else
{
IO_CLR;
delay();
}
SCK_SET;
delay();
SCK_CLR;
delay();
d = d >> 1;
}
RST_CLR;
delay();//停止DS1302总线
}
/*------------------------------------------------
从DS1302读出一字节数据
------------------------------------------------*/
unsigned char Ds1302_Read_Byte(unsigned char addr)
{
unsigned char i;
unsigned char temp;
RST_SET;
delay();
//写入目标地址:addr
addr = addr | 0x01;//最低位置高
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
IO_SET;
delay();
}
else
{
IO_CLR;
delay();
}
SCK_SET;
delay();
SCK_CLR;
delay();
addr = addr >> 1;
}
//输出数据:temp
P0DIR&=(1<<5);
delay();
for (i = 0; i < 8; i ++)
{
temp = temp >> 1;
if (IO_R)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
SCK_SET;
delay();
SCK_CLR;
delay();
}
P0DIR|=(1<<5);
RST_CLR; //停止DS1302总线
delay();
return temp;
}
/*------------------------------------------------
向DS1302写入时钟数据
------------------------------------------------*/
void Ds1302_Write_Time(void)
{
/**
* 设定时钟数据
其中涉及二进制和BCD转换 因为ds1302存BCD ,但是机器只认识二进制
例如:存入ds1302为50 0101 0000 那么我们写入的是ds1302_write(80)
*/
unsigned char i,tmp;
for(i=0;i<8;i++)
{ //BCD处理转化为二进制
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
Ds1302_Write_Byte(ds1302_control_add,0x00); //关闭写保护
Ds1302_Write_Byte(ds1302_sec_add,0x80); //暂停
//Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充电
Ds1302_Write_Byte(ds1302_year_add,time_buf[1]); //年
Ds1302_Write_Byte(ds1302_month_add,time_buf[2]); //月
Ds1302_Write_Byte(ds1302_date_add,time_buf[3]); //日
//Ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
Ds1302_Write_Byte(ds1302_hr_add,time_buf[4]); //时
Ds1302_Write_Byte(ds1302_min_add,time_buf[5]); //分
Ds1302_Write_Byte(ds1302_sec_add,time_buf[6]); //秒
Ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
Ds1302_Write_Byte(ds1302_control_add,0x80); //打开写保护
}
/*------------------------------------------------
从DS1302读出时钟数据
------------------------------------------------*/
void Ds1302_Read_Time(void)
{
//unsigned char i,tmp;
time_buf[1]=Ds1302_Read_Byte(ds1302_year_add); //年
time_buf[2]=Ds1302_Read_Byte(ds1302_month_add); //月
time_buf[3]=Ds1302_Read_Byte(ds1302_date_add); //日
time_buf[4]=Ds1302_Read_Byte(ds1302_hr_add); //时
time_buf[5]=Ds1302_Read_Byte(ds1302_min_add); //分
time_buf[6]=(Ds1302_Read_Byte(ds1302_sec_add))&0x7F; //秒
time_buf[7]=Ds1302_Read_Byte(ds1302_day_add); //周
/*
for(i=0;i<8;i++)
{ //BCD处理
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
// UartTX_Send_String(time_buf1,16);
}
*/
}
/*------------------------------------------------
DS1302初始化
------------------------------------------------*/
void Ds1302_Init(void)
{
RST_CLR; //RST脚置低
delay();
SCK_CLR; //SCK脚置低
delay();
Ds1302_Write_Byte(ds1302_sec_add,0x00);
}
/****************************************************************
串口初始化函数
****************************************************************/
void initUARTSEND(void)
{
CLKCONCMD &= 0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定
CLKCONCMD &= 0x47; //设置系统主时钟频率为32MHZ
PERCFG = 0x00; //位置1 P0口
P0SEL = 0x3c; //P0_2,P0_3,P0_4,P0_5用作串口
P2DIR &= 0XC0; //P0优先作为UART0
U0CSR |= 0x80; //UART方式
U0GCR |= 9;
U0BAUD |= 59; //波特率设为19200
UTX0IF = 0; //UART0 TX中断标志初始置位0
}
/****************************************************************
串口发送字符串函数
****************************************************************/
/**
* UART发送一字节
*/
void UART_Send_Byte(unsigned char dat)
{
U0DBUF=dat;
while(!UTX0IF);
UTX0IF=0;
}
/**
* 将数据转换成ASC码并通过UART发送出去
*/
void UART_Send_Dat(uchar dat) //注意是关于16取余
{
UART_Send_Byte(dat/16+'0');
UART_Send_Byte(dat%16+'0');
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main (void)
{
Ds1302_Write_Time() ;
initUARTSEND();
Ds1302_Init();
while (1) //主循环
{
Ds1302_Read_Time();
// UartSend_String(time_buf1,20);
UART_Send_Dat(time_buf1[1]);
UART_Send_Byte('-');
UART_Send_Dat(time_buf1[2]);
UART_Send_Byte('-');
UART_Send_Dat(time_buf1[3]);
UART_Send_Byte(' ');
UART_Send_Dat(time_buf1[4]);
UART_Send_Byte(':');
UART_Send_Dat(time_buf1[5]);
UART_Send_Byte(':');
UART_Send_Dat(time_buf1[6]);
UART_Send_Byte('\r');
UART_Send_Byte('\n');
delay();
}
}