OpenEdv-开源电子网

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

F407 ZGT6 串口1中断接收的数据全是0

[复制链接]

1

主题

2

帖子

0

精华

新手入门

积分
8
金钱
8
注册时间
2020-5-12
在线时间
3 小时
发表于 2020-5-12 10:38:38 | 显示全部楼层 |阅读模式
2金钱
串口1和MVB模块通信,发送正常,串口1接收出现问题,用示波器打了串口1输入的信号,可以肯定数据不是0啊,不知道为啥接收的数据全是0,大佬提点一下!
void USART1_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef  NVIC_InitStructure;
        USART_ClockInitTypeDef USART_ClockInitStructure;

        /* 第1步:打开GPIO和USART部件的时钟 */
       
        /* Enable USART3 GPIO clocks */
        RCC_AHB1PeriphClockCmd(UART1_GPIO_RCC, ENABLE);
        /* Enable USART3 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
       
        /* Connect alternate function */
   GPIO_PinAFConfig(UART1_GPIO, UART1_TX_PIN_SOURCE, GPIO_AF_USART1);
   GPIO_PinAFConfig(UART1_GPIO, UART1_RX_PIN_SOURCE, GPIO_AF_USART1);
       
        /* 第2步:将USART Tx的GPIO配置为推挽复用模式 */
        GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX ;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* 第3步:将USART Rx的GPIO配置为浮空输入模式
                由于CPU复位后,GPIO缺省都是浮空输入模式,因此下面这个步骤不是必须的
                但是,我还是建议加上便于阅读,并且防止其它地方修改了这个口线的设置参数
        */
        GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* 第4步:配置USART参数
            - BaudRate = 115200 baud
            - Word Length = 8 Bits
            - One Stop Bit
            - No parity
            - Hardware flow control disabled (RTS and CTS signals)
            - Receive and transmit enabled
        */
        USART_InitStructure.USART_BaudRate = 115200;//  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(USART1, &USART_InitStructure);
        USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
        USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
        USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
        USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
        USART_Init(USART1, &USART_InitStructure);
        /* 第5步:使能 USART, 配置完毕 */
        USART_ClockInit(USART1, &USART_ClockInitStructure);
        USART_Cmd(USART1, ENABLE);

        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;        //USART3全局中断
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//设置USART1在NVIC_IRQChannel中的抢占优先级
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //设置USART1在NVIC_IRQChannel中的响应优先级
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //USART1中断使能
        NVIC_Init(&NVIC_InitStructure);


        /* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
                如下语句解决第1个字节无法正确发送出去的问题 */
        USART_ClearFlag(USART1, USART_FLAG_TC);     /* 清发送外城标志,Transmission Complete flag */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

}

/*******************************************************************************
* Function Name  : USARTx_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
        extern u8 UART1_RxBuffer[72];

        static u16 Rx1Count=0;
        if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
        {
                (uint16_t)(USART1->DR & (uint16_t)0x01FF);
                       
        }  

        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {   
                UART1_RxBuffer[Rx1Count++] = USART_ReceiveData(USART1);                    //
                       
                if(Rx1Count > RxLen)
                {
                        Rx1Count = 0x00;
                        Uart1RxFlag=1;
             GPIO_ResetBits(GPIOF, GPIO_Pin_11);
                }

                USART_ClearFlag(USART1, USART_FLAG_RXNE);
        }

}

最佳答案

查看完整内容[请看2#楼]

低级错误,程序没问题,已解决
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

1

主题

2

帖子

0

精华

新手入门

积分
8
金钱
8
注册时间
2020-5-12
在线时间
3 小时
 楼主| 发表于 2020-5-12 10:38:39 | 显示全部楼层
低级错误,程序没问题,已解决
回复

使用道具 举报

0

主题

60

帖子

0

精华

初级会员

Rank: 2

积分
63
金钱
63
注册时间
2018-12-27
在线时间
0 小时
发表于 2020-5-12 13:05:50 | 显示全部楼层
顶起!!!!!!
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-5-1 23:43

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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