0
\$\begingroup\$

I'm using the MPLAB X IDE v3.50 to type the code and download it in the PIC24FJ64GB004. It is successfully compiling but the ADC is not giving an appropriate value according to the Voltage(Vin1) received on the RC0 pin. Whether the pin gets 3.3V or 0V, the converted value is a constant 0.

#define ADC_CHANNEL_CH_1            ADC_CHANNEL_6 // Pin RC0

Static FILEIO_OBJECT myFile;
uint16_t adc1Result;
uint16 Ch1_Value = 0;
uint16 Ch1_SUM = 0;


void ADC_Calculation()
{
    ADC_ChannelEnable(ADC_CHANNEL_CH_1);
    adc1Result = ADC_Read10bit(ADC_CHANNEL_CH1);

    // ****ADC Calculation****//
Ch1_Value = Ch1_Value + adc1Result;
Ch1_SUM  = Ch1_Value;
Ch1_Value = 0; 

if (Ch1_SUM  < 20)
{
    Ch1_SUM   = 20; //Minimum value of 20
}
    Else
{
    Ch1_SUM  = Ch1_SUM;
}

I'm exporting the ADC converted value using the USB port to an Excel sheet in the Thumbdrive.

When I changed the ADC input pin(Vin1) from RC0 to RC1 (AN7), it gave a correct value till 341(Vin1 = 2.2V). But when I continue to use the RC1 pin with a new second input(Vin2) and then I give the original input(Vin1) to RC0, RC1 behaves the same(gives correct converted value till 341) but RC0 now gives a constant value of 341, '01 0101 0101' in binary, whether Vin1 = 0v/3.3V.

#define ADC_CHANNEL_CH_2            ADC_CHANNEL_7 // Pin RC1
#define ADC_CHANNEL_CH_1            ADC_CHANNEL_6 // Pin RC0

Static FILEIO_OBJECT myFile;
uint16_t adc1Result;
uint16_t adc2Result;

uint16 Ch1_Value = 0;
uint16 Ch2_Value = 0;
uint16 Ch1_SUM = 0;
uint16 Ch2_SUM = 0;


void ADC_Calculation()
{
    ADC_ChannelEnable(ADC_CHANNEL_CH_1);
    ADC_ChannelEnable(ADC_CHANNEL_CH_2);

    adc1Result = ADC_Read10bit(ADC_CHANNEL_CH1);
    adc2Result = ADC_Read10bit(ADC_CHANNEL_CH2);

// ****ADC 1 Calculation****//
Ch1_Value = Ch1_Value + adc1Result;
Ch1_SUM  = Ch1_Value;
Ch1_Value = 0; 

if (Ch1_SUM  < 20)
{
    Ch1_SUM   = 20; //Minimum value of 20
}
    Else
{
    Ch1_SUM  = Ch1_SUM ;
}


// ****ADC 2 Calculation****//
Ch2_Value = Ch2_Value + adc2Result;
Ch2_SUM  = Ch2_Value;
Ch2_Value = 0; 

if (Ch2_SUM  < 20)
{
    Ch2_SUM   = 20; //Minimum value of 20
}
    Else
{
    Ch2_SUM  = Ch2_SUM ;
}

Where am I going wrong? Why am I getting a digital value of 0 when I'm using only RC0 and I get a constant value of 341, when I use RC1 and RC0, at the output of RC0? The ADC in the PIC24FJ64GB004 is 10-bit so it should give a digital value from 0 to 1023. Why doesn't it go above 341?

I'm not collecting the converted values from RC0 and RC1 simultaneously. I'm using RC3 pin to toggle between the outputs. So when RC3=1, RC0's converted value is exported and when RC3=0, digital converted value of RC1 is exported to ecxel sheet.


So basically,

1] Vin1 -> RC0, output = 0 (constant) [RC1 is not used]

2] Vin1 -> RC1, output = 0,122,250 .. 341 (It gives a correct value till Vin1 = 2.2V. Above 2.2V, it gives a constant digital value 341. RC0 is not used)

3] Vin1 -> RC0 and Vin2 -> RC1, output at RC0 = constant 341; output at RC1 = 0.. 341. (constant 341 when Vin2 > 2.2V)

4] Vin2 -> RC0, output = 0 (constant) [RC1 is not used]

\$\endgroup\$
1
\$\begingroup\$

I'm not sure what compiler you're using, but it seems that you're not calling the function that actually triggers the ADC conversion or setting up the ADC before you use it. Usually the process to use the ADC is:

  1. Configure ADC
  2. Open/Enable ADC
  3. Check to see if ADC is not busy
  4. Call ADC convert function
  5. Check to see if ADC is not busy
  6. Read value in ADC registers

These functions are usually defined in the compiler manual library. So you could check there to find out the exact functions you need.

\$\endgroup\$
1
\$\begingroup\$

I think I found the solution. In addition to the information that I provided, I'm using a 3rd ADC(RC3)pin to select between RC0 and RC1. My microcontroller isn't getting enough voltage on its selection pin(RC3). Instead of 3.3v, its getting 3.2v. The reference voltage for the ADC is 3.3v. Maybe the microcontroller is not being able to differentiate between a high at 3.3v and a low at 0v and so it is only seeing RC0's digital value(341).

\$\endgroup\$
  • \$\begingroup\$ Everything is working now ? According to the datasheet (pg 316) the minimum input high voltage in 0.8Vdd which in your case in 2.64V \$\endgroup\$ – Simeon R Mar 26 '18 at 4:01
  • 1
    \$\begingroup\$ Yes, Its working now. Instead of simply reading 0v as low and 3.3v as high, I programmed the PIC24 to read the signal in digital values. The PIC24 has a 10 bit ADC, so the max digital value is 1023. In the new code I gave a digital threshold value of 896 ~ 2.8v. That solved the problem. \$\endgroup\$ – J2018 Mar 27 '18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.