糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > CubeMX STM32 常用函数HAL库

CubeMX STM32 常用函数HAL库

时间:2019-04-24 08:01:08

相关推荐

CubeMX STM32 常用函数HAL库

初始化函数:

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

读取输入电平函数:

HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_3)

设置输出电平函数:

HAL_GPIO_WritePin (GPIOA, GPIO_PIN_3, GPIO_PIN_SET)

电平翻转函数:

HAL_GPIO_TogglePin (GPIOA, GPIO_PIN_2)

延时200毫秒;

HAL_Delay(200);

中断处理函数:

void HAL_GPIO_EXTI_IRQHandler (uint16_t GPIO_Pin)

中断回调函数:

void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin)

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){

if(GPIO_Pin==Key_1_Pin){

HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1);

}

};

定时器更新中断回调函数

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if(htim->Instance == TIM6)

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);

}

启动定时器(使用更新中断)。

HAL_TIM_Base_Start_IT(&htim6)

PWM接口函数

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);

定时器比较/捕获寄存器CCR设置函数:

__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2,PWM_value);

while (1)

{

if(PWM_value==0||PWM_value<10000)

PWM_value++;

else if(PWM_value==10000)

PWM_value=0;

__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2,PWM_value);

}}

修改重装载寄存器ARR函数

HAL_TIM_SET_AUTORELOAD(&htime6, 500)

串口接收函数

HAL_UART_Receive(&huart1,RecBuf,8,100)

发送函数

HAL_UART_Transmit(&huart1 ,RecBuf,8,100);

uint8_t RecBuf[8];

while (1)

{

if(HAL_UART_Receive(&huart1,RecBuf,8,100)==HAL_OK){

printf ("the response is:\r");

HAL_UART_Transmit(&huart1 ,RecBuf,8,100);

printf("\r");

}

}

串口中断方式发送函数

HAL_UART_Transmit_IT(&huart1,(uint8_t *)RxBuffer,10);

串口中断方式接收函数

HAL_UART_Receive_IT(&huart1,(uint8_t *)RxBuffer,8);

串口发送中断回调函数:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){

if(huart->Instance==USART1){

Rxflag=1;

HAL_UART_Receive_IT(&huart1,(uint8_t *)RxBuffer,10);

}

}

while (1)

{

if(Rxflag==1){

Rxflag=0;

HAL_UART_Transmit_IT(&huart1,(uint8_t *)RxBuffer,10);

}

}

串口中断通用处理函数:

void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)

重写printf

#include "stdio.h"

int fputc(int ch,FILE *f){

HAL_UART_Transmit (&huart1 ,(uint8_t*)&ch ,1,HAL_MAX_DELAY);

return ch ;

}

int fgetc(FILE *f){

uint8_t ch;

HAL_UART_Receive (&huart1 ,(uint8_t*)&ch ,1,HAL_MAX_DELAY);

return ch ;

}

printf("%d",a);输出十进制整数 printf("\r");回车

DMA

void USART1_IRQHandler(void)

{

/* USER CODE BEGIN USART1_IRQn 0 */

/* USER CODE END USART1_IRQn 0 */

HAL_UART_IRQHandler(&huart1);

/* USER CODE BEGIN USART1_IRQn 1 */

if(__HAL_UART_GET_FLAG (&huart1,UART_FLAG_IDLE)!=RESET )

{

__HAL_UART_CLEAR_IDLEFLAG (&huart1);

HAL_UART_IdleCpltCallback(&huart1 );

}

/* USER CODE END USART1_IRQn 1 */

}

#define LENGTH 100

uint8_t RxBuffer[LENGTH];

uint8_t RecCount = 0;

uint8_t RxFlag = 0;

__HAL_UART_ENABLE_IT (&huart1,UART_IT_IDLE);

HAL_UART_Receive_DMA (&huart1 ,(uint8_t*)RxBuffer ,LENGTH );

while (1)

{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

if(RxFlag ==1)

{

RxFlag =0;

RecCount = LENGTH-__HAL_DMA_GET_COUNTER (&hdma_usart1_rx);

HAL_UART_Transmit_DMA (&huart1,(uint8_t*)RxBuffer,RecCount );

RecCount = 0;

__HAL_DMA_DISABLE (&hdma_usart1_rx );

HAL_UART_Receive_DMA (&huart1 ,(uint8_t*)RxBuffer ,LENGTH );

}

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

if(huart->Instance==USART2 )

{HAL_UART_Receive_DMA (&huart1 ,(uint8_t*)RxBuffer ,LENGTH );

}

}

void HAL_UART_IdleCpltCallback(UART_HandleTypeDef *huart)

{

RxFlag =1;

HAL_UART_DMAStop(&huart1);

}

如果觉得《CubeMX STM32 常用函数HAL库》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。