完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
main.c
UART_HandleTypeDef huart1; uint8_t USART1_RX_BUF[200]; //接收缓冲,最大USART_REC_LEN个字节. uint16_t USART1_RX_STA = 0; //接收状态标记 uint8_t USART1_Count = 0; int main(void) { while (1) { if(USART1_RX_STA == 1) { HAL_UART_Transmit(&huart1, (uint8_t*)USART1_RX_BUF, USART1_Count, 1000); //发送接收到的数据 while(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET); //等待发送结束 USART1_RX_STA = 0; USART1_Count = 0; memset(USART1_RX_BUF, 0, sizeof(USART1_RX_BUF)); } } } 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(); } __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); //开启接收中断 HAL_NVIC_EnableIRQ(USART1_IRQn); //使能USART1中断通道 HAL_NVIC_SetPriority(USART1_IRQn, 3, 3); //抢占优先级3,子优先级3 __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); } stm32f1xx_it.c extern uint8_t USART5_RX_BUF[200]; extern uint16_t USART5_RX_STA; extern uint8_t USART5_Count; void USART1_IRQHandler(void) { /* USER CODE BEGIN USART1_IRQn 0 */ uint8_t clear = 0, Res; if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); HAL_UART_Receive(&huart1, &Res, 1, 1000); USART1_RX_BUF[USART1_Count++] = Res; } else if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); clear = huart1.Instance->SR; clear = huart1.Instance->DR; USART1_RX_STA = 1; } /* USER CODE END USART1_IRQn 0 */ HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */ /* USER CODE END USART1_IRQn 1 */ } 注意!!! stm32f103串口4收发数据要改变DIR引脚电平:高电平打开发送TX,低电平打开接收RX 若需要将printf()函数转向串口输出,可在main.c文件添加如下代码 #ifdef __GNUC__ /* With GCC, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF); return ch; } 若要计算输出浮点型,则在project properties > C/C++ Build > Settings > Tool Settings (TAB) > MCU GCC Linker > Miscellaneous > other flag中添加 -u_printf_float |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1202 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1217 浏览 1 评论
625 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
470 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1146 浏览 2 评论
1680浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
339浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
335浏览 3评论
329浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
306浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-9-1 14:48 , Processed in 0.832339 second(s), Total 77, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191