GPIO
// void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init);
HAL_GPIO_Init(GPIOA, GPIO_PIN_1); // 初始化GPIO引脚 PA1// void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1); // 反初始化GPIO引脚 PA1// GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1); // 读取GPIO引脚电平状态// void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1); // 写入GPIO引脚电平状态
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0); // 写入GPIO引脚电平状态// void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); // 翻转GPIO引脚电平状态// HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
HAL_GPIO_LockPin(GPIOA, GPIO_PIN_1); // 锁住GPIO引脚电平状态
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin); // 外部中断处理函数
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin); // 外部中断回调函数
UART
// HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_UART_Transmit(&huart1, (uint8_t *) msg, strlen(msg), HAL_MAX_DELAY); // 串口发送数据// HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_UART_Receive(&huart1, (uint8_t *) msg, strlen(msg), HAL_MAX_DELAY); // 串口接收数据// HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size);
HAL_UART_Transmit_IT(&huart1, (uint8_t *) msg, strlen(msg)); // 串口中断模式发送 // HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_UART_Receive_IT(&huart1, (uint8_t *) msg, strlen(msg)); // 串口中断模式接收// HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *) msg, strlen(msg)); // 串口DMA模式接收// HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *) msg, strlen(msg)); // 串口DMA模式发送// HAL_UART_StateTypeDef HAL_UART_GetState(const UART_HandleTypeDef *huart);
HAL_UART_StateTypeDef HAL_UART_GetState(&huart1); // 查询串口接收状态
void HAL_UART_IRQHandler(UART_HandleTypeDef *huart); // 串口中断处理函数
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart); // 串口发送回调函数
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart); // 串口发送过半回调函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart); // 串口接收回调函数
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart); // 串口接收过半回调函数
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart); // 串口接收错误函数
// HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen, uint32_t Timeout);
HAL_UARTEx_ReceiveToIdle(&huart1, msg, strlen(msg), HAL_MAX_DELAY); // 串口接收不定长数据// HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_UARTEx_ReceiveToIdle_IT(&huart1, msg, strlen(msg)); // 中断模式串口接收不定长数据// HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, msg, strlen(msg)); // DMA模式串口接收不定长数据
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size); // 串口数据接收完毕回调函数
I2C
// HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_I2C_Master_Transmit(&hi2c1, ADDRESS, &sendBuffer, strlen(sendBuffer), HAL_MAX_DELAY); // I2C向从设备(地址左移补0)写入(一般为写入寄存器地址或寄存器地址+命令)// HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_I2C_Master_Receive(&hi2c1, ADDRESS, &readBuffer, strlen(readBuffer), HAL_MAX_DELAY); // I2C向从设备(地址左移补1)读取(可结合写入命令读取某个寄存器值)// HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
HAL_I2C_Master_Transmit_IT(&hi2c1, ADDRESS, &sendBuffer, strlen(sendBuffer)) // I2C中断模式向从设备写入数据// HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
HAL_I2C_Master_Receive_IT(&hi2c1, ADDRESS, &readBuffer, strlen(readBuffer)) // I2C中断模式向从设备读取数据// HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
HAL_I2C_Master_Transmit_DMA(&hi2c1, ADDRESS, &sendBuffer, strlen(sendBuffer)) // I2C DMA模式向从设备写入数据// HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
HAL_I2C_Master_Receive_DMA&hi2c1, ADDRESS, &readBuffer, strlen(readBuffer)) // I2C DMA模式向从设备读取数据// HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);// HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);// HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);// HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);// HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);// HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c); // I2C发送中断
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c); // I2C接收中断
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c); // I2C写入中断
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c); // I2C读取中断
定时器
void HAL_TIM_Base_Start_IT(&htim2); // 定时器启动函数
void HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL); // 编码器启动函数void HAL_TIM_IRQHandler(&htim2); // 定时器中断处理函数
void HAL_TIM_PeriodElapsedCallback(&htim2); // 定时器回调函数__HAL_TIM_GET_COUNTER(&htim2); // 获取计数器值(编码器值)
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); // 使能PWM输出
__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, 100); // 设置PWM占空比
- 参考文档:STM32HAL库常用函数速查手册 - __TRACKER - 博客园
- L丶小言 - STM32HAL库纯使用向教学,STM32HAL库学习 | BiliBili
- keysking - 【STM32入门教程】应该是全B站最好的STM32教程了 | BiliBili