drewegatti - 2022-08-25

I tried to integrate the Sensor into the Device Library but gave up because reading it direct form the device is easier

Enable usage in CODESYS
installing the i2c software tools after connecting
the hardware and activation of the i2c bus.

sudo raspi-config

Choose Advanced Options -> "activate I2C" -> "Yes" and exit the tool.
Now update the repository and install i2c-tools

sudo apt-get update
sudo apt-get install i2c-tools

check the addressing/connections
Once you have it installed check that the RTC has been detected using:

sudo i2cdetect -y 0  # (if using Raspberry Pi 1 or)
sudo i2cdetect -y 1  # (if using Raspberry Pi 2 or later)

Note:
The MC3200 has 4 possible i2c addresses in my case it is 0x28.
This output should now look something like this;

admin@CodesysPLC:~ $ i2cdetect -r -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- 28 -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Make sure to convert this address to decimal from Hex

//Pressure Sensor Structure
TYPE M3200 :
    STRUCT
        //Device Setup
        Address : USINT; //In Decimal Form
        WriteBuffer : BYTE := 1;
        ReadBuffer : ARRAY [0..15] OF BYTE  := [15(0)];
        WriteLength : DINT;
        ReadLenght: DINT;


        Praw : UINT;
        Pmax : REAL;
        Pmin : REAL;
        PSI : REAL;

        Traw : UINT;
        Tmax : REAL;
        Tmin : REAL;
        TEMP : REAL;
    END_STRUCT
END_TYPE
//Structure Declaration

PROGRAM PressureCRTL
VAR
    S01 : M3200; 
END_VAR
//Program

//Device Setup
S01.Address := 40; //In Decimal Form
S01.Pmax := 100;
S01.Pmin := 0;
S01.Tmax := 131;
S01.Tmin := 32;



//Write Register to Initiate Communications 
S01.WriteLength := I2C_master.write(S01.Address, ADR(S01.WriteBuffer), 1); 

//Read Register into Bytes
S01.ReadLenght := I2C_master.read(S01.Address, ADR(S01.ReadBuffer), 4);



//Map Bytes to Int
S01.Praw.0 := S01.ReadBuffer[1].0;
S01.Praw.1 := S01.ReadBuffer[1].1;
S01.Praw.2 := S01.ReadBuffer[1].2;
S01.Praw.3 := S01.ReadBuffer[1].3;
S01.Praw.4 := S01.ReadBuffer[1].4;
S01.Praw.5 := S01.ReadBuffer[1].5;
S01.Praw.6 := S01.ReadBuffer[1].6;
S01.Praw.7 := S01.ReadBuffer[1].7;
S01.Praw.8 := S01.ReadBuffer[0].0;
S01.Praw.9 := S01.ReadBuffer[0].1;
S01.Praw.10 := S01.ReadBuffer[0].2;
S01.Praw.11 := S01.ReadBuffer[0].3;
S01.Praw.12 := S01.ReadBuffer[0].4;
S01.Praw.13 := S01.ReadBuffer[0].5;

//CONVERT
S01.PSI := ((((S01.Pmax - S01.Pmin) / 14000.0) * (S01.Praw - 1000.0)) + S01.Pmin);



//Map Bytes to Int
S01.Traw.0 := S01.ReadBuffer[3].5;
S01.Traw.1 := S01.ReadBuffer[3].6;
S01.Traw.2 := S01.ReadBuffer[3].7;
S01.Traw.3 := S01.ReadBuffer[2].0;
S01.Traw.4 := S01.ReadBuffer[2].1;
S01.Traw.5 := S01.ReadBuffer[2].2;
S01.Traw.6 := S01.ReadBuffer[2].3;
S01.Traw.7 := S01.ReadBuffer[2].4;
S01.Traw.8 := S01.ReadBuffer[2].5;
S01.Traw.9 := S01.ReadBuffer[2].6;
S01.Traw.10 := S01.ReadBuffer[2].7;

//CONVERT
S01.TEMP := ((((S01.Tmax - S01.Tmin) / 563.0) * (S01.Traw - 512.0)) + S01.Tmin);

Talk Topic about project #i2c-te-m3200

 

Last edit: drewegatti 2022-08-25