' -----[ Title ]----------------------------------------------------------- ' ' File...... PCF8570.BSP ' Purpose... Demonstrates I2CIN and I2COUT ' Author.... Jon Williams ' E-mail.... jonwms@aol.com ' {$STAMP BS2p} ' -----[ Program Description ]--------------------------------------------- ' ' Writes to and reads from I2C RAM. Data is displayed on line 2 of a 2x16 ' LCD. ' Program requires 2x16 LCD ' - LCD.E --> Pin0 (pulled down [to ground] through 4.7K) ' - LCD.R/W --> Pin2 (or grounded for write-only operation) ' - LCD.RS --> Pin3 ' - LCD.D4 --> Pin4 ' - LCD.D5 --> Pin5 ' - LCD.D6 --> Pin6 ' - LCD.D7 --> Pin7 ' -----[ Constants ]------------------------------------------------------- ' LCDpin CON 0 ' LCD is connected to OutL I2Cpin CON 8 ' SDA on 8; SCL on 9 NoCmd CON 0 ClrLCD CON $01 ' clear the LCD CrsrHm CON $02 ' move cursor to home position CrsrLf CON $10 ' move cursor left CrsrRt CON $14 ' move cursor right DispLf CON $18 ' shift displayed chars left DispRt CON $1C ' shift displayed chars right DDRam CON $80 ' Display Data RAM control CGRam CON $40 ' Custom character RAM control Line1 CON $80 Line2 CON $C0 addr VAR Byte ' address to write to / read from rVar VAR Word ' for random number tOut VAR Byte ' test value to write to LCD tIn VAR Byte ' test value to read from LCD temp VAR Word ' temp value for numeric display width VAR Nib ' width of rt justified display pos VAR Byte ' column position for display digits VAR Nib ' number of digits to display ' -----[ EEPROM Data ]----------------------------------------------------- ' Super2 DATA %01100 ' super-script 2 DATA %00010 DATA %00100 DATA %01000 DATA %01110 DATA %00000 DATA %00000 DATA %00000 ' -----[ Initialization ]-------------------------------------------------- ' LCD_Setup: PAUSE 500 LCDCMD LCDpin,%00110000 : PAUSE 5 ' 8-bit mode LCDCMD LCDpin,%00110000 : PAUSE 0 LCDCMD LCDpin,%00110000 : PAUSE 0 LCDCMD LCDpin,%00100000 ' 4-bit mode LCDCMD LCDpin,%00101000 ' 2-line mode LCDCMD LCDpin,%00001100 ' no crsr, no blink LCDCMD LCDpin,%00000110 ' inc crsr, no disp shift ' download custom character map to LCD LCDCMD LCDpin,CGRam ' write to CGRAM FOR addr = Super2 TO (Super2 + 7) ' build custom char READ addr,temp ' get byte from EEPROM LCDOUT LCDpin,NoCmd,[temp] ' put into LCD CG RAM NEXT ' -----[ Main Code ]------------------------------------------------------- ' Main: LCDOUT LCDpin,ClrLCD,[" BSP <--> I",0,"C"] LCDOUT LCDpin,Line2, [" Communications"] PAUSE 2000 LCDCMD LCDpin,ClrLCD LCDOUT LCDpin,ClrLCD,["I",0,"2: Out="] LCDOUT LCDpin,Line2 + 10,["In="] FOR addr = 0 TO 255 ' test all addresses RANDOM rVar ' create "random" value tOut = rVar.HighByte I2COUT I2Cpin,$A0,addr,[tOut] ' write to I2C RAM PAUSE 100 I2CIN I2Cpin,$A0,addr,[tIn] ' read it back ' display results LCDOUT LCDpin,Line1 + 4,[DEC addr] temp = tOut : width = 3 : pos = Line1 + 13 GOSUB RJ_Print temp = tIn : width = 3 : pos = Line2 + 13 GOSUB RJ_Print PAUSE 350 NEXT PAUSE 1000 GOTO Main END ' -----[ Subroutines ]----------------------------------------------------- ' RJ_Print: ' right justify digits = width LOOKDOWN temp,<[0,10,100,1000,65535],digits LCDOUT LCDpin,pos,[REP " "\(width-digits),DEC temp] RETURN