Author Topic: Ch32v003 ADC not working!  (Read 1244 times)

0 Members and 1 Guest are viewing this topic.

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 225
  • Country: ca
Ch32v003 ADC not working!
« on: August 28, 2024, 02:51:13 pm »
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
Code: [Select]
#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;
}
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1650
  • Country: gb
Re: Ch32v003 ADC not working!
« Reply #1 on: August 28, 2024, 08:30:34 pm »
Code: [Select]
void adc_init(void)
{
    RCC->APB2PRSTR |= RCC_APB2Periph_ADC1;

Are you sure you didn't mean to use APB2PCENR here? Nowhere are you actually enabling the ADC peripheral clock.
 
The following users thanked this post: kgavionics

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 16368
  • Country: fr
Re: Ch32v003 ADC not working!
« Reply #2 on: August 28, 2024, 08:36:41 pm »
Are you sure the clock for the ADC is enabled?
 
The following users thanked this post: kgavionics

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 225
  • Country: ca
Re: Ch32v003 ADC not working!
« Reply #3 on: August 28, 2024, 10:39:58 pm »
Code: [Select]
void adc_init(void)
{
    RCC->APB2PRSTR |= RCC_APB2Periph_ADC1;

Are you sure you didn't mean to use APB2PCENR here? Nowhere are you actually enabling the ADC peripheral clock.
Thank you very much guys
Sorry! I copied the example without even double-checking!
So, I corrected that line, it works, but there is a lot of fluctuation, which is another problem, that I'm debugging right now!
Code: [Select]
RCC->APB2PCENR |=RCC_ADC1EN;
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf