新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > ARM的串口通信配置

        ARM的串口通信配置

        作者: 時間:2016-11-26 來源:網絡 收藏
        #include "Usart.h"

        #include "stdio.h"

        本文引用地址:http://www.104case.com/article/201611/321631.htm

        void usart_Configuration(void)
        {GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        USART_ClockInitTypeDefUSART_ClockInitStructure;
        //////////////////////////////////////////////////////////////////
        //
        USART_ClockInit(USART1, &USART_ClockInitStructure);

        USART_InitStructure.USART_BaudRate = 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_Cmd(USART1, ENABLE);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
        USART_ITConfig(USART1,USART_IT_TXE,ENABLE);

        USART_ClearFlag(USART1, USART_FLAG_TC);
        }


        int fputc(int ch, FILE *f)
        {


        USART_SendData(USART1,(uint8_t) ch);


        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
        {

        }

        return ch;
        }


        int fgetc(FILE *f)
        {

        while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
        {}
        return (int)USART_ReceiveData(USART1);
        }

        void USART1_IRQHandler(void)
        {u16 RxBuf;
        if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
        {
        RxBuf=USART_ReceiveData(USART1);
        //printf("%d",RxBuf);
        //printf("");
        switch(RxBuf)
        {//
        case 1: printf("1 ");printf("%d",255);break;//對方發送是1
        case 2: printf("2 ");printf("%d",255);break;//對方發送是2
        case 3: printf("3 ");printf("%d",255);break;//對方發送是3
        default: printf("4 ");printf("%d",255);break;
        }
        }
        if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)//這段是為了避免STM32 USART 第一個字節發不出去的BUG
        {
        USART_ITConfig(USART1, USART_IT_TXE, DISABLE);//禁止發緩沖器空中斷,
        }
        }



        關鍵詞: ARM串口通信配

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 遂平县| 蓝山县| 辉南县| 临安市| 正阳县| 呼和浩特市| 泰顺县| 邯郸县| 长沙县| 昔阳县| 安龙县| 唐海县| 南开区| 台州市| 岱山县| 临安市| 东至县| 嵊州市| 双鸭山市| 三亚市| 许昌县| 宜阳县| 剑阁县| 岳阳市| 会昌县| 吉水县| 武安市| 同心县| 西和县| 无棣县| 抚远县| 扶风县| 沅陵县| 鄯善县| 广宁县| 图木舒克市| 红桥区| 象州县| 玛多县| 中西区| 芜湖市|