Hello, i am trying to get some normal values from the chip but i get only non sense from it. I get like 13-14bits of data instead of 12.
I use the 5V of the usb as a ref and as the adc input. I now wnat to see it comunicating ok bits, 12bits, even if they ar not the same every time. I know i do not have the right 5V ref voltage to measure.
The code is this:
#include <SPI.h>
int value;
byte byte0;//highbyte for bit11-8
byte byte1;//lowbyte for bit7-0
int cs=9;//chipselect
void setup(){
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
SPI.begin(); //initializing
SPI.setBitOrder(MSBFIRST); //MSB goes first out
SPI.setClockDivider(SPI_CLOCK_DIV16);/1Mhz SPI clock
Serial.begin(9600);
}
void loop(){
digitalWrite(cs, LOW);
SPI.transfer(0x18);//0b0000011000 configuration bits, five 0's, startbit, sind/diff, D2,D1,D0(in this case channel 0
byte0=SPI.transfer(0x00);
byte1=SPI.transfer(0x00);
digitalWrite(cs, HIGH);
Serial.print("high:");
Serial.println(byte0, BIN);
Serial.print("low :");
Serial.println(byte1, BIN);
Serial.print("hig+low");
Serial.print(byte0,BIN);
Serial.println(byte1, BIN);
delay(2000);
}
I see the problem to be in the code, as i send 8 bits and another 2, then i send again 8bits and read the high data in but i do not get always back 8 bits, then i read low data and i get sometimes 8 bits.
Solution:
I found that i first need to sned 0x06, first 8 bits, then send the next 8 bits(0x00 for chanel 0) but at the same time read the data in pin, at at the end send 0x00 for the last 8 bits. Another important thing is that if the first bits from the 8 are 0 arduino does not show them.
Question:
How do i merge byte0 and byte1 into a 2 byte word, and read only the lst 12 bits and convert them into decimal?