Hi everyone,
Im new here, and I am trying to make this ADC work on arduino DUE. But Im go blank and I don't know how to read the values.
I connected the pins as it is specified in the datasheet. And I wrote this code, but it isnt working. With the osciloscope i can see the clock is working, the MOSI is working but the MISO stay allways the same.
Here's the code:
#include <SPI.h>
#define SELPIN 52
void setup ()
{
// selecciona el modo del ss
pinMode (SELPIN, OUTPUT);
// inicializa
SPI.begin ();
Serial.begin (9600);
SPI.setClockDivider (SELPIN, 21);
//SPI.setBitOrder(LSBFIRST);
digitalWrite (SELPIN,LOW);
}
void loop ()
{
byte response1 = SPI.transfer (SELPIN, B10010100, SPI_CONTINUE);
byte response2 = SPI.transfer (SELPIN, 0x00, SPI_CONTINUE);
byte response3 = SPI.transfer (SELPIN, 0x00, SPI_LAST);
Serial.print (response1, DEC);
Serial.print (response2, DEC);
Serial.println (response3, DEC);
}
I tried modifing the next code for arduino DUE too, but still not working.
http://forum.arduino.cc/index.php/topic,18893.0.html
#define SELPIN 52 // Pin de selección (ver cual voy a conectar al adc)
#define DATAIN 74 // MISO
#define DATAOUT 75 // MOSI
#define SPICLOCK 76 // Clock
unsigned int readvalue;
void setup()
{
// selecciona el modo de los pines
pinMode(SELPIN, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
// me aseguro de que el dispositivo esté desactivado
digitalWrite(SELPIN,HIGH);
digitalWrite(DATAOUT,LOW);
digitalWrite(SPICLOCK,LOW);
Serial.begin(9600);
Serial.println("hasta aca va todo bien");
}
unsigned int read_adc(int channel)
{
unsigned int adcvalue = 0;
int cnumber = 0;
byte commandbits = B00000000;// No lee nada...
switch(channel)// Selecciona el canal
{
case 1: {
commandbits = B10010100;// selecciona channel 0. Bit de comienzo (bit 7)- Selección de canal (bits 6,5,4)- No importa(bit 3)- single ended (bit 2)- modo reposo (bit 1,0)
}
break;
case 2: {
commandbits = B11010100;// channel 1
}
break;
case 3: {
commandbits = B10100100;// channel 2
}
break;
case 4: {
commandbits = B11100100;// channel 3
}
break;
}
digitalWrite(SELPIN,LOW); // Selecciona el ADC
// necesitamos 24 ciclos para una conversión completa
// 8 ciclos para enviar el byte de control y 16 para
// que los bits de setup sean escritos.
for (int i=7; i>=0; i--){
digitalWrite(DATAOUT,commandbits&1<<i);
// ciclo de reloj
digitalWrite(SPICLOCK,HIGH); //8 veces
digitalWrite(SPICLOCK,LOW);
}
// lee los bits del ADC
for (int i=15; i>=0; i--){ //
adcvalue+=digitalRead(DATAIN)<<i;
// ciclo de reloj
digitalWrite(SPICLOCK,HIGH);//16 veces
digitalWrite(SPICLOCK,LOW);
}
// 24 ciclos en total
digitalWrite(SELPIN, HIGH); // apaga el dispositivo
return adcvalue;
}
void loop()
{
// envia el valor de la entrada analógica 0:
char inByte;
int ichannel = 1;
int counter;
// inByte = Serial.read();
inByte = 'r';
if(inByte == 'r')// si inByte es r leemos
{
// for(ichannel=1;ichannel <=4;ichannel++)
// { // bucle para leer los cuatros canales y enviar lo leido por puerto serie
// en un programa más completo podría modificar el código para seleccionar
// el canal de acuerdo a la conveniencia. "ichannel" es el canal.
for (counter = 0; counter < 200; counter++){
readvalue = read_adc(ichannel);
Serial.println(readvalue,DEC);
}
// }
}
// espera un tiempo para estabilizar el conversor
// AD después de la última lecutra:
delay(10);
}
Sorry for my english.
Thanks a lot!