 /**
   Reads in the current conversion value, and sets the next channel to be
   sampled.
+
+  Returned value is 2s complemented 16 bit integer.
   */
 int16_t read_analog(uint8_t next_chan) {
+  // we really don't need any interrupts messing the timings in here up
+  noInterrupts();
+
   // select the channel for the next read
-  digitalWrite(_A0, next_chan&0x01);
-  digitalWrite(_A1, next_chan>>1);
-
-  // pre-select the high byte for reading
-  digitalWrite(_BYTE, LOW);
+  digitalWrite(_A0, next_chan&0x1?HIGH:LOW);
+  digitalWrite(_A1, next_chan&0x2?HIGH:LOW);
 
   // begin a conversion
   digitalWrite(_RC, LOW);

  // wait for busy to go high
  wait_busy();

  // use port manipulation to read the data pins. Note that our pinmapping
  // is such that port C maps exactly onto the parallel output from the
  // ADS7825.
  uint8_t hbyte = PINC;

  // now for the high byte
  digitalWrite(_BYTE, HIGH);
  delayMicroseconds(1);

  uint8_t lbyte = PINC;

  return (hbyte<<8 | lbyte)&0xFFFF;
}

void ser_print_si(char * s, int i) {
  Serial.print(s);
  Serial.print(i, DEC);
}

void ser_print_sx(char * s, int i) {
  Serial.print(s);
  Serial.print(i, HEX);
}

int16_t cnt = 0;
void loop() {
  Serial.print(cnt, DEC);
  ser_print_si(" ", read_analog(1));
  ser_print_si(" ", read_analog(2));
  ser_print_si(" ", read_analog(3));
  ser_print_si(" ", read_analog(0));
  Serial.println();
  cnt += 1;
}

 */

// Adding _ prefix b/c arduino api uses some of these names otherwise
#define     _A0       (22)  // PB3
#define     _A1       (23)  // PB4
#define     _BYTE     (24)
#define     _RC       (25)
#define     _BUSY     (26)

// these pins map to PORTC
#define     _D0       (37)
#define     _D1       (36)
#define     _D2       (35)
