Last active
June 27, 2022 07:41
-
-
Save equaliser/8216031 to your computer and use it in GitHub Desktop.
Testing the MCP4922 DAC with the standard SPI library included with the Teensy 3/Teensyduino.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MCP4922 DAC test with standard SPI library, Teensy 3.0 | |
mostly pinched from http://forum.pjrc.com/threads/24082-Teensy-3-mcp4921-SPI-Dac-anybody-tried | |
if we delete the delay(5) in the loop | |
at SPI_CLOCK_DIV2 (24MHz on standard Teensy 3.0) | |
generates 50hz ramp wave | |
clock period ~42ns | |
also faster than the rated speed of the MCP4922 (20Mhz), but it seems to work. | |
Teensy pin 10 - MCP pin 3 (SS - slave select) | |
Teensy pin 11 - MCP pin 5 (MOSI) | |
Teensy pin 13 - MCP pin 4 (SCK - clock) | |
+3.3v - MCP pin 1 (vdd), 13 (DACa vref) | |
GND - MCP pin 8 (LDAC), 12 (Analogue ground ref) | |
*/ | |
#include <SPI.h> // include the SPI library: | |
const int slaveSelectPin = 10; | |
int i = 0; | |
void setup() { | |
// set the slaveSelectPin as an output: | |
pinMode (slaveSelectPin, OUTPUT); | |
// initialize SPI: | |
SPI.begin(); | |
SPI.setClockDivider(SPI_CLOCK_DIV2); // 24MHz | |
} | |
void loop() { | |
i++; | |
write_value(i); | |
delay(5); | |
if(i > 4095) { | |
i = 0; | |
} | |
} | |
void write_value(int value) { | |
// channel (0 = DACA, 1 = DACB) // Vref input buffer (0 = unbuffered, 1 = buffered) // gain (1 = 1x, 0 = 2x) // Output power down power down (0 = output buffer disabled) // 12 bits of data | |
uint16_t out = (0 << 15) | (1 << 14) | (1<< 13) | (1 << 12) | ( value ); | |
digitalWriteFast(slaveSelectPin,LOW); | |
SPI.transfer(out >> 8); //you can only put out one byte at a time so this is splitting the 16 bit value. | |
SPI.transfer(out & 0xFF); | |
digitalWriteFast(slaveSelectPin,HIGH); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment