ds1302.c
来自「STC51系列的源码」· C语言 代码 · 共 277 行
C
277 行
/********************************************************************************/
/* filename : DS1302.c */
/* created : xillinx */
/* descript : 时钟芯片的处理函数 */
/********************************************************************************/
#include "../CPU/CPU.h"
#include "../DS1302/DS1302.h"
st_ds1302_time DS1302_time;
unsigned char DS1302_second;
/********************************************************************************/
/* function : DS1302_set_clk() */
/* recension: xillinx */
/* descript : 设置或者清除DS1302的CLK引脚 */
/********************************************************************************/
void DS1302_set_clk(unsigned char clk)
{ if(clk)
{ c51_U19_latch|=0x02;
}
else
{ c51_U19_latch&=0xFD;
}
c51_hc138_cs_io2[0x0]=c51_U19_latch;
}
/********************************************************************************/
/* function : DS1302_set_sel() */
/* recension: xillinx */
/* descript : 设置或者清除DS1302的CS引脚 */
/********************************************************************************/
void DS1302_set_sel(unsigned char sel)
{ if(sel)
{ c51_U19_latch|=0x01;
}
else
{ c51_U19_latch&=0xFE;
}
c51_hc138_cs_io2[0x0]=c51_U19_latch;
}
/********************************************************************************/
/* function : DS1302_set_dat() */
/* recension: xillinx */
/* descript : 设置或者清除DS1302的DATA引脚 */
/********************************************************************************/
void DS1302_set_dat(unsigned char dat)
{ DS1302_DIO=dat;
}
/********************************************************************************/
/* function : DS1302_get_dat() */
/* recension: xillinx */
/* descript : 设置或者清除DS1302的DATA引脚 */
/********************************************************************************/
unsigned char DS1302_get_dat(void)
{ return DS1302_DIO;
}
/********************************************************************************/
/* funname : DS1302_delay() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 简单的时间延时 */
/********************************************************************************/
void DS1302_delay_max(void)
{ _nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void DS1302_delay_min(void)
{ _nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
/********************************************************************************/
/* funname : DS1302_open() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 打开DS1302 */
/********************************************************************************/
void DS1302_open(void)
{
DS1302_set_clk(0x0);
DS1302_delay_min();
DS1302_set_sel(0x0); //* 禁止DS1302
DS1302_delay_min();
DS1302_set_sel(0x1); //* 使能DS1302
}
/********************************************************************************/
/* funname : DS1302_close() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 关闭DS1302 */
/********************************************************************************/
void DS1302_close(void)
{ DS1302_set_sel(0x0); //* 禁止DS1302
DS1302_delay_min();
DS1302_set_dat(0x1); //* 释放数据总线
DS1302_delay_min();
DS1302_set_clk(0x1);
}
/********************************************************************************/
/* funname : DS1302_write_8_bit() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 写入8位数据 */
/********************************************************************************/
void DS1302_write_8_bit(unsigned char mdata)
{ unsigned char i;
for(i=0x0; i<0x8; i++)
{ if(mdata&0x1)
{ DS1302_set_dat(0x1);
}
else
{ DS1302_set_dat(0x0);
}
DS1302_delay_min();
DS1302_set_clk(0x1); //* 上升沿写入数据
DS1302_delay_max();
DS1302_set_clk(0x0);
DS1302_delay_min();
mdata >>= 0x1;
}
}
/********************************************************************************/
/* funname : DS1302_read_8_bit() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 读取8位数据 */
/********************************************************************************/
unsigned char DS1302_read_8_bit(void)
{ unsigned char i,mdata=0x0;
DS1302_set_dat(0x1);
for(i=0x0; i<0x8; i++)
{ mdata >>=0x1;
DS1302_delay_min();
if(DS1302_get_dat()==0x1)
{ mdata|=0x80;
}
DS1302_set_clk(0x1);
DS1302_delay_max();
DS1302_set_clk(0x0);
DS1302_delay_min();
}
return mdata;
}
/********************************************************************************/
/* funname : DS1302_read_byte() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 读DS1302字节 */
/********************************************************************************/
unsigned char DS1302_read_byte(unsigned char address)
{ unsigned char mdata=0x0;
DS1302_open(); //* 打开DS1302
DS1302_write_8_bit(address|0x1);//* 写入DS1302命令
mdata=DS1302_read_8_bit(); //* 读入DS1302数据
DS1302_close(); //* 关闭DS1302
return mdata; //* 返回数据
}
/********************************************************************************/
/* funname : DS1302_write_byte() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 写DS1302字节 */
/********************************************************************************/
void DS1302_write_byte(unsigned char address, unsigned char mdata)
{ DS1302_open(); //* 打开DS1302
DS1302_write_8_bit(address);//* 写地址
DS1302_write_8_bit(mdata); //* 写数据
DS1302_close(); //* 关闭DS1302
}
/********************************************************************************/
/* funname : DS1302_read_ram_byte() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 读DS1302的RAM字节 */
/********************************************************************************/
unsigned char DS1302_read_ram_byte(unsigned char address)
{ unsigned char mdata;
DS1302_open(); //* 打开DS1302
DS1302_write_8_bit((address<<0x1)|0xC1); //* 写入DS1302命令
mdata=DS1302_read_8_bit(); //* 读入DS1302数据
DS1302_close(); //* 关闭DS1302
return mdata;
}
/********************************************************************************/
/* funname : DS1302_read_ram_byte() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 写DS1302的RAM字节 */
/********************************************************************************/
void DS1302_write_ram_byte(unsigned char address, unsigned char mdata)
{ DS1302_open();
DS1302_write_8_bit((address<<0x1)|0xC0);
DS1302_write_8_bit(mdata);
DS1302_close();
}
/********************************************************************************/
/* funname : DS1302_set_protect() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS1302是否写保护 */
/********************************************************************************/
void DS1302_set_protect(unsigned char flag)
{ if(flag)
{ DS1302_write_byte(DS1302_CONTROL,0x80); //* 0x8E 控制字节地址,bit7=WP WP=1 禁止数据写入DS1302
}
else
{ DS1302_write_byte(DS1302_CONTROL,0x00); //* WP=0 允许写入DS1302
}
}
/********************************************************************************/
/* funname : DS1302_start() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS1302开始工作 */
/********************************************************************************/
void DS1302_start(void)
{ unsigned char mdata;
mdata =DS1302_read_byte(DS1302_SECOND);//* 读秒数据
mdata&=0x7F; //* 打开晶振
DS1302_set_protect(0x0); //* 开放DS1302写保护
DS1302_write_byte(DS1302_SECOND,mdata);//打开晶振
DS1302_set_protect(0x1); //使能DS1302写保护
}
/********************************************************************************/
/* funname : DS1302_get_time() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS1302开始工作 */
/********************************************************************************/
void DS1302_get_time(void)
{ unsigned char mdata;
memset(&DS1302_time,0x0,sizeof(st_ds1302_time));
mdata = DS1302_read_byte(DS1302_SECOND);
DS1302_time.second = ((mdata&0x70)>>4)*10 + (mdata&0x0F);
mdata = DS1302_read_byte(DS1302_MINUTE);
DS1302_time.minute = ((mdata&0x70)>>4)*10 + (mdata&0x0F);
mdata = DS1302_read_byte(DS1302_HOUR);
DS1302_time.hour = ((mdata&0x10)>>4)*10 + (mdata&0x0F);
mdata = DS1302_read_byte(DS1302_DAY);
DS1302_time.day = ((mdata&0x70)>>4)*10 + (mdata&0x0F);
mdata = DS1302_read_byte(DS1302_WEEK);
DS1302_time.week = ((mdata&0x70)>>4)*10 + (mdata&0x0F);
mdata = DS1302_read_byte(DS1302_MONTH);
DS1302_time.month = ((mdata&0x70)>>4)*10 + (mdata&0x0F);
mdata = DS1302_read_byte(DS1302_YEAR);
DS1302_time.year = ((mdata&0x70)>>4)*10 + (mdata&0x0F);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?