Hello
I'm using the GitHub project CH32V003fun, I used this repository to modify many examples, and they worked fine, except for the ADC which I'm having trouble to make it work.
I have the following code to measure a voltage on pin PA1, but when I call the adc_get() function, the LED (on PC0) stops flashing and the function blocks the execution of the program.
I analyzed my code a lot of times, but I don't see what's the problem! Furthermore, I posted this issue on Ch32v003fun GitHub, but I didn't get an answer yet! Can someone help me please?
TIA
#include "ch32v003fun.h"
void adc_init(void);
uint16_t adc_get(void);
volatile uint32_t count;
int main ()
{
SystemInit();// system clock config
adc_init();
RCC->APB2PCENR |=RCC_IOPCEN ;// enable GPIOC FOR LED
GPIOC->CFGLR =0x44444441;//PC0 as output 10Mhz, the rest are kept on the reset value which is 0x4444444
while(1)
{
GPIOC->BSHR |=(1<<0);
Delay_Ms(1000);
GPIOC->BSHR |=(1<<16);
Delay_Ms(1000);
printf( "Count: %d adc: %d\n\r", count++, adc_get() );
}
}
void adc_init(void)
{
RCC->APB2PRSTR |= RCC_APB2Periph_ADC1;
RCC->CFGR0 |=(0b01000<<11);//FOR 3.2V MAX ADC FREQ IS 12MZ ADCPRES[11:15] 010XX
RCC->APB2PCENR |=RCC_IOPAEN;// enable GPIOA PA1
GPIOA->CFGLR =0X44444404;//PA1 AS ANALOG INPUT
ADC1->CTLR2 &=~(ADC_ADON);
// Set up single conversion on chl 0
ADC1->RSQR1 = 0;
ADC1->RSQR3 = 0; // 0-9 for 8 ext inputs and two internals
ADC1->SAMPTR2 |=6; // 241 CYCLES
ADC1->CTLR2 |= ADC_ADON | ADC_EXTSEL;// turn on ADC and set rule group to sw trig
//-------------------------
// Reset calibration
}
uint16_t adc_get( void )
{
// start sw conversion (auto clears)
ADC1->CTLR2 |= ADC_SWSTART;
// wait for conversion complete
while(!(ADC1->STATR & ADC_EOC));
// get result
return ADC1->RDATAR;
}