Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Wiring Hardware >> Tangible computing >> 12-Bit ADC Not working on Wiring
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1216011478

Message started by pd2kplus10 on 07/14/08 at 04:57:57

Title: 12-Bit ADC Not working on Wiring
Post by pd2kplus10 on 07/14/08 at 04:57:57
 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);
}

Title: Re: 12-Bit ADC Not working on Wiring
Post by barragan on 07/14/08 at 16:03:17
run it on the same pins, you have for the arduino. Which Wiring version and OS are you using?

Title: Re: 12-Bit ADC Not working on Wiring
Post by pd2kplus10 on 07/14/08 at 19:27:46
So I should leave the code exactly the way it is and connect the ADC to 10, 11, 12, 13?  .. isn't the pin mapping totally different between the arduino & wiring mini?


I'm using Wiring version 0016 Alpha



thanks,
Phil

Title: Re: 12-Bit ADC Not working on Wiring
Post by barragan on 07/14/08 at 20:31:54
Internally it is (the mapping), but Wiring has those pins so you can connect your circuit to them, and use them in the code as you have them right now, that should be transparent for us as users.

Title: Re: 12-Bit ADC Not working on Wiring
Post by pd2kplus10 on 07/14/08 at 20:58:37
 I just tried it. I get the same results. Are you sure about the pin mapping? The Arduino Mini PDF file shows 24, 25, 26, 27 as the SPI pins ... miso, mosi, clk, select.

 Is there any more documentation for the Mini? I'm very confused now.

thanks,
Phil

Title: Re: 12-Bit ADC Not working on Wiring
Post by barragan on 07/14/08 at 22:00:03
oh, Ok, got it, your code is using an SPI protocol but is not using the SPI harware features of the arduino or Wiring boards. The communication protocol you are using, as it is in your code can be run in any pins. If you are looking for the wiring pins that actually work as hardware SPI you´re right those are SS (24), SCK (25), MOSI (26), MISO (27), but the code as you have it can be run on any pins.

Title: Re: 12-Bit ADC Not working on Wiring
Post by pd2kplus10 on 07/14/08 at 22:40:06
Ok, thanks. I wasn't sure if that was the case or not. I guess I'll try it on a bunch of other pins.

Have you had any luck with other 12 Bit ADC's? I like this one because it has a separate analog ground as well as a Vref.



thanks,
Phil

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.