Trigger ADC1 conversion using TIM3 using registers
Hello,
I am trying to have analog voltage converted every 2 seconds using the TRGO signal from TIM3. Without much success. For now, I managed to generate interrupt of TIM3 and in TIM3 Handler to use software trigger for ADC. Here is the code:
TIM3:
void adtim1(void){
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // Enable the peripheral clock of GPIO Port A
//TIM2->CR1 &= ~TIM_CR1_CEN;
// Enable timer 2 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
// Counting direction: 0 = up-counting, 1 = down-counting
//TIM2->CR1 &= ~TIM_CR1_DIR;
TIM3->CR2 &= ~TIM_CR2_MMS;
TIM3->CR2 |= TIM_CR2_MMS_2;
// Clock prescaler (16 bits, up to 65,535)
TIM3->PSC = 7999;
//Auto-reload:
TIM3->ARR = 1000;
TIM3->CCR2 = 50;
TIM3->CCMR1 &= ~TIM_CCMR1_OC2M;
TIM3->CCMR1 |= (0b110 << TIM_CCMR1_OC2M_Pos);
//TIM3->CCMR1 |= 0b01 << TIM_CCMR1_CC2S_Pos;
//TIM3->CCMR2 &= ~TIM_CCMR2_OC4PE;
// Select output polarity: 0 = active high, 1 = active Low
TIM3->CCER &= ~TIM_CCER_CC2P;
TIM3->CCER |= TIM_CCER_CC2P;
TIM3->CCER |= TIM_CCER_CC2E;
//TIM3->SMCR |= 0b110 << TIM_SMCR_SMS_Pos;
//TIM3->SMCR |= 0b110 << TIM_SMCR_TS_Pos;
// Enable timer 2
//TIM2->CR1 |= (0b01 << TIM_CR1_CMS_Pos);
TIM3->CR1 &= ~TIM_CR1_ARPE;
TIM3->CR1 |= TIM_CR1_CEN;
TIM3->DIER |=TIM_DIER_UIE; //Update Interrupt Enable
TIM3 -> SR &= ~TIM_SR_UIF; //Update Interrupt Flag
TIM3->DIER |=TIM_DIER_TIE; //Trigger Interrupt Enable
TIM3 -> SR &= ~TIM_SR_TIF; //Trigger Interrupt Flag
NVIC_EnableIRQ(TIM3_IRQn); //Interrupt Set-Enable Register
__enable_irq(); //Enable Interrupt
}ADC:
void ADC_General_Init(ADC_TypeDef * ADCx){
ADCx->CR2 |= ADC_CR2_ADON;
delay(20);
ADCx->CR2 |= ADC_CR2_CAL;
while ((ADCx->CR2 & ADC_CR2_CAL)==ADC_CR2_CAL);
ADCx->CR2 &= ~ADC_CR2_ADON;
ADCx->CR2 &= ~ADC_CR2_CONT; // CONT = 0: Single conversion mode
ADCx->CR2 |= ADC_CR2_TSVREFE;
ADCx->SQR1 &= ~ADC_SQR1_L; // L[3:0]=0000: 1 conversion, ADC_SQR1
ADCx->SQR3 &= ~ADC_SQR3_SQ1;
ADCx->SQR3 |= (1U << ADC_SQR3_SQ1_Pos);
ADCx->CR2 &= ~ADC_CR2_EXTSEL;
ADCx->CR2 |= (0b111U << ADC_CR2_EXTSEL_Pos); // EXTSEL[2:0] = 111: SWSTART
// ADC ON
ADCx->CR2 |= ADC_CR2_ADON;
}
void ADC_GPIO_Init(void) {
GPIOA->CRL &= ~(GPIO_CRL_MODE1 | GPIO_CRL_CNF1);
}
void ADC_Init(void) {
// Enable the clock of ADC 1
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC 1 clock
ADC_GPIO_Init();
ADC_General_Init(ADC1);
ADC1->CR1 |= ADC_CR1_EOCIE; // Enable interrupt
NVIC_SetPriority(ADC1_IRQn, 1); // Set Priority to 1
//NVIC->ISER[0] |= NVIC_ISER_SETENA_18;
//NVIC_EnableIRQ(ADC1_IRQn); // Enable interrupt of ADC 1 & 2
NVIC_EnableIRQ(ADC1_2_IRQn); // Same thing
}Sorry if comments make code hard to read.
Can somebody help me configure settings in order to trigger conversion using TIM3?
Thank you!
I am working in Eclipse and Proteus, on STM32F103R6