Home    Impressum / Datenschutz    Shop    Download    Links     Blog  

Ansteuerung der I2C-Baugruppen mit Java

Java-Code und Klassen für den I2C-Koppler

 Zitat:

Hallo,

Ich habe mich daran gemacht eine Implementierung mit dem
I2C-Koppler in Java zu Programmieren. Ich stelle ihnen hiermit den Code zur Veröffentlichung zur Verfügung.

Im Anhang finden Sie die beiden Java Dateien. Eine Klasse mit Deklarationen und ein Beispiel für den Temperatursensor.

Mfg, Philipp S.

Java-Code "Main.java"

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package horter_i2c;

import javax.comm.*;
/**
 *
 * @author Philipp
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        // Open Serial Connection for I2C Interface
        try
        {
            I2CBus i2cBus = new I2CBus();
            SerialPort serialport = i2cBus.connect("COM1"); // Linux: /dev/ttyS0
            i2cBus.i2cInit(serialport);
            i2cBus.i2cStart(serialport);
            i2cBus.i2cStop(serialport);
            
             // Tempsensor 1
            i2cBus.i2cStart(serialport);
            // Register setzen
            if(i2cBus.i2cSlave(serialport, 158))
            {
                i2cBus.i2cOut(serialport, 0);
                i2cBus.i2cStop(serialport);
            }

            i2cBus.i2cStart(serialport);
            // Register Setzen
            if(i2cBus.i2cSlave(serialport, 159))
            {
                int by1 = i2cBus.i2cIn(serialport);
                i2cBus.i2cAck(serialport);
                int by2 = i2cBus.i2cIn(serialport);
                i2cBus.i2cNoAck(serialport);
                i2cBus.i2cStop(serialport);

                double wert=0.0;

                if((by1 & 128) == 0)
                {
                    wert = by1;
                }
                else
                {
                    wert = by1 - 255;
                }

                if((by2 & 128) != 0)
                {
                    wert = wert + 0.5;
                }

                System.out.println("Es hat "+wert+" °C");
            }
        }
        catch(java.lang.Exception e)
        {
            System.out.println(e);
        }
    }
}

Java-Code "I2CBus.java"

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package horter_i2c;

import javax.comm.*;

public class I2CBus
{
    public I2CBus()
    {
        super();
    }
    
    // Set the SCL (RTS on the Com)
    public void SCL(SerialPort serialport, boolean wert)
    {
        serialport.setRTS(wert);
    }
    
    // Set the SDA (DTR on the Com)
    public void SDA(SerialPort serialport, boolean wert)
    {
        serialport.setDTR(wert);
    }

    // Get the SDA (DSR on the Com)
    public boolean SDA_in(SerialPort serialport)
    {
        boolean sda_in=false;
        if(serialport.isDSR())
        {
            sda_in = true;
        }
        else
        {
            sda_in = false;
        }
        return sda_in;
    }

    // Get the INT (CTS on the Com)
    public boolean I2C_INT(SerialPort serialport)
    {
        boolean I2C_INT=false;
        if(serialport.isCTS())
        {
            I2C_INT = true;
        }
        else
        {
            I2C_INT = false;
        }
        return I2C_INT;
    }

    // Send the I2C Initiation Sequence
    public void i2cInit(SerialPort serialport)
    {
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        if(!this.SDA_in(serialport))
        {
            System.out.println("Keine Antwort vom I2C Interface");
        }
    }

    // Send the I2C Start Sequence
    public void i2cStart(SerialPort serialport)
    {
        this.SDA(serialport, false);
        this.SCL(serialport, false);
    }

    // Send the I2C Stop Sequence
    public void i2cStop(SerialPort serialport)
    {
        this.SDA(serialport, false);
        this.SCL(serialport, true);
        this.SDA(serialport, true);
    }
    
    // Send the I2C Ack Sequence
    public void i2cAck(SerialPort serialport)
    {
        this.SDA(serialport, false);
        this.SCL(serialport, true);
        this.SCL(serialport, false);
        this.SDA(serialport, true);
    }
    
    // Send the I2C NoAck Sequence
    public void i2cNoAck(SerialPort serialport)
    {
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        this.SCL(serialport, false);
    }
    
    // Access the Slave (Adress)
    public boolean i2cSlave(SerialPort serialport, int Adresse)
    {
        boolean i2cslave = false;
        int bit=128;
        for(int n=1; n<9; n++)
        {
            if((bit & Adresse) == 0)
            {
                this.SDA(serialport, false);
            }
            else
            {
                this.SDA(serialport, true);
            }
        
            this.SCL(serialport, true);
            this.SCL(serialport, false);
            bit = bit/2;
        }
        
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        
        if(this.SDA_in(serialport))
        {
            System.out.println("Kein I2C Slave an der Adresse "+Adresse);
            i2cslave = false;
        }
        else
        {
            i2cslave = true;
        }
        this.SCL(serialport, false);
        return i2cslave;
    }
    
    // Reading Data from the I2C Interface
    public int i2cIn(SerialPort serialport)
    {
        int bit=128;
        int wert=0;
        this.SDA(serialport, true);
        for(int n=1; n<9; n++)
        {
            this.SCL(serialport, true);
            if(this.SDA_in(serialport))
            {
                wert = wert + bit;
            }
            this.SCL(serialport, false);
            bit = bit/2;
        }
        
        return wert;
    }
    
    // Sending Data to the I2C Interface
    public void i2cOut(SerialPort serialport, int wert)
    {
        int bit=128;
        for(int n=1; n<9; n++)
        {
            if((wert & bit) == bit)
            {
                this.SDA(serialport, true);
            }
            else
            {
                this.SDA(serialport, false);
            }
            this.SCL(serialport, true);
            this.SCL(serialport, false);
            bit = bit/2;
        }
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        
        if(this.SDA_in(serialport))
        {
            System.out.println("Keine quittierung der Daten vom Slave!");
        }
        this.SCL(serialport, false);
    }
    
    // Connect to the Interface vie Serial Port
    public SerialPort connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
        SerialPort serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

        return serialPort;
    }
}

 

Beispiel Java Quellcode
Main.java (2kB)
I2CBus.java (5kB)
Java Quellcode  mit einem Beispiel zum I2C-Koppler und Java.
(Bitte mit der rechten Maustaste "speichern unter" wählen! )

Benötigt wird eventuell noch das javax.comm-Paket. Dieses kann unter folgender Adresse heruntergeladen werden: http://users.frii.com/jarvi/rxtx/