打印
[STM32F4]

STM32F407 采用串口+DMA方式接收传感器数据有误,求大神解答!

[复制链接]
888|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
奥卡姆剃刀|  楼主 | 2019-3-25 11:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
以下是部分代码
void USART_Configuration(uint8_t no)
{                                                                                                
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  DMA_InitTypeDef DMA_InitStruct;

//#if        PRINT_EN
  RCC_AHB1PeriphClockCmd(Open_USART_TX_GPIO_CLK,ENABLE);
  RCC_AHB1PeriphClockCmd(Open_USART_RX_GPIO_CLK,ENABLE);


#if        (USARTx_OPEN == 1 || USARTx_OPEN == 6)
  RCC_APB2PeriphClockCmd(Open_USART_CLK,ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, ENABLE);
  RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, DISABLE);
#elif (USARTx_OPEN == 2 || USARTx_OPEN == 3 || USARTx_OPEN == 4 || USARTx_OPEN == 5)
  RCC_APB1PeriphClockCmd(Open_USART_CLK,ENABLE);
#endif

  GPIO_PinAFConfig(Open_USART_TX_GPIO_PORT, Open_USART_TX_SOURCE, Open_USART_TX_AF);
  GPIO_PinAFConfig(Open_USART_RX_GPIO_PORT, Open_USART_RX_SOURCE, Open_USART_RX_AF);

  /*
  *  Open_USART_TX -> PA9 , Open_USART_RX -PA10
  */
  GPIO_InitStructure.GPIO_Pin = Open_USART_TX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(Open_USART_TX_GPIO_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = Open_USART_RX_PIN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(Open_USART_RX_GPIO_PORT, &GPIO_InitStructure);

/*
                    USARTx configured as follow:
         - BaudRate = 115200 baud  
                 - Word Length = 8 Bits
         - One Stop Bit
         - No parity
         - Hardware flow control disabled (RTS and CTS signals)
         - Receive and transmit   
*/

  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(Open_USART, &USART_InitStructure);
  /* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
     Open_USART transmit data register is empty */
  (USART_DMAy_Streamx[no-1], DISABLE);
                          //Check if the DMA Stream has been effectively disabled.
                          while (DMA_GetCmdStatus(USART_DMAy_Streamx[no-1]) != DISABLE);

                          //init DMA channel
                          DMA_StructInit(&DMA_InitStruct);
                          DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART_PORT[no-1]->DR;
                          DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&USART_RxBuffer[no-1][0];
                          DMA_InitStruct.DMA_BufferSize = USART_MAX_BUF_LEN;
                          DMA_InitStruct.DMA_Channel = USART_DMA_CHAN[no-1];
                          DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
                          DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
                          DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
                          DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
                          DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
                          DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
                          DMA_InitStruct.DMA_Priority = DMA_Priority_High;
                          DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
                          DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
                          DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
                          DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
                          DMA_Init(USART_DMAy_Streamx[no-1], &DMA_InitStruct);
                          // Enable DMA Stream Transfer Complete interrupt
                          DMA_ITConfig(USART_DMAy_Streamx[no-1], DMA_IT_TC, ENABLE);
                          // DMA Stream enable
                          DMA_Cmd(USART_DMAy_Streamx[no-1], ENABLE);
                          //Check if the DMA Stream has been effectively enabled.
                          while (DMA_GetCmdStatus(USART_DMAy_Streamx[no-1]) != ENABLE);
                          /* Enable the DMA Stream IRQ Channel */
                          NVIC_InitStructure.NVIC_IRQChannel = USART_DMA_IRQn[no-1];
                          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
                          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
                          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                          NVIC_Init(&NVIC_InitStructure);
                        USART_ITConfig(USART_PORT[no-1], USART_IT_IDLE, ENABLE);
                        USART_ITConfig(Open_USART,USART_IT_RXNE,DISABLE);
                          USART_DMACmd(USART_PORT[no-1], USART_DMAReq_Rx, ENABLE);


                  USART_Cmd(USART_PORT[no-1], ENABLE);
                  USART_NVIC_Config(no);
//                  UartData[no-1].len = 0;
//                  UartData[no-1].callback = callback;



//USART_Cmd(Open_USART, ENABLE);
//  USART_NVIC_Config(USARTx_OPEN);
}

