★ 可以直接複製貼上到 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++); // 延遲
}
}

Hi. Whatever I set the pin 3v or 0V. My_ADC_Value still print "0" continously. I just change PA3 to PB0. PB0 suppot ADC on my STM32F427board.
This code uses STM32F407 board, STM32F427 I have not tried. But you have to modify the initialization AHB1, APB2, GPIO, ADC Or view the STM32F427 datasheet : http://www.st.com/web/en/resource/technical/document/datasheet/DM00071990.pdf
您好,我是個新手,希望老師不要介意我的爛問題...我想請問老師,為什麼我使用了這個程式碼,但是結果顯示都是4095?拜託老師能夠教我。
顯示4095原因很多,我這程式碼我測是 ok,會不會是你的板子接到正電源了? 因為 4095 是 DA 的最大值
老師,您好,請問您該如何改變他的Sample rate 以及 傳輸出來的速率?我上網找了很久可是都看不大懂,麻煩老師願意教我,謝謝您。
你是說交叉讀取嗎 ? 用兩個 ADC 交叉讀取,把讀取性能提升兩倍嗎 ? 可以參考這裡 http://bbs.armfly.com/read.php?tid=7652
老師,非常的謝謝您,我馬上來試試看
請問將STM32F4 更改使用STM32F1時, 會出現一堆名稱錯誤,那些錯誤該如何修改? 謝謝~
您好,在我的板子上顯示也是4095。 硬體上這條線是接到按鍵上,所以當按鍵按下後,理論上應該要變成0。但實際上得到的數值還是4095。即使換成一般的ADC去讀取,也是得到相同的結果。 後來把硬體的這條線換成接到外部的電源供應器上。但不論電源供應器的輸出電源是多少,ADC讀出來的數據也是4095。 不過我仍然認為這些程式碼是沒有問題的,只是硬體上有什麼問題,還是說不同的IC有需要更多或不同的設定,目前還不知道...
Hi, I'm currently studying how to program stm32f407. This website is very useful, and the codes are working quite well, but I don't fully understand what each line means, may you help me understand them? 1. From other examples, the code usually includes header files as "stm32f4xx_RCC.h", "stm32f4xx_GPIO.h", etc. But over here, you code only includes "stmio.h", I wonder what's the difference and why does both of them works. 2. For the code "sprintf (buff, "ADC1: %d \n", My_ADC_Value);" I wonder what does "%d" and "\n" do.