Learn MoreFAQsTutorials

6 posts / 0 new
Last post
HamzaAffes
Offline
Last seen:12 hours 29 min ago
Joined:2020-10-03 13:11
ADC problem

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

//in 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

Device:
PM_Dialog
Offline
Last seen:1 hour 5 min ago
Staff
Joined:2018-02-08 11:03
Hi HamzaAffes,

Hi HamzaAffes,

谢谢你的问题line. Could you please indicate what is the value that the adc_get_voltage_sample() and adc_get_sample() return?

Thanks, PM_Dialog

HamzaAffes
Offline
Last seen:12 hours 29 min ago
Joined:2020-10-03 13:11
thank you for your response .

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.

PM_Dialog
Offline
Last seen:1 hour 5 min ago
Staff
Joined:2018-02-08 11:03
Hi HamzaAffes,

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

HamzaAffes
Offline
Last seen:12 hours 29 min ago
Joined:2020-10-03 13:11
thank you for your response,

thank you for your response,

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

PM_Dialog
Offline
Last seen:1 hour 5 min ago
Staff
Joined:2018-02-08 11:03
Hi HamzaAffes,

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