查看: 8648|回复: 0

STM32 4个串口同时使用,中断接收,支持连续接收数据

[复制链接]
发表于 2013-1-8 18:55:40 | 显示全部楼层 |阅读模式
关键词: STM32 , 4个串口
STM32 4个串口同时使用,中断接收,支持连续接收数据,很好用.
主程序

int main(void)
{
    u16 aaa=1;
    ChipHalInit();            //片内硬件初始化
    ChipOutHalInit();        //片外硬件初始化
    GPIO_SetBits(GPIOD,GPIO_Pin_8);
    USART1_Puts("USART1 TEST 57600\r\n");
    delay(100);
    USART2_Puts("USART2 TEST 57600\r\n");
    delay(100);
    USART3_Puts("USART3 TEST 57600\r\n");
    delay(100);
    USART4_Puts("UART4 TEST 57600\r\n");
    delay(100);
    GPIO_ResetBits(GPIOD,GPIO_Pin_8);
    delay(50000);
    GPIO_Write(GPIOD,0XFFFF);
    GPIO_ResetBits(GPIOD,GPIO_Pin_8);
#if 1
    while(1)
    {
        delay(5000);
        GPIO_Write(GPIOD,aaa);
        if(aaa>=0x8000)
        {
            aaa = 1;
        }
        else
            aaa = aaa<<1;
    }
#endif

}

串口配置程序

void USART_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;
   
    //使能串口1,PA,AFIO总线
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
            RCC_APB2Periph_AFIO |
            RCC_APB2Periph_USART1 ,
            ENABLE);

    /* A9 USART1_Tx */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //推挽输出-TX
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* A10 USART1_Rx  */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX
    GPIO_Init(GPIOA, &GPIO_InitStructure);


    USART_InitStructure.USART_BaudRate = Uart1_Band_Rate_Set;
    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_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_ClockInit(USART1, &USART_ClockInitStructure);
    USART_Init(USART1, &USART_InitStructure);
    /* Enable the USARTx */
    USART_Cmd(USART1, ENABLE);
    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
   
   
    //使能串口2时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
   
    // A2 做T2X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // A3 做R2X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    USART_InitStructure.USART_BaudRate = Uart2_Band_Rate_Set;
    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_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_ClockInit(USART2, &USART_ClockInitStructure);
    USART_Init(USART2, &USART_InitStructure);
   
    USART_Cmd(USART2, ENABLE);
    //串口2使用接收中断
    USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);


    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
   
    // PB10 做T3X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    // PB11 做R3X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   
    USART_InitStructure.USART_BaudRate = Uart3_Band_Rate_Set;
    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_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_ClockInit(USART3, &USART_ClockInitStructure);
    USART_Init(USART3, &USART_InitStructure);
   
    USART_Cmd(USART3, ENABLE);
    USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
   
    // PB10 做T4X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    // PB11 做R4X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
   
    USART_InitStructure.USART_BaudRate = Uart4_Band_Rate_Set;
    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_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_ClockInit(UART4, &USART_ClockInitStructure);
    USART_Init(UART4, &USART_InitStructure);
   
    USART_Cmd(UART4, ENABLE);
    USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
}

接收中断程序

void USART1_IRQHandler(void)
{
    //接收中断
#if 0
    if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
    {
        USART_ClearITPendingBit(USART1,USART_IT_RXNE);
        Uart1_Get_Data=USART_ReceiveData(USART1);
        Uart1_Get_Flag=1;
        USART1_Putc(Uart1_Get_Data);
    }
#endif
    if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
    {
        TIM_Cmd(TIM3, DISABLE);
        USART_ClearITPendingBit(USART1,USART_IT_RXNE);
        UART1_Data_Buff[UART1_Data_Buff_Count++] = USART_ReceiveData(USART1);
        if(UART1_Data_Buff[UART1_Data_Buff_Count-1] == 0x0a)
        {
             if(UART1_Data_Buff[UART1_Data_Buff_Count-2] == 0x0d)
             {
                       UART1_CMD_Process_ALL_Pointer++;
                    for(UART1_CMD_Process_ALL_Count__ = 0;UART1_CMD_Process_ALL_Count__
                    {
                        UART1_CMD_Process_ALL[UART1_CMD_Process_ALL_Pointer][UART1_CMD_Process_ALL_Count__] = UART1_Data_Buff[UART1_CMD_Process_ALL_Count__];
                    }
                   UART1_Data_Buff_Count = 0;
                   UART1_Data_Flag = 1;
             }
        }        
    }   
    //溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题
    if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
    {
        USART_ClearFlag(USART1,USART_FLAG_ORE);    //读SR
        USART_ReceiveData(USART1);                //读DR
    }
}

http://worldcreativedesign.com/read.php?tid=7








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

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表