I2C

Started by Tom Becker February 11, 2004
Has anyone done I2C in BasicX?

TIA Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700



Yes, I have for communication with a matrix orbital LCD. -----Original Message-----
From: [mailto:]
Sent: Wednesday, February 11, 2004 2:44 PM
To:
Subject: [BasicX] I2C Has anyone done I2C in BasicX?

TIA Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700 Yahoo! Groups Links



> ... I have...

Can you share your core code for detecting Start at 100KHz, or did you
do that in hardware? Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700


P.H. Anderson (KZ3K) provides some useful BasicX source code including I2C at
http://www.phanderson.com/basicx/

Mike

> Has anyone done I2C in BasicX?
>
> TIA > Tom >
> Tom Becker
> --... ...--
> www.RighTime.com
> The RighTime Clock Company, Inc., Cape Coral, Florida USA
> +1239 540 5700 > Yahoo! Groups Links >
>




These are the routines that I got from Peter H Anderson's site.
Attribute VB_Name = "I2C"
'------------
' This module is a collection of low level I2C routines intended to be
' included in a project using such I2C devices as the Microchip 24 series
' EEPROMs, MAX518 D/A, PCF8574 I/O Expander, DS1803 Digital Pot, PCF8591
' A/D plus D/A and many other devices.
' Public Sub I2C_out_byte(ByVal O_byte As Byte) - sends O_byte to I2C
' slave, most significant bit first.
'
' Public Sub I2C_nack() - provides high Z on 6 for one clock pulse
' allowing slave to acknowledge receipt of a byte.
'
' Public Sub I2C_in_byte(ByRef I_byte As Byte) - receives a byte from I2C
' slave.
'
' Public Sub I2C_ack() - send ack to slave by bringing 6 low for one
' clock pulse.
'
' Public Sub I2C_start() - initiates sequence by bringing 6 low while
' 5 is high. (Could be Private rather than Public).
'
' Public Sub I2C_stop() - terminates sequence by bringing 6 high
' while 5 is high. (Could be Private rather than Public).
'
' Public Sub I2C_high_6() - bring 6 high (high Z)
'
' Public Sub I2C_high_5() - bring 5 high (high Z)
'
' Public Sub I2C_low_6() - bring 6 low, hard logic zero
'
' Public Sub I2C_low_5() - bring 5 low, hard logic zero
'
' Copyright, Peter H. Anderson, Baltimore, MD, Sept, '99
'////////////////////////////////////////////////////////////////////////
Public Sub I2C_in_byte(ByRef I_byte As Byte)
DIM N as Byte, Y as Byte
I_byte = 0
For N = 1 to 8 Step 1
Call I2C_high_5() ' bring clock high
Y = GetPin(6) ' read 6
Call I2C_low_5()
I_byte = I_byte * 2 + Y ' shift left and insert Y
Next
Call ReadAck
End Sub
'///////////////////////////////////////////////////////////////////////
Public Sub I2C_out_byte(ByVal O_byte As Byte)

DIM N as Byte
For N = 1 TO 8 Step 1
If (O_byte >= 128) then ' most sig bit is a one
Call I2C_high_6()
Else
Call I2C_low_6() ' set 6
End If

Call I2C_high_5() 'Clock Pulse
Call I2C_low_5() 'Clock Pulse

O_byte = O_byte * 2 ' shift left
Next
call ReadAck()'Reads the acknowledge from the slave device.
End Sub
'///////////////////////////////////////////////////////////////////////////
//
Public Sub ReadAck() ' allows slave to acknowledge
Call I2C_high_6() ' 6 high
Call I2C_high_5() ' and then clock
'Call I2cReport() 'Reports the status of the
acknowledge condition.
Call delay(0.0)
Call I2C_low_5()
End Sub
'///////////////////////////////////////////////////////////////////////////
////
Public Sub I2cReport()

Dim ackack as Byte
ackack=GetPin(6)
If ackack=0 Then
'debug.print "Acknowledge Received"
Exit Sub
End If
'debug.print "Problem"
End Sub
'///////////////////////////////////////////////////////////////////////////
Public Sub SendAck() ' send ack to slave by bringing 6 low
Call I2C_low_6()
Call I2C_high_5()
call delay(0.0)
Call I2C_low_5()
Call I2C_high_6() ' be sure 6 is again high
End Sub
'///////////////////////////////////////////////////////////////////////////
Public Sub I2C_start() ' bring 6 low while 5 is high
'Call I2C_low_5()
Call I2C_high_6()
Call I2C_high_5()
Call I2C_low_6()
Call I2C_low_5()
End Sub
'///////////////////////////////////////////////////////////////////////////
/
Public Sub I2C_stop() ' bring 6 high while 5 is high
Call I2C_low_5()
Call I2C_low_6()
Call I2C_high_5()
Call I2C_high_6()
End Sub
'///////////////////////////////////////////////////////////////////////////
Public Sub I2C_high_6()
Call PutPin(6, 2) ' tristate
End Sub
'//////////////////////////////////////////////////////////////////////////
Public Sub I2C_high_5() ' tristate
Call PutPin(5, 2)
End Sub
'///////////////////////////////////////////////////////////////////////////
Public Sub I2C_low_6()
Call PutPin(6, 0) ' hard logic zero
End Sub
'///////////////////////////////////////////////////////////////////////////
//
Public Sub I2C_low_5()
Call PutPin(5, 0) ' hard logic zero
End Sub

-----Original Message-----
From: [mailto:]
Sent: Wednesday, February 11, 2004 4:43 PM
To:
Subject: RE: [BasicX] I2C > ... I have...

Can you share your core code for detecting Start at 100KHz, or did you
do that in hardware? Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700
Yahoo! Groups Links



Tom,

I have done some I2C stuff on the BX-24. Sometime ago, I uploaded some of
my routines and a word file on how to use them.

My web shows what I was doing, but not the code
http://webs.cedar-rapids.net/kwradio/BasicX/

I no longer have access to uploaded stuff, since I'm a privacy stuff and
don't like to register with yahoo. However, I still have it someplace on
my hard disk and could look it up if you can't find it listed.

Regards,

John, W0GN >Message: 2
> Date: Wed, 11 Feb 2004 14:44:28 -0500
> From: "Tom Becker" <>
>Subject: I2C
>
>
>Has anyone done I2C in BasicX?
>
>
>TIA
>
>
>
>Tom
>
>
>
>
>Tom Becker



> ... I have done some I2C stuff on the BX-24...

Thanks to the I2C suggestions, guys (are there any females here?).

It is apparently an SMBus implementation of I2C that requires
recognizing and acting on a Start state at 100KHz rates; that's what I
need, so I'll offload it to a PIC, I guess. Here goes my first PIC app. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700