Projekte
HobbyCi
Singer Trainer
LC-Meter
Pianotrainer
RomkI
Nachttischlampe
Car Akku Tester
K8055 als I2C Master
Dies und das
Bilder

K8055 als I2C Master

Die SDA und SCL Leitung muss mit ein 1k Ohm auf 5V verbunden werden!
Im Sourcecode k8055_i2c.zip ist ein Beispiel enthalten.
Für das compilieren und starten wird TWUsb benötigt.

// (c) 2008 Thomas Oltzen                                                  //
// This program is free software; you can redistribute it and/or           //
// modify it under the terms of the GNU  General Public License            //
// as published by the Free Software Foundation; either version2 of        //
// the License, or (at your option) any later version.                     //
//                                                                         //
// This program is distributed in the hope that it will be useful,         //
// but WITHOUT ANY WARRANTY; without even the implied warranty of          //

// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // // General Public License for more details. // // // // You should have received a copy of the GNU General Public License // // along with this program (see COPYING); if not, write to the // // Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //

package i2c;

import java.io.IOException;

import de.wenzlaff.twusb.schnittstelle.TWUsb; import de.wenzlaff.twusb.schnittstelle.exception.TWUsbException;

public class K8055_I2C { int device = 3;

private boolean openflag;

final static int SDA_PIN = 1;

final static int SCL_PIN = 2;

public K8055_I2C(int device_no) { device = device_no; }

/** Set the data line. */

private void SDA(boolean bool) throws TWUsbException { // Wenn der Ausgang auf Off gesetzt wird, setzt der Pull-up Widerstand auf 5V! // Ist der Ausgang auf On, so wird die Leitung auf GND gelegt.

if (bool) TWUsb.ClearDigitalChannel(SDA_PIN); else TWUsb.SetDigitalChannel(SDA_PIN); }

/** Set the clock line. */ private void SCL(boolean bool) throws TWUsbException { // Wenn der Ausgang auf Off gesetzt wird, setzt der Pull-up Widerstand auf 5V!

// Ist der Ausgang auf On, so wird die Leitung auf GND gelegt. if (bool) TWUsb.ClearDigitalChannel(SCL_PIN); else TWUsb.SetDigitalChannel(SCL_PIN); }

/** Send a bit on I2C */ public void sendBit(boolean bool) throws TWUsbException { if (bool) { SDA(true); } SCL(true); SCL(false); SDA(false); }

/** Send the start condition. */

public void sendStart() throws TWUsbException {

SCL(true); SDA(true); SDA(false); SCL(false); }

/** Send the stop condition. */

public void sendStop() throws TWUsbException { SDA(false); SCL(true); SDA(true);

}

/** Receive a bit from slave. */

public boolean receiveBit() throws TWUsbException { SDA(true); SCL(true); // Leider muss hier ca. 300ms gewartet werden bis der K8055 die Eingangsleitung erkennt. delay(); boolean ret = TWUsb.ReadDigitalChannel(SDA_PIN); SCL(false); return ret; }

/** Read a acknowledge from slave. */

public void readAck() throws Exception { if (!receiveBit()) throw new Exception("No I2C ACK from Slave."); }

/** Send a byte to Slave * * @param by * @throws Exception */

public void sendDataByte(int by) throws Exception { for (int k = 7; k >= 0; k--) sendBit((by & (1 << k)) > 0);

}

/** Send a Adress to Slave * * @param addr * @throws Exception */

public void sendAddress(int addr) throws Exception { System.out.println("sendAddress " + addr); for (int k = 6; k >= 0; k--) sendBit((addr & (1 << k)) > 0);

}

/** Read a byte from I2C. * * @return * @throws Exception */

public int recDataByte() throws Exception { int k, val = 0;

for (k = 7; k >= 0; k--) if (receiveBit()) val |= 1 << k; System.out.println("read=" + val); return val; }

/** Close the connection to k8055 */

public void closeConnection() { TWUsb.CloseDevice(); openflag = false; }

/** Is the connection to K8055 establish. */ public boolean isOpen() { return openflag; }

/** Open the connection to k8055 */

public void openConnection() throws TWUsbException { System.out.println("openConnection:" + TWUsb.OpenDevice(3)); sendStop(); openflag = true;

}

/** Send the write bit to I2C. */

public void sendWriteFlag() throws TWUsbException { System.out.println("Send Write Flag"); sendBit(false); }

/** Send the read bit to I2C. */ public void sendReadFlag() throws TWUsbException { System.out.println("Send Read Flag"); sendBit(true); }

/** Wait 300ms. */

private void delay() { try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

/** Send the acknowledge bit to I2C. */

public void sendAck(boolean b) throws TWUsbException { sendBit(!b);

}

}

(29.11.14)


[Projekte] [Impressum]

Bei Fragen oder Kommentaren wenden Sie sich bitte an Thomas Oltzen. .
© Copyright 2011 Thomas Oltzen, Alle Rechte vorbehalten.
Sat Jan 31 15:52:16 CET 2015

Oltzen, Consulting, Analyse, Design, Softwareentwicklung, Lösungen, Lösung,