cancel
Showing results for 
Search instead for 
Did you mean: 

STM8L ADC Single conversion not working (Solved)

Plum
Associate II

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;
}

1 ACCEPTED SOLUTION

Accepted Solutions
AA1
Senior III
  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.

View solution in original post

2 REPLIES 2
AA1
Senior III
  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.
Plum
Associate II

You are wonderful! Thank you for spotting my typo.