当前位置:   article > 正文

基于STM32的智能药盒(1)--实时时钟部分_stm32智能药盒

stm32智能药盒

使用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即可生成工程。

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "stdio.h"
  27. #include "ds1302.h"
  28. #include "Oled.h"
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32. uint8_t shi=12,fen=0,miao=0,nian=24,yue=2,ri=13,week=6;
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39. /* USER CODE END PM */
  40. /* Private variables ---------------------------------------------------------*/
  41. /* USER CODE BEGIN PV */
  42. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  43. {
  44. if(htim->Instance==TIM3)
  45. {
  46. DS1302_ReadTime();
  47. }
  48. }
  49. /* USER CODE END PV */
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. /* USER CODE BEGIN PFP */
  53. /* USER CODE END PFP */
  54. /* Private user code ---------------------------------------------------------*/
  55. /* USER CODE BEGIN 0 */
  56. /* USER CODE END 0 */
  57. /**
  58. * @brief The application entry point.
  59. * @retval int
  60. */
  61. int main(void)
  62. {
  63. /* USER CODE BEGIN 1 */
  64. uint8_t menu=1;
  65. /* USER CODE END 1 */
  66. /* MCU Configuration--------------------------------------------------------*/
  67. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  68. HAL_Init();
  69. /* USER CODE BEGIN Init */
  70. /* USER CODE END Init */
  71. /* Configure the system clock */
  72. SystemClock_Config();
  73. /* USER CODE BEGIN SysInit */
  74. /* USER CODE END SysInit */
  75. /* Initialize all configured peripherals */
  76. MX_GPIO_Init();
  77. MX_USART1_UART_Init();
  78. MX_TIM3_Init();
  79. /* USER CODE BEGIN 2 */
  80. HAL_TIM_Base_Start_IT(&htim3);
  81. DS1302_Init();
  82. OLED_Init();
  83. /* USER CODE END 2 */
  84. /* Infinite loop */
  85. /* USER CODE BEGIN WHILE */
  86. while (1)
  87. {
  88. if(menu==1)
  89. {
  90. OLED_Printf(20, 0, OLED_8X16," 20%d-%02d-%02d ",nian,yue,ri);
  91. OLED_Printf(20, 16, OLED_8X16," %02d-%02d-%02d ",shi,fen,miao);
  92. }
  93. OLED_Update();
  94. /* USER CODE END WHILE */
  95. /* USER CODE BEGIN 3 */
  96. }
  97. /* USER CODE END 3 */
  98. }
  99. /**
  100. * @brief System Clock Configuration
  101. * @retval None
  102. */
  103. void SystemClock_Config(void)
  104. {
  105. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  106. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  107. /** Initializes the RCC Oscillators according to the specified parameters
  108. * in the RCC_OscInitTypeDef structure.
  109. */
  110. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  111. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  112. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  113. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  114. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  115. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  116. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  117. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  118. {
  119. Error_Handler();
  120. }
  121. /** Initializes the CPU, AHB and APB buses clocks
  122. */
  123. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  124. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  125. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  126. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  127. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  128. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  129. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  130. {
  131. Error_Handler();
  132. }
  133. }
  134. /* USER CODE BEGIN 4 */
  135. /* USER CODE END 4 */
  136. /**
  137. * @brief This function is executed in case of error occurrence.
  138. * @retval None
  139. */
  140. void Error_Handler(void)
  141. {
  142. /* USER CODE BEGIN Error_Handler_Debug */
  143. /* User can add his own implementation to report the HAL error return state */
  144. __disable_irq();
  145. while (1)
  146. {
  147. }
  148. /* USER CODE END Error_Handler_Debug */
  149. }
  150. #ifdef USE_FULL_ASSERT
  151. /**
  152. * @brief Reports the name of the source file and the source line number
  153. * where the assert_param error has occurred.
  154. * @param file: pointer to the source file name
  155. * @param line: assert_param error line source number
  156. * @retval None
  157. */
  158. void assert_failed(uint8_t *file, uint32_t line)
  159. {
  160. /* USER CODE BEGIN 6 */
  161. /* User can add his own implementation to report the file name and line number,
  162. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  163. /* USER CODE END 6 */
  164. }
  165. #endif /* USE_FULL_ASSERT */

