OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6477|回复: 1

STM32F407的UART4&5接收不对

[复制链接]

18

主题

62

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
264
金钱
264
注册时间
2012-10-26
在线时间
27 小时
发表于 2016-2-26 11:44:20 | 显示全部楼层 |阅读模式
7金钱
UART4和5使用DMA然后空闲中断里面接收,接收不到数据,中断可以产生,求解
[mw_shl_code=applescript,true]#include "UART5.h"
#include "misc.h"
#include "stdio.h"
#include "string.h"
#include "includes.h"
uint8_t Uart5_Rx1[UART5_RX1_LEN];//UART1接收数组
//uint8_t Uart4_Rx2[UART4_RX2_LEN];//UART1接收数组



void UART5_Config(u32 bound)
{       
        GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;       
       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE); //开GPIOC时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);//开UART4时钟
       
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12; //GPIOC11与GPIOC10
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        //IO速度50M
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推免复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化IO
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
        GPIO_Init(GPIOD,&GPIO_InitStructure);
        //串口4对应引脚复用映射
        GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //GPIOC12¸复用为USART5
        GPIO_PinAFConfig(GPIOD,GPIO_PinSource2 ,GPIO_AF_UART5);  //GPIOD2
        //USART4 初始化
        USART_InitStructure.USART_BaudRate = bound;//波特率设置
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8bit
        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(UART5, &USART_InitStructure); //初始化串口4
       

        USART_DMACmd(UART5, USART_DMAReq_Rx, ENABLE);
       
//中断设置
        USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);//空闲中断
  NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//串口1中断通道
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级2
        NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;                //子优先级0
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
        NVIC_Init(&NVIC_InitStructure);        //初始化
        //DMA使能
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);  //打开DMA2的时钟

        DMA_DeInit(DMA1_Stream0);         //DeInit   
       
    DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //通道选择
        DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(UART5->DR));//源地址
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(Uart5_Rx1);     //目的地址
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;            //外设到内存
        DMA_InitStructure.DMA_BufferSize = UART5_RX1_LEN;               //数据传输量
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;      //内存增量模式
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//数据长度8bit
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;    //存储器数据长度8bit
        DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                //普通模式
        DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;      // 优先级高
    //DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;         
    //DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;       
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
    DMA_Init(DMA1_Stream0, &DMA_InitStructure);//初始化
        DMA_Cmd(DMA1_Stream0,ENABLE);     //使能
  USART_Cmd(UART5, ENABLE);  //开启串口4

}

void UART5_IRQHandler(void)                                 
{     
        uint32_t Length = 0;
        u16 i;
        if(USART_GetITStatus(UART5, USART_IT_IDLE) != RESET)  
        {  
                  DMA_Cmd(DMA1_Stream0,DISABLE);
                  Length = UART5->SR;  
                  Length = UART5->DR; //清除标志位
                        Length = UART5_RX1_LEN - DMA_GetCurrDataCounter(DMA1_Stream0);

                                printf("\r\n%d\r\n",Length);
         for(i=0;i<Length;i++)
                          {
                                        //Putc_UART4(Uart4_Rx1);
                                        printf("%c ",Uart5_Rx1);
         }               
       
      DMA_SetCurrDataCounter(DMA1_Stream0,UART5_RX1_LEN);//重装填,并让接收地址从0开始
                  DMA_Cmd(DMA1_Stream0, ENABLE);//处理完,重开DMA
        }

        __nop();   
}

void  Putc_UART5(u8 ch)
{
        while((UART5->SR&0X40)==0);
        USART_SendData(UART5, ch);       
}
        [/mw_shl_code]
       

无个性,不签名
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165371
金钱
165371
注册时间
2010-12-1
在线时间
2110 小时
发表于 2016-3-3 00:25:42 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-26 06:13

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表