完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
串口初始化 USART_InitTypeDef usart_init_structure; GPIO_InitTypeDef gpio_init_structure; NVIC_InitTypeDef nvic_init_structure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART4, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_4); GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_4); gpio_init_structure.GPIO_Pin = (GPIO_Pin_0| GPIO_Pin_1); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_PP; gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz; gpio_init_structure.GPIO_PuPd= GPIO_PuPd_UP; GPIO_Init(GPIOA, &gpio_init_structure); usart_init_structure.USART_BaudRate=115200; usart_init_structure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; usart_init_structure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx; usart_init_structure.USART_Parity = USART_Parity_No; usart_init_structure.USART_StopBits = USART_StopBits_1; usart_init_structure.USART_WordLength = USART_WordLength_8b; USART_Init(USART4,&usart_init_structure); USART_ITConfig(USART4,USART_IT_RXNE,ENABLE); USART_ITConfig(USART4,USART_IT_TC,ENABLE); USART_ITConfig(USART4,USART_IT_IDLE,ENABLE); USART_Cmd(USART4,ENABLE); nvic_init_structure.NVIC_IRQChannel=USART3_4_IRQn; nvic_init_structure.NVIC_IRQChannelPriority=1; nvic_init_structure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&nvic_init_structure);
最佳答案
|
|
|
|
|
|
|
|
void USART3_4_IRQHandler(void)
{ uint8_t Clear=Clear; if(USART_GetITStatus(USART4, USART_IT_RXNE) != RESET) { Recvbuffer4[USART4_RX_STA++]=USART4->RDR; } else if(USART_GetITStatus(USART4, USART_IT_IDLE) != RESET) { Clear=USART4->ISR; Clear=USART4->RDR ; Recivestate=1; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
还是不行啊 |
|
|
|
本帖最后由 j***boox 于 2018-3-8 11:15 编辑 我是用HAL库实现的,可能和你不太一样。 |
|
|
|
|
|
|
|
给你一个参考,一个空闲中断,一个事件型任务
/* USART6 串口空闲中断函数 */ void HeaterComm_Callback(void) { uint32_t TmpFlag = 0; uint32_t TmpLength; TmpFlag = __HAL_UART_GET_FLAG(&huart6, UART_FLAG_IDLE); if((TmpFlag != RESET)) { __HAL_UART_CLEAR_IDLEFLAG(&huart6); //HAL_UART_AbortReceive_IT(&huart6); HAL_UART_DMAStop(&huart6); TmpLength = __HAL_DMA_GET_COUNTER((DMA_HandleTypeDef *)&hdma_usart6_rx); //TmpLength = hdma_usart6_rx.Instance->NDTR; HeaterComm.DataLength = 32 - TmpLength; osSignalSet (HeaterComm_TaskHandle, 1); //HeaterComm.ReceiveFlag = 1; } } /* HeaterCommTask function */ void HeaterCommTask(void const * argument) { /* USER CODE BEGIN HeaterCommTask */ osEvent Event; HeaterComm.CommStatus = 1; HeadData.Sensor.TcTemp[0].Status = Normal; HeadData.Sensor.TcTemp[1].Status = Normal; HeadData.Sensor.TcTemp[2].Status = Normal; HeadData.Sensor.TcTemp[0].Value = 25; HeadData.Sensor.TcTemp[1].Value = 25; HeadData.Sensor.TcTemp[2].Value = 25; __HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart6, Rs422RxData.Char, 32); /* Infinite loop */ for(;;) { Event = osSignalWait(1, 2000); if(Event.status == osEventSignal) {//接收到一帧数据 osMutexWait (MutexGetTempHandle, 3); if((Rs422RxData.Sensor.Head == 0x4c) && (crc16(Rs422RxData.Char, HeaterComm.DataLength - 2) == Rs422RxData.Sensor.Crc)) { HeadData.Sensor.NtcTemp = Rs422RxData.Sensor.NtcTemp; for(uint8_t i=0; i<3; i++) { HeadData.Sensor.TcTemp.Data = Rs422RxData.Sensor.TcTemp.Data; // HeadData.Sensor.TcTemp.Value = Rs422RxData.Sensor.TcTemp.Value; } HeaterComm.CommStatus = 1; } else { TestData++; // HeaterComm.DataLength = 0; } HeaterComm.DataLength = 0; osMutexRelease (MutexGetTempHandle); HAL_UART_Receive_DMA(&huart6, Rs422RxData.Char, 32); } else {//接收超时 HeaterComm.CommStatus = 0; } xEventGroupSetBits(xCreatedEventGroup, EVENT_BIT_HEATER_COMM_TASK); osThreadYield(); } /* USER CODE END HeaterCommTask */ } |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2105 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1941 浏览 3 评论
4537 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2084 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 23:21 , Processed in 0.908791 second(s), Total 97, Slave 79 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号