Problem With ADC C code

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello, I am running a very basic application of varying input voltage into an ADC and changing the LED color based on the voltage range. I keep receiving an error about not being able to define my variable adcSample (which is the result from the ADC).

   

 

   

int main()
{
    /* Initialize ADC. Conversion is not enabled yet. */
    void ADC_Start();

   

 

   

    /* Start ADC conversion. */
   void ADC_StartConvert();

   

   adcSample = ADC_GetResult16(uint32 0)

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I cannot see a line like

   

uint16 adcSample;

   

or

   

int adcSample;

   

Additionally: You did not check whether a conversion result is already availlable. Check your ADC datasheet for the correct API.

   

 

   

Bob

View solution in original post

0 Likes
6 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I cannot see a line like

   

uint16 adcSample;

   

or

   

int adcSample;

   

Additionally: You did not check whether a conversion result is already availlable. Check your ADC datasheet for the correct API.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I had not copied that part of the code. Here it is:

   

 

   

/* ADC measurement status and result */
int16 adcSample = 0;
uint8 userRefresh = USER_LED_REFRESH - 1u;

   



int main()
{
    /* Initialize ADC. Conversion is not enabled yet. */
    void ADC_Start();

   

    
        
    /* Start ADC conversion. */
   void ADC_StartConvert();

   

   adcSample = ADC_SAR_Seq_1_GetResult16(0x00u); 

0 Likes
Anonymous
Not applicable

Hello,

   

     

   

try using this code..wait for the conversion of ADC is done

   

ADC_Start();

   

CyDelay(50);
 ADC_StartConvert();
   CyDelay(50);
   
        if(ADC_IsEndConversion(ADC_WAIT_FOR_RESULT))
        {
            adcSample =ADC_SAR_Seq_1_GetResult16(0);
         

   

 

   

 

   

from here u can continue the code snippet of yours

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Can you please post your actual complete project, so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I have attached the minimal compression, please take a look and I would highly appreciate any advice greatly. Again, all I am trying to do is use the ADC output and compare it to 4 ranges which will light up 4 different LEDs. My device is a 4200 BLE

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The line

   

   adcSample = ADC_SAR_Seq_1_GetResult16(0x00u);

   


is only executed once, it is outside of your main loop. So adcSample will never change.

   

Again: You did not check whether a conversion result is already available. Use ADC_SAR_Seq_1_IsEndConversion(), see datasheet.

   

Project compiles without warnings, only 5 notes.

   

 

   

Bob

0 Likes