求一個(gè)ds1302時(shí)鐘芯片的c程序和電路圖。最好是能調(diào)節(jié)時(shí)間的。
提問者:rhjf38872013-09-14 00:00
最佳答案
電路圖就自己接了,網(wǎng)上大把呢
#include
#define Writesecond 0x80
#define Readsecond 0x81
#define Writeminute 0x82
#define Readminute 0x83
#define Writehour 0x84
#define Readhour 0x85
#define Writeday 0x86
#define Readday 0x87
#define Writemonth 0x88
#define Readmonth 0x89
#define Writeweek 0x8a
#define Readweek 0x8b
#define Writeyear 0x8c
#define Readyear 0x8d
#define Writeprotect 0x8e
#define uchar unsigned char
#define uint unsigned int
sbit DS1302_CLK=P3^3;
sbit DS1302_IO=P3^4;
sbit DS1302_RST=P3^5;
uchar bdata dsdata;
sbit dsbit=dsdata^7;
uchar time[3]={19,2,30};
uchar date[4]={12,9,19,3};
uchar Times[]="19:02:00";
uchar Dates[]="2012/05/13";
uchar num[]="0123456789";
/*************寫入一字節(jié)地址/數(shù)據(jù)**********/
void DS1302_Writebyte(uchar dat)
{
uchar i,rdsdata;
rdsdata=dat;
for(i=0;i<8;i++)
{
DS1302_CLK=0;
_nop_();_nop_();_nop_();
DS1302_IO=(bit)(rdsdata&0x01); DS1302_CLK=1; //上升沿寫入數(shù)據(jù)/地址
_nop_();_nop_();_nop_();
rdsdata>>=1; }
}
/***************讀取一字節(jié)數(shù)據(jù)******************/
uchar DS1302_Readbyte()
{
uchar i;
for(i=0;i<8;i++)
{
dsbit=DS1302_IO;
DS1302_CLK=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
dsdata>>=1;
DS1302_CLK=0; //下降沿讀取數(shù)據(jù)
_nop_();_nop_();_nop_();_nop_();_nop_();
}
return dsdata;
}
/**************寫入數(shù)據(jù)**********************/
void DS1302_Write(uchar addr,dat)
{
DS1302_RST=0; //置低,終止數(shù)據(jù)傳遞
DS1302_CLK=0; //清空時(shí)鐘線
DS1302_RST=1; //置高,開始數(shù)據(jù)傳遞
DS1302_Writebyte(addr);
DS1302_Writebyte(dat);
DS1302_CLK=1;
DS1302_RST=0;
}
/***************讀取數(shù)據(jù)****************/
uchar DS1302_Read(uchar addr)
{
uchar rdsdata,rdsdatah,rdsdatal;
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
DS1302_Writebyte(addr);
rdsdata=DS1302_Readbyte();
DS1302_CLK=1;
DS1302_RST=0;
rdsdatah=rdsdata/16; //進(jìn)制轉(zhuǎn)換,十六進(jìn)制轉(zhuǎn)換為十進(jìn)制
rdsdatal=rdsdata%16;
rdsdata=rdsdatah*10+rdsdatal;
return rdsdata;
}
/**************DS1302初始化***************/
void DS1302_Init(void)
{
DS1302_Write(Writeprotect,0x00);
_nop_();_nop_();_nop_();_nop_();_nop_();
DS1302_Write(Writeprotect,0x80);
unsigned char timechange(unsigned char num)//將十進(jìn)制表示的時(shí)間轉(zhuǎn)換為十六進(jìn)制表示
{
unsigned char a;
a=num/10*6+num;
return a;
}
void DS1302_Settime(uchar tab[]) //格式:時(shí)分秒數(shù)值型數(shù)組
{
DS1302_Write(Writeprotect,0x00);
DS1302_Write(Writesecond,timechange(tab[2]));
DS1302_Write(Writeminute,timechange(tab[1]));
DS1302_Write(Writehour,timechange(tab[0]));
DS1302_Write(Writeprotect,0x80);
}
void DS1302_Setdate(uchar tab[]) //格式:年月日星期數(shù)值型數(shù)組
{
DS1302_Write(Writeprotect,0x00);
DS1302_Write(Writeday,timechange(tab[2]));
DS1302_Write(Writemonth,timechange(tab[1]));
DS1302_Write(Writeyear,timechange(tab[0]));
DS1302_Write(Writeweek,timechange(tab[3]));
DS1302_Write(Writeprotect,0x80);
}
void DS1302_Default() //載入初始時(shí)間
{
DS1302_Write(Writeprotect,0x00);
_nop_();_nop_();_nop_();_nop_();_nop_();
DS1302_Write(0x90, 0x03); //失能充電
_nop_();_nop_();_nop_();_nop_();_nop_();
DS1302_Settime(time);
DS1302_Setdate(date);
DS1302_Write(Writeprotect,0x80);
}
void Time2Times()
{
Times[0]=num[time[0]/10]; //時(shí)
Times[1]=num[time[0]%10];
Times[3]=num[time[1]/10]; //分
Times[4]=num[time[1]%10];
Times[6]=num[time[2]/10]; //秒
Times[7]=num[time[2]%10];
}
void Date2Dates()
{
Dates[2]=num[date[0]/10]; //年
Dates[3]=num[date[0]%10];
Dates[5]=num[date[1]/10]; //月
Dates[6]=num[date[1]%10];
Dates[8]=num[date[2]/10]; //日
Dates[9]=num[date[2]%10];
}
/***************獲取時(shí)間***************/
void DS1302_Gettime(void) // 獲得時(shí)分秒信息
{
time[0]=DS1302_Read(Readhour); //時(shí)
time[1]=DS1302_Read(Readminute); //分
time[2]=DS1302_Read(Readsecond); //秒
Time2Times();
}
/***************獲取日期***************/
void DS1302_Getdate(void) // 獲得年月日星期信息
{
date[2]=DS1302_Read(Readday); //日
date[1]=DS1302_Read(Readmonth); //月
date[0]=DS1302_Read(Readyear); //年
date[3]=DS1302_Read(Readweek); //星期
Date2Dates();
}
回答者:Tomy4921892016-09-14 00:00
DS 5相關(guān)問題
-
說明p1.2是用了非門按制RST腳,所以SETBP1.2;令=0
DS_READ?SETBP1.2;令=0。
CLRP1.1;令SCLK=0。
CLRP1.2;令=1,啟動(dòng)芯片。
提問者:s55353942013-08-03
-
/*******************************************************************************
文件:DS1302.C
環(huán)境:編譯為ICC A
提問者:zncwtb2013-10-13
-
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit SCK=P3^
提問者:baobao4221252013-09-04
-
/*************** writer:shopping.w ******************/
#include
#include
#include
提問者:guiymbo54282014-10-10
-
僅供參考,不懂再問我,哈哈……
--------------------------------------------------------------
#include
#include
提問者:y2787121422013-04-28
-
摘 要:介紹美國DALLAS公司推出的具有涓細(xì)電流充電能力的低功耗實(shí)時(shí)時(shí)鐘電路DS1302的結(jié)構(gòu)、工作原理及其在實(shí)時(shí)顯示時(shí)間中的應(yīng)用。它可以對年、月、日、周日、時(shí)、分、秒進(jìn)行計(jì)時(shí),且具有閏年補(bǔ)償?shù)榷喾N功能。給出DS130
提問者:pengjiamnq2016-03-24
- DS 5熱門車型
- DS 5同品牌車系
- 上市新車
- 即將上市新車