本帖最后由 花开不语 于 2019-5-7 19:45 编辑
用ADC采集三个通道的电压,采用DMA的方式,问题是就是ADC采集不到我自己定义的一个全局数组名ADC_ConvertedValue上的数据,代码如下:
adc.c
#include "adc.h"
#include "delay.h"
ADC_HandleTypeDef hadcx;
DMA_HandleTypeDef hdma_adcx;
void DMA_Config(void);
void MY_ADC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef ADCClkInitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_ADC12_CLK_ENABLE();
ADCClkInitStruct.PeriphClockSelection=RCC_PERIPHCLK_ADC;
ADCClkInitStruct.AdcClockSelection=RCC_ADCCLKSOURCE_CLKP;
HAL_RCCEx_PeriphCLKConfig(&ADCClkInitStruct);
//òy½Å3õê¼»ˉ
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull=GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// ADC1|ÄüÅäÖÃ
hadcx.Instance=ADC1;
hadcx.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4;
hadcx.Init.Resolution=ADC_RESOLUTION_16B;
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_ADC_Init(&hadcx);
HAL_ADCEx_Calibration_Start(&hadcx,ADC_CALIB_OFFSET,ADC_SINGLE_ENDED);
}
u16 Get_Adc(u32 ch)
{
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ch;
sConfig.Rank = 1;
sConfig.SamplingTime=ADC_SAMPLETIME_810CYCLES_5;
sConfig.SingleDiff=ADC_SINGLE_ENDED;
sConfig.OffsetNumber=ADC_OFFSET_NONE;
sConfig.Offset=0;
HAL_ADC_ConfigChannel(&hadcx, &sConfig);
sConfig.Channel = ch;
sConfig.Rank = 2;
sConfig.SamplingTime=ADC_SAMPLETIME_810CYCLES_5;
sConfig.SingleDiff=ADC_SINGLE_ENDED;
sConfig.OffsetNumber=ADC_OFFSET_NONE;
sConfig.Offset=0;
HAL_ADC_ConfigChannel(&hadcx, &sConfig);
sConfig.Channel = ch;
sConfig.Rank = 3;
sConfig.SamplingTime=ADC_SAMPLETIME_810CYCLES_5;
sConfig.SingleDiff=ADC_SINGLE_ENDED;
sConfig.OffsetNumber=ADC_OFFSET_NONE;
sConfig.Offset=0;
HAL_ADC_ConfigChannel(&hadcx, &sConfig);
HAL_ADC_Start(&hadcx);
HAL_ADC_Start_DMA(&hadcx, (uint32_t *)&ADC_ConvertedValueRead[0],3);
HAL_ADC_ConvCpltCallback(&hadcx);
HAL_ADC_Stop_DMA(&hadcx);
return (u16)HAL_ADC_GetValue(&hadcx);
}
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx)
{
if((u32)DMA_Streamx>(u32)DMA2)//μÃμ½μ±Ç°streamêÇêôóúDMA2»1êÇDMA1
{
__HAL_RCC_DMA2_CLK_ENABLE();//DMA2ê±Öóê1Äü
}else
{
__HAL_RCC_DMA1_CLK_ENABLE();//DMA1ê±Öóê1Äü
}
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.h
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
extern ADC_HandleTypeDef hadcx;
extern DMA_HandleTypeDef hdma_adcx;
extern __IO float ADC_ConvertedValueLocal[4];
extern uint32_t ADC_ConvertedValue[4];
extern uint32_t ADC_ConvertedValueRead[6];
extern uint32_t DMA_Transfer_Complete_Count;
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx);
void MY_ADC_Init(void); //ADCí¨μà3õê¼»ˉ
u16 Get_Adc(u32 ch);
#endif
main.c
__IO float ADC_ConvertedValueLocal[4];
uint32_t ADC_ConvertedValue[4];
uint32_t ADC_ConvertedValueRead[6];
uint32_t DMA_Transfer_Complete_Count=0;
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(); //3õê¼»ˉADC1í¨μà5
POINT_COLOR=RED;
MYDMA_Config(DMA2_Stream0);
LCD_ShowString(30,50,200,16,16,"Apollo STM32FH7");
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,"2017/8/12");
POINT_COLOR=BLUE;//éèÖÃ×ÖìåÎaà¶é«
LCD_ShowString(30,130,200,16,16,"ADC1_CH19_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH19_VOL:0.000V");
LCD_ShowString(30,170,200,16,16,"ADC1_CH18_VAL:");
LCD_ShowString(30,190,200,16,16,"ADC1_CH18_VOL:0.000V"); //ÏèÔú1춨λÖÃÏÔê¾D¡êyμã
LCD_ShowString(30,210,200,16,16,"ADC1_CH15_VAL:");
LCD_ShowString(30,230,200,16,16,"ADC1_CH15_VOL:0.000V");
while(1)
{
ADC_ConvertedValue[0]=Get_Adc(ADC_CHANNEL_19);
LCD_ShowxNum(142,130,ADC_ConvertedValue[0],5,16,0);
ADC_ConvertedValue[1]=Get_Adc(ADC_CHANNEL_18);/
LCD_ShowxNum(142,170,ADC_ConvertedValue[1],5,16,0);
ADC_ConvertedValue[2]=Get_Adc(ADC_CHANNEL_15);
LCD_ShowxNum(142,210,ADC_ConvertedValue[2],5,16,0);
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("
");
LED0_Toggle;
delay_ms(250);
}
}
主要的问题就是ADC_ConvertedValue里面没有数据,导致在串口采集不到通道电压,看了很多牛人写的,其中有一个说是是目标缓存地址没有对齐,加一句 ALIGN_32BYTES (volatile uint16_t ADC_Cov[ADC_CONVERTED_DATA_BUFFER_SIZE]);即可,我试了一下海曙不行,链接
http://www.openedv.com/forum.php ... 92533&highlight=ADC,搞了十几天了,真的找不出问题,希望大神们帮帮我吧!!
一周热门 更多>