赞
踩
使用STM32设计智能药盒首先要完成的功能是类似与实时时钟和闹钟功能,因为后续需要使用定时吃药提醒功能,所以都说类似。虽然STM32有内部使用RTC计时和闹钟功能,做简单DEMO还是可行的,但是好像RTC计时时间一长,几个小时会出现卡死现象。所以这边选择使用外部时钟芯片DS1302如下图所示,配备了CR2032纽扣电池可以实现掉电时间数据不丢失。
显示模块选择的是0.96寸的OLED模块,如下图所示。
关于两个模块的详细资料可以搜索其它博主的博客都有详细介绍这里就不一一赘述了。下面进入代码环节。
打开cubex新建工程项目
选择我们使用的芯片,stm32f103c8t6。这里根据自己使用芯片进行具体选型新建工程即可。
进入工程后,我们先打开调试引脚,配置基本时钟树等等,具体配置如下图
开启串口1,后续调试观察用串口也是很方便的。
此处选择PC13作为1302的RST信号线,PC14连接data数据线 ,PC15连接CLK时钟线。3个引脚均配置为GPIO输出模式即可,如下图。
oled屏幕驱动采用模拟iic,使用PA6和PA7引脚,均配置成开漏输出模式用于模拟IIC。
本阶段代码希望通过OLED实时显示DS1302的时间,那么就需要定时1s中更新一次时间,所以需要开启一个定时器用于计时,此处选用定时器3,配置如下图所示。
72M主频,分频系数为7199,重装载值为9999,刚好是1s的定时中断。
接着就完成上面两图的基本配置,工程名字可以自己命名无需一定一样,但是不能有中文出现。设置后点击GENERATE CODE即可生成工程。
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- #include "ds1302.h"
- #include "Oled.h"
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- uint8_t shi=12,fen=0,miao=0,nian=24,yue=2,ri=13,week=6;
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
-
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM3)
- {
- DS1302_ReadTime();
- }
- }
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
-
- /* USER CODE END 0 */
-
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint8_t menu=1;
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
-
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
-
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_TIM3_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim3);
- DS1302_Init();
- OLED_Init();
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- if(menu==1)
- {
- OLED_Printf(20, 0, OLED_8X16," 20%d-%02d-%02d ",nian,yue,ri);
- OLED_Printf(20, 16, OLED_8X16," %02d-%02d-%02d ",shi,fen,miao);
- }
- OLED_Update();
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
-
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
-
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /* USER CODE BEGIN 4 */
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
上面是main.c代码,初始化时候先将oled和ds1302进行初始化,开启定时器,在HAL_TIM_PeriodElapsedCallback函数中每1s中调用一次读取ds1302存储的时间数据,通过OLED动态显示函数就可以实现一个实时时间显示。OLED的代码可以参考往上版本的,能够动态显示数字即可。下面是附上ds1302的c和h代码。
- #include "ds1302.h"
-
-
- extern uint8_t shi,fen,miao,nian,yue,ri,week;
-
- //单片机上电后默认置高电平,所以初始化
- void DS1302_Init()
- {
- HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
- }
-
- void ds1302data_outmode(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStruct = {0};
-
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOC, ds1302_data_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pins : PCPin PCPin PCPin */
- GPIO_InitStruct.Pin = ds1302_data_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- }
-
- void ds1302data_inputmode(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStruct = {0};
-
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOC, ds1302_data_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pins : PCPin PCPin PCPin */
- GPIO_InitStruct.Pin = ds1302_data_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- }
-
- /**
- * @brief DS1302写一个字节
- * @param Command 命令字/地址
- * @param Data 要写入的数据
- * @retval 无
- */
- void DS1302_WriteByte(uint8_t Command,uint8_t Data)
- {
- uint8_t i;
- HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_SET);
- for(i=0;i<8;i++)
- {
- if(Command&(0x01<<i))
- {
- HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_SET);
- }
- else
- {
- HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
- }
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
- }
- for(i=0;i<8;i++)
- {
- if(Data&(0x01<<i))
- {
- HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_SET);
- }
- else
- {
- HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
- }
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
- }
- HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_RESET);
- }
-
- /**
- * @brief DS1302读一个字节
- * @param Command 命令字/地址
- * @retval 读出的数据
- */
- uint8_t DS1302_ReadByte(uint8_t Command)
- {
- unsigned char i,Data=0x00;
- ds1302data_outmode();
- Command|=0x01; //将指令转换为读指令
- HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_SET);
- for(i=0;i<8;i++)
- {
- if(Command&(0x01<<i))HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_SET);
- else HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
- }
- ds1302data_inputmode();
- for(i=0;i<8;i++)
- {
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
- if(HAL_GPIO_ReadPin(ds1302_data_GPIO_Port,ds1302_data_Pin)==1){Data|=(0x01<<i);}
- }
- HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
- return Data; //写完指令后,单片机释放对I/O口的控制,把I/O口控制权交给DS1302;
- }
-
- /**
- * @brief DS1302设置时间,调用之后,DS1302_Time数组的数字会被设置到DS1302中
- * @param 无
- * @retval 无
- */
-
- void DS1302_SetTime() //十进制转BCD码后写入
- {
- DS1302_WriteByte(DS1302_WP,0x00);
- DS1302_WriteByte(DS1302_YEAR,nian/10*16+nian%10);
- DS1302_WriteByte(DS1302_MONTH,yue/10*16+yue%10);
- DS1302_WriteByte(DS1302_DATE,ri/10*16+ri%10);
- DS1302_WriteByte(DS1302_HOUR,shi/10*16+shi%10);
- DS1302_WriteByte(DS1302_MINUTE,fen/10*16+fen%10);
- DS1302_WriteByte(DS1302_SECOND,miao/10*16+miao%10);
- DS1302_WriteByte(DS1302_DAY,week/10*16+week%10);
- DS1302_WriteByte(DS1302_WP,0x80);
- }
-
- /**
- * @brief DS1302读取时间,调用之后,DS1302中的数据会被读取到DS1302_Time数组中
- * @param 无
- * @retval 无
- */
- void DS1302_ReadTime() //BCD码转十进制后读取
- {
- uint8_t Temp;
- Temp=DS1302_ReadByte(DS1302_YEAR);
- nian=Temp/16*10+Temp%16;
- Temp=DS1302_ReadByte(DS1302_MONTH);
- yue=Temp/16*10+Temp%16;
- Temp=DS1302_ReadByte(DS1302_DATE);
- ri=Temp/16*10+Temp%16;
- Temp=DS1302_ReadByte(DS1302_HOUR);
- shi=Temp/16*10+Temp%16;
- Temp=DS1302_ReadByte(DS1302_MINUTE);
- fen=Temp/16*10+Temp%16;
- Temp=DS1302_ReadByte(DS1302_SECOND);
- miao=Temp/16*10+Temp%16;
- Temp=DS1302_ReadByte(DS1302_DAY);
- week=Temp/16*10+Temp%16;
- }
-
-
- #ifndef __DS1302_H__
- #define __DS1302_H__
-
-
- #include "main.h"
-
- //寄存器写入地址/指令定义
- #define DS1302_SECOND 0x80
- #define DS1302_MINUTE 0x82
- #define DS1302_HOUR 0x84
- #define DS1302_DATE 0x86
- #define DS1302_MONTH 0x88
- #define DS1302_DAY 0x8A
- #define DS1302_YEAR 0x8C
- #define DS1302_WP 0x8E
-
- void DS1302_Init(void);
- void DS1302_WriteByte(uint8_t Command,uint8_t Data);
- uint8_t DS1302_ReadByte(uint8_t Command);
- void DS1302_SetTime(void);
- void DS1302_ReadTime(void);
-
- #endif
需要注意的是在读取字节时候data引脚先是配置成输出模式,而后读入数据要配置成输入模式。在h文件中对8个地址进行宏定义,分别是是时分秒年月日周以及写保护地址,设置新时间时候需要先关闭写保护再写入而后开启写保护。烧入代码即可看到现象啦。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。