本帖最后由 jinglixixi 于 2025-3-26 00:02 编辑
在厂家提供的例程中,配有RTC的示例,但好像是要考验使用者的功力,却没有为其配置相应显示后输出功能。
为此,我们可以通过为它添加串口输出功能或是将RTC计时功能添加到类似Hellow World类的程序中以便观察和使用。
为方便实现,这里采用的是将串口输出功能添加到RTC例程中的方式,其测试效果如图1所示。
图1 测试效果
其实现的主程序为:
<font _mstmutation="1">int main(void)
{
int32_t timeout;
MPU_Config();
CPU_CACHE_Enable();
timeout = 0xFFFF;
while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));
if ( timeout < 0 )
{
Error_Handler();
}
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED2);
UartHandle.Instance = USARTx;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1;
UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
Error_Handler();
}
HAL_UARTEx_SetRxFifoThreshold(&UartHandle, UART_RXFIFO_THRESHOLD_1_4);
HAL_UARTEx_EnableFifoMode(&UartHandle);
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_Handler();
}
RTC_AlarmConfig();
__HAL_RTC_ALARM_EXTID2_CLEAR_FLAG();
HAL_EXTI_D2_EventInputConfig(EXTI_LINE17 , EXTI_MODE_IT, ENABLE);
while (1)
{
RTC_TimeShow(aShowTime);</font>
}
}
经修改,实现电子时钟输出效果的RTC显示函数为:
static void RTC_TimeShow(uint8_t* showtime)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN);
cmd4[0] =stimestructureget.Hours/10+'0';
if(BY!=stimestructureget.Seconds)
{
cmd4[1] =stimestructureget.Hours%10+'0';
cmd4[3] =stimestructureget.Minutes/10+'0';
cmd4[4] =stimestructureget.Minutes%10+'0';
cmd4[6] =stimestructureget.Seconds/10+'0';
cmd4[7] =stimestructureget.Seconds%10+'0';
HAL_UART_Transmit(&UartHandle, (uint8_t*)&cmd4, countof(cmd3), HAL_TIMEOUT_VALUE);
BY=stimestructureget.Seconds;
}
}
除此之外,该需要main.h文件中添加如下内容:
#define USARTx USART3
#define USARTx_CLK_ENABLE() __HAL_RCC_USART3_CLK_ENABLE()
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define RCC_PERIPHCLK_USARTx RCC_PERIPHCLK_USART3
#define RCC_USARTxCLKSOURCE_HSI RCC_USART3CLKSOURCE_HSI
#define USARTx_FORCE_RESET() __HAL_RCC_USART3_FORCE_RESET()
#define USARTx_RELEASE_RESET() __HAL_RCC_USART3_RELEASE_RESET()
#define USARTx_IRQn USART3_IRQn
#define USARTx_IRQHandler USART3_IRQHandler
#define USARTx_TX_PIN GPIO_PIN_10
#define USARTx_TX_GPIO_PORT GPIOB
#define USARTx_TX_AF GPIO_AF7_USART3
#define USARTx_RX_PIN GPIO_PIN_11
#define USARTx_RX_GPIO_PORT GPIOB
#define USARTx_RX_AF GPIO_AF7_USART3
在stm32h7xx_hal_msp.c文件中添加如下内容:
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USARTx;
RCC_PeriphClkInit.Usart234578ClockSelection = RCC_USARTxCLKSOURCE_HSI;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
USARTx_CLK_ENABLE();
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USARTx_IRQn);
}
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
USARTx_FORCE_RESET();
USARTx_RELEASE_RESET();
HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
}
在stm32h7xx_it.c文件中添加如下内容:
extern UART_HandleTypeDef UartHandle;
这样,就可以实现基于RTC计时器的电子时钟输出显示功能。
此外,若将串口输出功能添加到A/D检测程序中,则可显示出检测值,见图2所示。
图2 检测结果
相应得程序内容为:
int main(void)
{
int32_t timeout;
MPU_Config();
CPU_CACHE_Enable();
timeout = 0xFFFF;
while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));
if ( timeout < 0 )
{
Error_Handler();
}
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED2);
BSP_LED_Init(LED1);
UartHandle.Instance = USARTx;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1;
UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
Error_Handler();
}
HAL_UARTEx_SetRxFifoThreshold(&UartHandle, UART_RXFIFO_THRESHOLD_1_4);
HAL_UARTEx_EnableFifoMode(&UartHandle);
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
ADC_Config();
if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
{
Error_Handler();
}
#if defined(ADC_TRIGGER_FROM_TIMER)
TIM_Config();
#endif /* ADC_TRIGGER_FROM_TIMER */
#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
WaveformVoltageGenerationForTest_Config();
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */
#if defined(ADC_TRIGGER_FROM_TIMER)
if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK)
{
Error_Handler();
}
#endif /* ADC_TRIGGER_FROM_TIMER */
if (HAL_ADC_Start_DMA(&AdcHandle,
(uint32_t *)aADCxConvertedValues,
ADCCONVERTEDVALUES_BUFFER_SIZE
) != HAL_OK)
{
Error_Handler();
}
while (1)
{
while ((ubUserButtonClickEvent) == RESET)
{
}
ubUserButtonClickEvent = RESET;
if (HAL_ADCEx_InjectedStart_IT(&AdcHandle) != HAL_OK)
{
Error_Handler();
}
#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
WaveformVoltageGenerationForTest_Update();
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */
HAL_Delay(31);
cmd4[0]=uhADCxConvertedValue_Injected/10000+'0';
cmd4[1]=uhADCxConvertedValue_Injected%10000/1000+'0';
cmd4[2]=uhADCxConvertedValue_Injected%1000/100+'0';
cmd4[3]=uhADCxConvertedValue_Injected%100/10+'0';
cmd4[4]=uhADCxConvertedValue_Injected%10+'0';
HAL_UART_Transmit(&UartHandle, (uint8_t*)&cmd4, countof(cmd4), HAL_TIMEOUT_VALUE);
if (uhADCxConvertedValue_Injected < *puhADCxConvertedValue_Regular_Avg)
{
BSP_LED_Off(LED1);
}
else
{
BSP_LED_On(LED1);
}
}
}
|