折騰了兩天ADC多通道采樣,采樣的結果都很亂,完全不是預期值。在amobbs求助也沒有找到結果。於是決定從簡單開始,一步步折騰着破ADC。
【ADC試驗1實驗說明】
1、這個實驗僅僅是初始化一個ADC,對其輸入進行采樣。
2、使用STM32F4的ADC1進行采樣,采樣值不輸出之在編譯器里邊觀察。
3、使用ST外設庫進行實驗
4、本實驗只為采集到數據,采樣周期、采樣間隔設置為最大。
【ADC試驗1實驗結果】
成功采集到了ADC1,通道1引腳PA1上的輸入。數據穩定不跳變。
【ADC試驗1實驗步驟】
1、首先懷疑是工程中使用的USART、EXTI什么的影響了ADC的。重建工程,加入ST外設庫,添加引用位置。這一步就不說了。
2、開啟GPIOA、ADC時鍾。因為使用ADC1的通道1,對應的PA1引腳作為輸入。
ADC掛接在APB2時鍾上,GPIOA掛接在AHB1時鍾上。所以要開啟這兩個時鍾。
代碼如下:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
3、輸入引腳配置
輸入端口PA,引腳1.模擬輸入,引腳時鍾100M
//PA1 PA2 PA3,模擬輸入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource1 | GPIO_PinSource2 | GPIO_PinSource3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
4、ADC通用的初始化
這是F4系列新出來的東西,與F1不同。這里通過庫函數配置一個新增的寄存器ADC_CCR,這個配置將影響片上所有的ADC。
//ADC通用配置
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //不用DMA
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //獨立采樣
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8; //速度盡可能低
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
5、ADC1初始化
之后對單獨的ADC1進行配置,設定一些參數。最后的ADC_RegularChannelConfig完成通道選擇和掃描順序設置
主要參數:連續采樣/數據右對齊/不用外部觸發/采樣通道數1/12位精度
//ADC1配置
ADC_InitStructyre.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructyre.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructyre.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStructyre.ADC_NbrOfConversion = 1; //通道數
ADC_InitStructyre.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructyre.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1,&ADC_InitStructyre);
ADC_Cmd(ADC1,ENABLE);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_144Cycles);
6、手動開始ADC采樣,由於配置為連續掃描所有只需要開啟一次即可:
使用函數ADC_SoftwareStartConv(ADC1);完成
7、獲取ADC采樣數據:
可以通過讀ADC的DR或者使用庫函數ADC_GetConversionValue來完成。我使用的是庫函數。
adcvalue1 = ADC_GetConversionValue(ADC1);
adcvota = adcvalue1 *3300 / 0xfff;
8、最后加上串口功能,讓數據回顯
while(1)
{
adcvalue1 = ADC_GetConversionValue(ADC1);
adcvota = adcvalue1 *3300 / 0xfff;
for(i = 0;i<10000;i++)
{
sum += adcvota;
if(i ==9999)
{
avgvota = sum/10000;
sum = 0;
printf("avg vota is: %d\r\n",avgvota);
}
}
}
9、完整的代碼:
#include <stm32f4xx.h>
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_conf.h"#include "usart.h"
#include <stdio.h>
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructyre;int adcvalue1,adcvalue2,adcvalue3;
int adcvota ;int i,sum,avgvota;
int main()
{
USART_Config();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
//PA1 PA2 PA3,模擬輸入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource1 | GPIO_PinSource2 | GPIO_PinSource3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//ADC通用配置
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //不用DMA
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //獨立采樣
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8; //速度盡可能低
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
//ADC1配置
ADC_InitStructyre.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructyre.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructyre.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStructyre.ADC_NbrOfConversion = 1;
ADC_InitStructyre.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructyre.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1,&ADC_InitStructyre);
ADC_Cmd(ADC1,ENABLE);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_144Cycles);
ADC_SoftwareStartConv(ADC1);
sum = 0;
while(1)
{
adcvalue1 = ADC_GetConversionValue(ADC1);
adcvota = adcvalue1 *3300 / 0xfff;
for(i = 0;i<10000;i++)
{
sum += adcvota;
if(i ==9999)
{
avgvota = sum/10000;
sum = 0;
printf("avg vota is: %d\r\n",avgvota);
}
}
}
}
//重定義printf
int fputc(int ch,FILE *f)
{
//檢查發送寄存器為空之后再往里邊放數據
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
//USART發送一個字符
USART_SendData(USART1, (uint8_t) ch);
return ch;
}