void USART_IRQHandler(uint8_t no)
{
        volatile char ch;
        if(no > 6)
                return;

        CoEnterISR();
        if(USART_GetITStatus(USART_PORT[no-1], USART_IT_RXNE) != RESET)
        {
                ch = USART_ReceiveData(USART_PORT[no-1]);       // get received character

                if(UartData[no-1].len >= USART_MAX_BUF_LEN)
                {
                        mem_cpy(&UartData[no-1].data[0], &UartData[no-1].data[1], USART_MAX_BUF_LEN-1);
                        UartData[no-1].data[USART_MAX_BUF_LEN-1] = ch;
                }
                else
                {
                        UartData[no-1].data[UartData[no-1].len] = ch;
                        UartData[no-1].len += 1;
                }
//
        }
        else if(USART_GetITStatus(USART_PORT[no-1], USART_IT_IDLE) != RESET)
        {
                DMA_InitTypeDef DMA_InitStruct;
                ch = USART_ReceiveData(USART_PORT[no-1]);        //clear IDLE flag by read USART_SR and USART_DR

                //get received data length
                UartData[no-1].len = USART_MAX_BUF_LEN - USART_DMAy_Streamx[no-1]->NDTR;
                mem_cpy(&UartData[no-1].data[0], &USART_RxBuffer[no-1][0], UartData[no-1].len);
                //analyze the received data and send out the response
                if(UartData[no-1].callback != NULL)
                        ((void (*)(uint8_t))UartData[no-1].callback)(no);
                //----------------
                switch(no)
                {
                        case 1:
                        case 6:
                                RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, ENABLE);
                                RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, DISABLE);
                                break;
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                                RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA1, ENABLE);
                                RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA1, DISABLE);
                                break;
                }
                //----------------
                //disable DMA
                DMA_Cmd(USART_DMAy_Streamx[no-1], DISABLE);
                //Check if the DMA Stream has been effectively disabled.
                while (DMA_GetCmdStatus(USART_DMAy_Streamx[no-1]) != DISABLE);
                //init DMA channel
                DMA_StructInit(&DMA_InitStruct);
                DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART_PORT[no-1]->DR;
                DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&USART_RxBuffer[no-1][0];
                DMA_InitStruct.DMA_BufferSize = USART_MAX_BUF_LEN;
                DMA_InitStruct.DMA_Channel = USART_DMA_CHAN[no-1];
                DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
                DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
                DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
                DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
                DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
                DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
                DMA_InitStruct.DMA_Priority = DMA_Priority_High;
                DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
                DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
                DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
                DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
                DMA_Init(USART_DMAy_Streamx[no-1], &DMA_InitStruct);
                // Enable DMA Stream Transfer Complete interrupt
                DMA_ITConfig(USART_DMAy_Streamx[no-1], DMA_IT_TC, ENABLE);
                // DMA Stream enable
                DMA_Cmd(USART_DMAy_Streamx[no-1], ENABLE);
                //Check if the DMA Stream has been effectively enabled.
                while (DMA_GetCmdStatus(USART_DMAy_Streamx[no-1]) != ENABLE);
        }
        CoExitISR();
}

问题:将串口1收到的传感器数据在经过串口1打印输出到屏幕上时,与传感器发送数据对不上;若将触感器直接通过串口工具传到串口调试助手上,数据正常,请问大神问题出在哪里?

使用特权

评论回复
沙发
mmuuss586| | 2019-4-6 19:56 | 只看该作者

使用特权

评论回复
板凳
yklstudent| | 2019-4-7 15:50 | 只看该作者
串口1收再串口1发,你串口1什么总线?挂了多少东西

使用特权

评论回复
地板
磨砂| | 2019-4-10 08:54 | 只看该作者
半双工的吗?

使用特权

评论回复
5
晓伍| | 2019-4-10 09:00 | 只看该作者
用示波器看看发送数据有没有问题

使用特权

评论回复
6
八层楼| | 2019-4-10 09:10 | 只看该作者
普通模式会有问题吗

使用特权

评论回复
7
观海| | 2019-4-10 09:17 | 只看该作者
首先要找到是发送方的问题还是接收方的问题

使用特权

评论回复
8
guanjiaer| | 2019-4-10 09:20 | 只看该作者
时序安排不合理

使用特权

评论回复
9
heimaojingzhang| | 2019-4-10 10:00 | 只看该作者
先发 别收 试试看

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

14

主题

289

帖子

4

粉丝