Skip to main content
Visitor II
December 14, 2022
Solved

STM8L ADC Single conversion not working (Solved)

  • December 14, 2022
  • 2 replies
  • 1463 views

I can't seem to figure out what I've done wrong. Trying to do a single ADC conversion on the STM8L051F3 using pin PC4 but I'm just getting 0xFFFFD. Here's my ADC related code can anyone spot where I've gone wrong?

void ADC_Init() {
 CLK_PCKENR2 |= 1; //Enable ADC CLK
 ADC1_CR1 |= 1; // Enable ADC
 ADC1_CR2 = 0x04; //Sampling time = 48 ADC clock cycles
 ADC1_SQR1 |= 0x80; // DMA off
 ADC1_SQR4 = 0x80; // Configure ADC channel 4 (PC4)
}
 
uint16_t ADC_Read(void) {
 uint16_t adc_res;
 uint16_t value = 0;
 uint8_t cntr;
 
 for (cntr = 0; cntr < 4; cntr++) {
 ADC1_CR1 |= 2; // Start ADC conversion, by software trigger
 while (!(ADC1_SR & 1)); // Wait for the conversion ends
 adc_res = (ADC1_DRH << 8); // Get ADC converted data
 adc_res |= ADC1_DRL;
 value += adc_res;
 if (cntr) value >>= 1;
 }
 
 return value;
}

    This topic has been closed for replies.
    Best answer by AA1
    1. You should wait some time after power-on ADC.
    2. With 0x80 you select channel 7 not channel 4. For channel 4 value is 0x10.
    3. Increase sampling time. 48 ADC clocks seems low.

    2 replies

    AA1Answer
    Visitor II
    December 14, 2022
    1. You should wait some time after power-on ADC.
    2. With 0x80 you select channel 7 not channel 4. For channel 4 value is 0x10.
    3. Increase sampling time. 48 ADC clocks seems low.
    PlumAuthor
    Visitor II
    December 14, 2022

    You are wonderful! Thank you for spotting my typo.