DAC in DAC out.

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};
byte sinePos = 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);
  sinePos %= 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);
}

The error

C:\Users\Documents\Arduino\DAC_in_DAC_out\DAC_in_DAC_out.ino: In function 'void nextSample()':

DAC_in_DAC_out:42:35: error: too few arguments to function 'void i2c_str_sendBytes(i2c_dev*, uint8_t, uint8_t)'

  i2c_str_sendBytes(DACDevice, volt);

                                   ^

In file included from C:\Users\ Documents\Arduino\DAC_in_DAC_out\DAC_in_DAC_out.ino:1:0:

C:\Users\ted\Documents\Arduino\libraries\MCP4725/i2c_stretch.h:36:6: note: declared here

 void i2c_str_sendBytes(i2c_dev *dev, uint8_t data, uint8_t data2);

      ^~~~~~~~~~~~~~~~~

exit status 1
too few arguments to function 'void i2c_str_sendBytes(i2c_dev*, uint8_t, uint8_t)'

How to fix this line ?

 i2c_str_sendBytes(DACDevice, volt);

Vik321:
How to fix this line ?

 i2c_str_sendBytes(DACDevice, volt);

Add the missing argument.

The line you commented out

i2c_str_sendBytes(DACDevice, sineLookup[sinePos] >> 8, sineLookup[sinePos++] & 255);

has three arguments in the function call.

Your line

i2c_str_sendBytes(DACDevice, volt);

has only two

The function needs three

DAC_in_DAC_out:42:35: error: too few arguments to function 'void i2c_str_sendBytes(i2c_dev*, uint8_t, uint8_t)'

 i2c_str_sendBytes(DACDevice, volt);

says so right in the error message, "too few arguments".

That I see, but what I should put as third argument ?

Vik321:
That I see, but what I should put as third argument ?

Whatever the parameter is as defined in the library you are using.

FYI. We can only help you with as much information as you provide us.

libraries, #include "i2c_stretch.h"

#include "i2c_stretch.h"

static void (*i2c_str_old_error_handler)(i2c_dev *dev);

void i2c_str_InitPort(i2c_dev *device, uint8_t divider)
{
  i2c_peripheral_disable(device);
  i2c_bus_reset(device);
  i2c_init(device);//initialize it
  i2c_config_gpios(device);//configure the gpios
  device->regs->CR2 = I2C_CR2_ITERREN | 36; //dma enabled, peripheral frequency is 36Mhz
  device->regs->CCR = I2C_CCR_FS | divider; //default 30
  device->regs->TRISE = 11;
}
void i2c_str_ReleasePort(i2c_dev *device)
{
  i2c_str_StopSending(device);
}

void i2c_str_StartSending(i2c_dev *device, uint8_t address)
{
  i2c_peripheral_enable(device);//enable the port
  i2c_start_condition(device);//set the start condition

  uint32_t sr1 = device->regs->SR1;
  uint32_t sr2 = device->regs->SR2;
  uint16_t wait = 0;
  while(!(sr1&I2C_SR1_SB))
  {
    if(wait++ > I2C_STRETCH_SB_TIMEOUT)
    {
      i2c_peripheral_disable(device);
      return;
    }
    sr1 = device->regs->SR1;
    sr2 = device->regs->SR2;
  }
  i2c_write(device, address<<1);//write the address of the device you want to contact (shifted 1 to the left)
}
void i2c_str_StopSending(i2c_dev *device)
{
  i2c_stop_condition(device);
  i2c_peripheral_disable(device);
}

bool i2c_str_IsSending(i2c_dev *device)
{
  return !(device->regs->SR1 & I2C_SR1_TXE);
}

bool i2c_str_IsError(i2c_dev *device)
{
  return device->regs->SR1 & (I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT);
}

bool i2c_str_PortEnabled(i2c_dev *device)
{
  return device->regs->CR1 & I2C_CR1_PE;
}

void i2c_str_sendByte(i2c_dev *device, uint8_t data)
{
  uint32_t sr1 = device->regs->SR1;
  uint32_t sr2 = device->regs->SR2;
  if(sr1&(I2C_SR1_BTF | I2C_SR1_TXE | I2C_SR1_ADDR))
  {
    i2c_write(device, data);
  }
}

void i2c_str_sendBytes(i2c_dev *device, uint8_t data, uint8_t data2)
{
  uint32_t sr1 = device->regs->SR1;
  uint32_t sr2 = device->regs->SR2;
  if(sr1&(I2C_SR1_BTF | I2C_SR1_ADDR))
  {
    i2c_write(device, data);
    i2c_write(device, data2);
  }
}
/**
 * @file i2c_dma.h
 * @author Sven Michiels
 * @Library for driving the STM32 f103 i2c ports via DMA
 */

#ifndef _I2cStretch_H_
#define _I2cStretch_H_

#include <libmaple/i2c.h>
#include <wirish.h>

#define I2C_STRETCH_SB_TIMEOUT 10000 //how many times we will check the status registers to see if the device is starting

#define I2C_STRETCH_DIV_400KHZ 30
#define I2C_STRETCH_DIV_800KHZ 15
#define I2C_STRETCH_DIV_1200KHZ 10

struct I2cStretchSetting
{
  uint8_t slaveAddress;
};

static I2cStretchSetting I2cStretchSettings[2] = {{255},{255}};
static I2cStretchSetting* I2CStretch1 = I2cStretchSettings;
static I2cStretchSetting* I2CStretch2 = I2cStretchSettings + 1;

void i2c_str_InitPort(i2c_dev *dev, uint8_t divider = I2C_STRETCH_DIV_400KHZ);
void i2c_str_ReleasePort(i2c_dev *dev);
void i2c_str_StartSending(i2c_dev *dev, uint8_t address);
void i2c_str_StopSending(i2c_dev *dev);
bool i2c_str_IsSending(i2c_dev *dev);
bool i2c_str_IsError(i2c_dev *dev);
bool i2c_str_PortEnabled(i2c_dev *dev);
void i2c_str_sendByte(i2c_dev *dev, uint8_t data);
void i2c_str_sendBytes(i2c_dev *dev, uint8_t data, uint8_t data2);

static void i2c_str_errIrqHandler(i2c_dev *device);

#endif

You don't have a complex question, yet. According to the library, what is the third parameter for the function i2c_str_sendBytes()?

(be sure to note the plural 's' on the end when you look through the library code).

I will also head off your next problem while you are looking at the function in the library, with these two questions:

  1. What is the data type for the 2nd and 3rd parameters in the function i2c_str_sendBytes()?
  2. What data type are you using (ie., what is the data type of 'volt')?

I can also head off the following question, by noting you have also so far left out all details of your hardware.

I did like that, no errors no output on DAC.

 i2c_str_sendBytes(DACDevice, volt,volt>>8);

Vik321:
I did like that, no errors no output on DAC.

 i2c_str_sendBytes(DACDevice, volt,volt>>8);

Yep, I knew that would happen. I posted 4 items, you did the first. Now you need the next 3.

more changes

//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 = 0;

Still not working.
STM32f103 and MCP4725

Is your declared "uint16_t volt" the same as the function required "uint8_t data"?