kaeroul:
Your pulsing the SS pin low three time to change from I2C to SPI and then going on to write commands but do you maybe need to add another HIGH/LOW toggle to finish the SPI setup?
should I modified this way?
//pull low clatch three times
digitalWrite(ETHCS, LOW);
digitalWrite(ETHCS, HIGH);
digitalWrite(ETHCS, LOW);
digitalWrite(ETHCS, HIGH);
digitalWrite(ETHCS, LOW);
digitalWrite(ETHCS, HIGH);
//end pull low clatch three times
digitalWrite(ETHCS, LOW);
// write
//read
digitalWrite(ETHCS, HIGH);
From the links to sample code you supplied yes you need the extra ETHCS = HIGH to finish the change from I2C to SPI. And I’ve read the **CONTROL REGISTERS SETUP** mentioned but still not understand it yet and dont know how to respond and take action. Hence, could you guys please advise me on how do I proceed for the control register configuration in the Arduino script code or likewise to solve on other problems too? Thanks.
I would start by breaking the code down into blocks (functions) that will aid in the readability of your program flow and reduce risk of errors. Below is a sample of what I mean, it is obviously untested but should work (famous last words) and give you an idea of what I'm referring to.
The DSP_Init code sets the SPI mode and sends the minimum config required as per data sheet. There are 16 bit (2 byte) read/write functions that should be expanded to do between 1 & 5 bytes. Maybe the way to go would be to use a 5 byte buffer to send/receive data where you load the buffer with data and call a function to send the required number of bytes or read them back. (So many different ways of doing this though).
#include <SPI.h>
int ETHCS = 10;
//byte DSP_Buffer[5];
void setup() {
SPI.begin();
DSP_Init();
pinMode(9, OUTPUT);
digitalWrite(9,LOW);
Serial.begin(57600);
Serial.println(DSP_Read16(0x081C),HEX);
}
void loop(){
}
void DSP_Init(){
//SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
pinMode(ETHCS, OUTPUT);
//pull low clatch three times to put DSP into SPI mode
digitalWrite(ETHCS, HIGH);
digitalWrite(ETHCS, LOW);
digitalWrite(ETHCS, HIGH);
digitalWrite(ETHCS, LOW);
digitalWrite(ETHCS, HIGH);
digitalWrite(ETHCS, LOW);
digitalWrite(ETHCS, HIGH);
delay(50);
DSP_Write16(0x081C,0x1C); // DSP Core Control (ADM, DAM, CR)
DSP_Write16(0x0827,0x01); // DAC Setup (DS0)
}
unsigned int DSP_Read16(unsigned int address){
byte result8;
unsigned int result16;
digitalWrite(ETHCS, LOW);
SPI.transfer(0x01); // chip address and read bit
SPI.transfer((byte) address >> 8); // MSB address
SPI.transfer((byte) address); // LSB address
result16 = SPI.transfer(0x00); // store read result in data0
result8 = SPI.transfer(0x00); // store read result in data1
result16 = (result16 << 8) | result8;
digitalWrite(ETHCS, HIGH);
return result16;
}
void DSP_Write16(unsigned int address, unsigned int data){
digitalWrite(ETHCS, LOW);
SPI.transfer(0x00); // chip address and write bit
SPI.transfer((byte) address >> 8); // MSB address
SPI.transfer((byte) address); // LSB address
SPI.transfer((byte) data >> 8); // MSB data
SPI.transfer((byte) data); // LSB data
digitalWrite(ETHCS, HIGH);
}