I am trying to incorporate a MCP4725 dac into my project. The one issue is that it freezes my code on the end of the transmission. Here is my test code (stripped down for debugging)
#include <hcms29xx.h>
#include<Wire.h> //Include Wire library for using I2C functions
#define MCP4725 0x62 //MCP4725 address as 0x61 Change yours accordingly
unsigned int adc;
byte buffer[3];
// Define pins for the LED display.
#define dataPin 2 // connects to the display's data in
#define registerSelect 3 // the display's register select pin
#define clockPin 4 // the display's clock pin
#define enable 8 // the display's chip enable pin
#define reset 9 // the display's reset pin
#define displayLength 8 // number of characters in the display
// create am instance of the LED display library:
hcms29xx myDisplay = hcms29xx(dataPin, registerSelect, clockPin,
enable, reset, displayLength);
// screen brightness
int brightness = 15;
const int tempPin = A3; // select the input pin for the potentiometer
int targetTemp = 0;
int targetTempOld = 0;
int hitemp = 85; // max temp of ac unit
int lotemp = 65; // min temp of ac unit
int displayUpdate = 0;
int count;
int modeselect = 1;
int readtemp;
void setup() {
Serial.begin(115200);
Wire.begin();
// initialize the display library:
myDisplay.begin();
// set the brightness of the display:
myDisplay.setBrightness(brightness);
}
void loop() {
temp();
if (displayUpdate == 1) { //only update the display when the value changes
myDisplay.home();
myDisplay.clear();
myDisplay.print(targetTemp);
count++; //debug to see when display is updating
Serial.println (count);
displayUpdate = 0; //set value to 0 until next change in value
}
dacout();
}
void temp() {
readtemp = analogRead(tempPin);
targetTemp = map(readtemp, 1023, 0, lotemp, hitemp);
if (targetTempOld != targetTemp) {
displayUpdate = 1;
}
targetTempOld = targetTemp;
}
void dacout()
{
buffer[0] = 0b01000000; //Sets the buffer0 with control byte (010-Sets in Write mode)
adc = (modeselect) * 2048; //multiplies the modeselect int by 2048 in order to get 0, 2.5 & 5v dac out
if (adc == 4096) {
adc = 4095;
}
// float ipvolt = (5.0/4096.0)* adc; //Finding voltage formula (A0)
buffer[1] = adc >> 4; //Puts the most significant bit values
buffer[2] = adc << 4; //Puts the Least significant bit values
delay (10);
Serial.println ("delay");
Wire.beginTransmission(MCP4725); //Joins I2C bus with MCP4725 with 0x61 address
Serial.println ("wire begin");
Wire.write(buffer[0]); //Sends the control byte to I2C
Wire.write(buffer[1]); //Sends the MSB to I2C
Wire.write(buffer[2]); //Sends the LSB to I2C
delay (10);
Serial.println ("buffer write");
Wire.endTransmission(); //Ends the transmission
Serial.println ("wire end");
}
If I comment out the "dacout();" the code runs fine.
If "dacout():" is not commented out the script freezes at the "Wire.endTransmission():"
Here is the serial monitor output:
1
delay
wire begin
buffer write
Here is the display library:
https://www.pjrc.com/teensy/td_libs_LedDisplay.html
I'm using an arduino nano as well
Can someone help me figure this one out?
Thanks in advance