Shop OBEX P1 Docs P2 Docs Learn Events
Has anyone a C code example for reading the BOE's ADC? (AD7993) — Parallax Forums

Has anyone a C code example for reading the BOE's ADC? (AD7993)

Alex.StanfieldAlex.Stanfield Posts: 198
edited 2014-06-13 23:07 in Propeller 1
I'm testing C waters with the BOE and needed an ADC driver.

Unless I missed something, the C learn pages are for the activity board which has a different ADC chip...

So if anyone has a working driver I'll be happy to learn from it.

Thanks!
Alex

PD: I found this thread (http://forums.parallax.com/showthread.php/142801-Anyone-have-examples-of-using-the-Prop-Boe-s-ADC-from-PASM) (see post #10) however I couldn't build it right away

Comments

  • dgatelydgately Posts: 1,630
    edited 2014-06-11 20:16
    Can't remember where or in what thread I got this code, but it looks for 2 versions of the Prop BOE's ADC chip...

    You'll need to mod the #if 0 to #if 1 to try several options to see what works for you.

    EDIT: Seems to be the same code from the previous thread... Builds for me with SimpleIDE. What kind of errors are you getting if it's not in fact building???
    /* ADC test program */
    
    
    #include <stdio.h>
    #include "i2c.h"
    #include "adc.h"
    #include "simpletools.h"
    
    
    int main(void)
    {
        int i;
        int adcAddr;
        I2C *bus;
        
    #if 0  
      
        if (!(bus = i2cBootOpen())) {
            printf("error: can't open boot i2c bus\n");
            return 1;
        }
            
        /* check for the AD7993BRUZ-0 chip */
        if (DetectAdcChip(bus, AD7993BRUZ_0) == 0) {
            printf("AD7993BRUZ-0 found\n");
            adcAddr = AD7993BRUZ_0;
        }
        
        /* check for the AD7993BRUZ-1 chip */
        else if (DetectAdcChip(bus, AD7993BRUZ_1) == 0) {
            printf("AD7993BRUZ-1 found\n");
            adcAddr = AD7993BRUZ_1;
        }
        
        /* no adc chip found */
        else {
            printf("error: no adc chip found\n");
            return 1;
        }
            
        for (i=0; i < 10; i++) {
            printf("%c", HOME);          // clear
            printf("adc[0] = %g\n", adcVolts(bus, adcAddr, 0));
           // printf("adc[1] = %g\n", adcVolts(bus, adcAddr, 1));
           // printf("adc[2] = %g\n", adcVolts(bus, adcAddr, 2));
            usleep(1000000);
        }
        
    #else
    
    
        uint16_t  buffer[3*16];
        ADC       adc, *dev;
        uint16_t  newVal[2];
        uint16_t  oldVal = 0;
        
        if (!(dev = adcOpen(&adc, 28, 29, AD7993BRUZ_0, 7, 10000, buffer, 3*16))) {
            printf("error: can't open adc device\n");
            return 1;
        }
        
        for (;;)  {
            uint16_t buf[3];
            printf("%c", HOME);          // clear
            adcRead(dev, newVal, 1);
            if (newVal[0] = oldVal) {
              printf("adc[0] = %d   old: %d",newVal[0] & 0xfff,oldVal);
              oldVal = newVal[0];
            }
            //printf("adc[%d] = %d\n", (buf[1] >> 12) & 3, buf[1] & 0x0fff);
            //printf("adc[%d] = %d\n", (buf[2] >> 12) & 3, buf[2] & 0x0fff);
            usleep(100000);
            //printf("oPtr %04x, iPtr %04x\n", dev->state.mailbox.oPtr, dev->state.mailbox.iPtr);
        }
        
    #endif
        
        return 0;
    }
    

    dgately
  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2014-06-11 20:22
    Thanks for that dgately!

    Would you be kind enough to pack the whole thing? adc.* i2c.* will be needed (possibly more than that) and I don't have a complete set for that.

    Thanks again!
    Alex
  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2014-06-12 05:08
    dgately wrote: »
    ...

    EDIT: Seems to be the same code from the previous thread... Builds for me with SimpleIDE. What kind of errors are you getting if it's not in fact building???
    ...
    dgately

    Unpacking the file in the adc dir with no further modification; this is what I get
    Project Directory: C:/Users/PEST/Documents/SimpleIDE/adc/
    
    
    propeller-elf-gcc.exe -v GCC 4.6.1 (propellergcc_v1_0_0_2162)
    propeller-elf-gcc.exe -I . -L . -I ./include/ -I ./lib -Os -mlmm -Wall -m32bit-doubles -fno-exceptions -Dprintf=__simple_printf -c adc_test.c -o lmm/adc_test.o
    propeller-elf-gcc.exe -I . -L . -I ./include/ -I ./lib -o lmm/adc_test.elf -Os -mlmm -Wall -m32bit-doubles -fno-exceptions -Dprintf=__simple_printf lmm/adc_test.o ./lib/libi2c.a
    propeller-elf-gcc.exe: error: ./lib/libi2c.a: No such file or directory
    Done. Build Failed!
    
    Click error or warning messages above to debug.
    
    This is what I have on my project view
    ProjectView.png


    The "lib" dir is not in the zip, neither is the "libi2c.a" file

    Alex

    Alex
    644 x 435 - 25K
  • ReinhardReinhard Posts: 489
    edited 2014-06-12 06:30
    Hi Alex,
    I have download adc.zip
    unfortunaly I have no Propgcc Toolchain around me yet.
    But I read the Makefile and it seems you have to build the adc_driver and i2c_driver first,
    then you can use the adc_test.side ...
    I try it later if I am at home
    Reinhard
  • ReinhardReinhard Posts: 489
    edited 2014-06-12 07:54
    Now it' s build.
    Can not test because I have not the hardware.

    cheers Reinhard
    adc.zip 60.3K
  • dgatelydgately Posts: 1,630
    edited 2014-06-12 08:55
    Here' a .zip of a SimpleIDE project that should build (includes libi2c library as well)...

    adcSimpleIDE.zip


    dgately
  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2014-06-13 17:05
    Thanks guys. I haven't checked in detail, but I had some trouble with one of the zips above and the code seemed to be re-reading the chip instead of converting and reading (output was fixed without any change) so I finally decided to digg into the AD7993 manual and build an ultra-simple routine, no libs required besides "simpletools"

    Here it is
    /**
     * This is the main ADC_BOE_test program file.
     */
    #include "simpletools.h"
    
    
    #define uint8 unsigned char
    
    
    ///////////////////////////////////////////////////////////////////
    // ADC routines for BOE
    ///////////////////////////////////////////////////////////////////
    i2c *adcBus;                    // I2C bus ID
    uint8 adcAddr;                  // Address for this module
    uint8 addList[5]= { 0x20, 0x21, 0x22, 0x23, 0x24 }; //I2C bus addresses for ADC
    #define AD7993BRUZ_0    (0x21)  // Don't shift addresses, it's done by simpletools
    #define AD7993BRUZ_1    (0x23)  // 
    
    
    //-------------------------------------------------------------------------
    // Init I2C bus and find/define the AD7993 address
    // Returns: 1=ok
    //          0=chip not found, address defaulted
    //-------------------------------------------------------------------------
    int AD7993start(unsigned int address) {
      uint8 i, t;
    
    
      adcBus = i2c_newbus(28,  29,   0);           // Set up I2C bus, get bus ID
      if (address == 0) { //Scan bus for AD7993 chips
        for (i=0; i<sizeof(addList); i++) {
          if (i2c_in(adcBus, addList[i], 0, 0, &t, 1) == 2) {
            adcAddr = addList[i];
            //printf("AD7993 found on 0x%X\n", adcAddr);
            return 1;
          }
        }
      } else {
        if (i2c_in(adcBus, address, 0, 0, &t, 1) == 2) {
          adcAddr = address;
          //printf("AD7993 found on 0x%X\n", adcAddr);
          return 1;
        }
      }
      adcAddr = AD7993BRUZ_0;   //force address if everything else failed
      //printf("AD7993 defaulted to 0x%X\n",adcAddr);
      return 0; //return in error
    }
    
    
    //-------------------------------------------------------------------------
    // Get ADC value for channel in binary form (0-1023)
    //-------------------------------------------------------------------------
    int AD7993in(int channel) {
      uint8 adcVal[2];
    
      
      i2c_out(adcBus, adcAddr, 1<<((channel&3)+4), 1,NULL, 0); //Set channel, mem ptr and start convert
      i2c_in( adcBus, adcAddr, 0, 0, adcVal, 2);           //Get 2 bytes from adc
      
      return ((adcVal[0] & 0xf) << 6) | (adcVal[1] >> 2);
    }
    
    
    //-------------------------------------------------------------------------
    // Get ADC value in mV (0-5000)
    //-------------------------------------------------------------------------
    int AD7993inV(int channel) {
      return AD7993in(channel) * 5000 / 1023;
    }
    
    
    //-------------------------------------------------------------------------
    int main(void) {
    
    
      uint8 i;
    
    
      pause(800);
    
    
      AD7993start(0);   //Init i2c bus and address
    
    
      while(1) {
        for (i=0; i<4; i++) {
          printf("ADC[%d]: %4d\t", i, AD7993inV(i));
        }
        printf("\n");
        pause(500);
      }
    
    
      return 0;
    }
    

    Again thanks for your support.

    Alex
  • dgatelydgately Posts: 1,630
    edited 2014-06-13 23:07
    Good for you, Alex!

    And thanks for responding with your code. I'll check it out!


    dgately
Sign In or Register to comment.