hola chicos, buenas tardes tengo problemas al querer leer una señal analogica (AnalogRead) y transferirla por medio de SPI a un convertidor Digital analogico DAC MCP4922 el cual es de 12 bits, por tanto es necesario tomar la lectura y enviarla en 2 bytes, pero tengo problemas ya que el samplin (muestreo) esta muy lento necesito mas velocidad.
encontre estos 2 metodos:
/*MCP4922 test
connections
====================================================
+5v > 4922 pin 1
Ard pin 10 > 4922 pin 3 (SS - slave select)
Ard pin 13 > 4922 pin 4 (SCK - clock)
Ard pin 11 > 4922 pin 5 (MOSI - data out)
Ground > 4922 pin 8 (LDAC)
+5v > 4922 pin 11 (voltage ref DAC B)
Ground > 4922 pin 12
+5v > 4922 pin 13 (voltage ref DAC A)
4922 pin 14 DAC A > 1k resistor > synth CV in
*/
// MCP4922 DAC out
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK 13//sck
#define SLAVESELECT0 10//ss
int valor=0;
void setup() {
SPI_setup();
// Serial.begin(9600);
}
void loop() {
valor = analogRead(A0)<< 2;;
write_note(valor);
}
void write_note(int i) {
write_valueDACA(i);
}
// **************************************************
// SPI for DAC
void SPI_setup(){
byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT0,OUTPUT);
digitalWrite(SLAVESELECT0,HIGH); //disable device
SPCR = (1<<SPE)|(1<<MSTR) | (0<<SPR1) | (0<<SPR0);
clr=SPSR;
clr=SPDR;
//delay(10);
}
// write out through DAC A
void write_valueDACA(int sample)
{
// splits int sample in to two bytes
byte dacSPI0 = 0;
byte dacSPI1 = 0;
dacSPI0 = (sample >> 8) & 0x00FF; //byte0 = takes bit 15 - 12
dacSPI0 |= 0x10;
dacSPI1 = sample & 0x00FF; //byte1 = takes bit 11 - 0
dacSPI0 |= (1<<5); // set gain of 1
digitalWrite(SLAVESELECT0,LOW);
SPDR = dacSPI0; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{};
SPDR = dacSPI1;
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{ };
digitalWrite(SLAVESELECT0,HIGH);
}
Alguna idea para mejorar la velocidad?? si introduzco una señal de 100Hz no tengo problema al recuperarla igualita, pero si aumento a 200Hz se pierden datos.
el otro codigo es necesario utilizar la libreria MCP4922
#include <MCP4922.h>
#include <SPI.h>
//MCP4922 DAC(51,52,53,5); // (MOSI,SCK,CS,LDAC) define Connections for MEGA_board,
MCP4922 DAC(11,13,10,5); // (MOSI,SCK,CS,LDAC) define Connections for UNO_board,
void setup()
{
SPI.begin();
}
void loop()
{
int i=analogRead(A0)<< 2;;
DAC.Set(i,0);
}
La respuesta es exactamente la misma si tienen alguna idea de transferir a una velocidad mayor por SPI (los pines MISO,MOSI, SCK,SS) lo agradeceria !!
la hoja de datos del DAC MCP4922 tratare de subirla si no, solo es importante que sepan que es un convertidor Digital analogico, de 12 bits y tiene 2 DAC que se pueden utilizar individual y en conjunto.
MCP4922.zip (4.65 KB)