Greetings, I am fairly new to programming. I have an 8-bit SPI DAC with 4 DACs on the chip which I have written a library for. The code already works for my project, but I am looking for suggestions to improve the syntax of my code. (I can't get any better at programming if I don't know what to improve!)
I guess the only thing you need to know about this is that the DAC has 4 different converters on it, and when the code is all done, I should be easily be able to switch via the slave select to multiple 8bit DAC chips.
Without further adieu...
this is the main file. (this is just a test program I made to test the custom library. This is the first library I have ever made)
#include <SPIDAC.h>
#include <SPI.h>
SPIDAC SPIDAC;
int SLAVESELECT = 53;
void setup(){
SPIDAC.SPIDACinit(SLAVESELECT);
Serial.begin(9600);
}
void loop(){
int pedalin=analogRead(0)/4;
SPIDAC.SPIDACset(SLAVESELECT,pedalin, pedalin, pedalin, pedalin);
..
..
..
(read functions to verify DAC functionality)
}
this is the h-file
#ifndef SPIDAC_h
#define SPIDAC_h
#include "WProgram.h"
class SPIDAC
{
public:
int SLAVESELECT;
void SPIDACinit(int SLAVESELECT);
void SPIDACset(int SLAVESELECT, int frontleft,int frontright,int rearleft,int rearright);
private:
static const int DATAOUT = 51;
static const int SPICLOCK = 52;
};
#endif
this is the cpp
#include "WProgram.h"
#include "SPIDAC.h"
#include "SPI.h"
void SPIDAC::SPIDACinit(int SLAVESELECT) {
SPCR = (1<<SPE)|(1<<MSTR);
// enable SPI, set as master
SPCR = (1<<CPHA);
// =Samples taken on falling edge
byte clr;
clr=SPSR; //clear bad data
clr=SPDR; //clear bad data
delay(10);
// set the SLAVESELECT as an output:
pinMode(SLAVESELECT, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK,OUTPUT);
SPI.begin();
}
void SPIDAC::SPIDACset(int SLAVESELECT,int frontleft,int frontright,int rearleft,int rearright){
for(int address=1;address<=4; address++){
// take the SS pin high to begin data transmission:
digitalWrite(SLAVESELECT,HIGH);
// send in the address and value via SPI:
switch(address){
case 1:
SPDR = 0b00000001; //load second byte with the DAC address 0 and range 2
while (!(SPSR & (1<<SPIF))); //wait to end magnitude transmission
SPDR = frontleft; //load first byte with the magnitude
break;
case 2:
SPDR = 0b00000011; //load second byte with the DAC address 1 and range 2
while (!(SPSR & (1<<SPIF))); //wait to end magnitude transmission
SPDR = frontright; //load first byte with the magnitude
break;
case 3:
SPDR = 0b00000101; //load second byte with the DAC address 2 and range 2
while (!(SPSR & (1<<SPIF))); //wait to end magnitude transmission
SPDR = rearleft; //load first byte with the magnitude
break;
case 4:
SPDR = 0b00000111; //load second byte with the DAC address 3 and range 2
while (!(SPSR & (1<<SPIF))); //wait to end magnitude transmission
SPDR = rearright; //load first byte with the magnitude
break;
}
while (!(SPSR & (1<<SPIF))); //wait to end address byte transmission
digitalWrite(SLAVESELECT,LOW);
delayMicroseconds(3);
}
}
I guess some suggestions on what i could use suggestions with is pointers. I think I could use those in here somewhere....
Are there any major syntax redflags? Can I simplify or generalize the code in anyway? Other suggestions?
I don't have much of a formal education on programming. You can assume if the code has poor form, it's because I don't know "the right way" to do it. All suggestions are welcome.
Also, idk if this is the wrong forum. I'd say this is a syntax improvement. not really an interfacing problem.
Thanks for reading. Thanks for helping me be a better programmer.
-Pearl