新聞中心

基于C8051F的DS1302程序

作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏

//===============================================
//函數(shù)名稱:byte Ds1302_Read_Byte ( byte addr )
//功能: 串行讀取數(shù)據(jù),先發(fā)低位,且在下降沿發(fā)送
//參數(shù)傳遞:有,地址
//返回值: 有,讀取的數(shù)據(jù)
//===============================================
uchar Ds1302_Read_Byte ( uchar addr )
{
uchar i;
uchar temp;
CE = 1;

本文引用地址:http://m.butianyuan.cn/article/201612/324202.htm

//寫入目標(biāo)地址:addr
addr = addr | 0x01; //最低位置高
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
SCLK = 1;
SCLK = 0;
addr = addr >> 1;
}

//輸出數(shù)據(jù):temp
//DIOIN; //數(shù)據(jù)端口定義為輸入
for (i = 0; i < 8; i ++)
{
temp = temp >> 1;
if (DIO)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
SCLK = 1;
SCLK = 0;
}
CE = 0; //停止DS1302總線
//DIOOUT; //數(shù)據(jù)端口定義為輸出
return temp;
}
//===============================================
// 向DS1302寫入時(shí)鐘數(shù)據(jù)
//===============================================
void Ds1302_Write_Time(void)
{

uchar 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(WRITE_PROTECT,0x00); //關(guān)閉寫保護(hù)
Ds1302_Write_Byte(WRITE_SECOND,0x80); //暫停
//Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充電
Ds1302_Write_Byte(WRITE_YEAR,time_buf[1]); //年
Ds1302_Write_Byte(WRITE_MONTH,time_buf[2]); //月
Ds1302_Write_Byte(WRITE_DAY,time_buf[3]); //日
Ds1302_Write_Byte(WRITE_HOUR,time_buf[4]); //時(shí)
Ds1302_Write_Byte(WRITE_MINUTE,time_buf[5]); //分
Ds1302_Write_Byte(WRITE_SECOND,time_buf[6]); //秒
Ds1302_Write_Byte(WRITE_WEEK,time_buf[7]); //周
Ds1302_Write_Byte(WRITE_PROTECT,0x80); //打開寫保護(hù)
}

//========================================
// 從DS1302讀出時(shí)鐘數(shù)據(jù)
//========================================
void Ds1302_Read_Time(void)
{
uchar i,tmp;
time_buf[1]=Ds1302_Read_Byte(READ_YEAR); //年
time_buf[2]=Ds1302_Read_Byte(READ_MONTH); //月
time_buf[3]=Ds1302_Read_Byte(READ_DAY); //日
time_buf[4]=Ds1302_Read_Byte(READ_HOUR); //時(shí)
time_buf[5]=Ds1302_Read_Byte(READ_MINUTE); //分
time_buf[6]=(Ds1302_Read_Byte(READ_SECOND))&0x7F; //秒
time_buf[7]=Ds1302_Read_Byte(READ_WEEK); //周


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;
}
}
//==========================================
// DS1302初始化
//==========================================
void Ds1302_Init(void)
{
CE = 0; //RST腳置低
SCLK = 0; //SCK腳置低
Ds1302_Write_Byte(WRITE_SECOND,0x00); //開始
}


上一頁 1 2 下一頁

關(guān)鍵詞: C8051FDS130

評論


技術(shù)專區(qū)

關(guān)閉