Hi, I have a project where I have to communicate with a AD9835 using SPI. The chip generates a sinewave which frequency and phase is influenced by the value of a 16 bit dataword (devided in two seperate bytes).
I did some research on the playground, and found the project named SPIEEPROM.
The SPI settings are made with the SPCR:
The SPI control register (SPCR) has 8 bits, each of which control a particular SPI setting.
SPCR
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 |
SPIE - Enables the SPI interrupt when 1
SPE - Enables the SPI when 1
DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0
MSTR - Sets the Arduino in master mode when 1, slave mode when 0
CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)
So I read that datasheet and concluded it should be like this: 01010100.
Further on, in the syntax, I can't find where they define these bits. It's in a comment in a textline, but nowhere in the actual program. So I wonder where or how the SPCR is configured in the program?
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
//opcodes
#define WREN 6
#define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2
byte eeprom_output_data;
byte eeprom_input_data=0;
byte clr;
int address=0;
//data buffer
char buffer [128];
void fill_buffer()
{
for (int I=0;I<128;I++)
{
buffer*=I;*
- }*
}
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()
{ - Serial.begin(9600);*
- pinMode(DATAOUT, OUTPUT);*
- pinMode(DATAIN, INPUT);*
- pinMode(SPICLOCK,OUTPUT);*
- pinMode(SLAVESELECT,OUTPUT);*
- digitalWrite(SLAVESELECT,HIGH); //disable device*
** // SPCR = 01010000**
** //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,**
** //sample on leading edge of clk,system clock/4 rate (fastest)**
** SPCR = (1<<SPE)|(1<<MSTR);** - clr=SPSR;*
- clr=SPDR;*
- delay(10);*
- //fill buffer with data*
- fill_buffer();*
- //fill eeprom w/ buffer*
- digitalWrite(SLAVESELECT,LOW);*
- spi_transfer(WREN); //write enable*
- digitalWrite(SLAVESELECT,HIGH);*
- delay(10);*
- digitalWrite(SLAVESELECT,LOW);*
- spi_transfer(WRITE); //write instruction*
- address=0;*
- spi_transfer((char)(address>>8)); //send MSByte address first*
- spi_transfer((char)(address)); //send LSByte address*
- //write 128 bytes*
- for (int I=0;I<128;I++)*
- {*
spi_transfer(buffer*); //write data byte*
* }*
* digitalWrite(SLAVESELECT,HIGH); //release chip*
* //wait for eeprom to finish writing*
* delay(3000);*
* Serial.print('h',BYTE);*
* Serial.print('i',BYTE);*
* Serial.print('\n',BYTE);//debug*
* delay(1000);*
}
byte read_eeprom(int EEPROM_address)
{
* //READ EEPROM*
* int data;*
* digitalWrite(SLAVESELECT,LOW);*
* spi_transfer(READ); //transmit read opcode*
* spi_transfer((char)(EEPROM_address>>8)); //send MSByte address first*
* spi_transfer((char)(EEPROM_address)); //send LSByte address*
* data = spi_transfer(0xFF); //get data byte*
* digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer*
* return data;*
}
void loop()
{
* eeprom_output_data = read_eeprom(address);
Serial.print(eeprom_output_data,DEC);
_ Serial.print('\n',BYTE);_
_ address++;_
_ if (address == 128)_
_ address = 0;_
_ delay(500); //pause for readability*_
}
Thank you for reading.