|
近幾天看到網上很多網友對STM32驅動DS1302這個程序表示很疑惑,于是寫了這個博客,歡迎大家過來學習交流!!!
- // 作者: heartbeat 來源:<span style="line-height: 1.5;">飛翔的麻雀的blog</span>
- // 說明: STM32驅動DS1302,歡迎大家來交流,如有轉載,請說明來源,謝謝!!!
- // 程序名: STM32驅動DS1302
- //頭文件
- #include "stm32f10x.h"
- #include "usart.h"
- #define uchar unsigned char
- #define uint unsigned int
- ////DS1302引腳定義,可根據實際情況自行修改端口定義
- #define RST PAout(5)
- #define IO PAout(6)
- #define SCK PAout(7)
- //DS1302地址定義
- #define ds1302_sec_add 0x80 //秒數據地址
- #define ds1302_min_add 0x82 //分數據地址
- #define ds1302_hr_add 0x84 //時數據地址
- #define ds1302_date_add 0x86 //日數據地址
- #define ds1302_month_add 0x88 //月數據地址
- #define ds1302_day_add 0x8a //星期數據地址
- #define ds1302_year_add 0x8c //年數據地址
- #define ds1302_control_add 0x8e //控制數據地址
- #define ds1302_charger_add 0x90
- #define ds1302_clkburst_add 0xbe
- //初始時間定義
- uchar time_buf[8] = {0x20,0x16,0x03,0x20,0x21,0x22,0x30};//初始時間
- uchar readtime[14];//當前時間
- uchar sec_buf=0; //秒緩存
- uchar sec_flag=0; //秒標志位
- //作者使用的是mini Stm32 IO口上沒有外接上拉電阻 所以IO口的雙向性需要通過軟件進行切換
- //如果IO上外接了上拉電阻 程序可將管腳IO直接設置為開漏輸出 這種模式下的IO口是雙向性的
- //GPIO口的初始化配置,先默認為高電平
- void DS1302_GPIOInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //開啟GPIOB外設時鐘
- 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_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
- }
- //模擬I2C 這里把IO設置為推挽輸出 向DS1302輸入
- void DS1302_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- //模擬I2C 這里把IO設置為上拉輸入 從DS1302接收
- void DS1302_IN(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- //功能:延時1毫秒
- //入口參數:x
- //出口參數:無
- //說明:晶振為12M
- void Delay_xms(uint x)
- {
- uint i,j;
- for(i=0;i
- for(j=0;j<112;j++);
- }
- //DS1302初始化函數
- void ds1302_init(void)
- {
- RST=0; //RST腳置低,將ds1302復位
- SCK=0; //SCK腳置低,時鐘置低電平
- }
- //向DS1302寫入一字節數據的函數
- void ds1302_write_byte(uchar addr, uchar d)
- {
- uchar i;
- DS1302_OUT();
- RST=1; //啟動DS1302總線
- //寫入目標地址:addr
- addr = addr & 0xFE; //最低位置零,寄存器0位為0時寫,為1時讀
- for (i = 0; i < 8; i ++) {
- if (addr & 0x01) {
- IO=1;
- }
- else {
- IO=0;
- }
- SCK=1; //產生時鐘
- SCK=0;
- addr = addr >> 1;
- }
- //寫入數據:d
- for (i = 0; i < 8; i ++) {
- if (d & 0x01) {
- IO=1;
- }
- else {
- IO=0;
- }
- SCK=1; //產生時鐘
- SCK=0;
- d = d >> 1;
- }
- RST=0; //停止DS1302總線
- }
- //從DS1302讀出一字節數據
- uchar ds1302_read_byte(uchar addr) {
- uchar i,temp;
- DS1302_OUT();
- RST=1; //啟動DS1302總線
- //寫入目標地址:addr
- addr = addr | 0x01; //最低位置高,寄存器0位為0時寫,為1時讀
- for (i = 0; i < 8; i ++) {
- if (addr & 0x01) {
- IO=1;
- }
- else {
- IO=0;
- }
- SCK=1;
- SCK=0;
- addr = addr >> 1;
- }
- //輸出數據:temp
- DS1302_IN();
- for (i = 0; i < 8; i ++) {
- temp = temp >> 1;
- if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)) {
- temp |= 0x80;
- }
- else {
- temp &= 0x7F;
- }
- SCK=1;
- SCK=0;
- }
- RST=0; //停止DS1302總線
- return temp;
- }
- //向DS1302寫入時鐘數據
- void ds1302_write_time(void)
- {
- ds1302_write_byte(ds1302_control_add,0x00); //關閉寫保護
- ds1302_write_byte(ds1302_sec_add,0x80); //暫停時鐘
- //ds1302_write_byte(ds1302_charger_add,0xa9); //涓流充電
- ds1302_write_byte(ds1302_year_add,time_buf[1]); //年
- ds1302_write_byte(ds1302_month_add,time_buf[2]); //月
- ds1302_write_byte(ds1302_date_add,time_buf[3]); //日
- ds1302_write_byte(ds1302_hr_add,time_buf[4]); //時
- ds1302_write_byte(ds1302_min_add,time_buf[5]); //分
- ds1302_write_byte(ds1302_sec_add,time_buf[6]); //秒
- ds1302_write_byte(ds1302_day_add,time_buf[7]); //周
- ds1302_write_byte(ds1302_control_add,0x80); //打開寫保護
- }
- //從DS302讀出時鐘數據
- void ds1302_read_time(void)
- {
- time_buf[1]=ds1302_read_byte(ds1302_year_add); //年
- time_buf[2]=ds1302_read_byte(ds1302_month_add); //月
- time_buf[3]=ds1302_read_byte(ds1302_date_add); //日
- time_buf[4]=ds1302_read_byte(ds1302_hr_add); //時
- time_buf[5]=ds1302_read_byte(ds1302_min_add); //分
- time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7f;//秒,屏蔽秒的第7位,避免超出59
- time_buf[7]=ds1302_read_byte(ds1302_day_add); //周
- }
- //主函數main
- int main(void)
- {
- DS1302_GPIOInit();
- Delay_xms(50);//等待系統穩定
- ds1302_init(); //DS1302初始化
- Delay_xms(50);//等待系統穩定
- uart_init(9600);//設置串口波特率為9600
- Delay_xms(10);
- ds1302_write_time(); //寫入初始值
- while(1)
- {
- ds1302_read_time(); //讀取時間
- readtime[0]=(time_buf[0]>>4); //分離出年千位
- readtime[1]=(time_buf[0]&0x0F); //分離出年百位
- readtime[2]=(time_buf[1]>>4); //分離出年十位
- readtime[3]=(time_buf[1]&0x0F); //分離出年個位
- readtime[4]=(time_buf[2]>>4); //分離出月十位
- readtime[5]=(time_buf[2]&0x0F); //分離出月個位
- readtime[6]=(time_buf[3]>>4); //分離出日十位
- readtime[7]=(time_buf[3]&0x0F); //分離出日個位
- readtime[8]=(time_buf[4]>>4); //分離出小時十位
- readtime[9]=(time_buf[4]&0x0F); //分離出小時個位
- readtime[10]=(time_buf[5]>>4); //分離出分鐘十位
- readtime[11]=(time_buf[5]&0x0F); //分離出分鐘個位
- readtime[12]=(time_buf[6]>>4); //分離出秒鐘十位
- readtime[13]=(time_buf[6]&0x0F); //分離出秒鐘個位
- if(readtime[13]!=sec_buf)
- {
- printf("%d%d%d%d-%d%d-%d%d %d%d.%d%d.%d%d \r\n",readtime[0],readtime[1],readtime[2],readtime[3],readtime[4],\
- readtime[5],readtime[6],readtime[7],readtime[8],readtime[9],readtime[10],\
- readtime[11],readtime[12],readtime[13]);
- Delay_xms(6000);/////////////////通過串口顯示時間 輸出格式可自行更改
- }
- }
- }
復制代碼
|
|