Source Code File:"i2cbaslib.bas"

'********************************************************
'*                                                      *
'*       I2C LIBRARY FOR USE WITH BOOST BASIC           *
'*                                                      *
'*      WRITTEN BY      :  PUNERJOT SINGH MANGAT        *
'*      COUNTRY         :  INDIA                        *
'*      CITY            :  PATIALA                      *
'*      WEB PAGE        :  www.rackeys.com/punerjot     *
'*      EMAIL           :  punerjot@rackeys.com         *
'*      SOURCEBOOST ID  :  c58_4311                     *
'*      RELEASE DATE    :  March 03, 2006               *
'*      REVISED DATE    :  May 24, 2006                 *
'*                                                      *
'********************************************************
'
'
'These are the software routines for connecting the pic16f877a to the
'24c64 eeprom. The eeprom is connected to pin(4) and pin(5) of portc of the pic
'SDA of i2c to pin(5) of portc and SCL of i2c to pin(4) of portc
'the SDA as well as SCL pin is pulled high via 10K resistor. 4.7 K can also be used
'
'Note: any pic can be used with these routines, but the port and pn numbers have to be redefined
'for using any other pic or any pin, replace all occurences of:
'portc.4 with portX.pin_no (where X is the port of the pic and pin_no is the pin which is connected to the SCL of I2C
'trisc.4 with trisX.pin_no (where X is the port of the pic and pin_no is the pin which is connected to the SCL of I2C
'portc.5 with portX.pin_no (where X is the port of the pic and pin_no is the pin which is connected to the SDA of I2C
'trisc.5 with trisX.pin_no (where X is the port of the pic and pin_no is the pin which is connected to the SCL of I2C
'
'
'
'commands are:
'
' call i2c_init()
' call i2c_write(address as word, data as byte)
' call i2c_read_random(address as word)as byte
' call i2c_read_current() as byte
' call i2c_start_seq_read(address as word)
' call i2c_end sub_seq_read() as byte
' call i2c_readbyte() as byte
' call i2c_ack()
' call i2c_nack()
' pin RC5 = SDA (imput and output)
' pin RC4 = SCL (output)





'****************************** eeprom routines starts ************************

'******************** procedure to make SCL pin High starts ***********************
sub set_scl()
portc.4 = 1
end sub
'******************** procedure to make SCL pin High ends ***********************



'******************** procedure to make SCL pin Low starts ***********************
sub clr_scl()
portc.4 = 0
end sub
'******************** procedure to make SCL pin Low Ends *************************



'******************** procedure to make SDA pin High starts ***********************
sub set_sda()
trisc.5  = 1
trisc.4  = 0
portc.5  = 1
end sub
'********************* procedure to make SDA pin High ends *************************



'******************** procedure to make SDA pin Low starts ***********************
sub clr_sda()
trisc.5  = 0
trisc.4  = 0
portc.5  = 0
end sub
'********************** procedure to make SDA pin Low Ends ***********************



'******************** procedure to Initialize I2C starts ***********************
sub i2c_init()
call clr_scl()
call clr_sda()
end sub
'******************** procedure to Initialize I2C ends *************************



'******************** procedure to output a start pulse on I2c Lines starts ***********************
sub i2c_start()
call clr_scl()
call set_sda()
call set_scl()
call clr_sda()
call clr_scl()
end sub
'********************** procedure to output a start pulse on I2c Lines ends ***********************



'******************** procedure to output a stop pulse on I2c Lines starts ***********************
sub i2c_stop()
call clr_scl()
call clr_sda()
call set_scl()
call set_sda()
end sub
'********************** procedure to output a stop pulse on I2c Lines ends ***********************



'********************** procedure to output a NACK pulse on I2c Lines starts ***********************
sub i2c_nack()
call set_sda()
call set_scl()
call clr_scl()
end sub
'********************** procedure to output a NACK pulse on I2c Lines ends ***********************



