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

Manipulation of 12 bit ADC data using C programming

Hi all,

My current ADC configuration is such that ADLJST=0, that is ADC0H[3:0]:ADC0L[7:0]. What should I do to my code (underlined bold) below to handle both ADC0H and ADC0L ?

Thank you.

/*------------------
// ADC Conversion
 -------------------*/

void adcdata()
{

                ADCINT = 0;
                value=0;

                ADBUSY = 1;
                ldelay(100);

                while(ADCINT==0);               //Is conversion complete ?

                value = ADC0H;
                ldelay(900);

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

}

/*------------------------------------------------------
// Bit Resolution: result = Voltage reference*(4095/4096)
 -------------------------------------------------------*/

void bit_resolution()
{

        bit_value = 3/(4096);
        result = value*bit_value;
}

//-----------------------------------------------------------------------------
//                                                      Conversion
//-----------------------------------------------------------------------------

void one()              /*convert 1st value     A.xxx */
{
        temp=result;
        temp = temp%10;
        a = temp+48;

}

void dec1()             /*convert 1st decimal place value       x.Bxx */
{
        temp=result*10;
        temp = temp%10;
        b = temp+48;
}

void dec2()             /*convert 2nd decimal place value       x.xCx */
{

        temp = result*100;
        temp = temp%10;
        c = temp+48;
}

void dec3()             /*convert 3rd decimal place value       x.xxD */
{
        temp = result*1000;
        temp = temp%10;
        d = temp+48;
}

void convert_all()
{
        one();
        dec1();
        dec2();
        dec3();
}

void displayD()         /* display 3 digit number */
{
        RS = 1;

        P3 = a;
        enable();

        print(0x2E);    /*display ".", ASCII code is 2E for "."*/

        P3 = b;
        enable();
        P3 = c;
        enable();
        P3 = d;
        enable();
}

/*------------------
// Display result
 -------------------*/

void result_dsp()
{

        bit_resolution();
        convert_all();
        displayD();
}
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main()
{
        config();

        lcdinit();              //Initialize LCD

        ADCEN = 1;              //Enable ADC

        ldelay(5000);

        lcd_command(0x80);      //LCD command to display on the first line

        voltage();              //Display "Voltage="

        lcd_command(0x02);      //LCD command to return the cursor home
        lcd_command(0xC0);      //LCD command to display on the second line

        adcdata();

        result_dsp();

        print(0x56);    //Display "V"

        ldelay(5000);

}

  • Read them out in the order specified in the data sheet.
    Shift the high-order byte left as many steps as is required, based on how many bits the data sheet says in the low-order byte.
    Check align of the bits in the low-order byte and possibly shift to correct position before oring together the high and low values. Just make sure that your intermediate results fits.

    By the way. Stay away from magical numbers. You are adding 48 to convert from binary to ASCII. Instead add with '0' to make it more readable.

    You would get a lot less code if you use a loop for converting the ADC value to decimal. Something like.

    Make sure that your function names says what they do. A function "one()" doesn't say anything about what it do. A function "process_ones_digit()" gives more info.

    I don't see your variable declarations, but your function bit_resolution() plays with floating point. Try to avoid if possible. Computing 3/4096 do require floating point.

    Computing (value*3)/4096 will not require floating point, unless the end result is expected to have fractions. If the end result is expected to have one decimal, you can do (10*value*3)/4096 and remember that when you convert from binary to decimal, there should be a decimal separator before the last emitted digit. Once more - make sure that the intermediate product must not suffer from numeric overflow.