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

說明:本範例實際上是使用 UART 非 USART ,因為 STM32 的 PA9-PA10 預設是USART,但是我們練習用不需要使用到 USART的同步功能,大多應用上"異步"的UART已經很夠用了,而且UART用起來也比USART簡單。

本範例所使用的 MCU:STM32F103C8T6

經驗談:如果開發版上有 J-TAG 插座,請將電源(3.3V-GND) 插到 J-TAG 上,輸出結果比較不會出錯。(之前把電源直接送到開發版預設電源孔上,結果輸出結果全是亂碼...,使用 2 種開發版都是這樣子....)

如果 出現亂碼 (!@#$%):1.檢查外部石英震盪器是否為 8 MHz。如果不是,請將 stm32f10x.h 檔案中的 #define HSE_VALUE    ((uint32_t)8000000) 程式碼修改為 #define HSE_VALUE    ((uint32_t)16000000) 即可正常顯示(假設你的震盪器是16MHz 則修改 16000000 ,如果是 24MHz 則修改為 24000000,其他數字以此類推)。(注意:預設 stm32f10x.h 檔案是唯讀的,所以必須手動將檔案唯讀取消才可修改)

程式碼:

#include "stm32f10x.h"

int i;

void My_Usart1_Send(char *string){
    while(*string){
        /* 傳送訊息至 USART1 */
        USART_SendData(USART1, (unsigned short int) *string++);

        /* 等待訊息傳送完畢 */
        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    }
}

int main(void){
    /******** 宣告 USART、GPIO 結構體 ********/
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /******** 啟用 GPIOA、USART1 的 RCC 時鐘 ********/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);

    /******** 設定 PA9 為 Tx 覆用 ********/
    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); // 初始化 PA9

    /******** 設定 PA10 為 Rx 覆用 ********/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化 PA10


    /******** USART 基本參數設定 ********/

    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); // 初始化 UART1

    USART_Cmd(USART1, ENABLE); // 啟用 USART1


    while(1){
        for(i = 0; i<5000000; i++);
        My_Usart1_Send("Hello Kitty \n");
    }

}


輸出結果:

UARTST  


上面程式碼簡化版本:

#include "stm32f10x.h"

int i;

void My_Usart1_Send(char *string){
    while(*string){
        /* 傳送訊息至 USART1 */
        USART_SendData(USART1, (unsigned short int) *string++);

        /* 等待訊息傳送完畢 */
        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    }
}

int main(void){
    /******** 宣告 USART、GPIO 結構體 ********/
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /******** 啟用 GPIOA、USART1 的 RCC 時鐘 ********/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);

    /******** 設定 PA9 為 Tx 覆用 ********/
    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); // 初始化 PA9

    /******** 設定 PA10 為 Rx 覆用 ********/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化 PA10


    /******** USART 基本參數設定 ********/

    USART_StructInit(&USART_InitStructure); // 9600, 8, 1, No, No, Rx-Enable, Tx-Enable

    USART_Init(USART1, &USART_InitStructure); // 初始化 UART1

    USART_Cmd(USART1, ENABLE); // 啟用 USART1


    while(1){ 
        for(i = 0; i<5000000; i++); 
        My_Usart1_Send("Hello Kitty \n"); 
    }

}

 

 

arrow
arrow
    全站熱搜

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