Error in code execution

Hi all, I have a code in Arduino which I uploaded into STM32F103C8 for digital simulation in Proteus. The project was to have an analog input and take into the controller then send the digital value to a DAC(MCP4725) whose analog output I will read. The DAC input and the DAC output will be read and displayed on the LCD screen. I am operating at 72MHz for the controller and the simulations not happening in real time due to load on the CPU. The problem is that the introductory text message is displayed but the numerical values are not being displayed. The code is given below can someone please help. The original project I got is How to use Digital-to-Analog Converter (DAC) with STM32F10C8 Board.

#include<Wire.h>                   //Include Wire library for using I2C functions 
#include<SoftWire.h>    
#include <LiquidCrystal.h>         //Include LCD library for using LCD display functions 
#define MCP4725 0x60            //MCP4725 address as 0x60 Change yours accordingly
const int rs = PB11, en = PB10, d4 = PB0, d5 = PB1, d6 = PC13, d7 = PC14;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned int adc;
byte buffer[3];                   
void setup() 
{
  Wire.begin();                    //Begins the I2C communication
  lcd.begin(16,2);                 //Sets LCD in 16X2 Mode
  lcd.setCursor(0,0);
  lcd.print("STM32F103C8");
  lcd.setCursor(0,1);
  lcd.print("DAC with MCP4725");
  delay(2000);
  lcd.clear();
}
void loop() 
{
  buffer[0] = 0b01000000;            //Sets the buffer0 with control byte (010-Sets in Write mode)
  adc = analogRead(PA0);             //Read Analog value from pin PA0
  float ipvolt = (3.3/4096.0)* adc;  //Finding voltage formula
  buffer[1] = adc >> 4;              //Puts the most significant bit values
  buffer[2] = adc << 4;              //Puts the Least significant bit values
  unsigned int analogread = analogRead(PA1) ; //Reads analog value from PA1
  float opvolt = (3.3/4096.0)* analogread; //Finding Voltage Formula
  Wire.beginTransmission(MCP4725);         //Joins I2C bus with MCP4725 with 0x60 address
  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
  Wire.endTransmission();           //Ends the transmission
  lcd.setCursor(0,0);     
  lcd.print("A IP:");
  lcd.print(adc);                   //Prints the ADC value from PA0
  lcd.setCursor(10,0); 
  lcd.print("V:");                  //Prints the Input Voltage at PA0
  lcd.print(ipvolt);
  lcd.setCursor(0,1);
  lcd.print("D OP:");
  lcd.print(analogread);             //Prints the ADC value from PA1 (From DAC)
  lcd.setCursor(10,1);
  lcd.print("V:");
  lcd.print(opvolt);                 //Prints the Input Voltage at PA1 (From DAC)
}

During the simulation I found that the pins PB6 and PB7 are not sending anything, hence there is no value. I have also shared the screenshot of the simulation.

This code does not look correct to me.

could you please explain a little, from what I understood from this is to break the final value into 2 parts and then send them serially

Also I would like to add that the code is working fine in real life. The youtube link for that is: https://youtu.be/mNA9rHX-yJQ.

Maybe it is correct. If the value being written to the chip is converted from 12-bit (0 to 4095) to 16-bit (0 to 65535), and then broken into 2 parts (least significant byte and most significant byte) then it should work.

Looking at the code in this library, something similar seems to be done:

//  PAGE 19 DATASHEET
//  reg = MCP4725_DAC | MCP4725_EEPROM
int MCP4725::_writeRegisterMode(const uint16_t value, uint8_t reg)
{
  if (reg & MCP4725_DACEEPROM)
  {
    _lastWriteEEPROM = millis();
  }
  uint8_t h = (value / 16);
  uint8_t l = (value & 0x0F) << 4;
  _wire->beginTransmission(_deviceAddress);
  reg = reg | (_powerDownMode << 1);
  _wire->write(reg);
  _wire->write(h);
  _wire->write(l);
  return _wire->endTransmission();
}

So the problem is in the Proteus simulator? Then don't use it. Or maybe find out if there is a Proteus forum and ask your question there. People on this forum are generally more expert in Arduino than in Proteus.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.