★ 可以直接複製貼上到 C 語言開發環境中,此程式碼可以直接編譯執行(只要你配置好開發環境與硬體)。
這次教學是 [基本原理版]
要 [方便使用版] 請看這篇文章:http://lolikitty.pixnet.net/blog/post/167837634
執行結果:(輸入 Hello 後面 會 顯示 OK 字串,輸入 Hello 之外 的 字串 將 不會 顯示)
程式碼:
#include "stm32f4xx.h"

程式碼:
#include "stm32f4xx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void My_Usart3_Printf(char* string){
while(*string){
/* 傳送訊息至 USART3 */
USART_SendData(USART3, (unsigned short int) *string++);
/* 等待訊息傳送完畢 */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
}
void Uart_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);
}
int main(){
Uart_Init();
while(1){
}
}
/* 判斷 2 個 字串陣列 (Char Array) 是否相同
* 相同回傳 1,不相同回傳 0
*/
int charArrayEquals(char a [], char b []){
int as = strlen(a);
int bs = strlen(b);
int i = 0;
if(as != bs){
return 0;
}
for(i = 0; i < as; i++){
if(a [i] != b [i]){
return 0;
}
}
return 1;
}
char buff [] = "";
/*
* USART3 中斷函數
*/
void USART3_IRQHandler(){
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
char c = USART_ReceiveData(USART3); // 接收的字元
char check [] = "Hello"; // 要判斷的字串陣列
if(c != '\r' && c != '\n'){
// 把接收到的字元 加入 到 buff 最後面,其實就是高階語言的 "A" += "B" 超原始寫法...
sprintf (buff, "%s%c", buff,c);
}else{
My_Usart3_Printf(buff); // 回傳只用者輸入的訊息
if(charArrayEquals(buff, check)){ // 判斷 buff 與 check 字串陣列是否相同
My_Usart3_Printf(" ... OK\n"); // 如果輸入為 Hello 則顯示 OK
}else{
My_Usart3_Printf("\n"); // 輸入不是 Hello 則直接換行
}
memset(buff, 0, strlen(buff)); // 將 buff 字串清空
}
}
}
全站熱搜