The DAC testing

Hi
In this code input to the DAC is sine wave so output is sine.
For testing quality of the DAC I want to replace input to it by signal applied to analog pin PA7.
This is what I did but it is not working.

#include "i2c_stretch.h"

#define DACDevice I2C1
//#define DACAddress 0x60
#define DACAddress 0x67

//uint16_t sineLookup[20] = {2048, 2680, 3251, 3704, 3995, 4095, 3995, 3704, 3251, 2680, 2048, 1415, 844, 391, 100, 0, 100, 391, 844, 1415};
///////////////////
uint16_t volt = 4096;
///////////////////////
//byte sinePos = 0;
byte voltPos = 0;
bool toggle = 0;
//int volt;
void setup()
{
  volt = analogRead(PA7);
  pinMode(PA7, INPUT_ANALOG);
  pinMode(PA1, PWM);
  pinMode(33, OUTPUT);
  startCommunication();
  initTimer();
}

void loop()
{
  ///////////////////
  float volt = analogRead(PA7);
  volt = (volt * 3.3) / 4095.0;
  /////////////////////
  if (i2c_str_IsError(DACDevice) | !i2c_str_PortEnabled(DACDevice))
  {
    digitalWrite(33, toggle = !toggle);
    startCommunication();
  }
  delay(100);
}

void nextSample()
{

  // i2c_str_sendBytes(DACDevice, sineLookup[sinePos] >> 8, sineLookup[sinePos++] & 255);
  //sinePos %= 128;

  ////////////////////////////////////
  i2c_str_sendBytes(DACDevice,  volt >> 8,volt & 255);
voltPos %= 128;
  ////////////////////////////////////

}

void startCommunication()
{
  i2c_str_InitPort(DACDevice, I2C_STRETCH_DIV_1200KHZ);
  i2c_str_StartSending(DACDevice, DACAddress);
}

void initTimer()
{
  nvic_irq_set_priority(NVIC_TIMER2, 0);
  Timer2.setChannel1Mode(TIMER_OUTPUTCOMPARE);
  Timer2.setPrescaleFactor(3);
  Timer2.setOverflow(133); //should be about 60khz
  Timer2.setCompare1(1);
  Timer2.attachCompare1Interrupt(nextSample);
}

I’m not sure what device you are using, though either way, it would be worth adding some debug statements so that you can inspect what is going on under the hood.

Some tips to fault find:

  • Strip the code, right back. Just try and control the PWM manually with fixed values. Ensure you have that working.
  • Echo to console the analogue readings, so you can witness they are correct and your hardware is not the problem

Either of the above could be the issue - but taking this cut-down approach will reveal which. More useful, it will also highlight if there’s an issue with your hardware.

Let me know how you get on.

The timer is for DAC clock so PWM is not modulated, “SCL I2C Serial Clock Input”. The data "SDA I2C Serial Data " is come from this lines
void nextSample()
{
i2c_str_sendBytes(DACDevice, sineLookup[sinePos] >> 8, sineLookup[sinePos++] & 255);
sinePos %= 20;
}

DAC MCP4725

I did like this "i2c_str_sendBytes(DACDevice, analogRead(PA7)>> 8, analogRead(PA7) & 255); "
and is working but I have some noise, I think I need to do something with this line " uint16_t sineLookup[20] "
uint16_t ???;