OpenEdv-开源电子网

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

207板子串口通信问题,串口中断进去了,但是不能收到数据

[复制链接]

1

主题

8

帖子

0

精华

新手上路

积分
22
金钱
22
注册时间
2016-9-19
在线时间
5 小时
发表于 2016-9-21 08:46:38 | 显示全部楼层 |阅读模式
代码如下:
/* Enable GPIOs clocks */
        RCC_AHB1PeriphClockCmd(        RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
                                                         RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD |
                                                           RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF |
                                                         RCC_AHB1Periph_GPIOG|SD_DETECT_GPIO_CLK, ENABLE);
       

        /* Enable SYSCFG clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);   /* 使能串口外设的时钟 */



        GPIO_InitTypeDef  GPIO_InitStructure;
       
       

    /* Connect PXx to USARTx_Tx*/
           GPIO_PinAFConfig(PORT_UARTTX,PINSOURCE_UARTTX,GPIO_AF_USART2);
        /* Connect PXx to USARTx_Rx*/
        GPIO_PinAFConfig(PORT_UARTRX,PINSOURCE_UARTRX,GPIO_AF_USART2);

        /* Configure USART2 Rx (PD.06) as input floating */
        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_InitStructure.GPIO_Pin = PIN_UARTRX;
          GPIO_Init(PORT_UARTRX, &GPIO_InitStructure);


          /* Configure USART2 Tx (PD.05) as alternate function push-pull */
        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_InitStructure.GPIO_Pin = PIN_UARTTX;
          GPIO_Init(PORT_UARTTX, &GPIO_InitStructure);
       

NVIC_InitTypeDef NVIC_InitStructure;

        #ifdef  VECT_TAB_RAM
          /* Set the Vector Table base location at 0x20000000 */
          NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else  /* VECT_TAB_FLASH  */
         /* Set the Vector Table base location at 0x08000000 */
          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif


        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
       
        // Enable the USART2 Interrupt
          NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
       
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStructure);
/* Enable USART2 Receive and Transmit interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);



void USART2_IRQHandler(void)
{
        /*        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)        //判断发生接收中断
        {
                USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志

                uart2_recv_fifo[uart2_recv_fifo_in++]=USART_ReceiveData(USART2);
                if(uart2_recv_fifo_in>=UART2_FIFO_SIZE)uart2_recv_fifo_in=0;

        }

*/


#if 1
        //接收中断
        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
        {
                USART_ClearITPendingBit(USART2, USART_IT_RXNE);
                if(((((uart2_recv_fifo_in + 1) == UART2_FIFO_SIZE) ? 0 : (uart2_recv_fifo_in + 1)) == uart2_recv_fifo_out))
                {
                        USART_ReceiveData(USART2);
                        return;
                }
                /* Read one byte from the receive data register */
                 uart2_recv_fifo[uart2_recv_fifo_in++] = USART_ReceiveData(USART2);      
                if(uart2_recv_fifo_in == UART2_FIFO_SIZE)
                {
                        uart2_recv_fifo_in=0;
                }
        }
#endif

        //发送中断
        if(USART_GetITStatus(USART2, USART_IT_TC) != RESET)
        {
                USART_ClearITPendingBit(USART2, USART_IT_TC);

                uart2_send_fifo_out++;
                if(uart2_send_fifo_out == UART2_FIFO_SIZE)
                {
                        uart2_send_fifo_out = 0;
                }
                /* Write one byte to the transmit data register */
                if(uart2_send_fifo_out != uart2_send_fifo_in)
                {
                        USART_SendData(USART2, uart2_send_fifo[uart2_send_fifo_out]);
                                        //USART_SendData(USART2,0x11);
                }
                else
                {
                        uart2_status = 0;
                        USART_ITConfig(USART2, USART_IT_TC, DISABLE);
                }

        }


       
}



uint8 uart2_write_buf(uint8 *buf, uint16 length)
{
        uint16 send_fifo_in_backup=uart2_send_fifo_in;

        while(length--)
        {
                //测试缓冲区是否满
                uint16 fifo_in_test=uart2_send_fifo_in;
                fifo_in_test++;
                if(fifo_in_test>=UART2_FIFO_SIZE)fifo_in_test=0;
                if(fifo_in_test==uart2_send_fifo_out)
                {
                        //缓冲区满恢复发送之前的指针
                        uart2_send_fifo_in=send_fifo_in_backup;
                        USART_ITConfig(USART2, USART_IT_TXE,ENABLE);
                        return 0;
                }
                //向FIFO填充数据
                uart2_send_fifo[uart2_send_fifo_in++]=*buf++;
                if(uart2_send_fifo_in>=UART2_FIFO_SIZE)uart2_send_fifo_in=0;
        }
        //开发送中断 利用中断发送 如果这时候硬件发送FIFO有空位置(UART2_SR&0x02) 就会进入中断自动发送
        //UART2_MAX485_CTRL_REG|=(0x0001<<UART2_MAX485_CTRL_BIT);//MAX485发送状态
        USART_ITConfig(USART2, USART_IT_TXE,ENABLE);

        return 1;
}

uint8 uart2_read_char(uint8 *ch)
{
        if(uart2_recv_fifo_out==uart2_recv_fifo_in)return 0;
        *ch=uart2_recv_fifo[uart2_recv_fifo_out];//得到当前要读取的字符
        uart2_recv_fifo_out++;//指向下一个要读取的字符
        if(uart2_recv_fifo_out>=UART2_FIFO_SIZE) uart2_recv_fifo_out=0;//如果到了fifo末尾 则重从头开始

        return 1;
}



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

58

主题

6294

帖子

1

精华

资深版主

Rank: 8Rank: 8

积分
11537
金钱
11537
注册时间
2014-4-1
在线时间
1314 小时
发表于 2016-9-21 08:50:33 | 显示全部楼层

但是不能收到数据   什么意思?收到错误数据?

回复 支持 反对

使用道具 举报

1

主题

8

帖子

0

精华

新手上路

积分
22
金钱
22
注册时间
2016-9-19
在线时间
5 小时
 楼主| 发表于 2016-9-21 08:52:05 | 显示全部楼层
就是这样的话,中断进去了,但是USART_GetITStatus(USART2, USART_IT_RXNE) == RESET,所以进入不了下面的接收处理,我已经连着串口线,给板子发送着数据了。
回复 支持 反对

使用道具 举报

1

主题

8

帖子

0

精华

新手上路

积分
22
金钱
22
注册时间
2016-9-19
在线时间
5 小时
 楼主| 发表于 2016-9-22 08:50:21 | 显示全部楼层
问题已经解决,关于接受中断进不去的问题,是因为板子的一个引脚虚焊了;关于发送中断发送不出去数据的问题,需要修改uint8 uart2_write_buf(uint8 *buf, uint16 length)中的 USART_ITConfig(USART2, USART_IT_TXE,ENABLE);为:USART_ITConfig(USART2, USART_IT_TC, ENABLE);
回复 支持 反对

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-5-25 03:01

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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