Hi halabut,
Thank you very much for replying so early. I finally got it working with a little modification on your code. Instead of:
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
//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);
}
I changed it to:
int adcvalue = 0;
byte commandbits = B110000 + channel; //command bits - start, mode, chn (3)
digitalWrite(SELPIN,LOW); //Select adc
// setup bits to be written
for (int i=4; i>=0; i--)
{
if ((commmandbits>>i)%2 == 1)
{ digitalWrite(DATAOUT,HIGH); }
else
{ digitalWrite(DATAOUT, LOW); }
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
I'm not sure why u had (channel-1) in your code but i got it working in the end using most of your code. Thank you very much.
I found it was very nice to manually adjust the clock pin HIGH and LOW rather than having it running automatically in the background. Thus, timing is up to us to control rather than the arduino controller.
Part of my project is to also have a DAC12bit (TLV5616). I tried similar thing with your code but it does not work. I always get zero. Do you have any idea, please help me out. I already tried using the SPI library but didn't work. Thanks in advance.
Here's my code so far for the DAC:
void writeDacSPI(byte voltValue)
{
byte commandbits = B11111111;
displayOnLCD(0,1, commandbits);
digitalWrite(dacFSPin, LOW); // turn on DAC
for (int i = 7; i >= 0; i--)
{
if ((commandbits>>i)%2 == 1)
{ digitalWrite(dataOutSPI, HIGH);}
else
{ digitalWrite(dataOutSPI, LOW); }
digitalWrite(clockSPI,HIGH);
digitalWrite(clockSPI,LOW);
}
for (int i = 7; i >= 0; i--)
{
if ((voltValue>>i)%2 == 1)
{ digitalWrite(dataOutSPI, HIGH);}
else
{ digitalWrite(dataOutSPI, LOW); }
digitalWrite(clockSPI,HIGH);
digitalWrite(clockSPI,LOW);
}
digitalWrite(clockSPI,HIGH);
digitalWrite(clockSPI,LOW);
digitalWrite(dacFSPin,HIGH); // turn off DAC device
}