i tried several ways to read a word from an i2c device without the need of any (complex) devicedesc.xml and device.library
I want to read the cap and voltage of a UPS HAT (http://www.raspberrypiwiki.com/images/6/6f/How-to-use-via-i2c.pdf)
i can read the word from the device with i2cget from terminal
i2cget -f -y 1 0x36 4 w
Is there a way to read those 2 bytes or this word from within a FB?
I tried something like https://forum.codesys.com/viewtopic.php?f=23&t=6247&p=19944#p19944 but did not succeed.
Any help will be appreciated.
Thanks in advance
Yiedermann thank you for the snippet as my codesys is reading an i2c slave.....any chance you know how to write to a slave as I tried anything I could think of. Seems the usage mimics the I2C method comparing the read so for write I tried below:
datatoread: ARRAY [0..1] OF BYTE := [2(0)];
I2C_master.write(16#68, 16#15, 1);
I2C_master.write(16#68, ADR(datatostore), 2);
for I2C guess this example here would do the job:
l viewtopic.php?f=23&t=6247&p=19944#p19944 l
"c:\Users\<yourwindowsusername>\CODESYS Control for Raspberry PI\18.104.22.168\Examples\PiFace_FB.project"
Thank you for the fast answer.
That was the example i already tried.
But i found another one...
This example was the missing point.
i need to write to the register before i read it back...
just in case someone else is fighting with the basics of i2c here is my working code:
usiAddress : USINT := 16#36;
rVoltage : REAL := 0;
byRegVoltage : BYTE := 16#02;
baReadBufferV : ARRAY [0..1] OF BYTE := [2(0)];
// Register Spannung auf Bus schreiben...
I2C_master.write(usiAddress, ADR(byRegVoltage), 1);
// ... und Wert lesen.
nReadLenght := I2C_master.read(usiAddress, ADR(baReadBufferV), 2);
// wenn zwei Bytes gelesen worden sind die Spannung berechnen uns ausgeben
IF nReadLenght = 2 THEN
// Daten zwischenspeichern
//swap_word := MEM.PackBytesToWord(baReadBuffer,baReadBuffer);
//rVoltage := (swap_word * 78.125 / 1000000);
rVoltage := (MEM.PackBytesToWord(baReadBufferV,baReadBufferV) * 78.125 / 1000000);
thanks for the feedback,
if you want you could publish your application here:
Raspberry Pi: List of available example applications
think other user would be very thankful for these kind of Information and applications.
Log in to post a comment.