Pages: [1]   Go Down
Author Topic: ADC library  (Read 3986 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Name:
Datasheet URL:
Is it working with SPI?:

Jan
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you included the library?

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

Jan
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wally, try skipping the library and just control the SPI access directly. SPI access of the part is pretty simple.

Code:
// 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!
}
« Last Edit: July 09, 2011, 10:49:02 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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."
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int value = readADC();

should read

Code:
int value = myadc.readADC();
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That was beyond my guessing capability ...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

following code works nice now:
Thanks for your help!

Code:
// 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!
}
« Last Edit: July 11, 2011, 10:55:51 pm by wally123 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To paraphrase an old Bogart movie:  "Library?  We ain't got no Library! We don't need no steenkin' library!"  smiley-cool
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: