RTC
RTC(Real-Time Clock)是实时时钟模块,用于跟踪实际时间(年、月、日、时、分、秒),即使在系统断电或处于低功耗模式下也能保持时间的准确性。
特点
时间和日期跟踪 低功耗模式支持 可编程闹钟和定时器 备份寄存器
使用方法
开启RCC的High SPeed Clock的Crystal/Ceramic模式 配置时钟电路。 RTC所用时钟 配置RTC - active clock source - active Calendar 使用USART1将其改为异步通信 具体代码
# include "main.h"
RTC_HandleTypeDef hrtc;
UART_HandleTypeDef huart1;
void SystemClock_Config ( void ) ;
static void MX_GPIO_Init ( void ) ;
static void MX_RTC_Init ( void ) ;
static void MX_USART1_UART_Init ( void ) ;
int __io_putchar ( int ch)
{
HAL_UART_Transmit ( & huart1, ( uint8_t * ) & ch, 1 , HAL_MAX_DELAY) ;
return ch;
}
int main ( void )
{
HAL_Init ( ) ;
SystemClock_Config ( ) ;
MX_GPIO_Init ( ) ;
MX_RTC_Init ( ) ;
MX_USART1_UART_Init ( ) ;
RTC_DateTypeDef date;
date. Year = 24 ;
date. Month = 9 ;
date. Date = 13 ;
date. WeekDay = RTC_WEEKDAY_FRIDAY;
HAL_RTC_SetDate ( & hrtc, & date, RTC_FORMAT_BIN) ;
RTC_TimeTypeDef time;
time. Hours = 16 ;
time. Minutes = 10 ;
time. Seconds = 50 ;
HAL_RTC_SetTime ( & hrtc, & time, RTC_FORMAT_BIN) ;
while ( 1 )
{
HAL_RTC_GetDate ( & hrtc, & date, RTC_FORMAT_BIN) ;
HAL_RTC_GetTime ( & hrtc, & time, RTC_FORMAT_BIN) ;
printf ( "TIME:20%02d-%02d-%02d %02d:%02d:%02d \r\n" ,
date. Year, date. Month, date. Date, time. Hours, time. Minutes, time. Seconds) ;
HAL_Delay ( 1000 ) ;
}
}
ADC (Analog-to-Digital Converter)
模式转换器,负责将模拟信号转换成数字信号。 允许微控制器读取来自传感器或其它模拟输入的电压变化,并将其转换为可以处理的数字值。 使用方法 1. 查找电路图选择要进行模数转换的接口,我的是ADC的通道8 2. 设置ADC1的通道8的continuous为Enabled。 3. 开启USART1串口为异步通信,用于调试。 4. 设置RCC为Crystal 5. 配置时钟,设置ADC1为8MHZ。 6. 获取对应的值,并进行*3300/4095(其中3300是参考电压,4095是最大值ADC值) 7. 通过这种方式,你可以准确地将ADC读取的数字值转换为对应的电压值,从而实现对模拟信号的有效监测。 8. 代码示例
# include "main.h"
ADC_HandleTypeDef hadc1;
UART_HandleTypeDef huart1;
void SystemClock_Config ( void ) ;
static void MX_GPIO_Init ( void ) ;
static void MX_ADC1_Init ( void ) ;
static void MX_USART1_UART_Init ( void ) ;
int __io_putchar ( int ch)
{
HAL_UART_Transmit ( & huart1, ( uint8_t * ) & ch, 1 , HAL_MAX_DELAY) ;
return ch;
}
int main ( void )
{
HAL_Init ( ) ;
SystemClock_Config ( ) ;
MX_GPIO_Init ( ) ;
MX_ADC1_Init ( ) ;
MX_USART1_UART_Init ( ) ;
HAL_ADC_Start ( & hadc1) ;
while ( 1 )
{
uint32_t regVal = HAL_ADC_GetValue ( & hadc1) ;
uint32_t mv = regVal * 3300 / 4095 ;
printf ( "voltage = %d mv\r\n" , mv) ;
HAL_Delay ( 500 ) ;
}
}
void SystemClock_Config ( void )
{
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 } ;
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 } ;
RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 } ;
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_MUL8;
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_DIV2;
RCC_ClkInitStruct. APB2CLKDivider = RCC_HCLK_DIV1;
if ( HAL_RCC_ClockConfig ( & RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler ( ) ;
}
PeriphClkInit. PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit. AdcClockSelection = RCC_ADCPCLK2_DIV8;
if ( HAL_RCCEx_PeriphCLKConfig ( & PeriphClkInit) != HAL_OK)
{
Error_Handler ( ) ;
}
}
static void MX_ADC1_Init ( void )
{
ADC_ChannelConfTypeDef sConfig = { 0 } ;
hadc1. Instance = ADC1;
hadc1. Init. ScanConvMode = ADC_SCAN_DISABLE;
hadc1. Init. ContinuousConvMode = ENABLE;
hadc1. Init. DiscontinuousConvMode = DISABLE;
hadc1. Init. ExternalTrigConv = ADC_SOFTWARE_START;
hadc1. Init. DataAlign = ADC_DATAALIGN_RIGHT;
hadc1. Init. NbrOfConversion = 1 ;
if ( HAL_ADC_Init ( & hadc1) != HAL_OK)
{
Error_Handler ( ) ;
}
sConfig. Channel = ADC_CHANNEL_8;
sConfig. Rank = ADC_REGULAR_RANK_1;
sConfig. SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if ( HAL_ADC_ConfigChannel ( & hadc1, & sConfig) != HAL_OK)
{
Error_Handler ( ) ;
}
}
static void MX_USART1_UART_Init ( void )
{
huart1. Instance = USART1;
huart1. Init. BaudRate = 115200 ;
huart1. Init. WordLength = UART_WORDLENGTH_8B;
huart1. Init. StopBits = UART_STOPBITS_1;
huart1. Init. Parity = UART_PARITY_NONE;
huart1. Init. Mode = UART_MODE_TX_RX;
huart1. Init. HwFlowCtl = UART_HWCONTROL_NONE;
huart1. Init. OverSampling = UART_OVERSAMPLING_16;
if ( HAL_UART_Init ( & huart1) != HAL_OK)
{
Error_Handler ( ) ;
}
}
static void MX_GPIO_Init ( void )
{
__HAL_RCC_GPIOD_CLK_ENABLE ( ) ;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
__HAL_RCC_GPIOA_CLK_ENABLE ( ) ;
}
void Error_Handler ( void )
{
__disable_irq ( ) ;
while ( 1 )
{
}
}
# ifdef USE_FULL_ASSERT
void assert_failed ( uint8_t * file, uint32_t line)
{
}
# endif