Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

ADC s DMA

Příklad inicializace ADC převodníku s ukládáním dat do DMA. Převodník běží v nekonečné smyčce a vždy provede počet vzorků podle #define ADC_AVG_SIZE 16 . Po navzorkování 16 vzorků se data ukládají cirkulárně opět od začátku pole ADCvalueDMA.

#define ADC_AVG_SIZE	16
uint16_t ADCvalueDMA[ADC_AVG_SIZE];
 
 
void ADCInitDMA(void){
	// --- ADC Part --- 
	RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
	ADC1->SQR3 |= 0 << ADC_SQR3_SQ1_Pos;
	ADC1->SMPR2 |= 7 << ADC_SMPR2_SMP0_Pos; 	//480 cycles
	ADC1->CR2 |= ADC_CR2_ADON | ADC_CR2_CONT;
 
	RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
	GPIOA->MODER |= GPIO_MODE_ANALOG << GPIO_MODER_MODE0_Pos;
 
	// --- DMA part ---
	RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;	        // enable clock
	// CH0 selection, Memory and peripheral data size is 16 bits, P->M direction, circular mode
	DMA2_Stream0->CR |= 0x00 << DMA_SxCR_CHSEL_Pos; // CH0 select
        // Peripheral and Memory have size 16bits
	DMA2_Stream0->CR |= (0x01 << DMA_SxCR_MSIZE_Pos) | (0x01 << DMA_SxCR_PSIZE_Pos);	
	DMA2_Stream0->CR |= 0x00 << DMA_SxCR_DIR_Pos;	// Peripheral to Memory direction
	DMA2_Stream0->CR |= DMA_SxCR_CIRC;		// Circular
	DMA2_Stream0->CR |= DMA_SxCR_MINC;		// Index increment for Memory destination
	DMA2_Stream0->NDTR = ADC_AVG_SIZE;              // Number of data
	DMA2_Stream0->M0AR = (uint32_t*) ADCvalueDMA;   // Target variable (array)
	DMA2_Stream0->PAR = &ADC1->DR;
	DMA2_Stream0->CR |= DMA_SxCR_EN;
 
	ADC1->CR2 |= ADC_CR2_DMA | ADC_CR2_DDS;
	ADC1->CR2 |= ADC_CR2_SWSTART;
}
 
int main(void){
 
  ADCInitDMA();
 
  while (1){
    for(int i = 0; i < ADC_AVG_SIZE; i++){
      // process ADC values from ADCvalueDMA[i]
    }
 
  }
}

courses/b2m37mam/priklady/stm32/04b-adc_dma.txt · Last modified: 2023/09/07 15:17 by nentvond