以uart为例
首先打开stm32f4xx_hal_conf.h中的define:
然后在工程中添加相关的.c文件
定义模块初始化函数,并在main中调用
/* USART3 init function */
static void MX_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 19200;//9600;//
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
在stm32f4xx_hal_msp.c中定义模块的MspInit和Deinit(一般没啥用,主要是Init)设置gpio,dma及中断等配置
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART3)
{
/* USER CODE BEGIN USART3_MspInit 0 */
/* USER CODE END USART3_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
/**USART3 GPIO Configuration
PC10 ------> USART3_TX
PC11 ------> USART3_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USART3 DMA Init */
/* USART3_TX Init */
hdma_usart3_tx.Instance = DMA1_Stream3;
hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart3_tx.Init.Mode = DMA_NORMAL;
hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(huart,hdmatx,hdma_usart3_tx);
/* USART3_RX Init */
hdma_usart3_rx.Instance = DMA1_Stream1;
hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart3_rx.Init.Mode = DMA_NORMAL;
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(huart,hdmarx,hdma_usart3_rx);
/* USART3 interrupt Init */
HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_IRQn);
/* USER CODE BEGIN USART3_MspInit 1 */
/* USER CODE END USART3_MspInit 1 */
}
}
如果使能了中断,在stm32f4xx_it.c中定义模块中断句柄,这个文件中缺少中断句柄会导致hardfault
extern uint8_t recv_end_flag;
extern uint8_t Rx_len;
void USART3_IRQHandler(void)
{
/* USER CODE BEGIN USART3_IRQn 0 */
/* USER CODE END USART3_IRQn 0 */
HAL_UART_IRQHandler(&huart3);
/* USER CODE BEGIN USART3_IRQn 1 */
uint32_t temp;
/* 如果是串口3中断 */
if(USART3 == huart3.Instance)
{
/* 如果是串口3IDLE中断 */
if(RESET != __HAL_UART_GET_FLAG(&huart3,UART_FLAG_IDLE))
{
/* 清除中断标志 */
__HAL_UART_CLEAR_IDLEFLAG(&huart3);
/* 停止DMA接收 */
HAL_UART_DMAStop(&huart3);
/* 获取DMA当前还有多少未填充 */
temp = __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);
/* 计算串口接收到的数据个数 */
Rx_len = 100 - temp;
recv_end_flag = 1;
}
}
/* USER CODE END USART3_IRQn 1 */
}
在相应的位置包含stm32f4xx_hal.h文件,就可以调用模块stm32f4xx_hal_uart.h中的接口了
/* IO operation functions *******************************************************/
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart);
HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart);
HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart);
上一篇:STM32驱动LCD12864显示屏
下一篇:学习笔记∣stm32l0xx时钟系统详解与代码配置
推荐阅读最新更新时间:2024-11-13 05:54
设计资源 培训 开发板 精华推荐
- 使用 Diodes Incorporated 的 PT8A3515 的参考设计
- LTM8052IV ±5A、2.5V、2 象限稳压器的典型应用
- LTC3630AEDHC 4V 至 63V 输入至 -12V 输出、正负转换器的典型应用电路
- AD8339-EVALZ,基于 AD8339 50MHz I/Q 解调器的评估板
- ACE1001/8001、ACE1101、ACE1202和ACE1502 MCU的ACEx系列在线编程教程
- DC383B,基于 LTC1930ES5/LTC1931ES5 1.2 MHz DC/DC 转换器的演示板
- LTC2856、20Mbps、转换速率受限的15kV RS485/RS422收发器的典型应用电路
- 【物联网】鸿蒙智能WIFI开关+4224505A
- 带有使能复位和看门狗的 NCV8141 5V、500mA 线性稳压器的典型应用电路
- 高效率数字电源解决方案源码