新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 USART 串口簡單使用

        STM32 USART 串口簡單使用

        作者: 時間:2016-12-01 來源:網絡 收藏
        STM32 的庫實在強大~!函數長的像句子......

        好了開始了:

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

        使用查詢方式的USART:

        設置時鐘:

        RCC_APB2Periph_AFIO功能復用IO時鐘
        RCC_APB2Periph_GPIOAGPIOA時鐘

        RCC_APB2Periph_USART1 USART1時鐘

        你可以用
        //使能串口1,PA,AFIO總線RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);

        或直接

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL,ENABLE); //全部APB2外設時鐘開啟

        注意USART2的你開啟為RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

        設置GPIO:

        GPIO_InitTypeDefGPIO_InitStructure;


        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);


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

        設置USART:

        這里我用的是3.0的庫相對于2.0的庫來說多了一步,先說2.0

        USART_InitTypeDef USART_InitStructure;

        USART_StructInit(&USART_InitStructure);//裝填默認值

        USART_Init(USART1, &USART_InitStructure);//根據USART_InitStruct中指定的參數初始化外設USARTx寄存器
        USART_Cmd(USART1, ENABLE); //啟用

        就好了~!

        而3.0的庫需要

        USART_InitTypeDefUSART_InitStructure;

        USART_ClockInitTypeDefUSART_ClockInitStructure;

        USART_StructInit(&USART_InitStructure);
        USART_ClockStructInit(&USART_ClockInitStructure);
        USART_ClockInit(USART1,&USART_ClockInitStructure);
        USART_Init(USART1,&USART_InitStructure);
        USART_Cmd(USART1,ENABLE);

        //只是多分出了1個USART_ClockInitStructure 我也不知為啥要這樣??為了同步異步模式?USART_InitStruct中指定的參數內容為:(2.0的)

        typedef struct
        {
        u32 USART_BaudRate; //USART傳輸的波特率
        u16 USART_WordLength;//一個幀中傳輸或者接收到的數據位數通常是8
        u16 USART_StopBits; //停止位
        u16 USART_Parity; //奇偶校驗
        u16 USART_HardwareFlowControl; //硬件流控制模式使能還是失能
        u16 USART_Mode; //指定了使能或者失能發送和接收模式
        u16 USART_Clock;//提示了USART時鐘使能還是失能
        u16 USART_CPOL;//指定了下SLCK引腳上時鐘輸出的極性
        u16 USART_CPHA;//指定了下SLCK引腳上時鐘輸出的相位
        u16 USART_LastBit;

        //來控制是否在同步模式下,在SCLK引腳上輸出最后發送的那個數據字通常用USART_LastBit_Disable
        } USART_InitTypeDef;

        我靠~!太細了~!我只知道(9600,8,n,1)這就夠了 其他的統統默認~!

        USART_StructInit(&USART_InitStructure);
        USART_ClockStructInit(&USART_ClockInitStructure); //2.0不用這句,這樣就設好了好了~!自動為您裝填了默認參數。默認的參數如下(3.0的庫):

        voidUSART_StructInit(USART_InitTypeDef* USART_InitStruct)
        {

        USART_InitStruct->USART_BaudRate= 9600;
        USART_InitStruct->USART_WordLength =USART_WordLength_8b;
        USART_InitStruct->USART_StopBits= USART_StopBits_1;
        USART_InitStruct->USART_Parity =USART_Parity_No ;
        USART_InitStruct->USART_Mode =USART_Mode_Rx | USART_Mode_Tx;
        USART_InitStruct->USART_HardwareFlowControl =USART_HardwareFlowControl_None;
        }


        void USART_ClockStructInit(USART_ClockInitTypeDef*USART_ClockInitStruct)
        {

        USART_ClockInitStruct->USART_Clock =USART_Clock_Disable;
        USART_ClockInitStruct->USART_CPOL= USART_CPOL_Low;
        USART_ClockInitStruct->USART_CPHA= USART_CPHA_1Edge;
        USART_ClockInitStruct->USART_LastBit =USART_LastBit_Disable;
        }

        /************************************************************************************************/

        當然了你也可以自己設參數,比如這樣。

        void USART_Configuration(void)

        {

        USART_InitTypeDef USART_InitStructure;
        USART_ClockInitTypeDefUSART_ClockInitStructure;

        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_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);

        USART_Init(USART1, &USART_InitStructure);

        USART_ClockInit(USART1,&USART_ClockInitStructure);
        USART_Cmd(USART1, ENABLE);

        } ////USART_ClockInitStructure.USART_CPHA=USART_CPHA_2Edge;除了這句以外其他的都和默認的參數一樣,二者有啥區別我至今也不太清楚但就一般的應用來說兩個都可以正常工作。

        收發的方法:

        1.發送

        void USART1_Puts(char *str)
        {
        while(*str)
        {
        USART_SendData(USART1, *str++);

        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
        }
        }

        USART1_Puts("hello-java~!"); //這樣就發送了hello-java~!

        跟C語言的printf不太一樣在于并沒有另起一行要用個這樣在終端上好看。

        2.接收

        u8 uart1_get_data; //存放接受的內容

        while(1)

        {

        if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
        {
        uart1_get_data = USART_ReceiveData(USART1);
        USART1_Puts("獲取到串口1數據:");
        USART1_Putc(uart1_get_data);
        USART1_Puts("");
        }

        }


        上一頁 1 2 下一頁

        關鍵詞: STM32USART串

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 桐梓县| 玉山县| 湘阴县| 马公市| 灌南县| 贵定县| 白山市| 怀集县| 英吉沙县| 延津县| 和平县| 镇平县| 青州市| 阜南县| 南阳市| 巴林右旗| 江源县| 阳高县| 香港 | 扶绥县| 新密市| 桦南县| 兴安县| 安吉县| 凤翔县| 东方市| 藁城市| 英山县| 秦皇岛市| 泸溪县| 本溪| 克东县| 甘泉县| 崇文区| 安丘市| 平原县| 饶平县| 封开县| 天峨县| 昌宁县| 奉节县|