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

作用

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

CubMx配置

在这里插入图片描述
keil配置

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

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的值

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文件

#include "Function.h"
#include "oled.h"
#include "i2c.h"
#include "rtc.h"
#include <stdio.h&gt;

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(&amp;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(&amp;hrtc, &amp;Function_GetDate, RTC_FORMAT_BIN);
	HAL_RTC_GetTime(&amp;hrtc, &amp;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;
}
#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:

#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(&amp;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(&amp;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(&amp;PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
 
void Error_Handler(void)
{
  
  __disable_irq();
  while (1)
  {
  }
  
}

效果

在这里插入图片描述

原文地址:https://blog.csdn.net/xyint/article/details/134755659

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_29996.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注