★ 可以直接複製貼上到 C 語言開發環境中,此程式碼可以直接編譯執行(只要你配置好開發環境與硬體)。
★ 需要 STM32 可編譯的 工程模板 (專案) 可到這裡下載:http://lolikitty.pixnet.net/blog/post/167849664

/*
* 本範例將ADC 轉換結果傳到 USART 的端口
* ACD 腳位:PA3
* UART TX 腳位:PC10
* UART RX 腳位:PC11
*/

#include "stm32f4xx.h"
#include "stdio.h"

unsigned int My_ADC_Value;

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

void My_ADC_Init(){

    ADC_InitTypeDef ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    /* 啟用 ADC3, DMA2, GPIO 的 RCC 時鐘 ****************************************/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);

    /* DMA2流0通道2配置 **************************************/
    DMA_InitStructure.DMA_Channel = DMA_Channel_2;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (unsigned int)0x4001224C;
    DMA_InitStructure.DMA_Memory0BaseAddr =(unsigned int) &My_ADC_Value;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = 1;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init(DMA2_Stream0, &DMA_InitStructure);
    DMA_Cmd(DMA2_Stream0, ENABLE);

    /* 配置 PA3 的 ADC 輸入 ******************************/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* ADC 常見的初始化 **********************************************************/
    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&ADC_CommonInitStructure);

    /* ADC3 初始化****************************************************************/
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 1;
    ADC_Init(ADC3, &ADC_InitStructure);

    /* ADC3定期通道3配置 *************************************/
    ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 1, ADC_SampleTime_3Cycles);

    /* 啟用DMA請求後,最後轉移(單ADC模式)*/
    ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);

    /* 啟用ADC3 的 DMA */
    ADC_DMACmd(ADC3, ENABLE);

    /* 啟用 ADC3 */
    ADC_Cmd(ADC3, ENABLE);

    /* 開始軟體轉換 */
    ADC_SoftwareStartConv(ADC3);

}


int main(void)
{
    int i;
    char buff [] = "";

    My_USART3_Init ();
    My_ADC_Init ();

    while (1)
    {
        sprintf (buff, "ADC1: %d \n", My_ADC_Value);
        My_Usart3_Printf(buff); // 傳送字串至 USART3
        for(i=0; i<30000000; i++); // 延遲
    }
}

 

arrow
arrow
    全站熱搜

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