上面是main.c代码,初始化时候先将oled和ds1302进行初始化,开启定时器,在HAL_TIM_PeriodElapsedCallback函数中每1s中调用一次读取ds1302存储的时间数据,通过OLED动态显示函数就可以实现一个实时时间显示。OLED的代码可以参考往上版本的,能够动态显示数字即可。下面是附上ds1302的c和h代码。

  1. #include "ds1302.h"
  2. extern uint8_t shi,fen,miao,nian,yue,ri,week;
  3. //单片机上电后默认置高电平,所以初始化
  4. void DS1302_Init()
  5. {
  6. HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_RESET);
  7. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
  8. }
  9. void ds1302data_outmode(void)
  10. {
  11. GPIO_InitTypeDef GPIO_InitStruct = {0};
  12. /* GPIO Ports Clock Enable */
  13. __HAL_RCC_GPIOC_CLK_ENABLE();
  14. /*Configure GPIO pin Output Level */
  15. HAL_GPIO_WritePin(GPIOC, ds1302_data_Pin, GPIO_PIN_RESET);
  16. /*Configure GPIO pins : PCPin PCPin PCPin */
  17. GPIO_InitStruct.Pin = ds1302_data_Pin;
  18. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  19. GPIO_InitStruct.Pull = GPIO_NOPULL;
  20. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  21. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  22. }
  23. void ds1302data_inputmode(void)
  24. {
  25. GPIO_InitTypeDef GPIO_InitStruct = {0};
  26. /* GPIO Ports Clock Enable */
  27. __HAL_RCC_GPIOC_CLK_ENABLE();
  28. /*Configure GPIO pin Output Level */
  29. HAL_GPIO_WritePin(GPIOC, ds1302_data_Pin, GPIO_PIN_RESET);
  30. /*Configure GPIO pins : PCPin PCPin PCPin */
  31. GPIO_InitStruct.Pin = ds1302_data_Pin;
  32. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  33. GPIO_InitStruct.Pull = GPIO_NOPULL;
  34. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  35. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  36. }
  37. /**
  38. * @brief DS1302写一个字节
  39. * @param Command 命令字/地址
  40. * @param Data 要写入的数据
  41. * @retval 无
  42. */
  43. void DS1302_WriteByte(uint8_t Command,uint8_t Data)
  44. {
  45. uint8_t i;
  46. HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_SET);
  47. for(i=0;i<8;i++)
  48. {
  49. if(Command&(0x01<<i))
  50. {
  51. HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_SET);
  52. }
  53. else
  54. {
  55. HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
  56. }
  57. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
  58. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
  59. }
  60. for(i=0;i<8;i++)
  61. {
  62. if(Data&(0x01<<i))
  63. {
  64. HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_SET);
  65. }
  66. else
  67. {
  68. HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
  69. }
  70. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
  71. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
  72. }
  73. HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_RESET);
  74. }
  75. /**
  76. * @brief DS1302读一个字节
  77. * @param Command 命令字/地址
  78. * @retval 读出的数据
  79. */
  80. uint8_t DS1302_ReadByte(uint8_t Command)
  81. {
  82. unsigned char i,Data=0x00;
  83. ds1302data_outmode();
  84. Command|=0x01; //将指令转换为读指令
  85. HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_SET);
  86. for(i=0;i<8;i++)
  87. {
  88. if(Command&(0x01<<i))HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_SET);
  89. else HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
  90. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
  91. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
  92. }
  93. ds1302data_inputmode();
  94. for(i=0;i<8;i++)
  95. {
  96. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_SET);
  97. HAL_GPIO_WritePin(ds1302_clk_GPIO_Port,ds1302_clk_Pin,GPIO_PIN_RESET);
  98. if(HAL_GPIO_ReadPin(ds1302_data_GPIO_Port,ds1302_data_Pin)==1){Data|=(0x01<<i);}
  99. }
  100. HAL_GPIO_WritePin(ds1302_CE_GPIO_Port,ds1302_CE_Pin,GPIO_PIN_RESET);
  101. HAL_GPIO_WritePin(ds1302_data_GPIO_Port,ds1302_data_Pin,GPIO_PIN_RESET);
  102. return Data; //写完指令后,单片机释放对I/O口的控制,把I/O口控制权交给DS1302;
  103. }
  104. /**
  105. * @brief DS1302设置时间,调用之后,DS1302_Time数组的数字会被设置到DS1302中
  106. * @param 无
  107. * @retval 无
  108. */
  109. void DS1302_SetTime() //十进制转BCD码后写入
  110. {
  111. DS1302_WriteByte(DS1302_WP,0x00);
  112. DS1302_WriteByte(DS1302_YEAR,nian/10*16+nian%10);
  113. DS1302_WriteByte(DS1302_MONTH,yue/10*16+yue%10);
  114. DS1302_WriteByte(DS1302_DATE,ri/10*16+ri%10);
  115. DS1302_WriteByte(DS1302_HOUR,shi/10*16+shi%10);
  116. DS1302_WriteByte(DS1302_MINUTE,fen/10*16+fen%10);
  117. DS1302_WriteByte(DS1302_SECOND,miao/10*16+miao%10);
  118. DS1302_WriteByte(DS1302_DAY,week/10*16+week%10);
  119. DS1302_WriteByte(DS1302_WP,0x80);
  120. }
  121. /**
  122. * @brief DS1302读取时间,调用之后,DS1302中的数据会被读取到DS1302_Time数组中
  123. * @param 无
  124. * @retval 无
  125. */
  126. void DS1302_ReadTime() //BCD码转十进制后读取
  127. {
  128. uint8_t Temp;
  129. Temp=DS1302_ReadByte(DS1302_YEAR);
  130. nian=Temp/16*10+Temp%16;
  131. Temp=DS1302_ReadByte(DS1302_MONTH);
  132. yue=Temp/16*10+Temp%16;
  133. Temp=DS1302_ReadByte(DS1302_DATE);
  134. ri=Temp/16*10+Temp%16;
  135. Temp=DS1302_ReadByte(DS1302_HOUR);
  136. shi=Temp/16*10+Temp%16;
  137. Temp=DS1302_ReadByte(DS1302_MINUTE);
  138. fen=Temp/16*10+Temp%16;
  139. Temp=DS1302_ReadByte(DS1302_SECOND);
  140. miao=Temp/16*10+Temp%16;
  141. Temp=DS1302_ReadByte(DS1302_DAY);
  142. week=Temp/16*10+Temp%16;
  143. }
  1. #ifndef __DS1302_H__
  2. #define __DS1302_H__
  3. #include "main.h"
  4. //寄存器写入地址/指令定义
  5. #define DS1302_SECOND 0x80
  6. #define DS1302_MINUTE 0x82
  7. #define DS1302_HOUR 0x84
  8. #define DS1302_DATE 0x86
  9. #define DS1302_MONTH 0x88
  10. #define DS1302_DAY 0x8A
  11. #define DS1302_YEAR 0x8C
  12. #define DS1302_WP 0x8E
  13. void DS1302_Init(void);
  14. void DS1302_WriteByte(uint8_t Command,uint8_t Data);
  15. uint8_t DS1302_ReadByte(uint8_t Command);
  16. void DS1302_SetTime(void);
  17. void DS1302_ReadTime(void);
  18. #endif

需要注意的是在读取字节时候data引脚先是配置成输出模式,而后读入数据要配置成输入模式。在h文件中对8个地址进行宏定义,分别是是时分秒年月日周以及写保护地址,设置新时间时候需要先关闭写保护再写入而后开启写保护。烧入代码即可看到现象啦。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/537213
推荐阅读
相关标签
  

闽ICP备14008679号