This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Differences in programming 8-bit ADC and 12-bit ADC

Hi,

I am currently trying to modify an existing code to display 12 bit data (C8051F206) instead of 8 bit data (C8051F226).

When I download the existing codes and run the program, my LCD is able to display some values. However, when I modify the program to display 12 bit data, the LCD just show 0.000V when I adjusted the ADC input. May I know what is different in the ADC configuration that I have to change ? What is wrong with my 12 bit code?

8-bit code:

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
        unsigned int q,value,temp;
        unsigned char a,b,c,d;
        float bit_value,result;
/*------------------
// ADC Conversion
 -------------------*/

void adcdata()
{

                ADCINT = 0;


                ADBUSY = 1;
                ldelay(100);

                while(ADCINT==0);

                value = ADC0L;
                value = value*256;
                ldelay(900);

                ldelay(1000);
                ADCINT=0;
                ADBUSY=0;

}

/*------------------------------------------------------
// Bit Resolution: result = Voltage reference*(255/256)
 -------------------------------------------------------*/

void bit_resolution()
{

        bit_value = 3.2/(256);
        result = value*bit_value;
}

My 12 bit code (Left justified ADCOH:ADCOL):

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
        unsigned int q,value,value1,temp,totvalue;
        unsigned char a,b,c,d,e;
/*------------------
// ADC Conversion
 -------------------*/

void adcdata()
{

                ADCINT = 0;
                value=0;

                ADBUSY = 1;
                ldelay(100);

                while(ADCINT==0);
                value = ADC0L;
                e= 0x0F & ADC0H;
                value1=e;
                totvalue= value + value1*256;
                ldelay(900);

                ldelay(1000);
                ADCINT=0;
                ADBUSY=0;

}

Appreciate if anyone could help. Thanks!!


  • Sorry, this is for the 12 bit code:

    /*------------------------------------------------------
    // Bit Resolution: result = Voltage reference*(4095/4096)
     -------------------------------------------------------*/
    
    void bit_resolution()
    {
    
            bit_value = 3/(4096);
            result = totvalue*bit_value;
    
    }
    

  • Sorry another correction to my 8 bit code:

    value=ADC0L;
    value=value*256;

    should be removed and replaced by:

    value=ADC0H;

    only.

  • "bit_value = 3.2/(256);"

    Here, a float constant divided by an integer constant yields a float result to be assigned to the float variable 'bit_value'.

    "bit_value = 3/(4096);"

    Here, the integer constant 3 divided by the integer constant 4096 an integer result of zero to be assigned to the float variable 'bit_value'.

  • Hi Dan,

    Thanks for your reply. However, when I changed from "3" to "3.0", I still could not get the program working.

    I am wondering if there is anything wrong with my syntax or is there anything needed to be changed to my current ADC configuration:

    /*----------------
     ADC Configuration
     -----------------*/
            AMX0SL = 0xE0;  // AMUX Channel Select Register; Analog input at P0.0
    
            ADC0CF = 0x78;  // ADC Configuration Register
            ADC0CN = 0x40;  // ADC Control Register
    
            ADC0LTH = 0x00; // ADC Less-Than High Byte Register
            ADC0GTH = 0x00; // ADC Greater-Than High Byte Register
    

  • First output the counts on the display. To make sure the A2D is configured and working. Then Output more and more of the conversions. Read your code. Note that going from 8 to 12 bits can make 16 bit values overflow.
    value=value*256 is good for 8 bits, but overflows at 12.

  • Hey thanks!

    It was my mistake in the syntax. You are right about the float issue. Also, my analog input pin was faulty. After I reconfigured another pin as input, it worked.