Thread: Arduino Uno & ADS1231 24 Bit ADC - 2's Compliment to Binary conversion

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    10

    Arduino Uno & ADS1231 24 Bit ADC - 2's Compliment to Binary conversion

    Hello.
    I have an Arduino uno reading output values from a 24 bit adc ADS1231 I am able to get the data in 2's compliment format as expected (page 12 data sheet attached), but the problem is I would like it to be in Decimal format with a positive range as an example (0 to 20000) instead of (800000h to 7FFFFFh) and or at least initially a format like (-10000 to + 9999). Please help with how I could do this conversion. I know that 2's compliment to Decimal this is what I have to do:

    first check if the number is negative or positive by looking at the sign bit. If it is positive, simply convert it to decimal. If it is negative, make it positive by inverting the bits and adding one. Then, convert the result to decimal. The negative of this number is the value of the original binary. But how do I actually do this in an arduino platform c.
    To read the adc values at the moment I am using:

    data types:
    Code:
    uint32_t adcvalue;
     uint32_t c,y;
    reading the adc data:
    Code:
    void readADS1231(void) 
    {
    adcvalue=0; 
    while(digitalRead(D_OUT));//WAIT UNTIL DATA IS READY
    //24-->21 bits
    clock ();
    if(digitalRead(D_OUT)){adcvalue=adcvalue+0x800000;} //24
    clock ();
    if(digitalRead(D_OUT)){adcvalue=adcvalue+0x400000;}//23
    clock ();
    if(digitalRead(D_OUT)){adcvalue=adcvalue+0x200000;}//22
    .... etc
    displaying the data:
    Code:
    void displaydata(void)
      {
        readADS1231();
        y=(adcvalue);
        Serial.print("adcvalue DEC = ");Serial.print(y); 
        delay(1000);
      }
    Attached Images Attached Images Arduino Uno & ADS1231 24 Bit ADC - 2's Compliment to Binary conversion-outputdata-png Arduino Uno & ADS1231 24 Bit ADC - 2's Compliment to Binary conversion-data-retrieval-jpg 
    Last edited by tomnas; 09-27-2017 at 02:45 AM.

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I never have programmed for Arduino, but this should work.
    It shows also how a function return a value, so you don't need global variables.

    Code:
    uint32_t readADS1231(void) 
    {
        uint32_t adc_value = 0;
        int sign = 0, i = 0, adc_read;
    
    
        while(digitalRead(D_OUT));//WAIT UNTIL DATA IS READY
        clock();
    
    // loop for all 24 bits
        for (i = 0 ; i < 24 ; i++) {
            adc_read = digitalRead(D_OUT);
    
    // later bits (bit 1 to 23)
            if (i) {
                if ((adc_read == 1 && !sign) || (adc_read == 0 && sign)) {
                    adc_value |= 1 << (23 - i);
                }
            }
    // first bit (bit 0) look for sign
            else {
                if (adc_read) sign = 1;
            }
            clock();
        }
    
    // transform to negative
        if (sign) {
            adc_value++;
            adc_value *= -1;
        }
    
    
        return adc_value;
    }
    
    
    void displaydata(void)
    {
        uint32_t adc_value = readADS1231();
        Serial.print("adcvalue DEC = ");Serial.print(adc_value); 
        delay(1000);
    }
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. itoa, binary, negative numbers and 2's compliment
    By Nyarlathotep_ in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2009, 05:32 AM
  2. Binary Conversion anyone?
    By NoobieGecko in forum C Programming
    Replies: 4
    Last Post: 03-07-2008, 01:34 PM
  3. Binary to Hex Conversion
    By elrookie in forum C Programming
    Replies: 8
    Last Post: 06-26-2007, 10:58 AM
  4. Binary to BCD conversion
    By Andy_P in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 11:16 AM
  5. Conversion from INT to Binary
    By Junior89 in forum C++ Programming
    Replies: 15
    Last Post: 01-01-2005, 11:26 AM

Tags for this Thread