Pages: [1]   Go Down
Author Topic: can't get Simple SPI communication to work  (Read 1367 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on communicating with a AD9833 DDS chip and have not been able to get it to work.  After reading up on how to do hardware spi, I came up with the following code.  Also, from a data sheet I discovered that I need to send the following bits to the DDS chip:

0010 0001 0000 0000
0101 0000 1100 0111
0100 0000 0000 0000
1100 0000 0000 0000
0010 0000 0000 0000

Here's the code I have so far:

Code:
#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];

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()
{
  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);


digitalWrite(SLAVESELECT,LOW);
spi_transfer((char)(8448>>8));   //send MSByte  first
spi_transfer((char)(8448));      //send LSByte
digitalWrite(SLAVESELECT,HIGH);

  delay(10);

digitalWrite(SLAVESELECT,LOW);
spi_transfer((char)(20679>>8));   //send MSByte  first
spi_transfer((char)(20679));      //send LSByte
digitalWrite(SLAVESELECT,HIGH);

  delay(10);

digitalWrite(SLAVESELECT,LOW);
spi_transfer((char)(16384>>8));   //send MSByte  first
spi_transfer((char)(16384));      //send LSByte
digitalWrite(SLAVESELECT,HIGH);

  delay(10);

digitalWrite(SLAVESELECT,LOW);
spi_transfer((char)(49152>>8));   //send MSByte  first
spi_transfer((char)(49152));      //send LSByte
digitalWrite(SLAVESELECT,HIGH);

  delay(10);

digitalWrite(SLAVESELECT,LOW);
spi_transfer((char)(8192>>8));   //send MSByte  first
spi_transfer((char)(8192));      //send LSByte
digitalWrite(SLAVESELECT,HIGH);

}


void loop()
{

  delay(500); //pause for readability
}

Also, the DDS chips data sheet says :
Quote
When data is being transmitted to the AD9833, the FSYNC line is taken low (PC7). Serial data from the 68HC11/68L11 is transmitted in 8-bit bytes with only eight falling clock edges occurring in the transmit cycle. Data is transmitted MSB first. To load data into the AD9833, PC7 is held low after the first 8 bits are transferred, and a second serial write operation is performed to the AD9833. Only after the second 8 bits have been transferred should FSYNC be taken high again.

If anyone has any idea what I'm doing wrong, please let me know, it will help me immensely and I will be very grateful.

Thanks,
Chris
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I finally got it working by creating my own bit bang implementation of software spi.  Any idea how to get it working using hardware spi?

Thanks,
Chris
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you could post your bit bang code?  
Logged

Pages: [1]   Go Up
Jump to: