0

Bitbanging MCP4725 DAC
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Bitbanging MCP4725 DAC

by Nick_Pow62 on Wed Mar 24, 2021 5:32 pm

Currently, I'm trying to bitbang an I2C connection on the digital pins of our Arduino, so that we can manipulate the output voltage of the MCP4725 DAC via the Arduino's digital pins.

I'm an Arduino novice, and this is out of my traditional project scope. I am currently using this online github repo: Bitbang_I2c (https://github.com/bitbank2/BitBang_I2C).

Using this repo, I have the MCP4725 being seen on the digital pins (D7 and D8) of my Arduino. I am using the I2CWrite function from this repo; however, I do not know what bits to send to the DAC to get a desired output voltage.

Attached is the code for the I2CWrite function. If this is the correct way to go about bitbanging, what bits should I send to the DAC to get a, say, .5Vref voltage? If not, how else should I implement bitbanging of an MCP4725?

IE: I2CWrite(&bbi2c, 0x64, bits, length). <------ what bits would i send in this call?

Code: Select all | TOGGLE FULL SIZE
int I2CWrite(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen)
{
  int rc = 0;
 
  if (pI2C->bWire)
  {
#if !defined ( _LINUX_ ) && !defined( __AVR_ATtiny85__ )
    Wire.beginTransmission(iAddr);
    Wire.write(pData, (uint8_t)iLen);
    rc = !Wire.endTransmission();
#endif
#ifdef _LINUX_
    if (ioctl(pI2C->file_i2c, I2C_SLAVE, iAddr) >= 0)
    {
       if (write(pI2C->file_i2c, pData, iLen) >= 0)
          rc = 1;
    }
#endif // _LINUX_
    return rc;
  }
  rc = i2cBegin(pI2C, iAddr, 0);
  if (rc == 1) // slave sent ACK for its address
  {
     rc = i2cWrite(pI2C, pData, iLen);
  }
  i2cEnd(pI2C);


Code: Select all | TOGGLE FULL SIZE
static inline int i2cWrite(BBI2C *pI2C, uint8_t *pData, int iLen)
{
uint8_t b;
int rc, iOldLen = iLen;

   rc = 1;
   while (iLen && rc == 1)
   {
      b = *pData++;
#ifdef __AVR_ATtiny85__
      rc = i2cByteOutAVRFast(pI2C, b);
#else
#if defined ( __AVR__ ) && !defined( ARDUINO_ARCH_MEGAAVR )
     if (pI2C->iSDA >= 0xa0)
     {
        rc = i2cByteOutAVRFast(pI2C, b);
     }
     else
#endif
     {
        if (b == 0xff || b == 0)
           rc = i2cByteOutFast(pI2C, b); // speed it up a bit more if all bits are ==
        else
           rc = i2cByteOut(pI2C, b);
     }
#endif
      if (rc == 1) // success
      {
         iLen--;
      }
   } // for each byte
   return (rc == 1) ? (iOldLen - iLen) : 0; // 0 indicates bad ack from sending a byte


Here is my initialization code for the bbi2c object, if necessary.
Code: Select all | TOGGLE FULL SIZE
#include <Arduino.h>
#include <BitBang_I2C.h>

BBI2C bbi2c;

void setup() {
  Serial.begin(9600);
  memset(&bbi2c, 0, sizeof(bbi2c));
  bbi2c.bWire = 0; // use bit bang, not wire library
  bbi2c.iSDA = 0xD7; //d7
  bbi2c.iSCL = 0xB0; //d8
  I2CInit(&bbi2c, 100000L);
  delay(100); // allow devices to power up
}
void loop() {
I2CWrite(&bbi2c, 0x64, bits, length)

Nick_Pow62
 
Posts: 1
Joined: Wed Mar 24, 2021 5:22 pm

Re: Bitbanging MCP4725 DAC

by narra1985 on Tue May 04, 2021 2:23 pm

Hi, I think I am in a similar position as you are/were. I have an MCP4725 DAC which I would like to run from an Arduino UNO and have found something that runs on the Arduino IDE, but my aim is to be able to control the output voltage of the MCP4725 over Python script. Is this also your aim? I'm struggling to find anyone reporting to have done this and so far have not managed to make it work myself.

narra1985
 
Posts: 1
Joined: Tue May 04, 2021 2:20 pm

Please be positive and constructive with your questions and comments.