Fri May 25, 2007 2:44 pm
Fri May 25, 2007 3:23 pm
public class I2cDriver {
/**********************************************************************/
/* ATTRIBUTES */
/**********************************************************************/
private static final int TEMPO = 0;
private I2cHardware ihw;
/**********************************************************************/
/* CONSTRUCTORS */
/**********************************************************************/
public I2cDriver(I2cHardware ihw)
{
this.ihw = ihw;
}
/**********************************************************************/
/* METHODES */
/**********************************************************************/
// monter SDA
private void sda_h()
{
ihw.sda_h();
}
private void sda_l()
{
ihw.sda_l();
}
private void scl_h()
{
ihw.scl_h();
}
private void scl_l()
{
ihw.scl_l();
}
private boolean read_sda()
{
return ihw.read_sda();
}
private boolean read_scl() // n'est utile que pour tester si le bus est libre (is useful to only test if the bus is free)
{
return ihw.read_scl();
}
private void startI2c() throws Exception
{
delay(TEMPO);
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
int countdown = 10;
while ( !read_sda() || !read_scl()) {
delay(100);
countdown--;
if (countdown == 0)
throw new Exception("Echec de la prise de controle du bus."); //Failure of the takeover of the bus.
}
delay(TEMPO);
sda_l();
delay(TEMPO);
scl_l();
delay(TEMPO);
sda_h();
}
private void stopI2c() throws Exception
{
delay(TEMPO);
sda_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
sda_h();
delay(TEMPO);
}
private void clock() throws Exception
{
delay(TEMPO);
scl_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_l();
}
private boolean write_octet(int o) throws Exception
{
int result;
scl_l();
for(int cpt=7; cpt>=0 ; cpt--)
{
result = o & (1<<cpt); // (<<cpt)?
if(result!=0)
sda_h();
else
sda_l();
clock();
}
return getAck();
}
private int read_octet()
{
int result=0;
for(int cpt=0; cpt<8 ; cpt++)
{
scl_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
result <<= 1;
if(read_sda())
result++;
}
return 255-result;
}
private boolean getAck() throws Exception
{
boolean etat = false;
scl_l();
delay(TEMPO);
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
etat = read_sda();
scl_l();
delay(TEMPO);
return !etat;
}
private void giveAck() throws Exception
{
sda_l();
clock();
sda_h();
}
private void giveNack() throws Exception
{
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_l();
delay(TEMPO);
sda_h();
}
private void delay(int nsec)
{
try
{
Thread.sleep (0,nsec);
}
catch (InterruptedException e)
{
}
}
public synchronized void init()
{
sda_h();
scl_h();
}
public synchronized int read(int add) throws Exception
{
int vec[] = readOctets(1,add);
return vec[0];
}
/**
* Cette mط·آ£ط¢آ©thode permet de lire une suite d'octets sur la carte voulue.
* <br>
* @param octets Vecteur d'octets qui stockera le rط·آ£ط¢آ©sultat.
* @param add Adresse de la carte utilisط·آ£ط¢آ©e.
* @see I2cDriver#read(int add)
* @throws I2cException Si la carte ne se reconnaط·آ£ط¢آ®t pas.
*/
public synchronized int[] readOctets(int n, int add) throws Exception
{
int octets[]= new int[n];
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
// Bit 0 = 1 (LECTURE)
add |=1;
if (!write_octet(add))
{
stopI2c();
throw new Exception("readOctets : address error while reading.");
}
octets[0]=read_octet();
for(int cpt=1; cpt<octets.length; cpt++)
{
giveAck();
octets[cpt]=read_octet();
}
giveNack();
stopI2c();
ok = true;
}catch(Exception e){}
}
return octets;
}
public synchronized void write(int valeur, int add) throws Exception
{
int vec[] = new int[1];
vec[0]=valeur;
writeOctets(vec, add);
}
public synchronized void writeOctetsSansStop(int[] octets, int add) throws Exception
{
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
//Bit 0 = 0 (ECRITURE)
add &= 0xfe;
if (!write_octet(add))
{
stopI2c();
throw new Exception("WriteOctets : address error while writing.");
}
for(int cpt=0; cpt<octets.length; cpt++)
{
if (!write_octet(octets[cpt]))
{
stopI2c();
throw new Exception("WriteOctets : write error.");
}
}
// stopI2c();
ok = true;
}catch(Exception e){}
}
}
public synchronized int[] writeAndReadOctet(int octet, int add) throws
Exception
{
int vect[] = new int[1];
vect[0] = 255-octet;
writeOctetsSansStop(vect, add);
int[] result = readOctets(1, add);
return result;
}
public synchronized void writeOctets(int[] octets, int add) throws Exception
{
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
//Bit 0 = 0 (ECRITURE)
add &= 0xfe;
if (!write_octet(add))
{
stopI2c();
throw new Exception("WriteOctets : address error while writing.");
}
for(int cpt=0; cpt<octets.length; cpt++)
{
if (!write_octet(octets[cpt]))
{
stopI2c();
throw new Exception("WriteOctets : write error.");
}
}
stopI2c();
ok = true;
}catch(Exception e){}
}
}
public String getVersion(){
return ("0.0.1");
}
}
import java.util.*;
import javax.comm.*;
/**
*
*The I2cHardware class allows the implementation of protocol Iط·آ¢ط¢آ²C thanks to the port series.
*it orders the electronic interface between the port series and the bus i2c
*it simulates the existance of a bus i2c on the computer using the port series.
*
* @author Patrick Lebط·آ£ط¢آ¨gue
* @version 1.0
*/
public class I2cHardware {
private static CommPortIdentifier portId;
private static Enumeration portList;
private SerialPort serialPort = null;
public I2cHardware(String s) throws Exception {
System.out.println("starting I2cHardware...");
portList = CommPortIdentifier.getPortIdentifiers();
portId = null;
while (portList.hasMoreElements() && serialPort == null) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("****"+ portId.getName());
if (s.indexOf(portId.getName())!=-1){
try
{
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
serialPort = (SerialPort) portId.open("Temperature", 2000);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.disableReceiveThreshold();
serialPort.disableReceiveTimeout();
}
}
catch (PortInUseException pe)
{
throw new Exception("Port " + s + " is already used by " + pe.currentOwner);
}
catch (Exception e)
{
System.out.println("initialisation of i2c Driver failed");
e.printStackTrace();
portId = null;
}
}
}//while
if (serialPort == null)
System.out.println("Port not found");
else System.out.println("driver i2c initialised on " + portId.getName());
}//constructor
/* cablage sda-out = RTS, scl-out = DTR, sda-in = CTS, scl-in = DSR */
// SDA up
public synchronized void sda_h()
{
serialPort.setRTS(true);
}
// SDA down
public synchronized void sda_l()
{
serialPort.setRTS(false);
}
//SCL up
public synchronized void scl_h()
{
serialPort.setDTR(true);
}
//SCL down
public synchronized void scl_l()
{
serialPort.setDTR(false);
}
public synchronized boolean read_sda()
{
return serialPort.isCTS();
}
public synchronized boolean read_scl() // only used to test if the bus is free
{
return serialPort.isDSR();
}
}
public class I2cTerminal {
private I2cDriver i2cDriver;
private int adresseCarte;
public I2cTerminal(I2cDriver driver, int adr)
{
adresseCarte = adr;
i2cDriver = driver;
}
public void envoyerOctet(int octet) throws Exception
{
i2cDriver.write(octet,adresseCarte);
}
public int recevoirOctet() throws Exception
{
return i2cDriver.read(adresseCarte);
}
public int[] envoyerEtRecevoirOctet(int octet) throws Exception
{
return i2cDriver.writeAndReadOctet(octet, adresseCarte);
}
public void envoyerOctets(int octet[]) throws Exception
{
for (int i=0; i<octet.length; i++) octet[i] = (255-octet[i]);
i2cDriver.writeOctets(octet, adresseCarte);
}
public int[] recevoirOctets(int n) throws Exception
{
int[] result = i2cDriver.readOctets(n, adresseCarte);
return result;
}
public String getI2cDriverVersion()
{
return i2cDriver.getVersion();
}
}
Fri May 25, 2007 3:31 pm
Fri May 25, 2007 3:58 pm
Makaule wrote:btw in computer must be javacomm20-win32 if you are using windows
Fri May 25, 2007 4:05 pm
Tue Jun 12, 2007 2:32 pm
public class DS75 extends I2cTerminal {
public DS75(I2cDriver driver, int adr){
super(driver, adr);
}
public double getTemp(){
//some code
}
public static void main(String[] args){
DS75 ds75 = new DS75(new I2cDriver(new I2cHardware("COM1")), 0x90);
System.out.println(ds75.getTemp());
}
}
Tue Jun 12, 2007 3:31 pm
Tue Jun 12, 2007 3:40 pm
public class I2cDriver {
/**********************************************************************/
/* ATTRIBUTES */
/**********************************************************************/
private static final int TEMPO = 0;
private I2cHardware ihw;
/**********************************************************************/
/* CONSTRUCTORS */
/**********************************************************************/
public I2cDriver(I2cHardware ihw)
{
this.ihw = ihw;
}
/**********************************************************************/
/* METHODES */
/**********************************************************************/
// monter SDA
private void sda_h()
{
ihw.sda_h();
}
private void sda_l()
{
ihw.sda_l();
}
private void scl_h()
{
ihw.scl_h();
}
private void scl_l()
{
ihw.scl_l();
}
private boolean read_sda()
{
return ihw.read_sda();
}
private boolean read_scl() // n'est utile que pour tester si le bus est libre (is useful to only test if the bus is free)
{
return ihw.read_scl();
}
private void startI2c() throws Exception
{
delay(TEMPO);
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
int countdown = 10;
while ( !read_sda() || !read_scl()) {
delay(100);
countdown--;
if (countdown == 0)
throw new Exception("Echec de la prise de controle du bus."); //Failure of the takeover of the bus.
}
delay(TEMPO);
sda_l();
delay(TEMPO);
scl_l();
delay(TEMPO);
sda_h();
}
private void stopI2c() throws Exception
{
delay(TEMPO);
sda_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
sda_h();
delay(TEMPO);
}
private void clock() throws Exception
{
delay(TEMPO);
scl_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_l();
}
private boolean write_octet(int o) throws Exception
{
int result;
scl_l();
for(int cpt=7; cpt>=0 ; cpt--)
{
result = o & (1<<cpt); // (<<cpt)?
if(result!=0)
sda_h();
else
sda_l();
clock();
}
return getAck();
}
private int read_octet()
{
int result=0;
for(int cpt=0; cpt<8 ; cpt++)
{
scl_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
result <<= 1;
if(read_sda())
result++;
}
return 255-result;
}
private boolean getAck() throws Exception
{
boolean etat = false;
scl_l();
delay(TEMPO);
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
etat = read_sda();
scl_l();
delay(TEMPO);
return !etat;
}
private void giveAck() throws Exception
{
sda_l();
clock();
sda_h();
}
private void giveNack() throws Exception
{
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_l();
delay(TEMPO);
sda_h();
}
private void delay(int nsec)
{
try
{
Thread.sleep (0,nsec);
}
catch (InterruptedException e)
{
}
}
public synchronized void init()
{
sda_h();
scl_h();
}
public synchronized int read(int add) throws Exception
{
int vec[] = readOctets(1,add);
return vec[0];
}
/**
* Cette mط·آ£ط¢آ©thode permet de lire une suite d'octets sur la carte voulue.
* <br>
* @param octets Vecteur d'octets qui stockera le rط·آ£ط¢آ©sultat.
* @param add Adresse de la carte utilisط·آ£ط¢آ©e.
* @see I2cDriver#read(int add)
* @throws I2cException Si la carte ne se reconnaط·آ£ط¢آ®t pas.
*/
public synchronized int[] readOctets(int n, int add) throws Exception
{
int octets[]= new int[n];
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
// Bit 0 = 1 (LECTURE)
add |=1;
if (!write_octet(add))
{
stopI2c();
throw new Exception("readOctets : address error while reading.");
}
octets[0]=read_octet();
for(int cpt=1; cpt<octets.length; cpt++)
{
giveAck();
octets[cpt]=read_octet();
}
giveNack();
stopI2c();
ok = true;
}catch(Exception e){}
}
return octets;
}
public synchronized void write(int valeur, int add) throws Exception
{
int vec[] = new int[1];
vec[0]=valeur;
writeOctets(vec, add);
}
public synchronized void writeOctetsSansStop(int[] octets, int add) throws Exception
{
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
//Bit 0 = 0 (ECRITURE)
add &= 0xfe;
if (!write_octet(add))
{
stopI2c();
throw new Exception("WriteOctets : address error while writing.");
}
for(int cpt=0; cpt<octets.length; cpt++)
{
if (!write_octet(octets[cpt]))
{
stopI2c();
throw new Exception("WriteOctets : write error.");
}
}
// stopI2c();
ok = true;
}catch(Exception e){}
}
}
public synchronized int[] writeAndReadOctet(int octet, int add) throws
Exception
{
int vect[] = new int[1];
vect[0] = 255-octet;
writeOctetsSansStop(vect, add);
int[] result = readOctets(1, add);
return result;
}
public synchronized void writeOctets(int[] octets, int add) throws Exception
{
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
//Bit 0 = 0 (ECRITURE)
add &= 0xfe;
if (!write_octet(add))
{
stopI2c();
throw new Exception("WriteOctets : address error while writing.");
}
for(int cpt=0; cpt<octets.length; cpt++)
{
if (!write_octet(octets[cpt]))
{
stopI2c();
throw new Exception("WriteOctets : write error.");
}
}
stopI2c();
ok = true;
}catch(Exception e){}
}
}
public String getVersion(){
return ("0.0.1");
}
}
import java.util.*;
import javax.comm.*;
/**
* La classe I2cHardware permet l'implط·آ£ط¢آ©mentation du protocole Iط·آ¢ط¢آ²C grط·آ£ط¢آ¢ce au port sط·آ£ط¢آ©rie.
* elle commande l'interface electronique entre le port sط·آ£ط¢آ©rie et le bus i2c
* elle simule l'existance d'un bus i2c sur l'ordinateur ط·آ£ l'aide du port sط·آ£ط¢آ©rie.
*
*The I2cHardware class allows the implementation of protocol Iط·آ¢ط¢آ²C thanks to the port series.
*it orders the electronic interface between the port series and the bus i2c
*it simulates the existance of a bus i2c on the computer using the port series.
*
* @author Patrick Lebط·آ£ط¢آ¨gue
* @version 1.0
*/
public class I2cHardware {
private static CommPortIdentifier portId;
private static Enumeration portList;
private SerialPort serialPort = null;
public I2cHardware(String s) throws Exception {
System.out.println("starting I2cHardware...");
portList = CommPortIdentifier.getPortIdentifiers();
portId = null;
while (portList.hasMoreElements() && serialPort == null) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("****"+ portId.getName());
if (s.indexOf(portId.getName())!=-1){
try
{
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
serialPort = (SerialPort) portId.open("Temperature", 2000);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.disableReceiveThreshold();
serialPort.disableReceiveTimeout();
}
}
catch (PortInUseException pe)
{
throw new Exception("Port " + s + " is already used by " + pe.currentOwner);
}
catch (Exception e)
{
System.out.println("initialisation of i2c Driver failed");
e.printStackTrace();
portId = null;
}
}
}//while
if (serialPort == null)
System.out.println("Port not found");
else System.out.println("driver i2c initialised on " + portId.getName());
}//constructor
/* cablage sda-out = RTS, scl-out = DTR, sda-in = CTS, scl-in = DSR */
// SDA up
public synchronized void sda_h()
{
serialPort.setRTS(true);
}
// SDA down
public synchronized void sda_l()
{
serialPort.setRTS(false);
}
//SCL up
public synchronized void scl_h()
{
serialPort.setDTR(true);
}
//SCL down
public synchronized void scl_l()
{
serialPort.setDTR(false);
}
public synchronized boolean read_sda()
{
return serialPort.isCTS();
}
public synchronized boolean read_scl() // only used to test if the bus is free
{
return serialPort.isDSR();
}
}
public class I2cTerminal {
private I2cDriver i2cDriver;
private int adresseCarte;
public I2cTerminal(I2cDriver driver, int adr)
{
adresseCarte = adr;
i2cDriver = driver;
}
public void envoyerOctet(int octet) throws Exception
{
i2cDriver.write(octet,adresseCarte);
}
public int recevoirOctet() throws Exception
{
return i2cDriver.read(adresseCarte);
}
public int[] envoyerEtRecevoirOctet(int octet) throws Exception
{
return i2cDriver.writeAndReadOctet(octet, adresseCarte);
}
public void envoyerOctets(int octet[]) throws Exception
{
for (int i=0; i<octet.length; i++) octet[i] = (255-octet[i]);
i2cDriver.writeOctets(octet, adresseCarte);
}
public int[] recevoirOctets(int n) throws Exception
{
int[] result = i2cDriver.readOctets(n, adresseCarte);
return result;
}
public String getI2cDriverVersion()
{
return i2cDriver.getVersion();
}
}
Fri Jun 22, 2007 4:28 pm
public class DS75 extends I2cTerminal {
double d;
int t;
public DS75(I2cDriver driver, int adr){
super(driver, adr);
}
public double getTemp(){
int [] t = new int[2];
t = recevoirOctets(2);
t[0]=1;
//t[0]=!t[0]; // 0xFF-t[0];
//t[1];
t = envoyerEtRecevoirOctet(0);
// beginning of 9
d=t[0];
if (t[1]!=0)
d=d+0.5;
// end of 9
// beginning of 10
d = t[0];
d=d+((t[1]&0xA0)/64)*0.25;
// end of 10
// beginning of 11
d=t[0];
d=d+((t[1]&0xB0)/64)*0.125;
// end of 11
// beginning of 12
d=t[0];
d=d+((t[1]&0xC0)/64)*0.0625;
// end of 12
return d;
}
public static void main(String[] args){
try {
t= recevoirOctets(2);
} catch (Exception e) {
e.printStackTrace();
}
DS75 ds75 = new DS75(new I2cDriver(new I2cHardware("COM1")), 0x90);
System.out.println(ds75.getTemp());
}
}
Fri Jun 22, 2007 4:41 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.