Hi everyone, I am doing a project with 12-bit ADC MCP3208 on DUE right now.
And I 've just written hardware SPI driver on DUE for MCP3208, similiar to Arduino Playground - MCP3208.
My code works fine on MEGA 2560, reading values well. However,when it runs on DUE, I could only get the value "4096" on each ADC channel.(using DUE extended SPI)
Can anyone tell me what's wrong with the code? The problem has bothered me for a week...
Thanks!
//for arduino DUE board use only
#include <SPI.h>
/*MEGA2560 use
//#define SELPIN 53 // chip-select
//#define DATAOUT 51 // MOSI
//#define DATAIN 50 // MISO
//#define SPICLOCK 52 // Clock
*/
int readvalue;
void setup() {
// while (!Serial) ;
//pinMode(SELPIN,OUTPUT);
//digitalWrite(SELPIN,HIGH);
SPI.setClockDivider(52,168);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(52,SPI_MODE0);
SPI.begin(52);
Serial.begin(115200);
}
int read_ADC(int channel){
int adcvalue = 0;
int b1 = 0 , b2 = 0;
// digitalWrite (SELPIN, LOW); // Select adc
//first byte
//first byte will be B0000011X where X is D2
byte commandbits =B00000110;
commandbits |=(channel >>2); //high bit of channel
SPI.transfer(52,commandbits); //send out first byte of command bits
//second byte Bxx000000; where xx are D1 D0 bit
commandbits |= B00000000;
commandbits |=(channel<<6) ; // if D1 and D0 are set it will be the left bit now
b1 = SPI.transfer(52,commandbits) ; //send out second byte of command bits
//SPI.transfer(commandbits) ;
//high byte will have XXX0BBBB
//set the top 3 don't care bits so it will format nicely
b1 |= B11100000;
int high = b1 & B00001111;
//read low byte
b2 = SPI.transfer(52,b2); // don't care what we send
//SPI.transfer(b2);
int low = b2;
// digitalWrite(SELPIN, HIGH);
int reading = high*256 + low;
return (reading);
}
void loop() {
// Serial.println("AAA");
Serial.print("value:");
int A = 0, B=0;
A = read_ADC(1)/2;
B = read_ADC(2)/2;
Serial.println(A);
Serial.println(B);
Serial.println(" ");
delay(250);
}