新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32 無中斷串口代碼

        STM32 無中斷串口代碼

        作者: 時間:2016-11-18 來源:網(wǎng)絡(luò) 收藏
        下面的代碼是我使用STM32庫編寫的串口輸出和讀取的代碼。

        1、串口初始化函數(shù):void USART_Ini(USART_TypeDef* USARTx,u16 buad)

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

        2、串口中斷開啟和關(guān)閉:USART_IT(USART_TypeDef* USARTx,FunctionalState NewState)

        3、串口接收:u16 Getch(USART_TypeDef* USARTx)

        4、串口單個字符輸出:void Putch(USART_TypeDef* USARTx,u16 ch)

        5、串口輸出字符串:void PutStr(USART_TypeDef* USARTx,u16 *SendBuf,u16 Length)

        #include "stm32f10x_lib.h"

        u16 RecDateBuffer[100];
        u16 RecLen;
        u8 SendDateBuffer[100];

        /*******************************************************************************
        * Function Name : Uart_Ini
        * Description : 串口初始化
        * Input :
        * Output : None
        * Return :
        *******************************************************************************/
        void USART_Ini(USART_TypeDef* USARTx,u16 buad)
        {
        USART_InitTypeDef USART_InitStructure;
        USART_ClockInitTypeDef USART_ClockIni;
        GPIO_InitTypeDef GPIO_InitStructure;

        /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOD, &GPIO_InitStructure);

        /* Configure USART1 Rx (PA.10) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOD, &GPIO_InitStructure);

        USART_InitStructure.USART_BaudRate = 9600; //串口波特率
        USART_InitStructure.USART_WordLength = USART_WordLength_8b; //串口數(shù)據(jù)長度
        USART_InitStructure.USART_StopBits = USART_StopBits_1; //串口停止位
        USART_InitStructure.USART_Parity = USART_Parity_No; //串口奇偶效驗位
        USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; //串口模式,開始起發(fā)送和接收
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //串口硬件流

        USART_ClockIni.USART_Clock = USART_Clock_Disable;
        USART_ClockIni.USART_CPOL = USART_CPOL_Low;
        USART_ClockIni.USART_CPHA = USART_CPHA_2Edge;
        USART_ClockIni.USART_LastBit = USART_LastBit_Disable;

        USART_Init(USARTx,&USART_InitStructure);
        USART_ClockInit(USARTx,&USART_ClockIni);
        /* Enable USART1 */
        USART_Cmd(USARTx, ENABLE); //開啟串口X
        }
        /*******************************************************************************
        * Function Name : Getch
        * Description : 串口中斷開啟或關(guān)閉
        * Input : USARTx:x=串口號
        NewState: ENABLE開啟中斷,DISABLE關(guān)閉中斷
        * Output : None
        * Return :
        *******************************************************************************/
        void USART_IT(USART_TypeDef* USARTx,FunctionalState NewState)
        {
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

        if(NewState==ENABLE)
        {
        USART_ITConfig(USARTx,USART_IT_RXNE | USART_IT_TXE,ENABLE);
        }
        else
        {
        USART_ITConfig(USARTx,USART_IT_RXNE | USART_IT_TXE,DISABLE);
        }
        }
        /*******************************************************************************
        * Function Name : Getch
        * Description : 串口接收字符
        * Input : USARTx:x=串口號
        * Output : None
        * Return :
        *******************************************************************************/
        u16 Getch(USART_TypeDef* USARTx)
        {
        u16 ch;
        if (USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==SET)
        {
        ch=USART_ReceiveData(USARTx);
        //return(ch);
        }
        return(ch);
        }
        /*******************************************************************************
        * Function Name : GetStr
        * Description : 接收字符串
        * Input : USARTx:x=串口號
        buffer:接收字符串?dāng)?shù)組
        * Output : None
        * Return :
        *******************************************************************************/
        void GetStr(USART_TypeDef* USARTx)
        {
        //u16 i;
        while(USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==SET)
        {
        if(USART_GetFlagStatus(USARTx,USART_FLAG_ORE)==RESET)
        {
        if(RecLen<100)
        {
        RecDateBuffer[RecLen]=USART_ReceiveData(USARTx);
        RecLen++;
        }
        }
        }

        }
        /*******************************************************************************
        * Function Name : Putch
        * Description : 串口輸出一個字符
        * Input : USARTx:x=串口號
        ch:串口輸出的字符
        * Output : None
        * Return :
        *******************************************************************************/
        void Putch(USART_TypeDef* USARTx,u16 ch)
        {
        if(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==SET)
        {
        USART_SendData(USARTx,ch);
        }
        }
        /*******************************************************************************
        * Function Name : PutStr
        * Description : 串口輸出字符串
        * Input : USARTx:x=串口號
        SendBuf:串口輸出字符串
        Length:輸出長度
        * Output : None
        * Return :
        *******************************************************************************/
        void PutStr(USART_TypeDef* USARTx,u16 *SendBuf,u16 Length)
        {
        u16 i;

        for(i=0;i
        {
        Putch(USARTx,SendBuf[i]);
        }
        }



        關(guān)鍵詞: STM32無中斷串

        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 鹿邑县| 丰镇市| 五原县| 阳朔县| 呼伦贝尔市| 南川市| 锡林郭勒盟| 安塞县| 义乌市| 渝中区| 宣武区| 体育| 阳高县| 全南县| 霞浦县| 南京市| 和林格尔县| 山东省| 喀喇沁旗| 太谷县| 华宁县| 虎林市| 阿拉善右旗| 屯门区| 河源市| 襄汾县| 梁平县| 新泰市| 吉木萨尔县| 白河县| 绩溪县| 延边| 安溪县| 增城市| 苗栗市| 汽车| 花莲县| 普洱| 昌平区| 阿合奇县| 二连浩特市|