Go Down

Topic: ADC library (Read 4119 times) previous topic - next topic

JanD

Hello,

I have written a library to interface with various SPI interfaced ADCs. It's available here: http://sites.google.com/site/alternativarduino/english/librarys/adc

If you have any ideas for additions or similar, please post them here.

If you want a specific ADC to be added please use this form:
Code: [Select]
Name:
Datasheet URL:
Is it working with SPI?:


Jan

wally123

Hello Jan,

I'm sure i do a stupid mistake but can not find the reason yet.

Compiling the original example i get:

In function 'void loop()':
error: 'readADC' was not declared in this scope

the library is in ... /arduino-0022/libraries/ADC

in next step i want to connect MAX187

wally

JanD

Have you included the library?

Perhaps if you can post the code, I can help you better.

Jan

wally123

Hello Jan,

i use your unmodified original code from bottommost link on your webseite.
ADC.zip - on Mar 1, 2011 5:58 AM by jan (version 1)
Installed libraries as usual. Import library from Arduino IDE works OK,

wally


CrossRoads

#4
Jul 10, 2011, 05:39 am Last Edit: Jul 10, 2011, 05:49 am by CrossRoads Reason: 1
Wally, try skipping the library and just control the SPI access directly. SPI access of the part is pretty simple.

Code: [Select]

// include the SPI library:
#include <SPI.h>

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
// pins 11, 12, 13 are set up by SPI library
int result;
byte highByte;
byte lowByte;

void setup() {
 // set the slaveSelectPin as an output:
 pinMode (slaveSelectPin, OUTPUT);
 // initialize SPI:
 SPI.begin();
 Serial.begin (38400);
}
void loop(){
 // take the SS pin low to select the chip:
 digitalWrite(slaveSelectPin,LOW);
 // wait 10uS for the conversion per the MAX187 data sheet
 delayMicroseconds(10) ;

 // read the top 8 bits
 highByte = SPI.transfer(0x00);
  // read the rest of the bits
  lowByte = SPI.transfer(0x00);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin,HIGH);

// Done reading,manipulate the data
 // shift the first byte left, then get the second byte:
 result = highByte << 8;
 // combine the byte you just got with the previous one:
 result = result | lowByte;

// may have trailing zero's to get rid of - maybe like this?  Confirm with real hardware.
result = result >>4;
 
// do something with the data now
Serial.println(result);
delay (1000); // don't start blasting out of the serial port yet!
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

"The MAX187/MAX189 serial interface is fully compatible with SPI, QSPI, and Microwire standard serial interfaces.

If a serial interface is available, set the CPU's serial interface in master mode so the CPU generates the serial clock.
Choose a clock frequency up to 2.5MHz.

1. Use a general-purpose I/O line on the CPU to pull CS low. Keep SCLK low.

2. Wait the for the maximum conversion time specified before activating SCLK.
Alternatively, look for a DOUT rising edge to determine the end of conversion.

3. Activate SCLK for a minimum of 13 clock cycles. The first falling clock edge will produce the MSB of the
DOUT conversion. DOUT output data transitions on SCLK's falling edge and is available in MSB-first format.
Observe the SCLK to DOUT valid timing characteristic.
Data can be clocked into the ?P on SCLK's rising edge.

4. Pull CS high at or after the 13th falling clock edge. If CS remains low, trailing zeros are clocked out after
the LSB.

5. With CS = high, wait the minimum specified time, tCS, before launching a new conversion by pulling CS
low. If a conversion is aborted by pulling CS high before the conversions end, wait for the minimum
acquisition time, tACQ, before starting a new conversion.

Data can be output in 1-byte chunks or continuously, as shown in Figure 8. The bytes will contain the result of
the conversion padded with one leading 1, and trailing 0s if SCLK is still active with CS kept low."
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

wally123

Hi CrossRoads,

thanks for your help.
All you explained works, but on the other hand i'm curious now , why Jans code gives this "not declared error". I checked adc.h, adc.cpp and the paths looks ok.
(arduino-0022/libraries/ADC/)
The line  "MyADC myadc(MAX1272, 10); does not make any troubles.
Why it does not accept the function call inside the loop() ? 

wally



CrossRoads

I don't know, I could not make it compile either.

On the other hand, the read is just the 2 SPI transfers.
So often folks get all caught up in having libraries & classes & so on, when the real thing needed is hardlly any code at all and it can be written as your own little function in just a few minutes, and often needs some customzing anyway for the device being used.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

wdl1908


The line  "MyADC myadc(MAX1272, 10); does not make any troubles.
Why it does not accept the function call inside the loop() ? 


The example included is wrong the line

Code: [Select]
int value = readADC();

should read

Code: [Select]
int value = myadc.readADC();

CrossRoads

That was beyond my guessing capability ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

wally123

#10
Jul 11, 2011, 06:59 pm Last Edit: Jul 12, 2011, 05:55 am by wally123 Reason: 1
Hi,

i tried MyADC::readADC();  and MyADC->readADC();
then ist was  beyond my guessing capability ...   :)

following code works nice now:
Thanks for your help!

Code: [Select]

// MAX187 ADC

#include <SPI.h>
const int slaveSelectPin = 10;

int result;
byte highByte;
byte lowByte;
int refVolt = 5000;
int voltage = 0.0;

void setup() {

  pinMode (slaveSelectPin, OUTPUT);
  SPI.begin();
  Serial.begin (115200);
}

void loop(){
  highByte = 0;
  lowByte = 0;
   
  digitalWrite(slaveSelectPin,LOW);
  delayMicroseconds(10) ;

  highByte = SPI.transfer(0x00);
  lowByte = SPI.transfer(0x00);
 
  digitalWrite(slaveSelectPin,HIGH);

  result = (highByte & 127) << 5;
  result = result + (lowByte >> 3);

  voltage =  (float) refVolt/4096 * result;
 
  Serial.print(result, DEC);
  Serial.print("\t");
  Serial.print(voltage);
  Serial.println("mV");

delay (1000); // don't start blasting out of the serial port yet!
}

CrossRoads

To paraphrase an old Bogart movie:  "Library?  We ain't got no Library! We don't need no steenkin' library!"  8)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up