由于STM32F413( STM32官网) 的内部集成有RTC,,这就为实时时钟的制作通过了方便,我们只需在配套的RTC例程基础上为其添加相应的显示器件即可。 从成本及方便性考虑,为它配置一个IIC接口的双色OLED屏是最经济的,下图即为其使用效果。 显示效果图
那OLED屏与STM32F413是如何连接的呢? 你只需找到板上印有AVDD、GND、PA5、PA6的引脚,如何将OLED屏的引脚插到板子上即可,怎么样很简单吧,你不需要进行任何的连线处理。 主程序则应改为如下的内容: - int main(void)
- {
- /* STM32F4xx HAL library initialization:
- - Configure the Flash prefetch, instruction and Data caches
- - Configure the Systick to generate an interrupt each 1 msec
- - Set NVIC Group Priority to 4
- - Global MSP (MCU Support Package) initialization
- */
- HAL_Init();
- /* Configure LED2 */
- BSP_LED_Init(LED2);
- /* Configure the system clock to 100 MHz */
- SystemClock_Config();
- Lcd_Init();
- OLED_ShowString(2,0,"STM32F413-RTC",16);
- OLED_ShowString(2,2,"Nucleo-144",16);
- OLED_ShowString(2,4,"1.5MB Flash",16);
- OLED_ShowString(2,6,"320-KB SRAM",16);
- delay_ms(1000);
- OLED_Clear();
- OLED_ShowString(2,0,"STM32F413-RTC",16);
- while(1);
-
- /*##-1- Configure the RTC peripheral #######################################*/
- /* Configure RTC prescaler and RTC data registers */
- /* RTC configured as follow:
- - Hour Format = Format 24
- - Asynch Prediv = Value according to source clock
- - Synch Prediv = Value according to source clock
- - OutPut = Output Disable
- - OutPutPolarity = High Polarity
- - OutPutType = Open Drain
- */
- RtcHandle.Instance = RTC;
- RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
- RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
- RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
- RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
- RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
-
- if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
- {
- /* Error */
- Error_Handler();
- }
- /*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
- /* Read the BackUp Register 0 Data */
- if(HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) != 0x32F2)
- {
- /* Configure RTC Calendar */
- RTC_CalendarConfig();
- }
- /* Infinite loop */
- while (1)
- {
- /*##-3- Display the updated Time and Date ################################*/
- RTC_CalendarShow(aShowTime, aShowDate);
- }
- }
复制代码其实还有一个地方是最需要修改的,那就是RTC_CalendarShow()函数,其修改后的内容如下: - static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
- {
- RTC_DateTypeDef sdatestructureget;
- RTC_TimeTypeDef stimestructureget;
-
- /* Get the RTC current Time */
- HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
- /* Get the RTC current Date */
- HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN);
-
- OLED_ShowString(2,2," ",16);
- OLED_ShowString(2,4," ",16);
- OLED_ShowNum(4,2,2000 + sdatestructureget.Year,4,16);
- OLED_ShowChar(36,2,':',16);
- OLED_ShowNum(44,2,sdatestructureget.Month,2,16);
- OLED_ShowChar(60,2,':',16);
- OLED_ShowNum(68,2,sdatestructureget.Date,2,16);
-
- OLED_ShowNum(20,4,stimestructureget.Hours,2,16);
- OLED_ShowChar(36,4,':',16);
- OLED_ShowNum(44,4,stimestructureget.Minutes,2,16);
- OLED_ShowChar(60,4,':',16);
- OLED_ShowNum(68,4,stimestructureget.Seconds,2,16);
-
- #ifdef DISPLAY_ON_DUBUGGER
- /* Display time Format: hh:mm:ss */
- sprintf((char*)showtime,"%02d:%02d:%02d",stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
- /* Display date Format: mm-dd-yy */
- sprintf((char*)showdate,"%02d-%02d-%02d",sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year);
- #endif
- }
复制代码 细心观察的你此时可能会发现一个问题,那就是起始时间的设置问题,由于在开发板上只提供了一个用户键,因此你至少需要再为它再添加一个键来与板上的用户键相配合,以完成起始时间的设置处理,感兴趣的你不妨一试。
|