//MSP430 Advanced Developping Components - [DS1302_430.C]////DS1302 Trickle Charge Timekeeping Chip
本文引用地址:http://m.butianyuan.cn/article/201611/320055.htm
//MSP430高級(jí)實(shí)驗(yàn)開發(fā)組件 - DS1302時(shí)鐘芯片
//時(shí)鐘設(shè)置:
////ACLK=N/A,MCLK=SMCLK=default(DCO~800k)
//硬件連接:
//// MSP430 MCU DS1302
//// ------------------ ------------------
//// | P20 | ------> | SCLK |
//// | P21 | ------> | RST |
//// | P22 | ------> | SDI |
//// ------------------ ------------------
//// MSP430 MCU KEYS
//// ------------------ ------------------
//// ------------------ ------------------
//當(dāng)前演示程序功能描述:
////
#include <msp430x14x.h>
#define DS1302_DIRP2DIR
#define DS1302_INP2IN
#define DS1302_OUTP2OUT
#define DS1302_RSTBIT1
#define DS1302_SCLKBIT0
#define DS1302_SDIBIT2//定義MSP320的端口
#define DS1302_RST_LODS1302_OUT &= ~DS1302_RST
#define DS1302_RST_HIDS1302_OUT |= DS1302_RST
#define DS1302_SCLK_LODS1302_OUT &= ~DS1302_SCLK
#define DS1302_SCLK_HIDS1302_OUT |= DS1302_SCLK
#define DS1302_SDI_LODS1302_OUT &= ~DS1302_SDI
#define DS1302_SDI_HIDS1302_OUT |= DS1302_SDI
void DS1302_Delay(unsigned int dtime);
void DS1302_Reset(void);
void DS1302_WriteOneByte(unsigned char w_dat);
void DS1302_WriteData(unsigned char addr,unsigned char w_dat);
void DS1302_SettingData(void);
void DS1302_GetData(unsigned char *str);
unsigned char DS1302_ReadOneByte(void);
unsigned char DS1302_ReadData(unsigned char addr);
unsigned char SettingData[7]={//bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
0x07,//--------十位-------|-------個(gè)位--------|年份(當(dāng)前07年)
0x07,// |-----個(gè)位-----|星期(當(dāng)前周日)
0x07,// |十位|-------個(gè)位--------|月份(當(dāng)前07月)
0x01,// |--十位---|-------個(gè)位--------|日期(當(dāng)前01號(hào))
0x18,//-12H| |--十位---|-------個(gè)位--------|小時(shí)(當(dāng)前18點(diǎn))
0x20,// |-----十位-----|-------個(gè)位--------|分鐘(當(dāng)前20分)
0x30// |-----十位-----|-------個(gè)位--------|秒鐘(當(dāng)前30秒)
};
unsigned char ReadingData[7];//讀出來(lái)的數(shù)據(jù),同SettingData定義與格式
//延時(shí)函數(shù)
void DS1302_Delay(unsigned int dtime) {
unsigned int temp;
for(temp=0;temp}
//DS1302復(fù)位
void DS1302_Reset(void) {
DS1302_DIR |= (DS1302_RST + DS1302_SCLK);
DS1302_SCLK_LO;
DS1302_RST_LO;
DS1302_Delay(10);
DS1302_SCLK_HI;
}
//向DS1302寫入一個(gè)字節(jié)
void DS1302_WriteOneByte(unsigned char w_dat) {
unsigned char temp;
DS1302_RST_HI;
DS1302_DIR |= DS1302_SDI;
for(temp=8;temp>0;temp--) {
DS1302_SDI_LO;
if(w_dat&BIT0) DS1302_SDI_HI;
DS1302_SCLK_LO;
DS1302_Delay(10);
DS1302_SCLK_HI;
DS1302_Delay(10);
w_dat >>=1;
}
}
//從DS1302中讀取一個(gè)字節(jié)
unsigned char DS1302_ReadOneByte(void) {
unsigned char temp,rdata;
rdata = 0x00;
DS1302_RST_HI;
DS1302_DIR &= ~DS1302_SDI;
for(temp=0;temp<7;temp++){
DS1302_SCLK_HI;
DS1302_Delay(10);
DS1302_SCLK_LO;
DS1302_Delay(10);
if((DS1302_IN&DS1302_SDI)==DS1302_SDI)
rdata |= BIT7;
rdata >>= 1;
}
return(rdata);
}
//向DS1302中寫入地址后寫入數(shù)據(jù)
void DS1302_WriteData(unsigned char addr,unsigned char w_dat) {
DS1302_RST_LO;
DS1302_SCLK_LO;
DS1302_RST_HI;
DS1302_WriteOneByte(addr);//寫入地址
DS1302_WriteOneByte(w_dat);//寫入數(shù)據(jù)
DS1302_SCLK_HI;
DS1302_RST_LO;
}
//向DS1302寫入地址后,從DS1302中讀取數(shù)據(jù)
unsigned char DS1302_ReadData(unsigned char addr) {
unsigned char r_dat;
DS1302_RST_LO;
DS1302_SCLK_LO;
DS1302_RST_HI;
DS1302_WriteOneByte(addr);//寫入地址
r_dat = DS1302_ReadOneByte();//讀出數(shù)據(jù)
DS1302_SCLK_LO;
DS1302_RST_LO;
return(r_dat);
}
//按照SettingData的設(shè)置設(shè)置DS1302的時(shí)間
void DS1302_SettingData(void) {
unsigned char temp;
unsigned char addr = 0x8C;
DS1302_WriteData(0x8E,0x00);//寫入控制命令,禁用寫保護(hù)
for(temp=0;temp<7;temp++) {
DS1302_WriteData(addr,SettingData[temp]);
addr -= 2;
}
DS1302_WriteData(0x8E,0x80);//寫入控制命令,啟用寫保護(hù)
}
//讀取DS1302時(shí)間到ReadingData中
void DS1302_GetData(unsigned char *str) {
unsigned char temp;
unsigned char addr = 0x8D;
for(temp=0;temp<7;temp++) {
str[temp] = DS1302_ReadData(addr);//年
addr -= 2;
}
}
評(píng)論