Okay, I am trying to use the 3208 ADC for two reasons. Better resolution (12 bit) and to increase analog inputs. I have the SPI part working fine and am communicating with the ACD fine. The problem is I am not getting the results I think I should be. Attached is a schematic of the VERY simple voltage divider. The voltage readings (4.7 on arduino and 2.2 in between the resistors) are actual measurements with a digital multimeter.
The sketch is designed to display the The analog pin number, current reading, min and max for each analog pin of the ADC. The up/down buttons cycle through the pins and the right button clears the min/max values for the currently displayed pin.
With the voltages as shown on the schematic, I am reading 875 - 895. using the following calculation I get 1.02 Volts:
Volts = 885 * (4.7 / 4096)
As I said, the meter shows 2.2 Volts
/* 3208 read test */
#include<SPI.h>
#include<LiquidCrystal.h>
int lcdpin = 9;
int adcpin = 2;
int anapin = 0;
int minmax[16];
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
void setup() {
lcd.begin(16, 2);
//analogReference(EXTERNAL);
SPI.begin();
Serial.begin(9600);
pinMode(adcpin, OUTPUT);
digitalWrite(adcpin, HIGH);
/*for (x = 0; x < 8; x++){
minmax[i] = 4095;
}
for (x = 8; x < 16; x++({
minmax[i] = 0;
}
*/
}
void loop() {
int analog;
int anacur;
int butpin = analogRead(0);
if ((butpin < 150) && (butpin >130)){
uppin();
}
if ((butpin <335) && (butpin >315)){
downpin();
}
if ((butpin < 751) && (butpin >721)){
minmax[anapin] = 0;
minmax[anapin + 8 ] = 0;
}
for(int i = 0; i < 8;i ++){
analog = adcpinread(i);
if(i == anapin){anacur = analog;}
if ((minmax[i] == 0) & (minmax[i + 8] == 0)){
minmax[i] = analog;
minmax[i + 8] = analog;
}
else{
if (minmax[i] < analog){
minmax[i] = analog;
}
if (minmax[i + 8] > analog){
minmax[i + 8] = analog;
}
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pin ");
lcd.print(anapin);
lcd.print(" = ");
lcd.print(anacur);
lcd.setCursor(0,1);
lcd.print("Mi:");
lcd.print(minmax[anapin]);
lcd.print(" Ma:");
lcd.print(minmax[anapin + 8]);
delay(500);
}
int adcpinread(int channel)
{
int adcvalue = 0;
byte transbyte = 6;
byte returnbyte;
digitalWrite(adcpin, LOW); // Turn on device "adcpin"
// Set the first byte to transfer,
// with with a start bit and a 1 for single ended
// (not differentail) and the MSB of the channel number
if (channel >3) {
transbyte = 7;
}
SPI.transfer(transbyte);
// Set the next transfer byte with the channel number
// minus the MSB and catch the first 4 bits of the
// return value
transbyte |= (channel << 6);
returnbyte = SPI.transfer(transbyte);
// add the first four bits of the valuse to adcvalue
// then shift them to the first four (of 12) bits
adcvalue = returnbyte & 15;
adcvalue = adcvalue << 8;
// recieve the last 8 bits and add them to adc value
returnbyte = SPI.transfer(0x0);
adcvalue += returnbyte;
//end communication with device adcpin
digitalWrite(adcpin, HIGH);
return adcvalue;
}
void uppin(void){
if (anapin == 7)
{
anapin = 0;
}
else
{
anapin += 1;
}
}
void downpin(void){
if (anapin == 0)
{
anapin = 7;
}
else
{
anapin -= 1;
}
}