★ 可以直接複製貼上到 C 語言開發環境中,此程式碼可以直接編譯執行(只要你配置好開發環境與硬體)。

這次教學是 [方便使用版]

要 [基本原理版] 請看這篇文章:http://lolikitty.pixnet.net/blog/post/167638569

[方便使用版] 有做一些改良,把輸入變成函式,直接讀取一整行來判斷,
像我下面程式碼這樣寫 : char * [] = My_Usart3_ReadLine()。
有點類似高階語言的寫法,這樣日後會比較好升級維護...(感動)

執行結果:

輸入 AAA  顯示 Love Hello Kitty
輸入 BBB  顯示 Love Loli
輸入 CCC  顯示 Love STM32
輸入其他字串則不顯示

PrtScr capture  

程式碼:

#include "stm32f4xx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
/*
 * 向 UART3 發送字串
 */
void My_Usart3_Printf(char *string){
    while(*string){
        /* 傳送訊息至 USART3 */
        USART_SendData(USART3, (unsigned short int) *string++);
 
        /* 等待訊息傳送完畢 */
        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
    }
}
 
/*
 * 初始化 UART3
 */
void My_Uart3_Init(){
    /******** 宣告 USART、GPIO 結構體 ********/
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
 
    /******** 啟用 GPIOC、USART3 的 RCC 時鐘 ********/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);  
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
 
    /******** 將 PC10、PC11 連接至 USART3 ********/
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
 
    /******** 設定 PC10 為 Tx 覆用  ********/
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 使用推挽式輸出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 使用上拉電阻
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 設置為覆用
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 設定第 10 腳
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 設定 GPIO 速度為 50 MHz
    GPIO_Init(GPIOC, &GPIO_InitStructure); // 套用以上 GPIO 設置,並初始化 GPIOC
 
    /******** 設定 PC11 為 Rx 覆用  ********/
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 設置為覆用
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // 設定第 11 腳
    GPIO_Init(GPIOC, &GPIO_InitStructure); // 套用以上 GPIO 設置,並初始化 GPIOC
 
    /******** USART 基本參數設定 ********/
    USART_InitStructure.USART_BaudRate = 9600; // 設定 USART 包率 (每秒位元數) 為 9600
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 設定 USART 傳輸的資料位元為 8
    USART_InitStructure.USART_StopBits = USART_StopBits_1; // 設定 USART 停止位元為 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 模式為 Rx (接收) 、 Tx (傳送)
    USART_Init(USART3, &USART_InitStructure); // 套用以上 USART 設置,並初始化UART3
 
    /******** 啟用 USART3 ********/
    USART_Cmd(USART3, ENABLE);
  
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
    /* 啟用 USART3 中斷 */
    NVIC_EnableIRQ(USART3_IRQn);
}
 
/*
 * 讀取一整行 USART3 傳來的訊息
 */
char buff [100] = "";
 
char * My_Usart3_ReadLine(){    
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){         
        char c = USART_ReceiveData(USART3);         
        if(c != '\r' && c != '\n'){
            sprintf (buff, "%s%c", buff, c);
        }else{
            char buff2 [100] = "";
            memcpy(buff2,buff, strlen(buff)); // 將 buff 複製到 buff2
            memset(buff, 0, strlen(buff)); // 清除 buff 字串裡所有內容            
            return buff2;
        }
    }
    return "";
}
 
/*
 * USART3 中斷函數
 * 
 * 函數名稱為官方設定,只要偵測到 UART3 有資料輸入,
 * 這個函示就會被自動執行。(收到一個字元就會被執行一次)
 */
void USART3_IRQHandler(){
 
    // 接收 USART3 字串
    char * data = My_Usart3_ReadLine();
    
    // 如果 data 等於 "AAA" 則回傳 0,所以要加上 驚嘆號 ! 反轉
    if(!strcmp(data, "AAA")){       
       My_Usart3_Printf("Love Hello Kitty \n");
    } 
    if(!strcmp(data, "BBB")){       
       My_Usart3_Printf("Love Loli \n");
    }   
    if(!strcmp(data, "CCC")){       
       My_Usart3_Printf("Love STM32 \n");
    }    
}
 
/*
 * 主函數
 */
int main(){    
    My_Uart3_Init();    
    while(1){
    }
}
 
 
 
 
 
 
 
 
arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(3) 人氣()