STM32F103驱动DS1302程序(带注释)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1简介主控芯片是STM32F103ZET6,DS1302模块在某宝购买,测试两个小时,发现一个小时大概差1秒钟。芯片受温度、电压影响较大。输出结果用串口打印到串口调试助手。如果要oled或者其他显示需要转换为十进制。2.代码部分2.1led部分------------led.h--------------------#ifndef__LED_H#define__LED_H#includesys.hvoidLED_Init(void);//初始化#endif------------led.c--------------------#includeled.h//LEDIO初始化voidLED_Init(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PB端口时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED0--PB.5端口配置GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHzGPIO_Init(GPIOB,&GPIO_InitStructure);//根据设定参数初始化GPIOB.5GPIO_SetBits(GPIOB,GPIO_Pin_5);//PB5输出高}2.2usart部分---------------usart.h------------------------#ifndef__USART_H#define__USART_H#includestdio.h#includesys.hvoiduart_init(u32bound);voidusart1_send_string(u8*BuffToSend);voidusart1_sendbyte(u8data);#endif---------------usart.c-----------------------#includesys.h#includeusart.hvoiduart_init(u32bound){//GPIO端口设置GPIO_InitTypeDefGPIO_InitStructure;USART_InitTypeDefUSART_InitStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);//使能USART1,GPIOA时钟//USART1_TXGPIOA.9GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//PA.9GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化GPIOA.9//USART1_RXGPIOA.10初始化GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//PA10GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化GPIOA.10//Usart1NVIC配置NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//子优先级3NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道使能NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化VIC寄存器//USART初始化设置USART_InitStructure.USART_BaudRate=bound;//串口波特率USART_InitStructure.USART_WordLength=USART_WordLength_8b;//字长为8位数据格式USART_InitStructure.USART_StopBits=USART_StopBits_1;//一个停止位USART_InitStructure.USART_Parity=USART_Parity_No;//无奇偶校验位USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//无硬件数据流控制USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//收发模式USART_Init(USART1,&USART_InitStructure);//初始化串口1USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启串口接受中断USART_Cmd(USART1,ENABLE);//使能串口1}//打印字节voidusart1_sendbyte(u8data){USART_SendData(USART1,data);while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);}//打印字符串voidusart1_send_string(u8*BuffToSend){u8i=0;while(BuffToSend[i]!='\0'){USART_SendData(USART1,BuffToSend[i]);while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);i++;}}2.3ds1302部分---------------ds1302.h-----------------------#ifndef__DS1302_H#define__DS1302_H#includesys.h//写年月日时间寄存器#defineWriteSecond0x80#defineWriteMinute0x82#defineWriteHour0x84#defineWriteDay0x86#defineWriteMonth0x88#definewriteWeek0x8a#definewriteYear0x8c//读年月日时间寄存器#defineReadSecond0x81#defineReadMinute0x83#defineReadHour0x85#defineReadDay0x87#defineReadMonth0x89#defineReadWeek0x8b#defineReadYear0x8d//引脚分配SDA-PC3SCL-PC4RST-PC5#defineDS_SDA_IN{GPIOC-CRL&=0XFFFF0FFF;GPIOC-CRL|=(u32)812;}#defineDS_SDA_OUT{GPIOC-CRL&=0XFFFF0FFF;GPIOC-CRL|=(u32)312;}#defineDS_SDA_DATAGPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)//SDA#defineDS_SDA_HIGHGPIO_SetBits(GPIOC,GPIO_Pin_3)#defineDS_SDA_LOWGPIO_ResetBits(GPIOC,GPIO_Pin_3)#defineDS_SCK_HIGHGPIO_SetBits(GPIOC,GPIO_Pin_4)//SCL#defineDS_SCK_LOWGPIO_ResetBits(GPIOC,GPIO_Pin_4)#defineDS_RST_HIGHGPIO_SetBits(GPIOC,GPIO_Pin_5)//CE#defineDS_RST_LOWGPIO_ResetBits(GPIOC,GPIO_Pin_5)voidds1302_init(void);voidwrite_one_byte(u8data);u8read_one_byte(void);voidds1302_write_data(u8reg,u8data);u8ds1302_read_data(u8reg);voidtime_init(void);voidtime_read(void);u8hex_to_bcd(u8hex_data);u8bcd_to_hex(u8bcd_data);#endif---------------ds1302.c-----------------------#includeds1302.h#includedelay.hu8DSsecond,DSminute,DShour,DSweek,DSday,DSmonth,DSyear;u8time[7]={0x18,0x06,0x10,0x19,0x23,0x59,0x30};//年星期月日时分秒voidds1302_init(){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOC,&GPIO_InitStructure);GPIO_ResetBits(GPIOC,GPIO_Pin_5);//CE拉低GPIO_SetBits(GPIOC,GPIO_Pin_3|GPIO_Pin_4);//拉高}//写入一个字节voidwrite_one_byte(u8data){u8i;DS_SDA_OUT;for(i=0;i8;i++){DS_SCK_LOW;if(data&0x01)//从低位开始{DS_SDA_HIGH;}else{DS_SDA_LOW;}delay_us(2);DS_SCK_HIGH;data=1;delay_us(2);}}//读一个字节u8read_one_byte(){u8i,data;DS_SDA_IN;for(i=0;i8;i++){data=1;DS_SCK_HIGH;if(DS_SDA_DATA==1){data|=0x80;}else{data&=0x7F;}delay_us(2);DS_SCK_LOW;}returndata;}//ds1302写寄存器写数据voidds1302_write_data(u8reg,u8data){DS_SCK_LOW;DS_RST_LOW;//初始rst为低delay_us(2);DS_RST_HIGH;//sck为低时rst才可置高write_one_byte(reg);//sck低到高delay_us(5);DS_SCK_LOW;write_one_byte(data);//sck低到高delay_us(5);//DS_SCK_HIGH;DS_RST_LOW;}//读ds1302寄存器数据u

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功