I am attempting to talk to a analogue to digital converter by the spi port I looked at the examples and am only reading 0's from its registers. it is the ads1256 24 bit adc and is open source.
#include "ads1256.h"
#include "extera.h"//define pin names for pin #
#define CS 10 //Chip Select active low on pin 10
#define NEG_RESET 9 //RESET PIN active low on pin 9 on uc
#define DRDY 2 //DATA READY
#define SYNC 8 //syncronisation power down active low
#define DATAOUT 12//MOSI
#define DATAIN 11//MISO
#define SPICLOCK 13//sck// global variables
int val=0; //a counter
byte clr=0;//used to clear registers to bit trash bin.
byte x=0;
//*************************setup for spi to ad
void spi_to_AD_setup(){
SPCR=0;//clear control register
clr=SPCR; //coppy spi control register to clr to print out or clear it similar to clearing a status register
SPSR=0;
clr=SPSR;/*SPCR
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 |SPIE - Enables the SPI interrupt when 1 set to 0
SPE - Enables the SPI when 1 set to 1
DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0 set to 0
MSTR - Sets the Arduino in master mode when 1, slave mode when 0 set to 1
CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0 set to 0
CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0 set to 0
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz) set to 11SPSR=0;//makeing shure SPI is not going 2x speed.
ex.01010011
*/
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
//After setting our control register up we read the SPI status register (SPSR) and data register (SPDR) in to the junk clr variable
//to clear out any spurious data from past runs:
delay(10);
} //end of initilise spi
//simple spi transfer function char in char out
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}void setup() {
// initialize the communication port
Serial.begin(9600);
Serial.println("Serial Port Initilised.");Serial.println("set pins data direction");
pinMode(CS,OUTPUT);//10 //Chip Select active low on pin 10
pinMode(NEG_RESET,OUTPUT); //RESET PIN active low on pin 11
pinMode(DATAOUT,OUTPUT);// MOSI
pinMode(DATAIN,INPUT);//DATAIN MISO
pinMode(SPICLOCK,OUTPUT);// sck
pinMode(DRDY,INPUT);//Data is ready low
pinMode(SYNC,OUTPUT); //sync active low powerdown
Serial.println("Done seting pins data direction");Serial.println("Initilise pins to defalt states");
digitalWrite(CS,LOW); //enable device
digitalWrite(SPICLOCK,LOW);//defalt CLOCK NO-RESET state
digitalWrite(NEG_RESET,HIGH);//defalt reset state
digitalWrite(SYNC,HIGH);//defalt POWER UP state
Serial.println("Initiliseed pins to defalt states");Serial.println("setup spi port.");
spi_to_AD_setup();
Serial.println("spi port setup...");
Serial.print("SPCR: ");
Serial.println(SPCR, BIN);
Serial.print("SPSR: ");
Serial.println(SPSR, BIN);//reset chip
Serial.println("Reset chip by reset pin");
digitalWrite(NEG_RESET,LOW); //disable device reset self calibration is performed
delayMicroseconds(33);
digitalWrite(NEG_RESET,HIGH); //enable device
}//END OF SETUPvoid loop() {
val= digitalRead(DRDY);
if (val==LOW){
x=spi_transfer(CMD_RREG);
Serial.println(x, BIN);
x=spi_transfer(11);
Serial.println(x, BIN);
delayMicroseconds(8);x=spi_transfer(0xFF);
Serial.println(x, BIN);x=spi_transfer(0xFF);
Serial.println(x, BIN);x=spi_transfer(0);
Serial.println(x, BIN);x=spi_transfer(0);
Serial.println(x, BIN);
}
if (val==HIGH){
Serial.println("HIGH data is not ready?");
}
}//END OF LOOP