基于STM32的DS1302时钟芯片驱动
生活随笔
收集整理的這篇文章主要介紹了
基于STM32的DS1302时钟芯片驱动
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
基于STM32的DS1302時鐘芯片驅(qū)動
/************************************************************************************************************************* * 函數(shù) : DS1302.H * 功能 : DS1302頭文件 * 參數(shù) : 無 * 返回 : 無 * 依賴 : 底層讀寫函數(shù) * 作者 : li_qcxy@126.com * 時間 : 2016-12-9 * 最后修改時間 : * 說明 : DS1302采用GPIO模擬SPI的方式 DS1302 LSB格式,上升沿寫 下降沿讀 BCD碼存儲 * 參考 : *************************************************************************************************************************/ #include "sys.h" #include "DS1302.h" #include "delay.h" //#include "type.h" #include "stdio.h"u8 u8time[8]; u8 rtc_init[8] = { 58, //秒 011, //分 114, //小時212,//日 31,//月 44,//周 16,//2016年 60,//寫效許 7};/************************************************************************************************************************* * 函數(shù) : void DS1302_GPIO_Init(void) * 功能 : 配置STM32的GPIO和SPI接口,用于連接 DS1302 * 參數(shù) : 無 * 返回 : 無 * 依賴 : GPIO庫函數(shù) * 作者 : li_qcxy@126.com * 時間 : 2016-12-9 * 最后修改時間 : 2017-1-4修改驅(qū)動 * 說明 : DS1302_DIO配置為開漏模式,此模式下能夠?qū)崿F(xiàn)真正的雙向IO口 *************************************************************************************************************************/ void DS1302_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE); //使能PA,PD端口時鐘GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //IO口速度為50MHzGPIO_Init(GPIOB, &GPIO_InitStructure); //根據(jù)設(shè)定參數(shù)初始化GPIOA.8 }void Dio_In(void){ GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE); //使能PA,PD端口時鐘 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //推挽輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度為50MHz GPIO_Init(GPIOB, &GPIO_InitStructure);}void DIO_Out(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE); //使能PA,PD端口時鐘GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; //LED0-->PA.8 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度為50MHz GPIO_Init(GPIOB, &GPIO_InitStructure); //根據(jù)設(shè)定參數(shù)初始化GPIOA.8 } /************************************************************************************************************************* *函數(shù) : void DS1302_WriteByte(u8 data) *功能 : 寫一個Byte到DS1302 *參數(shù) : data:要寫入的Byte *返回 : 無 *依賴 : 底層宏定義 * 作者 : li_qcxy@126.com * 時間 : 2016-12-09 * 最后修改時間: *說明 : 寫一個字節(jié)的數(shù)據(jù)SCK上升沿寫數(shù)據(jù) *************************************************************************************************************************/ void DS1302_WriteByte(u8 addr,u8 data) { char i=0;int Coda=0;Coda=(data<<8)|addr;En_Ss( );for(i=0;i<16;i++){ if((Coda&0x01)!=0)Set_Dio( );elseClr_Dio();Set_Sclk( );Coda=Coda>>1; Clr_Sclk( );}Di_Ss( ); } /************************************************************************************************************************* *函數(shù) : u8 DS1302_ReadByte(u8 addr) *功能 : 從DS1302SPI總線上讀取一個字節(jié) *參數(shù) : addr:寄存器地址 *返回 : 讀取到的數(shù)據(jù) *依賴 : 底層宏定義 * 作者 : li_qcxy@126.com * 時間 : 2016-12-09 * 最后修改時間: *說明 : 讀一個字節(jié)的數(shù)據(jù) SCK下降沿讀數(shù)據(jù) *************************************************************************************************************************/ u8 DS1302_ReadByte(u8 addr) { char i,Coda;Coda=addr;En_Ss( );for(i=0;i<8;i++){ if((Coda&0x01)!=0)Set_Dio( );elseClr_Dio( );Set_Sclk( );Coda=Coda>>1;Clr_Sclk( );}/************************************/Dio_In(); //讀數(shù)據(jù)Coda=0;for(i=0;i<8;i++){ if( Read_Dio()!=0)Coda|=(1<<i);Set_Sclk( );Clr_Sclk( );}Di_Ss();DIO_Out(); return Coda ; }/************************************************************************************************************************* *函數(shù) : void DS1302_WriteData(u8 addr,u8 data) *功能 : 向指定寄存器寫入一個字節(jié)的數(shù)據(jù) *參數(shù) : addr:寄存器地址;data:需要寫入的數(shù)據(jù) *返回 : 無 *依賴 : 底層宏定義 * 作者 : li_qcxy@126.com * 時間 : 2016-12-09 * 最后修改時間: *說明 : *************************************************************************************************************************/ void DS1302_WriteData(u8 addr,u8 data) { Di_Ss(); Clr_Sclk(); delay_us(1); En_Ss(); delay_us(2); DS1302_WriteByte(addr,data); Di_Ss(); Clr_Sclk(); delay_us(1); }/************************************************************************************************************************* *函數(shù) : void DS1302_ReadTime(u8 addr,u8 time[8]) *功能 : 處理數(shù)據(jù)并通過串口打印 *參數(shù) : read:要寫入的Byte *返回 : 無 *依賴 : 底層宏定義 * 作者 : li_qcxy@126.com * 時間 : 2016-12-10 * 最后修改時間: *說明 : *************************************************************************************************************************/ void DS1302_ReadTime(u8 addr,u8 time[8]) { char i,j,Coda; u8 temp;Coda=addr; En_Ss();for(i=0;i<8;i++) { if((Coda&0x01)!=0)Set_Dio();else Clr_Dio();Set_Sclk();Coda=Coda>>1;Clr_Sclk();} Dio_In(); //讀數(shù)據(jù)for(i=0;i<8;i++ ){ time[i]=0;for(j=0;j<8;j++ ){ if( Read_Dio()!=0)time[i]|=(1<<j);Set_Sclk();Clr_Sclk();} temp = time[i] / 16; time[i] = temp * 10 + time[i] % 16;}Di_Ss();DIO_Out(); } /************************************************************************************************************************* *函數(shù) : void DS1302_Settime(u8 *Buffer) *功能 : 對時 *參數(shù) : 要寫入年 月 日 時 分 秒 *返回 : 無 *依賴 : 底層宏定義 * 作者 : li_qcxy@126.com * 時間 : 2016-12-10 * 最后修改時間: *說明 : *************************************************************************************************************************/ void DS1302_Settime(uint8_t addr,uint8_t time[8]) { char i,j,ge; int Coda=0;Coda=addr; En_Ss(); for(i=0;i<8;i++) //時間地址{ if((Coda&0x01)!=0)Set_Dio();else Clr_Dio();Set_Sclk();Coda=Coda>>1;Clr_Sclk(); }for(i=0;i<8;i++) //時間數(shù)據(jù){ ge=time[i]%10;//個位數(shù)部分 time[i]=(time[i]/10)*16+ge;Coda=time[i];for(j=0;j<8;j++){ if((Coda&0x01)!=0)Set_Dio();else Clr_Dio(); Set_Sclk();Coda=Coda>>1;Clr_Sclk(); } }Di_Ss(); }/************************************************************************************************************************* * 函數(shù) : DS1302.H * 功能 : DS1302頭文件 * 參數(shù) : 無 * 返回 : 無 * 依賴 : 底層讀寫函數(shù) * 作者 : li_qcxy@126.com * 時間 : 2016-12-9 * 最后修改時間 : * 說明 : DS1302采用GPIO模擬SPI的方式 DS1302 LSB格式,上升沿寫 下降沿讀 *************************************************************************************************************************/ #ifndef __DS1302_H #define __DS1302_H#include "sys.h" //#include "type.h" /*****************DS1302控制命令*************************************** *BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0 * 0數(shù)據(jù)不能寫入 存取日歷時鐘Data Addr4 Addr3 Addr2 Addr1 Addr0 寫操作 * 1必須為1 存取RAM時鐘Data 讀操作 **********************************************************************///寄存器地址 typedef enum { DS1302_RdSec = 0x81, //BIT7: 0正常工作; 1:低功耗模式 DS1302_RdMin = 0x83, DS1302_RdHour = 0x85, //BIT5: 0運(yùn)行為12時; 1:24時 DS1302_RdDate = 0x87, DS1302_RdMonth = 0x89, DS1302_RdWeek = 0x8b, DS1302_RdYear = 0x8d, DS1302_RdProtect = 0x8f, //讀保護(hù) DS1302_RdTrickleCharge = 0x91, DS1302_RdClockBurst = 0xbf, DS1302_WrSec = 0x80, //BIT7: 0正常工作; 1:低功耗模式 DS1302_WrMin = 0x82, DS1302_WrHour = 0x84, //BIT5: 0運(yùn)行為12時; 1:24時 DS1302_WrDate = 0x86, DS1302_WrMonth = 0x88, DS1302_WrWeek = 0x8a, DS1302_WrYear = 0x8c, DS1302_WrProtect = 0x8e, //寫保護(hù) DS1302_WrTrickleCharge = 0x90, }DS1302_RegAddr;typedef enum { DS1302_WrClockBurst = 0xbe, //寫時鐘突發(fā)模式 DS1302_RdRamBurst = 0xbf, //讀時鐘突發(fā)模式 DS1302_WrRAMBurst = 0xfe, //寫RAM突發(fā)模式 DS1302_RdRAMBurst = 0xff, //讀RAM突發(fā)模式 }DS1302_MODE;//定義時間結(jié)構(gòu)體 typedef struct { unsigned char sec; unsigned char min; unsigned char hour; unsigned char day; unsigned char month; unsigned char year; }TIME_TypeDef;//*****************DS1302控制命令******************* //相對應(yīng)的IO口配置移植時修改 #define DS1302_PORT GPIOB #define DS1302_SCK GPIO_Pin_6 //DS1302_SCK #define DS1302_DIO GPIO_Pin_7 //DS1302_DIO #define DS1302_CE GPIO_Pin_5 //DS1302_CE//寄存器IO口操作狀態(tài) #define Clr_Sclk() (GPIO_ResetBits(DS1302_PORT, DS1302_SCK)) #define Set_Sclk() (GPIO_SetBits(DS1302_PORT, DS1302_SCK))#define Clr_Dio() (GPIO_ResetBits(DS1302_PORT, DS1302_DIO)) #define Set_Dio() (GPIO_SetBits(DS1302_PORT, DS1302_DIO))#define Di_Ss() (GPIO_ResetBits(DS1302_PORT, DS1302_CE)) #define En_Ss() (GPIO_SetBits(DS1302_PORT, DS1302_CE))#define Read_Dio() (GPIO_ReadInputDataBit(DS1302_PORT, DS1302_DIO))extern u8 rtc_init[8]; extern u8 u8time[8]; /**********************************************函數(shù)聲明**************************************************/ void DS1302_GPIO_Init(void); //配置STM32的GPIO和SPI接口 void DS1302_Settime(uint8_t addr,uint8_t time[8]); //設(shè)置時間 void DS1302_ReadTime(u8 addr,u8 time[8]); //讀取DS1302時間 #endif總結(jié)
以上是生活随笔為你收集整理的基于STM32的DS1302时钟芯片驱动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用STM32 的串口来发送和接收数据实
- 下一篇: STM32与DS1302的接口电路