Teensy 4.0 using wire.h to access MCP4451 Potentiometer Registers

Hi, I am a newbie programmer and I have some doubts with this register accesing using wire.h on teensy4.0. I would glad some help of you and will be grateful.
Currently I can write values to the MCP4451 (https://ww1.microchip.com/downloads/en/DeviceDoc/MCP4451.pdf )using this:

C++:
#include <Wire.h>

void setup() {

Serial.begin(115200);
Wire1.begin();

}

void loop() {

Wire1.beginTransmission(44);
Wire1.write(0x00);
Wire1.write(200);   //Pot value (0-255) in 8bits
Wire1.endTransmission();
}

View attachment 33918
For reading the last value works fine:
C++:
#include <Wire.h>

int16_t buffer;

void setup() {

Serial.begin(115200);
Wire1.begin();

}

void loop() {

  Wire1.requestFrom(44,2);// request from 2 byte in the device 1
  buffer = Wire1.read();
  buffer <<= 8;
  buffer |= Wire1.read();
  Serial.println(buffer);
}

The previous code gives the Last Memory Address Accessed, could be whatever wiper direction: (00h,01h,06h,07h). So, for this example it gives 200 that was the value from the wiper in the first code shown.
View attachment 33919

But the problem is when I try to read a random Value from the wiper. If I want to access from an especific wiper Value from a specific device for me is confusing and not working well. I have been trying accessing sending the command shown in the datasheet:

C++:
#include <Wire.h>

int16_t buffer;

void setup() {

Serial.begin(115200);
Wire1.begin();

}

void loop() {
  Wire1.beginTransmission(44);
  Wire1.write(B01011000); //Fixed address(01011) + variable address (A0 for me is 0, also A1 is 0) they are set physically + write bit(0)
  Wire1.write(B00001100); //Device memory address 00 (00h,01h,06h,07h in binary format) + command(1 1) + x + x
  Wire1.write(B01011001); //Fixed address(01011) + variable address (A0 for me is 0, also A1 is 0) they are set physically + read bit(1
  Wire1.endTransmission();
  delay(1000);
  Wire1.requestFrom(44,2);
  buffer = Wire1.read();
  //Serial.println(buffer);
  buffer <<= 8;
  buffer |= Wire1.read();
  Serial.println(buffer);
}

This code does not give the value in the address selected. Any ideas to solve this problem with the registers or what is the correct way to handle this kind of Chips?
 
Back
Top