新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32串口通信設置(查詢方式)

        STM32串口通信設置(查詢方式)

        作者: 時間:2016-11-25 來源:網絡 收藏
        首先是配置UART的GPIO口


        void UART1_GPIO_Configuration(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        // Configure USART1_Tx as alternate push-pull
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        // Configure USART1_Rx as input floating
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        }

        然后是配置串口參數

        void UART1_Configuration(void)
        {

        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_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;


        USART_Init(USART1, &USART_InitStructure);


        USART_Cmd(USART1, ENABLE);
        }

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

        發送一個字符
        [
        u8 Uart1_PutChar(u8 ch)
        {

        USART_SendData(USART1, (u8) ch);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
        {
        }
        return ch;
        }
        發送一個字符串

        void Uart1_PutString(u8* buf , u8 len)
        {
        for(u8 i="0";i {
        Uart1_PutChar(*buf++);
        }
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 清远市| 江都市| 孟连| 廊坊市| 襄汾县| 德江县| 闽侯县| 鲁甸县| 黎城县| 海伦市| 锡林郭勒盟| 沅陵县| 郸城县| 南昌县| 铜山县| 绥中县| 大兴区| 商南县| 峨眉山市| 焦作市| 泊头市| 彝良县| 枣阳市| 延寿县| 固原市| 进贤县| 贵南县| 蒙城县| 延吉市| 平乐县| 新巴尔虎右旗| 枣庄市| 宁明县| 龙胜| 上虞市| 普宁市| 红原县| 沙雅县| 关岭| 白水县| 永登县|