freetech

aw16读写ds1302时时时钟芯片的程序例程

0
阅读(2038)

具体的协议参考数据手册 ds1302。
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
//////////////////////定义变量和声明函数/////////////////////
#define b_clk PTCD_PTCD2 
#define b_da PTCD_PTCD1
#define b_rst PTCD_PTCD0
#define b_da_in PTCDD_PTCDD1=0;
#define b_da_out PTCDD_PTCDD1=1;
void IO_INI(void);
void RTInputByte(uchar send);
uchar RTOutputByte(void);
void W1302(uchar ucAddr, uchar ucDa);
uchar R1302(uchar ucAddr);
void delay(unsigned int x);
void RTINIT(void);
byte send;
byte second;
byte minute_ten;
byte minute_ge;
byte hour_ten;
byte hour_ge;
byte minute;
byte hour;
byte day;
byte month;
byte week;
byte year;
byte ucCurtime[8];
///////////////////////主函数////////////////////////////////
void main(void) { 

IO_INI(); 
PTCDD_PTCDD0=1; 
PTCDD_PTCDD1=1; 
PTCDD_PTCDD2=1; 
b_clk=0; 
b_da=0; 
b_rst=0; 
RTINIT(); //第一次需要启动,以后就不用了,主要功能是设置ds1302的初值 
second=R1302(0X81);
minute=R1302(0X83);
hour=R1302(0X85);

day=R1302(0X87);
month=R1302(0X89);
week=R1302(0X8b);
year=R1302(0X8d);

EnableInterrupts; /* enable interrupts */
/* include your code here */

for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
//delay(10);
minute_ten=R1302(0X83)/16;
minute_ge=R1302(0X83)%16;
hour_ten=R1302(0X85)/16;
hour_ge=R1302(0X85)%16;
} /* loop forever */
/* please make sure that you never leave main */
}

///////////////////////IO初始化///////////////////////////////
void IO_INI(void){
PTCDD_PTCDD0 = 1; /* Set PTA0PTA1 as output */
//PTCDD_PTCDD1 = 1;
PTCDD_PTCDD2 = 1;
}
/******************************************************************** 
函 数 名:RTInputByte()
功 能:实时时钟写入一字节
说 明:往DS1302写入1Byte数据 (内部函数)
入口参数:d 写入的数据 
返 回 值:无

***********************************************************************/
void RTInputByte(uchar send) 
{ byte num,c; 
num=send;
PTCDD_PTCDD1 = 1;
PTCD_PTCD2=1;

for(c=0;c<8;c++){ 

if(num&0x01)
PTCD_PTCD1=1;
else
PTCD_PTCD1=0;

PTCD_PTCD0=1;
PTCD_PTCD0=0;

num>>=1; 
}

/******************************************************************** 

函 数 名:RTOutputByte()
功 能:实时时钟读取一字节
说 明:从DS1302读取1Byte数据 (内部函数)
入口参数:无 
返 回 值:ACC
***********************************************************************/
uchar RTOutputByte(void) 
{ unsigned char num=0,c;

PTCDD_PTCDD1 = 0;

PTCD_PTCD2=1;


for(c=0;c<8;c++){ 

num>>=1;

if(PTCD_PTCD1&0x01) {

num=num|0x80; 
}
else {
num=num&0x7f; 
}


PTCD_PTCD0=1;
PTCD_PTCD0=0; 

}
return(num);

}
/******************************************************************** 

函 数 名:W1302()
功 能:往DS1302写入数据
说 明:先写地址,后写命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址, ucData: 要写的数据
返 回 值:无
***********************************************************************/
void W1302(uchar ucAddr, uchar ucDa)
{
PTCD_PTCD2=0;
PTCD_PTCD0=0; 
PTCD_PTCD2=1;
RTInputByte(ucAddr); /* 地址,命令 */
RTInputByte(ucDa); /* 写1Byte数据*/

PTCD_PTCD0=1;
PTCD_PTCD2=0; 


/******************************************************************** 
函 数 名:R1302()
功 能:读取DS1302某地址的数据
说 明:先写地址,后读命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址
返 回 值:ucData :读取的数据 
***********************************************************************/
uchar R1302(uchar ucAddr)
{ unsigned char ucData;
PTCD_PTCD2=0;
PTCD_PTCD0=0; 
PTCD_PTCD2=1;

RTInputByte(ucAddr); /* 地址,命令 */
ucData = RTOutputByte(); /* 读1Byte数据 */

PTCD_PTCD0=1;
PTCD_PTCD2=0;
return(ucData);
}
/***********************************************************************/
void v_Get1302(uchar ucCurtime[]) 
{
uchar i;
uchar ucAddr = 0x81;
for (i=0;i<7;i++)
{
ucCurtime = R1302(ucAddr);/*格式为: 秒 分 时 日 月 
星期 年 */
ucAddr += 2;
}

/***************************延时函数*******************************/
void delay(unsigned int x){
unsigned int i,j;

for(i=0;i<x;i++) { 
__RESET_WATCHDOG();
for(j=0;j<250;j++) 
{;}
}

/***************************RT初始化函数*******************************/
void RTINIT(void) {
W1302(0x8e, 0x00);

W1302(0x80, 0x00);
W1302(0x82, 0x44);
W1302(0x84, 0x10);
W1302(0x86, 0x31);
W1302(0x88, 0x10);
W1302(0x8a, 0x03);
W1302(0x8c, 0x07);
W1302(0x8e, 0x00);
}