蓝桥杯物联网竞赛_STM32L071_6_RTC显示

作用:

RTC在STM32微控制器中通常由一个独立的低功耗晶振和相关的寄存器组成。它可以独立于主处理器运行,即使在系统电源关闭的情况下(需要备用纽扣电池),也能继续计时和记录日期。注意:RTC是芯片内部的功能,并没有和GPIO相连。

CubMx配置:

keil配置:

由于RTC输出的数据为整型数据,要想在oled上显示要添加输出整数的功能,所以添加了如下函数:

c 复制代码
uint32_t OLED_POW(uint32_t X, uint32_t Y){
	uint32_t result = 1;
	while(Y){
		result = result * X;
		Y --;
	}
	return result;
}

pow函数输出X^Y的值

c 复制代码
void OLED_ShowNumber(unsigned char x, unsigned char y, uint32_t chr,unsigned char len, unsigned char size){
	unsigned i = 0;
	unsigned j = 0;
	
	while(j < len){
		OLED_ShowChar(x, y, chr / OLED_POW(10, len - 1 - i) % 10 + '0', size);
	  i ++;
		j ++;
	  x += 8;
    if(x > 120){
       x = 0;
       y += 2;
    }
	}
}

上述函数的作用是将整数拆解为单个字符输出如:

123拆解:
123 / 100 %10 = 1
123 / 10 % 10 = 2
123 / 1 % 10 = 3

Function以及main文件:

c 复制代码
#include "Function.h"
#include "oled.h"
#include "i2c.h"
#include "rtc.h"
#include <stdio.h>

RTC_DateTypeDef Function_GetDate;
RTC_TimeTypeDef Function_GetTime; // 用结构体接收

void OLED_Write(unsigned char type, unsigned char data){
	unsigned char Write_data[2];
	Write_data[0] = type;
	Write_data[1] = data;
	HAL_I2C_Master_Transmit(&hi2c3, 0x78, Write_data, 2, 0xff);
}

void Function_OledEnable(unsigned char ms){
	HAL_GPIO_WritePin(OLED_POWER_GPIO_Port, OLED_POWER_Pin, GPIO_PIN_RESET);
	HAL_Delay(ms);
	OLED_Init();
}


unsigned char *Function_GetLocalTime(void){
	static unsigned char Function_LocalTime[6];
	HAL_RTC_GetDate(&hrtc, &Function_GetDate, RTC_FORMAT_BIN);
	HAL_RTC_GetTime(&hrtc, &Function_GetTime, RTC_FORMAT_BIN); // 获取二进制
	
  Function_LocalTime[0] = Function_GetDate.Year;
	Function_LocalTime[1] = Function_GetDate.Month;
	Function_LocalTime[2] = Function_GetDate.Date;
	
	Function_LocalTime[3] = Function_GetTime.Hours;
	Function_LocalTime[4] = Function_GetTime.Minutes;
	Function_LocalTime[5] = Function_GetTime.Seconds;
	
	return Function_LocalTime;
}
c 复制代码
#ifndef __FUNCTION__
#define __FUNCTION__

void OLED_Write(unsigned char type, unsigned char data);
void Function_OledEnable(unsigned char ms);
unsigned char *Function_GetLocalTime(void);
unsigned char * Function_Decimalism(unsigned char * ArrayBin);
#endif

main.c:

c 复制代码
#include "main.h"
#include "i2c.h"
#include "rtc.h"
#include "gpio.h"
#include "Function.h"
#include "oled.h"

 
void SystemClock_Config(void);
 
int main(void)
{
	unsigned char * Time;
	unsigned char * Temp;
  
  HAL_Init();
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_I2C3_Init();
  MX_RTC_Init();
 
  Function_OledEnable(50);
  
  while (1)
  {
    /* USER CODE END WHILE */
		 Time = Function_GetLocalTime();
     OLED_ShowString(0, 0, "TIME", 16);
		 OLED_ShowNumber(0, 2, Time[3], 2, 16);
		 OLED_ShowChar(16, 2, ':', 16);
		 OLED_ShowNumber(24, 2, Time[4], 2, 16);
		 OLED_ShowChar(40, 2, ':', 16);
		 OLED_ShowNumber(48, 2, Time[5], 2, 16);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3|RCC_PERIPHCLK_RTC;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
 
void Error_Handler(void)
{
  
  __disable_irq();
  while (1)
  {
  }
  
}

效果:

相关推荐
白-胖-子1 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-统计数字
开发语言·c++·算法·蓝桥杯·等考·13级
白-胖-子2 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-成绩排序
c++·算法·蓝桥杯·真题·蓝桥等考
白总Server2 小时前
JVM解说
网络·jvm·物联网·安全·web安全·架构·数据库架构
ahadee2 小时前
蓝桥杯每日真题 - 第11天
c语言·vscode·算法·蓝桥杯
触翔4 小时前
工业主板在气体自动报警设备中的使用
物联网
ahadee7 小时前
蓝桥杯每日真题 - 第7天
c++·vscode·算法·蓝桥杯
小刘同学-很乖9 小时前
MQTT从入门到精通之MQTT Dashboard
spring boot·stm32·物联网·iot
YuCaiH10 小时前
【STM32】USART串口数据包
笔记·stm32·单片机·嵌入式硬件
Kasen's experience12 小时前
STM32 GPIO 配置
stm32·单片机·嵌入式硬件
非概念13 小时前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机