PDA

View Full Version : I2C how to?



brucetoo
- 6th December 2006, 02:27
DOES anyone have some working examples of I2C E-prom code for 24LC32A or 24LC256A MicroChip eproms? Writing to and reading from, using word addressing? I am confused – According to - MicroChip -- 1010 is E-prom, bits 1-3 are the chip address, and bit 0 is low for write high for read.
PBP says bit 0 is for either byte or word addressing
I have tried all the examples I can find with a PIC16F628 and PBP. None seem to work.
I am going to make a data logger to read a current transformer with a PIC16F819 and a real time clock chip also I2C to see how long a pump runs and it’s current draw over several days.

skimask
- 6th December 2006, 02:42
DOES anyone have some working examples of I2C E-prom code for 24LC32A or 24LC256A MicroChip eproms? Writing to and reading from, using word addressing? I am confused – According to - MicroChip -- 1010 is E-prom, bits 1-3 are the chip address, and bit 0 is low for write high for read.
PBP says bit 0 is for either byte or word addressing
I have tried all the examples I can find with a PIC16F628 and PBP. None seem to work.
I am going to make a data logger to read a current transformer with a PIC16F819 and a real time clock chip also I2C to see how long a pump runs and it’s current draw over several days.


Do you have Google? Did you read the PicBasicPro manual?
I Google'd EEPROM and 24LC32 and 24LC256 and got over a million hits. Surely, somewhere in one of those million+ hits, somebody out there has gotten PicBasicPRo to work with one of those 2 chips. I mean after all this time, I have a tough time believing that nobody has ever thought of expanding the onchip memory with an external serial eeprom in this fashion. Surely, somebody at MeLabs would've thought that the users of PicBasicPro would deserve, dare I say DEMAND!, to have a built-in command designed just for the express purpose of easily accessing an external eeprom chip.
Or a guy could get really simple and plug an electrical clock with a mechanical movement in-line with the pump, set it at 12:00, and check it at the same time each day and reset the hands on the clock back to 12:00. But that's just me, I'm wierd that way. Not asking for much are we?

ronsimpson
- 6th December 2006, 03:42
Below are: pieces from a program
1)setup and VARs
2)LOG where I write to 24LC256 (5 words at a time) remenber to not over flow the 64 byte buffer!
3)SEND TO PC to read back from 24lc256

hope it helps
**********************
' ReadColorSensors counts the frequency value from the TCS235R
' The data is transmitted in ASCII so it
' is readable in the DEBUG window or Windows Hyperterminal.
' ALSO THE DATA IS ON THE LCD

'hardware

'microChip used 16F873
' 4k ROM
'192 RAM
'256 EEPROM

'EEPROM 24LC256
' 32K x 8 15 Bit address
' 64 byte buffer 5 mS write time i2c=400khz
' %10100000


key0 var byte 'temp var for key debounce
key1 var byte
key2 var byte
key3 var byte

DEFINE OSC 20 'xtal frequency in mhz

DEFINE HSER_RCSTA 90h 'receive enabled
DEFINE HSER_TXSTA 20h 'transmitter enabled
DEFINE HSER_BAUD 9600 'set baud

DEFINE I2C_HOLD 1
DEFINE I2C_SLOW 1

DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_BITS 8 '8 bit mode

DEFINE LCD_RSREG PORTC 'RS port
DEFINE LCD_RSBIT 2 'RS bit

DEFINE LCD_EREG PORTC 'E port
DEFINE LCD_EBIT 0 'E bit

DEFINE LCD_RWREG PORTC 'R/W port
DEFINE LCD_RWBIT 1 'R/W bit

DEFINE LCD_LINES 2 'lines

LCD_rs var portc.1

' DEFINE LCD_COMMANDUS 4000
' DEFINE LCD_DATAUS 100

ADCON1 = 7 'PIC16F87X PORTa ANALOG OFF DIGITAL ON


'osc=hs
'bod=off
'INCLUDE "MODEDEFS.BAS"



light VAR WORD[6] 'value of light sensor of TCS235R 0-5
lightX VAR WORD 'value of light sensor of TCS235R
lighty var word '
DATA_ VAR WORD 'bargraph
j VAR BYTE 'TEMP
i VAR BYTE 'TEMP
voltage VAR word
voltage1 VAR WORD
voltage2 VAR WORD
T VAR word[6] 'temp array


hour var byte ' Define hour variable
dhour var byte ' Define display hour variable
minute var byte ' Define minute variable
second var byte ' Define second variable
ticks var word ' Define pieces of seconds variable 4/61 of a second
update var byte ' Define when to update LCD
log var byte ' Define logging or not
snd var byte ' Define is sending or not
clr var byte ' do a clear?
address var word ' eeprom address 0 to 32k
address2 var word ' eeprom address 0 to 32k used for send
x var byte ' selects which light meter
z var word ' used as 0

'symbol xxxx = portA.5 'not used
symbol Freq = portA.4 'port connected to the frequency output of the TCS235R
symbol upbutton = portA.2
symbol downbutton = portA.3
symbol rightbutton = portA.1
symbol leftbutton = portA.0
symbol scl = portC.3 'used by I2C memory
symbol sda = portC.4 'used by I2C memory
'symbol xxx = portC.5 'not used yet
symbol tx = portC.6
symbol rx = portC.7

*******************************
'*****log?*****

if second=59 then 'every minute only once
if log = 1 then
LCDOUT $FE,$C0,"log" '$FE,$C0=second line
hserout [dec address/10]
for J=0 to 4
i2cwrite SDA, SCL,$a0,address, [light[J]]'send to EEPROM
hserout [" ",dec4 light[J]] LCDOUT $FE,$C0,"Time OverFlow"
endif
endif
call clrlight 'clear array light[j]
endif
****************************
'*****send to PC********

if snd=1 then 'send data from 0 to current address.
if address2 < address then
i2cread SDA, SCL,$a0,address2, [T[0],T[1],T[2],T[3],T[4]] 'get data from EEPROM
hserout [dec address2/10,$09,dec T[0],$09,dec T[1],$09,dec T[2],$09,dec T[3],$09,dec T[4], $0d]
'address,tab,(light,tab)x5,CR
address2=address2 + 10
else
snd=0
LCDOUT $FE,$C0,"done"
hserout ["End",$0d]
endif
endif
goto start
***************************