问题如下:ADC采集多通道信号时,请求DMA,发现自己定义的ADC_ConvertedValue[4]里面没有值,代码如下:
adc.h
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
extern __IO float ADC_ConvertedValueLocal[3];
extern uint32_t ADC_ConvertedValue[3];
extern uint32_t ADC_ConvertedValueRead[6];
extern uint32_t DMA_Transfer_Complete_Count;
extern ADC_HandleTypeDef hadcx;
extern DMA_HandleTypeDef hdma_adcx;
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx);
void MY_ADC_Init(void); //ADCí¨μà3õê¼»ˉ
u16 Get_Adc(u32 ch);
#endif
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "lcd.h"
#include "sdram.h"
#include "usmart.h"
#include "adc.h"
#define ADC_CONVERTED_DATA_BUFFER_SIZE
/************************************************
************************************************/
__IO float ADC_ConvertedValueLocal[3];//×îÖÕ×a»»μÄμçÑ1Öμ
uint32_t ADC_ConvertedValue[3];//μçÑ1μÄÔ-ê¼Öμ
uint32_t ADC_ConvertedValueRead[6];//ADC_DMA′æ′¢Çø
uint32_t DMA_Transfer_Complete_Count=0;//2aêÔADC_DMAÖD¶Ïíê3é′Îêy
int main(void)
{
Cache_Enable(); //′ò¿aL1-Cache
HAL_Init(); //3õê¼»ˉHAL¿a
Stm32_Clock_Init(160,5,2,4); //éèÖÃê±Öó,400Mhz
delay_init(400); //Ñóê±3õê¼»ˉ
uart_init(115200); //′®¿ú3õê¼»ˉ
usmart_dev.init(200); //3õê¼»ˉUSMART
LED_Init(); //3õê¼»ˉLED
KEY_Init(); //3õê¼»ˉ°′¼ü
SDRAM_Init(); //3õê¼»ˉSDRAM
LCD_Init(); //3õê¼»ˉLCD
MY_ADC_Init();
MYDMA_Config(DMA2_Stream0);
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Apollo STM32F4/F7");
LCD_ShowString(30,70,200,16,16,"ADC TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2016/7/12");
POINT_COLOR=BLUE;//éèÖÃ×ÖìåÎaà¶é«
LCD_ShowString(30,130,200,16,16,"ADC1_CH15_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH15_VOL:0.000V"); //ÏèÔú1춨λÖÃÏÔê¾D¡êyμã
while(1)
{
delay_ms(500);
LCD_ShowxNum(142,130,ADC_ConvertedValue[1],5,16,0); //检测ADC_ConvertedValue[1]有数据
ADC_ConvertedValueLocal[0] =(ADC_ConvertedValue[0]&0xFFF)*3.3/65536;
ADC_ConvertedValueLocal[1] =(ADC_ConvertedValue[1]&0xFFF)*3.3/65536;
ADC_ConvertedValueLocal[2] =(ADC_ConvertedValue[2]&0xFFF)*3.3/65536;
printf("CH15_PA3 value = %d -> %fV
",ADC_ConvertedValue[0]&0xFFF,ADC_ConvertedValueLocal[0]);
printf("CH18_PA4 value = %d -> %fV
",ADC_ConvertedValue[1]&0xFFF,ADC_ConvertedValueLocal[1]);
printf("CH19_PA5 value = %d -> %fV
",ADC_ConvertedValue[2]&0xFFF,ADC_ConvertedValueLocal[2]);
printf("òѾ-íê3éAD×a»»′Îêy£o%d
",DMA_Transfer_Complete_Count);
DMA_Transfer_Complete_Count=0;
printf("
");
}
}
adc.c
#include "adc.h"
#include "delay.h"
ADC_HandleTypeDef hadcx;
ADC_ChannelConfTypeDef hadcx_ChanConf;
DMA_HandleTypeDef hdma_adcx;
void DMA_Config(void);
void MY_ADC_Init(void)
{
hadcx.Instance = ADC1;
hadcx.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadcx.Init.Resolution = ADC_RESOLUTION_12B;
hadcx.Init.ScanConvMode = ENABLE;
hadcx.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadcx.Init.LowPowerAutoWait=DISABLE;
hadcx.Init.ContinuousConvMode = ENABLE;
hadcx.Init.NbrOfConversion = 3;
hadcx.Init.DiscontinuousConvMode = DISABLE;
hadcx.Init.NbrOfDiscConversion=0;
hadcx.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadcx.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadcx.Init.BoostMode=ENABLE;
hadcx.Init.Overrun=ADC_OVR_DATA_OVERWRITTEN;
hadcx.Init.OversamplingMode=DISABLE;
hadcx.Init.ConversionDataManagement=ADC_CONVERSIONDATA_DR;
HAL_ADCEx_Calibration_Start(&hadcx ,ADC_CALIB_OFFSET,ADC_SINGLE_ENDED);
hadcx_ChanConf.Channel = ADC_CHANNEL_15;
hadcx_ChanConf.Rank = ADC_REGULAR_RANK_1;
hadcx_ChanConf.SamplingTime = ADC_SAMPLETIME_64CYCLES_5;
hadcx_ChanConf.SingleDiff = ADC_SINGLE_ENDED;
hadcx_ChanConf.OffsetNumber = ADC_OFFSET_NONE;
hadcx_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&hadcx,&hadcx_ChanConf);
hadcx_ChanConf.Channel = ADC_CHANNEL_18;
hadcx_ChanConf.Rank = ADC_REGULAR_RANK_2;
hadcx_ChanConf.SamplingTime = ADC_SAMPLETIME_64CYCLES_5;
hadcx_ChanConf.SingleDiff = ADC_SINGLE_ENDED;
hadcx_ChanConf.OffsetNumber = ADC_OFFSET_NONE;
hadcx_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&hadcx,&hadcx_ChanConf);
hadcx_ChanConf.Channel = ADC_CHANNEL_19;
hadcx_ChanConf.Rank = ADC_REGULAR_RANK_3;
hadcx_ChanConf.SamplingTime = ADC_SAMPLETIME_64CYCLES_5;
hadcx_ChanConf.SingleDiff = ADC_SINGLE_ENDED;
hadcx_ChanConf.OffsetNumber = ADC_OFFSET_NONE;
hadcx_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&hadcx,&hadcx_ChanConf);
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_ADC12_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
GPIO_Initure.Pin=GPIO_PIN_3;
GPIO_Initure.Mode=GPIO_MODE_ANALOG;
GPIO_Initure.Pull=GPIO_NOPULL;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_4;
GPIO_Initure.Mode=GPIO_MODE_ANALOG;
GPIO_Initure.Pull=GPIO_NOPULL;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_5; //PA5
GPIO_Initure.Mode=GPIO_MODE_ANALOG;
GPIO_Initure.Pull=GPIO_NOPULL;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
}
u16 Get_Adc(u32 ch)
{
HAL_ADC_Start_DMA(&hadcx, (uint32_t *)&ADC_ConvertedValue[0],3);
HAL_ADC_ConvCpltCallback(&hadcx);
HAL_ADC_Stop_DMA(&hadcx);
}
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx)
{
if((u32)DMA_Streamx>(u32)DMA2)
{
__HAL_RCC_DMA2_CLK_ENABLE();
}else
{
__HAL_RCC_DMA1_CLK_ENABLE();
}
hdma_adcx.Instance=DMA_Streamx;
hdma_adcx.Init.Request = DMA_REQUEST_ADC1;
hdma_adcx.Init.Direction=DMA_PERIPH_TO_MEMORY;
hdma_adcx.Init.PeriphInc=DMA_PINC_DISABLE;
hdma_adcx.Init.MemInc=DMA_MINC_ENABLE;
hdma_adcx.Init.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD;
hdma_adcx.Init.MemDataAlignment=DMA_MDATAALIGN_HALFWORD;
hdma_adcx.Init.Mode=DMA_CIRCULAR;
hdma_adcx.Init.Priority=DMA_PRIORITY_HIGH;
hdma_adcx.Init.FIFOMode=DMA_FIFOMODE_DISABLE;
hdma_adcx.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
hdma_adcx.Init.MemBurst=DMA_MBURST_SINGLE;
hdma_adcx.Init.PeriphBurst=DMA_PBURST_SINGLE;
HAL_DMA_DeInit(&hdma_adcx);
HAL_DMA_Init(&hdma_adcx);
__HAL_LINKDMA(&hadcx,DMA_Handle,hdma_adcx);
}
遇到的问题就是ADC_ConvertedValue[]里没有数据,其中语句是HAL_ADC_Start_DMA(&hadcx, (uint32_t *)&ADC_ConvertedValue[0],3);
如果ADC还是可以读到数据得的话,那就说明你的adc程序里肯定用了HAL_ADC_GetValue(),然后就是ADC_DMA_Start()这条语句没有执行
一周热门 更多>