I2C Example | I2CSTART I2CSTOP I2CSEND I2CRECV |
' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ' Writes a pseudo-random value to a 24LC16B EEPROM and then reads the ' value back. Run program in Debug mode to view output and input values. ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- DEVICE SX28, OSCXT2, TURBO, STACKX, OPTIONX FREQ 4_000_000 ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- SDA VAR RA.0 SCL VAR RA.1 ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- SlaveID CON $A0 ' for 24LC16B Ack CON 0 Nak CON 1 ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- addrLo VAR Byte ' address in 24LC16 addrHi VAR Byte outVal VAR Byte ' to 24LC16 inVal VAR Byte ' from 24LC16 temp1 VAR Byte ' work vars temp2 VAR Byte temp3 VAR Byte temp4 VAR Byte Watch_List: WATCH outVal, 8, UHEX WATCH inVal, 8, UHEX ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Subroutine Declarations ' ------------------------------------------------------------------------- MEMOUT SUB 3 ' write value to memory MEMIN SUB 2 ' read value from memory ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: INPUT SDA ' float I2C bus pins INPUT SCL addrHi = 0 ' use just block 0 Main: FOR addrLo = 0 TO 255 ' loop through low addresses RANDOM outVal ' recreate new value MEMOUT addrLo, addrHi, outVal ' send to 24LC16 PAUSE 50 inVal = MEMIN addrLo, addrHi ' get from 24LC16 BREAK ' allow WATCH window view NEXT GOTO Main ' ------------------------------------------------------------------------- ' Subroutines Code ' ------------------------------------------------------------------------- ' Use: MEMOUT addrLo, addrHi, aVal ' -- writes 'aVal' to 24LC16 at location addrHi/addrLo MEMOUT: temp1 = __PARAM1 ' copy parameters temp2 = __PARAM2 temp3 = __PARAM3 I2CSTART SDA temp4 = temp2 & $03 ' get block value temp4 = temp4 << 1 temp4 = temp4 | SlaveID ' create control byte temp4.0 = 0 ' set RW bit for write I2CSEND SDA, temp4 ' send slave ID I2CSEND SDA, temp1 ' send word address I2CSEND SDA, temp3 ' send data byte I2CSTOP SDA ' finish RETURN ' Use: aVal = MEMIN addrLo, addrHi ' -- reads 'aVal' from 24LC16 at location addrHi/addrLo MEMIN: temp1 = __PARAM1 ' copy parameters temp2 = __PARAM2 I2CSTART SDA temp4 = temp2 & $03 ' get block value temp4 = temp4 << 1 temp4 = temp4 | SlaveID ' create control byte temp4.0 = 0 ' set RW bit for Write I2CSEND SDA, temp4 ' send slave ID I2CSEND SDA, temp1 ' send word address I2CSTART SDA ' restart for read temp4.0 = 1 ' set RW bit for Read I2CSEND SDA, temp4 ' resend slave ID I2CRECV SDA, temp4, Nak ' get one byte I2CSTOP SDA RETURN temp4
file: /Techref/parallax/sxb/sxb/i2c_ex.htm, 5KB, , updated: 2006/4/10 14:47, local time: 2025/5/21 20:53,
216.73.216.114,10-1-22-66:LOG IN
|
©2025 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.piclist.com/techref/parallax/sxb/sxb/i2c_ex.htm"> I2C Example</A> |
Did you find what you needed? |
![]() o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.