📄 c_ds1302.c
字号:
#include <msp430x14x.h>
/*--NOTE--*/
/*--The Access Modulate of the RealTime DS1302--*/
/*--Function: SINGLE BYTE READ --*/
/*-- SINGLE BYTE WRITE--*/
/*--Pin Configure--*/
/*--DS1302_RST#: P2.5--*/
/*--DS1302_SCLK: P2.6--*/
/*--DS1302_IO: P2.7--*/
/*--The INFORMATION of the DS1302 --*/
/*--The ADDRESS/COMMAND BYTE of the DS1302--*/
/*--1 RAM/CK# A4 A3 A2 A1 A0 RD/W# --*/
#define DS1302_RST BIT5
#define DS1302_SCLK BIT6
#define DS1302_IO BIT7
/*----------------------------------*/
/*--NAME: Read_Data_From_1302--*/
/*--FUNCTION: Read one Data From the DS1302--*/
/*--Parameter: The READING Item(eg. SEC. MIN, HOUR...)--*/
/*--Return Value: The Readed Data--*/
/*--NOTE: --*/
/*--eg. Read_Data_From_1302(con_SEC_READ); --*/
/*--will ReadOut the SEC value of the DS1302--*/
unsigned char Read_Data_From_1302(unsigned char Item){
unsigned char i;
unsigned char Temp_Data;
unsigned char Read_Out_Data;
P2OUT &= ~DS1302_SCLK; /*--SCLK: LOW--*/
P2OUT |= DS1302_RST;
/*--OUTPUT the COMMAND--*/
P2DIR |= DS1302_IO; Delay4us(); /*--DS1302_IO: OUTPUT Output the Command--*/
Temp_Data = 0x01;
for( i = 0; i < 8; i++){
P2OUT &= ~DS1302_SCLK;
if( Temp_Data & Item ){ P2OUT |= DS1302_IO; } else{ P2OUT &= ~DS1302_IO; }
P2OUT |= DS1302_SCLK;
Temp_Data <<= 1;
}
/*--INPUT the Item's Data--*/
P2DIR &= ~DS1302_IO; Delay4us(); /*--DS1302_IO: INPUT Input the Read Data--*/
Read_Out_Data = 0x00;
for( i = 0; i < 8; i++){
Read_Out_Data >>= 1;
P2OUT &= ~DS1302_SCLK;
if( P2IN & DS1302_IO ){ Read_Out_Data |= 0x80; }
P2OUT |= DS1302_SCLK;
}
P2OUT &= ~DS1302_SCLK; /*--SCLK: LOW--*/
P2OUT &= ~DS1302_RST; /*--RST: Low--*/
return Read_Out_Data;
}
/*----------------------------------*/
/*--NAME: Write_Data_To_1302--*/
/*--FUNCTION: Write Data To the DS1302--*/
/*--Parameter: The Writing Item(eg. SEC. MIN, HOUR...), The Write Data--*/
/*--Return Value: NONE--*/
/*--NOTE:--*/
void Write_Data_To_1302(unsigned char Item_Value, unsigned char Write_Data){
unsigned char i;
unsigned char Temp_Data;
P2OUT &= ~DS1302_SCLK; /*--SCLK: LOW--*/
P2OUT |= DS1302_RST;
/*--OUTPUT the COMMAND--*/
P2DIR |= DS1302_IO; Delay4us(); /*--DS1302_IO: OUTPUT Output the Command--*/
Temp_Data = 0x01;
for( i = 0; i < 8; i++){
P2OUT &= ~DS1302_SCLK;
if( Item_Value & Temp_Data ) { P2OUT |= DS1302_IO; } else{ P2OUT &= ~DS1302_IO; }
P2OUT |= DS1302_SCLK;
Temp_Data <<= 1;
}
/*--OUTPUT the Write_Data--*/
Temp_Data = 0x01;
for( i = 0; i < 8; i++){
P2OUT &= ~DS1302_SCLK;
if( Write_Data & Temp_Data ) { P2OUT |= DS1302_IO; } else{ P2OUT &= ~DS1302_IO; }
P2OUT |= DS1302_SCLK;
Temp_Data <<= 1;
}
P2OUT &= ~DS1302_SCLK; /*--SCLK: LOW--*/
P2OUT &= ~DS1302_RST;
}
#define con_SEC_READ 0x81
#define con_MIN_READ 0x83
#define con_HOUR_READ 0x85
#define con_DATE_READ 0x87
#define con_MONTH_READ 0x89
#define con_DAY_READ 0x8B
#define con_YEAR_READ 0x8D
#define con_CONTROL_READ 0x8F
#define con_CHARGER_READ 0x91
#define con_BURST_READ 0xBF
#define con_SEC_WRITE 0x80
#define con_MIN_WRITE 0x82
#define con_HOUR_WRITE 0x84
#define con_DATE_WRITE 0x86
#define con_MONTH_WRITE 0x88
#define con_DAY_WRITE 0x8A
#define con_YEAR_WRITE 0x8C
#define con_CONTROL_WRITE 0x8E
#define con_CHARGER_WRITE 0x90
#define con_BURST_WRITE 0xBE
#define con_RAM_READ 0xC1 /*--RAM operation Need an other variable --*/
#define con_RAM_BURST_READ 0xFF /*--This variable + con_RAM_READ/WRITE Denote the RAM ADDRESS --*/
#define con_RAM_WRITE 0xC0 /*--eg. 0x00 + 0xC1/0xC0 denote RAM0, 0x04 + 0xC1/0xC0 denote RAM1--*/
#define con_RAM_BURST_WRITE 0xFE /*-- 0x08 + 0xC1/0xC0 denote RAM2, 0x0C + 0xC1/0xC0 denote RAM3--*/
/*--The Increment of the Variable is 0x04 --*/
#define con_ENABLE_WRITE 0x00
#define con_DISENABLE_WRITE 0x80
#define con_START_CLOCK 0x00
#define con_STOP_CLOCK 0x80
/*----------------------------------*/
/*--NAME: Init_DS1302--*/
/*--FUNCTION: Started Clock, Disenable PROTECT Bit Init Sec Min Hour....--*/
/*--Parameter: the Value Data of SEC. MIN, HOUR...--*/
/*--Return Value: NONE--*/
/*--NOTE:--*/
void Init_DS1302(void){
// unsigned char i;
Write_Data_To_1302( con_CONTROL_WRITE, con_ENABLE_WRITE );
Write_Data_To_1302( con_SEC_WRITE, con_START_CLOCK );
// for( i = 0; i < 7; i++) Write_Data_To_1302( con_SEC_WRITE + i * 2, Time_Table[i] );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -