問題描述通過 uint16_t ConvData[8]保存DMA搬運的ADC轉換數值,但是這個數組數值的順序總是和ADC不是順序對應的。比如用7個通道的ADC,當設置ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward,是對 ...
- 問題描述
通過 uint16_t ConvData[8]保存DMA搬運的ADC轉換數值,但是這個數組數值的順序總是和ADC不是順序對應的。比如用7個通道的ADC,當設置ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward,是對應順序是:0->0,1->7,2->6…7->1 ; 當設置ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward,是對應順序是:0->7,1->0,2->1…7->6 。 - 問題原因
F0的ADC在使用之前需要校準。這個7位的校準值也是放在ADC_DR中的,它也會觸發DMA請求。
可以參照F0的ADC-DMA常式,先做ADC校準、然後再設置DMA,再使能ADC的DMA。 - 實例代碼
void ADC1_DMA_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = 0x00ff; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); ADC_DeInit(ADC1); //ADC恢復預設設置 ADC_StructInit(&ADC_InitStructure); //初始化ADC結構 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位精度 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //規定模式裝換工作在連續模式 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //數據對其為右對齊 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; // ADC_ScanDirection_Backward; //ADC的掃描方向 ADC_Init(ADC1, &ADC_InitStructure); ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_2, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_4, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_5, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_6, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_7, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_GetCalibrationFactor(ADC1); /* ADC Calibration */ ADC_Cmd(ADC1, ENABLE); /* Enable ADCperipheral[PerIdx] */ while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)) ; /* Wait the ADCEN falg */ //設置DMA要在校準ADC之後
DMA_DeInit(DMA1_Channel1); /* DMA1 Channel1 Config */ DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) 0x40012440; //ADC1->DR; //外設地址 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) RegularConvData_Tab; //記憶體地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外設作為數據傳輸的來源 DMA_InitStructure.DMA_BufferSize = 8; // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設地址寄存器不變 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //記憶體地址寄存器不變 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //數據寬度為16位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //數據寬度為16位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA_Priority設定DMA通道x的軟體優先順序 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x沒有設置為記憶體到記憶體傳輸 DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_Cmd(DMA1_Channel1, ENABLE);/* DMA1 Channel1 enable */ DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); ADC_StartOfConversion(ADC1); /* ADC1 regular Software Start Conv */ }