warpigs330
Active member
I am trying to get a dac working with the teensy 4.0 I have. I was successful getting it to work on an arduino uno, but it does not work on the teensy. I noticed in the documentation for spi that SPI.setClockDivider is outdated, so I tried using a bit of their SPI settings code, that in this version is commented out. I was using this tutorial http://www.kerrywong.com/2012/07/25/code-for-mcp4821-mcp4822/ and everything is wired to the arduino like in that photo, except for the capacitor. When I change out the teensy I wire the pins to the same pin numbers as on the arduino. I was worried it might be the 3.3v instead of the 5v on the arduino but it works on 3.3 volts with the arduino. I'm a little out of my depth with digital communication stuff. Any help you could provide would be very helpful.
Here is the code I was working with.
Here is the code I was working with.
#include <SPI.h>
const int PIN_CS = 10;
const int GAIN_1 = 0x1;
const int GAIN_2 = 0x0;
//SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
void setup()
{
pinMode(PIN_CS, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2);
}
//assuming single channel, gain=2
void setOutput(unsigned int val)
{
byte lowByte = val & 0xff;
byte highByte = ((val >> 8) & 0xff) | 0x10;
PORTB &= 0xfb;
SPI.transfer(highByte);
SPI.transfer(lowByte);
PORTB |= 0x4;
}
void setOutput(byte channel, byte gain, byte shutdown, unsigned int val)
{
byte lowByte = val & 0xff;
byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;
PORTB &= 0xfb;
//SPI.beginTransaction(settingsA);
SPI.transfer(highByte);
SPI.transfer(lowByte);
PORTB |= 0x4;
}
void loop()
{
//high-res triangular wave
/*for (int i=0; i < 4096; i+=32)
{
//setOutput(0, GAIN_2, 1, i);
setOutput(i);
}*/
setOutput(2000);
}