国产毛片a精品毛-国产毛片黄片-国产毛片久久国产-国产毛片久久精品-青娱乐极品在线-青娱乐精品

查看: 9191|回復: 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
快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产a三级三级三级 | 欧美日韩精品一区二区三区视频 | 国内精品久久久久久久久 | 黄色大片久久 | 亚洲黄色毛片 | 久久黄网站 | 麻豆影业 | 草莓香蕉绿巨人丝瓜榴莲18 | 91精品在线免费视频 | 四虎影永久在线观看网址 | 麻豆影业 | 国产九色在线 | 久久91精品牛牛 | 欧美aⅴ片 | 日韩毛片大全免费高清 | 五月天综合视频 | 落跑甜心电视剧高清全集免费观看 | www.噜噜噜| 五月婷婷六月爱 | 无码h肉动漫在线观看 | 国产一区二区三区视频 | 成人午夜免费观看 | 免费一级欧美大片久久网 | 亚洲四虎影视 | 99ri在线观看 | 四虎久久精品国产 | 激情欧美一区二区三区 | 国产精品中文字幕在线观看 | www.噜噜噜| 亚洲合集| 青青青国产色视频在线观看 | 91视频站| 四虎在线精品观看免费 | 猛乳3p市来美保天国在线观看 | 国内不卡一二三四区 | 亚洲欧美日韩国产综合久 | 久久国产经典 | 我们不能是朋友电视剧免费观看 | 欧美日韩国产三级 | 小嫩嫩12欧美 | 国产成人精品福利网站人 |