基于STM32对DS1302的驱动.doc

上传人:白大夫 文档编号:3412153 上传时间:2019-08-22 格式:DOC 页数:8 大小:36KB
返回 下载 相关 举报
基于STM32对DS1302的驱动.doc_第1页
第1页 / 共8页
亲,该文档总共8页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《基于STM32对DS1302的驱动.doc》由会员分享,可在线阅读,更多相关《基于STM32对DS1302的驱动.doc(8页珍藏版)》请在三一文库上搜索。

1、基于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 /分数据地址#d

2、efine 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_buf8 = 0

3、x20,0x16,0x03,0x20,0x21,0x22,0x30;/初始时间uchar readTIme14;/当前时间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_APB2PeriphCloc

4、kCmd(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_SetBits(GPIOA, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);/模拟I2C

5、这里把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, /模拟I2C 这里把IO设置为上拉输入 从DS1302接收void DS1302_IN(void)GPIO_InitTy

6、peDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; /上拉输入GPIO_Init(GPIOA, /功能:延时1毫秒/入口参数:x/出口参数:无/说明:晶振为12Mvoid Delay_xms(uint x)uint i,j;for(i=0;ifor(j=0;j112;j+);/DS1302初始化函数void ds1302_init(void)RST=0; /RST脚置低,将ds1302复位SCK=0; /SCK脚置低,时钟置

7、低电平/向DS1302写入一字节数据的函数void ds1302_write_byte(uchar addr, uchar d)uchar i;DS1302_OUT();RST=1; /启动DS1302总线/写入目标地址:addraddr = addr /最低位置零,寄存器0位为0时写,为1时读for (i = 0; i 8; i +) if (addr IO=1;else IO=0;SCK=1; /产生时钟SCK=0;addr = addr 1;/写入数据:dfor (i = 0; i 8; i +) if (d IO=1;else IO=0;SCK=1; /产生时钟SCK=0;d = d

8、1;RST=0; /停止DS1302总线/从DS1302读出一字节数据uchar ds1302_read_byte(uchar addr) uchar i,temp;DS1302_OUT();RST=1; /启动DS1302总线/写入目标地址:addraddr = addr | 0x01; /最低位置高,寄存器0位为0时写,为1时读for (i = 0; i 8; i +) if (addr IO=1;else IO=0;SCK=1;SCK=0;addr = addr 1;/输出数据:tempDS1302_IN();for (i = 0; i 8; i +) temp = temp 1;if

9、(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) temp |= 0x80;else temp 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

10、_write_byte(ds1302_year_add,TIme_buf1); /年ds1302_write_byte(ds1302_month_add,TIme_buf2); /月ds1302_write_byte(ds1302_date_add,time_buf3); /日ds1302_write_byte(ds1302_hr_add,time_buf4); /时ds1302_write_byte(ds1302_min_add,time_buf5); /分ds1302_write_byte(ds1302_sec_add,time_buf6); /秒ds1302_write_byte(ds1

11、302_day_add,time_buf7); /周ds1302_write_byte(ds1302_control_add,0x80); /打开写保护/从DS302读出时钟数据void ds1302_read_time(void)time_buf1=ds1302_read_byte(ds1302_year_add); /年time_buf2=ds1302_read_byte(ds1302_month_add); /月time_buf3=ds1302_read_byte(ds1302_date_add); /日time_buf4=ds1302_read_byte(ds1302_hr_add);

12、 /时time_buf5=ds1302_read_byte(ds1302_min_add); /分time_buf6=(ds1302_read_byte(ds1302_sec_add)/秒,屏蔽秒的第7位,避免超出59time_buf7=ds1302_read_byte(ds1302_day_add); /周/主函数mainint main(void)DS1302_GPIOInit();Delay_xms(50);/等待系统稳定ds1302_init(); /DS1302初始化Delay_xms(50);/等待系统稳定uart_init(9600);/设置串口波特率为9600Delay_xms

13、(10);ds1302_write_time(); /写入初始值while(1)ds1302_read_time(); /读取时间readtime0=(time_buf04); /分离出年千位readtime1=(time_buf0 /分离出年百位readtime2=(time_buf14); /分离出年十位readtime3=(time_buf1 /分离出年个位readtime4=(time_buf24); /分离出月十位readtime5=(time_buf2 /分离出月个位readtime6=(time_buf34); /分离出日十位readtime7=(time_buf3 /分离出日个

14、位readtime8=(time_buf44); /分离出小时十位readtime9=(time_buf4 /分离出小时个位readtime10=(time_buf54); /分离出分钟十位readtime11=(time_buf5 /分离出分钟个位readtime12=(time_buf64); /分离出秒钟十位readtime13=(time_buf6 /分离出秒钟个位if(readtime13!=sec_buf)printf(“%d%d%d%d-%d%d-%d%d %d%d.%d%d.%d%d rn”,readtime0,readtime1,readtime2,readtime3,readtime4,readtime5,readtime6,readtime7,readtime8,readtime9,readtime10,readtime11,readtime12,readtime13);Delay_xms(6000);/通过串口显示时间 输出格式可自行更改

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1