Problems with Interfacing MAX1272

#define SELPIN 10 //Selection Pin
#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//Clock
int readvalue;

void setup(){
 //set pin modes
 pinMode(SELPIN, OUTPUT);
 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK, OUTPUT);
 //disable device to start with
 digitalWrite(SELPIN,HIGH);
 digitalWrite(DATAOUT,LOW);
 digitalWrite(SPICLOCK,LOW);

 Serial.begin(9600);
}

int read_adc(/*int channel*/){
  int adcvalue = 0;
  byte commandbits = B10010001; //command bits

  //allow channel selection
  //commandbits|=((channel-1)<<3);

  digitalWrite(SELPIN,LOW); //Select adc
  // setup bits to be written
  for (int i=7; i >= 3; i--){
    digitalWrite(DATAOUT, commandbits&1<<i);
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }

  //digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  //digitalWrite(SPICLOCK,LOW);
  //digitalWrite(SPICLOCK,HIGH);
  //digitalWrite(SPICLOCK,LOW);

  //read bits from adc
  for (int i=11; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(SELPIN, HIGH); //turn off device
  return adcvalue;
}

void loop() {
 readvalue = read_adc(/*1*/);
 Serial.println(readvalue,DEC);
 //readvalue = read_adc(2);
 //Serial.println(readvalue,DEC);
 Serial.println(" ");
 delay(250);
}

I’ve tried to interface my MAX1272 with the above source code, but I only get “255” in the Serial Monitor. What have I done wrong? The code is a slightly modified version of the one presented here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206784528#14

Jan

I have tried to figure out whats wrong and I think it has anything to do with the two for loops, but for loops is something I hate, does anyone have a clue if they are the “thiefs” and if yes, what do I need to change?

Jan

EDIT: Yes, what was the problem. Now I get the right values.

for loops is something I hate

Why? They are one of the easiest statements to master, consisting of an initialization section, and do/while section, and a "do after each iteration" section.

does anyone have a clue if they are the "thiefs"

No. No for loop has ever stolen anything.

The code is a slightly modified version of the one presented here

Why did you modify it? What did you change?

You posted while I modified my last post.

Why did you modify it? What did you change?

At first, the value of commandBits, to get it work I also changed the values of the for loops.

Jan

For the benefit of anyone searching for MAX1272, it would be nice if you posted the code that actually works.

I’m working on a library to universally interface with almost all SPI ADCs on the base of the code I used for the MAX1272, but it will take some while and until then, here is the code for the MAX1272, it sends the value of the ADC over serial to the computer:

#define SELPIN 10 //Selection Pin
#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//Clock
int readvalue;
int numberOfDataBits = 12;
int numberOfSetupBits = 8;
byte commandbits = B10010001; //command bits

void setup(){
 //set pin modes
 pinMode(SELPIN, OUTPUT);
 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK, OUTPUT);
 //disable device to start with
 digitalWrite(SELPIN,HIGH);
 digitalWrite(DATAOUT,LOW);
 digitalWrite(SPICLOCK,LOW);

 Serial.begin(9600);
}

int read_adc(/*int channel*/){
  int adcvalue = 0;

  //allow channel selection
  //commandbits|=((channel-1)<<3);

  digitalWrite(SELPIN,LOW); //Select adc
  // setup bits to be written
  for (int i=numberOfSetupBits; i >= 0; i--){
    digitalWrite(DATAOUT, commandbits&1<<i);
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }

  //digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  //digitalWrite(SPICLOCK,LOW);
  //digitalWrite(SPICLOCK,HIGH);
  //digitalWrite(SPICLOCK,LOW);

  //read bits from adc
  for (int i=numberOfDataBits; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(SELPIN, HIGH); //turn off device
  return adcvalue;
}

void loop() {
 readvalue = read_adc(/*1*/);
 Serial.println(readvalue,DEC);
 //readvalue = read_adc(2);
 //Serial.println(readvalue,DEC);
 Serial.println(" ");
 delay(250);
}

Please note that I have NOT cleaned the code, there are still many out commented things that doesn’t need to be there.

Jan