Skip to main content

ADC problem

6 months ago

ADC problem

Posted byHamzaAffes0 points 5 replies
0 upvotes

im trying to read a voltage output from a voltage devider ( 3v to be precise), and i managed to understand some parts about the adc reading

using the sdk 6.0.14 i wrote this code based on the batt example in the peripheral examples

/ /在periph_setup.h

#define ADC_PORT GPIO_PORT_0
#define ADC_PIN GPIO_PIN_1

uint32_t adc_get_voltage_sample(void);

GPIO_ConfigurePin(ADC_PORT,ADC_PIN,INPUT,PID_ADC,false); // in set_pad_functions

/ /在periph_setup.c

uint32_t adc_get_voltage_sample(void)
{
uint32_t adc_sample;
adc_config_t cfg =
{
.mode = ADC_INPUT_MODE_SINGLE_ENDED,
.sign = true,
.attn = true
};
adc_init(&cfg);

adc_set_se_input(ADC_INPUT_SE_P0_1);
adc_sample = adc_get_sample();

adc_disable();
return adc_sample;

};

and i called this function in the main.c trying to get the value through teraterm

printf_byte_dec(UART, adc_get_voltage_sample());

the value i get is 24-25 ... i suppose if the code is correct this value is wrong ... thank you

accepted answer!

6 months ago

PM_Dialog

Hi HamzaAffes,

Thanks for your question online. Could you please indicate what is the value that the adc_get_voltage_sample() and adc_get_sample() return?

Thanks, PM_Dialog

6 months ago

HamzaAffes 0 points

thank you for your response .

the value i get is always 15 which is wrong . i tried to read from other channels but still same result . i'm trying to follow the batt exemple on how to read voltage since im still a beginner . if you could clarify the appropriate functions and files to get the correct result that would be helpful.

accepted answer!

6 months ago

PM_Dialog

Hi HamzaAffes,

Sure, the batt example of the SDK should be taken as reference. It seems that you are using the correct configuration. Could you please run it in debug mode and share the value of the ADC register? Additionally, you could add the adc_sample value in the watch window and check the value.

Thanks, PM_Dialog

6 months ago

HamzaAffes 0 points

thank you for your response,

could you pleaase help me by guiding me through this process since im still a beginner ?

accepted answer!

6 months ago

PM_Dialog

Hi HamzaAffes,

  1. Run it in debug mode and add a break point into adc_sample = adc_get_sample();
  2. What is the value of adc_sample?
  3. From the Watch Window à add expression and read the register : *(uint16_t *) GP_ADC_CTRL_REG. What is the value of the GP_ADC_CTRL_REG?

Thanks, PM_Dialog