用STM32F407的一个ADC做4通道的采样,但是只有前2个通道可以用,后2个通道数据奇怪(固定一个值不能变)
程序如下:
#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;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
#define ADC1_DR_Address ((uint32_t)0X4001204C)
__IO uint16_t AD_Value[4] = {0,0,0,0}; // 0-3 store Voltage value, 4 store temperature value.
__IO uint32_t ADC1ConvertedVolt_0 = 0;
__IO uint32_t ADC1ConvertedVolt_1 = 0;
__IO uint32_t ADC1ConvertedVolt_2 = 0;
__IO uint32_t ADC1ConvertedVolt_3 = 0;
int i,n,sum;
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_DMA2, ENABLE);
}
void DMA_Config(void)
{
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)ADC1_DR_Address; //ADC1 DATA ADDRESS
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&AD_Value; //DMA memory base address
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//
DMA_InitStructure.DMA_BufferSize =4; //
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//
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);
}
void USART_Config(void)
{
USART_InitStructure.USART_BaudRate = 115200;
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);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
}
void Gpio_Config(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_14);
GPIO_ResetBits(GPIOA, GPIO_Pin_15);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource0 | GPIO_PinSource1 | GPIO_PinSource2| GPIO_PinSource3;//ADC123_IN0¶ÔÓ¦PA0£¬ADC123_IN1¶ÔÓ¦PA1£¬ADC123_IN2¶ÔÓ¦PA2£¬ADC123_IN3¶ÔÓ¦PA3
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//PB7->TX PB6->Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
}
void ADC_Config(void)
{
//ADC
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;//ʹÓÃDMA
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; //
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
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 = 4;
ADC_InitStructyre.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructyre.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1,&ADC_InitStructyre);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_15Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); //if not have this function , can't get new value
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //make enable ADC1 WITH DMA2
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
}
void filter(void)
{
int sum = 0;
for (n=0;n<4;n++)
{
for (i=0;i<10000;i++)
{
sum += AD_Value[n];
if(i ==9999)
{
AD_Value[n]= sum/10000;
sum = 0;
}
}
}
}
void Delay(int m)
{
int x,j;
for(j=0;j<m;j++)
for(x=0;x<=200;x++);
}
int main(void)
{
RCC_Config();
USART_Config();
Gpio_Config();
ADC_Config();
DMA_Config();
ADC_SoftwareStartConv(ADC1);//Ò»¶¨·ÅÖ÷º¯Êý
sum = 0;
while(1)
{
filter();
ADC1ConvertedVolt_0 = AD_Value[0] *3300/0xFFF;
ADC1ConvertedVolt_1 = AD_Value[1] *3300/0xFFF;
ADC1ConvertedVolt_2 = AD_Value[2] *3300/0xFFF;
ADC1ConvertedVolt_3 = AD_Value[3] *3300/0xFFF;// adcvota = adcvalue1 *2930 / 0x0fff;
printf("value[0]=%d\r\n",ADC1ConvertedVolt_0);
Delay(10000);
printf("value[1]=%d\r\n",ADC1ConvertedVolt_1);
Delay(10000);
printf("value[2]=%d\r\n",ADC1ConvertedVolt_2);
Delay(10000);
printf("value[3]=%d\r\n",ADC1ConvertedVolt_3);
Delay(10000);
}
}
//printf
int fputc(int ch,FILE *f)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
//USART
USART_SendData(USART1, (uint8_t) ch);
return ch;
}
望各位大侠帮忙!十分感激!
|
|