STM8L ADC Single conversion not working (Solved)
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-12-13 7: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.
Labels:
- Labels:
-
ADC
This discussion is locked. Please start a new topic to ask your question.
1 ACCEPTED SOLUTION
Accepted Solutions
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-12-14 9:03 AM
- You should wait some time after power-on ADC.
- With 0x80 you select channel 7 not channel 4. For channel 4 value is 0x10.
- Increase sampling time. 48 ADC clocks seems low.
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-12-14 9:03 AM
- You should wait some time after power-on ADC.
- With 0x80 you select channel 7 not channel 4. For channel 4 value is 0x10.
- Increase sampling time. 48 ADC clocks seems low.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-12-14 9:12 AM
You are wonderful! Thank you for spotting my typo.
