您的位置 首页 芯闻

DS1302时钟芯片的MSP430程序

MSP430AdvancedDeveloppingComponents-[DS1302_430C]DS1302TrickleChargeTimekeepingChipMSP430高级实验开发组

//MSP430 Advanced Developping Components – [DS1302_430.C]

////DS1302 Trickle Charge Timekeeping Chip

//MSP430高档试验开发组件 – DS1302时钟芯片
//时钟设置:
////ACLK=N/A,MCLK=SMCLK=default(DCO~800k)
//硬件衔接:
//// MSP430 MCU DS1302
//// —————— ——————
//// | P20 | ——> | SCLK |
//// | P21 | ——> | RST |
//// | P22 | ——> | SDI |
//// —————— ——————
//// MSP430 MCU KEYS
//// —————— ——————
//// —————— ——————
//当时演示程序功用描绘:
////

#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,//——–十位——-|——-个位——–|年份(当时07年)
0x07,// |—–个位—–|星期(当时周日)
0x07,// |十位|——-个位——–|月份(当时07月)
0x01,// |–十位—|——-个位——–|日期(当时01号)
0x18,//-12H| |–十位—|——-个位——–|小时(当时18点)
0x20,// |—–十位—–|——-个位——–|分钟(当时20分)
0x30// |—–十位—–|——-个位——–|秒钟(当时30秒)
};
unsigned char ReadingData[7];//读出来的数据,同SettingData界说与格局

//延时函数
void DS1302_Delay(unsigned int dtime) {
unsigned int temp;
for(temp=0;temp}

//DS1302复位
void DS1302_Reset(void) {
DS1302_DIR |= (DS1302_RST + DS1302_SCLK);
DS1302_SCLK_LO;
DS1302_RST_LO;
DS1302_Delay(10);
DS1302_SCLK_HI;
}

//向DS1302写入一个字节
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中读取一个字节
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中写入地址后写入数据
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);//写入数据

DS1302_SCLK_HI;
DS1302_RST_LO;
}

//向DS1302写入地址后,从DS1302中读取数据
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();//读出数据

DS1302_SCLK_LO;
DS1302_RST_LO;

return(r_dat);
}

//依照SettingData的设置设置DS1302的时刻
void DS1302_SettingData(void) {
unsigned char temp;
unsigned char addr = 0x8C;

DS1302_WriteData(0x8E,0x00);//写入操控指令,禁用写保护

for(temp=0;temp<7;temp++) {
DS1302_WriteData(addr,SettingData[temp]);
addr -= 2;
}

DS1302_WriteData(0x8E,0x80);//写入操控指令,启用写保护
}

//读取DS1302时刻到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;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/262510.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部