I got bored this morning and decided to see if I could write my own code to get it to work as I didn't find any existing code. I got it kinda working but the accuracy is a bit off. When I apply 5 volts to CH0 I get 4074 instead of full scale 4095 but that might be due to the fact that I'm not using the exact capacitors mentioned in the datasheet for the internal voltage reference configuration which I currently have it set to.
When I set my potentiometer to have an output of 1.871V so I was expecting an ADC value of around 1533 but ended up with an ADC value of 722.
Hardware:
MSP430G2553
MAX1270 adc
separate 5 volt power supply to power the ADC
the code is currently configured to use internal clock (tried external clock but got the same results)
datasheet:
http://www.mouser.com/ds/2/256/MAX1270-MAX1271B-95542.pdf
At risk of making a fool out of myself I've posted the code.
This is the first time of trying to write my own code to work with an ADC that uses SPI (I've successfully worked with an ADC that uses a parallel interface but that of course requires quite a few pins)
#define clock 8
#define data 9
#define latch 10
#define din 7
int adcval;
void setup()
{
pinMode(clock,OUTPUT);
pinMode(data,OUTPUT);
pinMode(latch,OUTPUT);
pinMode(din,INPUT);
digitalWrite(clock,LOW);
digitalWrite(data,LOW);
digitalWrite(latch,HIGH);
Serial.begin(9600);
}
void loop()
{
readADC();
}
//send the initialization byte to configure the adc
void initalbit(){
//send the initialization byte to configure the adc
//see page 11 on datasheet for details
digitalWrite(latch,LOW);
digitalWrite(data,HIGH); //beginning of the control byte
clksyc();
digitalWrite(data,LOW); //channel address bit
clksyc();
digitalWrite(data,LOW); //channel address bit
clksyc();
digitalWrite(data,LOW); //channel address bit
clksyc();
digitalWrite(data,LOW); // full scale input voltage range
clksyc();
digitalWrite(data,LOW); //unipolar conversion mode
clksyc();
digitalWrite(data,LOW); //normal operation
clksyc();
digitalWrite(data,LOW); //internal clock mode
clksyc();
digitalWrite(data,LOW); //just setting pin back to low, not part of the byte
}
void clksyc()
{
digitalWrite(clock,HIGH);
digitalWrite(clock,LOW);
}
void readadc(){
//read the actual adc value
boolean dinr;
//clksyc(); removed temp
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+2048;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+1024;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+512;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+256;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+128;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+64;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+32;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+16;
}clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+8;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+4;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+2;
}
clksyc();
dinr=digitalRead(din);
if (dinr == 1){
adcval=adcval+1;
}
}
void readADC(){
initalbit();
delay(1);
// clksyc(); removed temp
// clksyc(); removed temp
// clksyc(); remvoed temp
// clksyc(); removed temp
readadc();
digitalWrite(latch,HIGH);
Serial.println(adcval);
adcval=0; //clear out the variable
delay(1000);
}