'********************** procedure to output an ACK pulse on I2c Lines starts ***********************
sub i2c_ack()
call clr_sda()
call set_scl()
call clr_scl()
call set_sda()
end sub
'********************** procedure to output an ACK pulse on I2c Lines ends ***********************



'********************** procedure to send a byte to the I2C eeprom starts ***********************
sub i2c_sendbyte(sendbuf as byte)
DIM local_count as byte
local_count = 128

Do while local_count > 0
   if ((sendbuf & local_count ) = 0) then call clr_sda() else call set_sda() end if
   call set_scl()
   call clr_scl()
   local_count = local_count / 2 
loop

call set_sda()
end sub
'********************** procedure to send a byte to the I2C eeprom ends ***********************



'********************* function to read a byte from the I2C eeprom starts *********************
function i2c_readbyte() as byte
DIM byteread as byte, local_count as  byte
byteread = 0
local_count = 0
i2c_readbyte = 0

call set_sda()

for  local_count =  1 to 8
   call set_scl()
   trisc.5  = 1
   trisc.4  = 0
   byteread = (byteread << 1) | portc.5
   call clr_scl()
Next local_count

i2c_readbyte = byteread
end function
'********************* function to read a byte from the I2C eeprom ends *********************



'******* procedure to position the address pointer at begning of the specified address, starts **********
sub i2c_pos_add(add as word)
DIM addhi as byte : DIM addlo as byte
addhi = 0
addlo = 0
'Sending the address for random read  
addhi = add >> 8 
addlo = add & 0xFF  
call i2c_start()
call i2c_sendbyte(10100000b)
call i2c_ack()
call i2c_sendbyte(addhi)
call i2c_ack()
call i2c_sendbyte(addlo)
call i2c_ack()
end sub
'******* procedure to position the address pointer at begning of the specified address, ends **********





'********************** procedure to write a byte to the I2C eeprom starts ***********************
sub i2c_write(address as word, data as byte)
call i2c_pos_add(address)
call i2c_sendbyte(data)
call i2c_ack()
call i2c_stop()
call delay_ms(10)     'for writing delay for eeprom
end sub
'********************** procedure to write a byte to the I2C eeprom ends ***********************



'********** procedure to send a control byte to I2C to put it into a read mode, starts *********
sub i2c_read_ctrl_byte()
call i2c_start()                'no intermediate stop
call i2c_sendbyte(10100001b)    'here last bit is 1 for reading
call i2c_ack()
end sub
'********** procedure to send a control byte to I2C to put it into a read mode, ends *********




'************** function to read a byte from the mentioned address of I2C eeprom starts ************
function i2c_read_random(address as word) as byte
i2c_read_random = 0
call i2c_pos_add(address)
call i2c_read_ctrl_byte()
i2c_read_random = call i2c_readbyte()
call i2c_nack()
call i2c_stop()
end function
'************** function to read a byte from the mentioned address of I2C eeprom ends ***************


'************** function to read a byte from the current address of I2C eeprom starts ***************
function i2c_read_current() as byte
call i2c_read_ctrl_byte()
i2c_read_current = call i2c_readbyte()
call i2c_nack()
call i2c_stop()
end function
'************** function to read a byte from the current address of I2C eeprom ends *****************



'*********** procedure to start the sequential read process from the given address, starts ***********
sub i2c_start_seq_read(address as word)
call i2c_pos_add(address)
call i2c_read_ctrl_byte()
end sub
'************ procedure to start the sequential read process from the given address, ends ************



'*********** procedure to end the sequential read process from the given address, starts ***********
sub i2c_end_seq_read()     'reads the next unwanted byte
call i2c_readbyte()
call i2c_nack()
call i2c_stop()
end sub
'************ procedure to end the sequential read process from the given address, ends ************

http://www.sourceboost.com

Copyright © 2006 SourceBoost Technologies