Has anyone a C code example for reading the BOE's ADC? (AD7993)
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
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
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
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
Unpacking the file in the adc dir with no further modification; this is what I get This is what I have on my project view
The "lib" dir is not in the zip, neither is the "libi2c.a" file
Alex
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
Can not test because I have not the hardware.
cheers Reinhard
adcSimpleIDE.zip
dgately
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
And thanks for responding with your code. I'll check it out!
dgately