Hello everyone. I'm trying to make a MCP3208 Analog to Digital converter work on my Wiring Mini board. I got it working great on my Arduino with the code below (Thanks to Halabut on the arduino forums).
The pins that are defined in the code below are for the Arduino. I'm a little unsure about which pins to use on the Wiring Mini. I was thinking 24, 25, 26, 27 and tried it with no luck. All I see on the serial monitor is either a 0 or 4095.
I know the Wiring Mini board is definitely capable of handling this task ... I just have no clue as to what might be the problem. The code does compile with no errors in the Wiring IDE, and it does upload to the board with no problem.
thanks, Phil
#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 = 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); }
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); }
IP Logged
|