2022-12-13 07:27 PM
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;
}
Solved! Go to Solution.
2022-12-14 09:03 AM
2022-12-14 09:03 AM
2022-12-14 09:12 AM
You are wonderful! Thank you for spotting my typo.