After a lot of searching on Github and other pages, I was able to build something ... Look, I'm a beginner, and I need your suggestions to make this code work ...
First, the libraries:
DAC8552.h
/*************************************************************************
** Device: DAC8552 **
** File: DAC8552.h - Biblioteca para obter valor de voltagem DAC **
*************************************************************************/
#ifndef DAC8552_h
#define DAC8552_h
#include "Arduino.h"
class DAC8552
{
public:
DAC8552(int DIN, int SCK, int SS, boolean DAC, boolean DAC1);
void setValue(int Value);
private:
int _DATAOUT;
int _SPICLOCK;
int _SLAVESELECT;
boolean _DAC;
boolean _DAC1;
void sendSPIHeader();
void mandaSPI(int Value);
void sendSPIClock();
};
#endif
DAC8552.ccp
/*************************************************************************
** Device: DAC8552 **
** File: DAC8552.h - Biblioteca para saída analógica **
**************************************************************************/
#include "DAC8552.h"
#include "Arduino.h"
DAC8552::DAC8552(int DIN, int SCK, int SS, boolean DAC, boolean DAC1) //Seleção dos pinos e das saídas
{
_DATAOUT = DIN;
_SPICLOCK = SCK;
_SLAVESELECT = SS;
_DAC = DAC;
_DAC1 = DAC1;
pinMode(_DATAOUT,OUTPUT);
pinMode(_SPICLOCK,OUTPUT);
pinMode(_SLAVESELECT,OUTPUT);
}
//************************************************************************
void DAC8552::setValue(int Value) {
sendIntValueSPI(Value);
}
//************************************************************************
void DAC8552::sendSPIClock() {
digitalWrite(_SPICLOCK, HIGH);
digitalWrite(_SPICLOCK, LOW);
delay(1);
}
//************************************************************************
void DAC8552::sendSPIHeader() {
// bit 23 Adress reservado = 0
digitalWrite(_DATAOUT,LOW);
sendSPIClock();
// bit 22 Adress reserved = 0
digitalWrite(_DATAOUT,LOW);
sendSPIClock();
// bit 21 Output on Channel B
digitalWrite(_DATAOUT,_DAC1);
sendSPIClock();
// bit 20 Output on Channel A
digitalWrite(_DATAOUT,_DAC);
sendSPIClock();
// bit 19 Don't care bit
digitalWrite(_DATAOUT,LOW);
sendSPIClock();
// bit 18 Buffer selection
digitalWrite(_DATAOUT,DAC);
sendSPIClock();
// bit 17 and 16 Power down mode - sets as Normal
digitalWrite(_DATAOUT,LOW);
sendSPIClock();
digitalWrite(_DATAOUT,LOW);
sendSPIClock();
}
//************************************************************************
void DAC8552::mandaSPI(int value) {
// initiate data transfer with 8552
digitalWrite(_SLAVESELECT,LOW);
// send 8 bit header
sendSPIHeader();
// send data
for(int i=16;i>=0;i--){
digitalWrite(_DATAOUT,((Value&(1<<i)))>>i);
sendSPIClock();
}
// finish data transfer
digitalWrite(_SLAVESELECT,HIGH);
}
And the test:
#include <DAC8552.h>
DAC8552 SaidaA(11, 13, 10, HIGH, LOW); //Seta a saída para canal A
int LeituraA;
void setup() {
Serial.begin(9600);
}
void loop() {
for(int i=0; i<65535; i++)
{
SaidaA.mandaSPI(i);
LeituraA = analogRead(0);
Serial.println(LeituraA);
delay(1000);
}
}
The debug get a error in "SaidaA.mandaSPI(i);" "within this context"
Whats wrong with my code?