Been working on a project involving the control of a DDS chip, an AD9834, and a DAC--an AD5620 via one-way SPI.
I thought since I had two special chips that I ought to write a class of sorts for each one, and package each class in its own library
so I can just treat them like objects in the IDE.
SPI can only communicate to the devices via half-duplex. I can only write to the devices, and cannot perform a read from them. Since
this is the case, it introduces a bit of a programming problem for me.
I'd like to have set() and get() functions incorporated into my library, but I feel that altering the data of the class object ought to be separated from operating
the device. Should I just write the object as a sort of data-handling class and incorporate it into a larger class that performs connection related functions? Or should setting the a variable in the class also change the settings of the device?
How would you handle class design for operating an SPI device that operates slightly differently than your other SPI device? I do know that the two chips I'm using do have compatible SPI buses.
Code: Select all
/*
AD9834.h
This library is for control of the AD9834 via SPI.
*/
#ifndef AD9834_h
#define AD9834_h
#include "WProgram.h"
#include "SPI.h"
class AD9834{
public:
AD9834(); //Empty Default Constructor
AD9834(int FSYNC, int SDATA, int SCLK); //Instantiate an AD9834 on the appropriate pins
void configure(unsigned int CONTROL_WORD); // Configure the Device's control word.
void setFreq(int reg);
void setPhase(int reg);
unsigned long getFreq(int reg);
unsigned int getPhase(int reg);
private:
unsigned long FREQ0; //28 bit Frequency registers
unsigned long FREQ1;
unsigned int PHASE0; //Phase Register, 12 bit
unsigned int PHASE1; //
unsigned int CONTROL; //Control Register
};
#endif