ds1302.c
- #include "ds1302.h"
- // 定義RTC初始化結構體,決定了初始化時間
- RTC_TIME rtc_time = {
- 25, // 秒
- 35, // 分
- 02, // 時
- 25, // 日
- 4, // 月
- 4, // 星期
- 19 // 年
- };
- /*******************************************************************************
- * 函 數 名 : bcd_to_hex
- * 函數功能 : 從時鐘芯片中讀出的時間數據,需轉換為十進制數。
- * 輸 入 : val:需要轉換的值
- * 輸 出 : 無
- *******************************************************************************/
- static uint8_t bcd_to_hex(uint8_t val)
- {
- uint8_t temp;
-
- temp = val & 0x0f;
- val >>= 4;
- val &= 0x0f;
- val *= 10;
- temp += val;
-
- return temp;
- }
- /*******************************************************************************
- * 函 數 名 : hex_to_bcd
- * 函數功能 : 往時鐘芯片寫入數據時,需將待寫的十進制數轉換為8421碼。
- * 輸 入 : val:需要轉換的值
- * 輸 出 : 無
- *******************************************************************************/
- static uint8_t hex_to_bcd(uint8_t val)
- {
- uint8_t i,j,k;
-
- i = val / 10;
- j = val % 10;
- k= j + (i << 4);
-
- return k;
- }
- /*******************************************************************************
- * 函 數 名 : DS1302_Write
- * 函數功能 : 向DS1302命令(地址+數據)
- * 輸 入 : addr,dat
- * 輸 出 : 無
- *******************************************************************************/
- static void DS1302_Write(uint8_t addr, uint8_t dat)
- {
- uint8_t n;
-
- RST = 0;
- _nop_();
- SCLK = 0;//先將SCLK置低電平。
- _nop_();
- RST = 1; //然后將RST(CE)置高電平。
- _nop_();
- for (n=0; n<8; n++)//開始傳送八位地址命令
- {
- DSIO = addr & 0x01;//數據從低位開始傳送
- addr >>= 1;
- SCLK = 1;//數據在上升沿時,DS1302讀取數據
- _nop_();
- SCLK = 0;
- _nop_();
- }
-
- for (n=0; n<8; n++)//寫入8位數據
- {
- DSIO = dat & 0x01;
- dat >>= 1;
- SCLK = 1;//數據在上升沿時,DS1302讀取數據
- _nop_();
- SCLK = 0;
- _nop_();
- }
-
- RST = 0;//傳送數據結束
- _nop_();
- }
- /*******************************************************************************
- * 函 數 名 : DS1302_Read
- * 函數功能 : 讀取一個地址的數據
- * 輸 入 : addr
- * 輸 出 : dat
- *******************************************************************************/
- static uint8_t DS1302_Read(uint8_t addr)
- {
- uint8_t n;
- uint8_t dat;
- uint8_t dat1;
-
- RST = 0;
- _nop_();
- /* 先將SCLK置低電平 */
- SCLK = 0;
- _nop_();
-
- /* 然后將RST(CE)置高電平 */
- RST = 1;
- _nop_();
- /* 開始傳送八位地址命令 */
- for(n=0; n<8; n++)
- {
- DSIO = addr & 0x01; /* 數據從低位開始傳送 */
- addr >>= 1;
- SCLK = 1; /* 數據在上升沿時,DS1302讀取數據 */
- _nop_();
- SCLK = 0; /* DS1302下降沿時,放置數據 */
- _nop_();
- }
- _nop_();
-
- /* 讀取8位數據 */
- for(n=0; n<8; n++)
- {
- dat1 = DSIO; /* 從最低位開始接收 */
- dat = (dat>>1) | (dat1<<7);
- SCLK = 1;
- _nop_();
- SCLK = 0; /* DS1302下降沿時,放置數據 */
- _nop_();
- }
-
- /* 以下為DS1302復位的穩定時間,必須的 */
- RST = 0;
- _nop_();
- SCLK = 1;
- _nop_();
- DSIO = 0;
- _nop_();
- DSIO = 1;
- _nop_();
-
- return dat;
- }
- /*******************************************************************************
- * 函 數 名 : DS1302_ReadTime
- * 函數功能 : 讀取時鐘信息
- * 輸 入 : RTC_TIME類型的結構體指針,RTC_TIME的成員有:
- rtc_sec 秒
- rtc_min 分鐘
- rtc_hour 小時
- rtc_date 日
- rtc_month 月
- rtc_day 星期
- rtc_year 年
- * 輸 出 : 無
- *******************************************************************************/
- void DS1302_ReadTime(RTC_TIME *rtc_time)
- {
- unsigned temp;
-
- temp = DS1302_Read(SEC_ADDR | 0x01);
- rtc_time->rtc_sec = bcd_to_hex(temp);
-
- temp = DS1302_Read(MIN_ADDR | 0x01);
- rtc_time->rtc_min = bcd_to_hex(temp);
-
- temp = DS1302_Read(HR_ADDR | 0x01);
- rtc_time->rtc_hour = bcd_to_hex(temp);
-
- temp = DS1302_Read(DATE_ADDR | 0x01);
- rtc_time->rtc_date = bcd_to_hex(temp);
-
- temp = DS1302_Read(MON_ADDR | 0x01);
- rtc_time->rtc_month = bcd_to_hex(temp);
-
- temp = DS1302_Read(DAY_ADDR | 0x01);
- rtc_time->rtc_day = bcd_to_hex(temp);
-
- temp = DS1302_Read(YEAR_ADDR | 0x01);
- rtc_time->rtc_year = bcd_to_hex(temp);
- }
- /*******************************************************************************
- * 函 數 名 : DS1302_SetTime
- * 函數功能 : 向DS1302寫入時間
- * 輸 入 : RTC_TIME類型的結構體指針,RTC_TIME的成員有:
- rtc_sec 秒
- rtc_min 分鐘
- rtc_hour 小時
- rtc_date 日
- rtc_month 月
- rtc_day 星期
- rtc_year 年
- * 輸 出 : 無
- *******************************************************************************/
- void DS1302_SetTime(RTC_TIME *rtc_time)
- {
- uint8_t temp;
-
- DS1302_Write(0x8E,0X00); //禁止寫保護,就是關閉寫保護功能
-
- temp = hex_to_bcd(rtc_time->rtc_sec);
- DS1302_Write(SEC_ADDR, temp);
-
- temp = hex_to_bcd(rtc_time->rtc_min);
- DS1302_Write(MIN_ADDR, temp);
-
- temp = hex_to_bcd(rtc_time->rtc_hour);
- DS1302_Write(HR_ADDR, temp);
-
- temp = hex_to_bcd(rtc_time->rtc_date);
- DS1302_Write(DATE_ADDR, temp);
-
- temp = hex_to_bcd(rtc_time->rtc_month);
- DS1302_Write(MON_ADDR, temp);
-
- temp = hex_to_bcd(rtc_time->rtc_day);
- DS1302_Write(DAY_ADDR, temp);
-
- temp = hex_to_bcd(rtc_time->rtc_year);
- DS1302_Write(YEAR_ADDR, temp);
- DS1302_Write(0x8E,0x80); //打開寫保護功能
- }
- /*******************************************************************************
- * 函 數 名 : DS1302_Init
- * 函數功能 : 初始化DS1302,初始化時間時由RTC初始化結構體決定的
- * 輸 入 : addr
- * 輸 出 : dat
- *******************************************************************************/
- void DS1302_Init(void)
- {
- DS1302_SetTime(&rtc_time);
- }
復制代碼 ds1302.h
- #ifndef __DS1302_H_
- #define __DS1302_H_
- #include "config.h"
- // DS1302的寄存器地址
- #define SEC_ADDR 0x80 // 秒
- #define MIN_ADDR 0x82 // 分
- #define HR_ADDR 0x84 // 時
- #define DATE_ADDR 0x86 // 日
- #define MON_ADDR 0x88 // 月
- #define DAY_ADDR 0x8a // 星期
- #define YEAR_ADDR 0x8c // 年
- //定義ds1302使用的IO口
- sbit DSIO = P1^1;
- sbit RST = P1^2;
- sbit SCLK = P1^3;
- typedef struct rtc_time{
- uint8_t rtc_sec;
- uint8_t rtc_min;
- uint8_t rtc_hour;
- uint8_t rtc_date;
- uint8_t rtc_month;
- uint8_t rtc_day;
- uint8_t rtc_year;
- }RTC_TIME;
- extern RTC_TIME rtc_time; // 聲明RTC初始化結構體,以便外部文件使用
- void DS1302_SetTime(RTC_TIME *rtc_time); // 設置時間
- void DS1302_ReadTime(RTC_TIME *rtc_time); // 讀取時間
- void DS1302_Init(void); // 初始化DS1302
- #endif
復制代碼 lcd12864.c
lcd12864.h
- #ifndef _12864_H_
- #define _12864_H_
- #include <STC12C5A60S2.H>
- #include "config.h"
- void LCD12864_Write_Com(unsigned char cmdcode);
- void LCD12864_Write_Data(unsigned char Dispdata);
- void LCD12864_ShowStr(uint8_t pos_x, uint8_t pos_y, char *str);
- void LCD12864_Init(void);
- //void LCD12864_Clear(void);
- #endif /* MSP430_C_X_12864_H_ */
復制代碼 config.h
- #ifndef __CONFIG_H
- #define __CONFIG_H
- #include <STC12C5A60S2.H>
- #include <intrins.h>
- #include <string.h>
- typedef unsigned char uint8_t;
- typedef unsigned short int uint16_t;
- typedef unsigned int uint32_t;
- #define KEY_SET KEY5
- #endif
復制代碼
main.c
- <div>#include <STC12C5A60S2.H>
- #include "lcd12864.h"
- #include "ds1302.h"
- void main(void)
- {
- LCD12864_Init(); // 初始化LCD12864
- DS1302_Init(); // 初始化DS1302時鐘芯片
-
- while (1)
- {
- DS1302_ReadTime(&rtc_time); // 讀取時間
-
- LCD12864_ShowStr(0, 1, "日期:");
- LCD12864_Write_Data('0' + rtc_time.rtc_year / 10);
- LCD12864_Write_Data('0' + rtc_time.rtc_year % 10);
- LCD12864_Write_Data('-');
- LCD12864_Write_Data('0' + rtc_time.rtc_month / 10);
- LCD12864_Write_Data('0' + rtc_time.rtc_month % 10);
- LCD12864_Write_Data('-');
- LCD12864_Write_Data('0' + rtc_time.rtc_date / 10);
- LCD12864_Write_Data('0' + rtc_time.rtc_date % 10);
-
- LCD12864_ShowStr(0, 2, "時間:");
- LCD12864_Write_Data('0' + rtc_time.rtc_hour / 10);
- LCD12864_Write_Data('0' + rtc_time.rtc_hour % 10);
- LCD12864_Write_Data('-');
- LCD12864_Write_Data('0' + rtc_time.rtc_min / 10);
- LCD12864_Write_Data('0' + rtc_time.rtc_min % 10);
- LCD12864_Write_Data('-');
- LCD12864_Write_Data('0' + rtc_time.rtc_sec / 10);
- LCD12864_Write_Data('0' + rtc_time.rtc_sec % 10);
- }
- }
復制代碼
|