Hi all,
I'm new to the Arduino and these forums i'm wondering if someone could guide me how to interface the Arduino Mega with MCP3208, i know its been achieved with other versions of the Arduino and the code is available on the Arduino Playground.
But i was wondering if the same code applied to Arduino Mega since the Pins are different.
On to my 2nd Question how can i integrate multiple MCP3208s to the Arduino?
But i was wondering if the same code applied to Arduino Mega since the Pins are different.
The MCP3208 playground code bit-bangs SPI instead of using the ATmega SPI hardware. This means it should work exactly as written on the MEGA (but I have not tested this).
Well behaved SPI peripherals share the data bus (SCL, MISO, MOSI) but use a unique chip select for each peripheral. Just hook up data, power, and ground the same on both 3208s, but use different arduino pins for the chip select inputs.
I modified the playground code so that I could pass the chip select line to the function; code is attached below.
-j
int read_adc(byte cs, int channel)
{
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
//allow channel selection
commandbits|=((channel-1)<<3);
digitalWrite(cs, 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(cs, HIGH); //turn off device
return adcvalue;
}