PDA

View Full Version : I2C help Please



snuff28
- 27th December 2011, 08:21
I am trying to get a 16F887 to communicate with the LCD backpack from web4robot working over I2C and can not for the life of me figure it out. This is my first go at I2C and have been working on it for the past few days. Please take a look at my code and point me in the right direction. Both the data and clock pin seems to be stuck high.


'COMMANDS
'Prefix Command Parameter Description
'$FE $00 null No operation
'$FE $01 1 Byte Changing the I2C Slave Address
'$FE $02 1 Byte Changing BAUD Rate
'$FE $03 1 Byte Set Backlight Brightness
'$FE $04 1 Byte Set Contrast
'$FE $05 None Save Splash/Startup Screen
'$FE $06 None Display Firmware Version Number
'$FE $07 None Display Serial Baud Rate
'$FE $08 None Display I2C Address
'$FE $09 Null No operation
'$FE $0A None Turn ON Display
'$FE $0B None Turn Off Display
'$FE $0C 2 bytes Set Cursor Position
'$FE $0D None HOME Cursor
'$FE $0E None Turn ON Underline Cursor
'$FE $0F None Turn Off Underline Cursor
'$FE $10 None Move Cursor Left One Space
'$FE $11 None Move Cursor Right One Space
'$FE $12 None Turn ON Blinking Cursor
'$FE $13 None Turn Off Blinking Cursor
'$FE $14 None Clear Screen
'$FE $15 Variable Print String
'$FE $16 1 Byte Init Horizontal Bar Graph
'$FE $17 4 bytes Draw Horizontal Bar Graph
'$FE $18 None Init Vertical Bar Graph
'$FE $19 4 bytes Draw Vertical Bar Graph
'$FE $1A 9 bytes Load Custom Characters
'$FE $1B None READ Keypad

Include "modedefs.bas"

OSCCON = %01110001 'Int CLK 8MHz
ANSEL = 0
ANSELH = 0 'All digital
ADCON1 = 7
DEFINE OSC 8 '8MHz

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %000
CPIN var PortC.3
DPIN var PortC.4

'ADDRESS = $4C

Main:
pause 2000 ' lcd start up

i2cwritE DPIN, CPIN, $4C,[$FE, $04, 99],NOACK 'Sets contrast (0-100)
PAUSE 100
i2cwritE DPIN, CPIN, $4C,[$FE, $03, 200], NOACK 'Sets the brightness (0-250)
PAUSE 200
i2cwritE DPIN, CPIN, $4C,["Hello World!"], NOACK 'text sent to the LCD, defaults to row 0, col 0
PAUSE 1500
'Next is to make sure all of above worked
HIGH PORTD.2
PAUSE 250
LOW PORTD.2
PAUSE 250
HIGH PORTD.2
PAUSE 250
LOW PORTD.2
PAUSE 250
HIGH PORTD.2
PAUSE 250
LOW PORTD.2
PAUSE 250
HIGH PORTD.2
PAUSE 250
LOW PORTD.2
goto main

NOACK:
HIGH PORTD.2
PAUSE 5000
LOW PORTD.2
PAUSE 1000
end

LinkMTech
- 27th December 2011, 15:07
I can't see why you don't get at least something out of the data and clock pins, these normally idle High anyways. Watch it with a scope if available.
Does your program go to NOACK every time? The "END" after it will cause it to stop right there. Put a GOTO Main instead.

I would also structure your program something like this:


Configs
pause 2000 ' lcd start up
i2cwritE DPIN, CPIN, $4C,[$FE, $04, 99],NOACK 'Sets contrast (0-100)
i2cwritE DPIN, CPIN, $4C,[$FE, $03, 200], NOACK 'Sets the brightness (0-250)

Main:
I2CWRITE DPIN, CPIN, $4C, [$FE,$14], NOACK ' Clears screen
i2cwritE DPIN, CPIN, $4C,["Hello World!"], NOACK 'text sent to the LCD, defaults to row 0, col 0
PAUSE 1500 'Next is to make sure all of above worked
HIGH PORTD.2 PAUSE 250
LOW PORTD.2 PAUSE 250
HIGH PORTD.2 PAUSE 250
LOW PORTD.2 PAUSE 250
HIGH PORTD.2 PAUSE 250
LOW PORTD.2 PAUSE 250
HIGH PORTD.2 PAUSE 250
LOW PORTD.2 goto main

NOACK:
HIGH PORTD.2
PAUSE 5000
LOW PORTD.2
PAUSE 1000
GOTO Main ' Start over

The LCD contrast and brightness only needs to be set up once before the Main loop unless you want to get fancy but save that till later.
Verify the Address $4C is correct otherwise all your attempts to write to the LCD will be ignored.

LinkMTech
- 27th December 2011, 16:49
Just one more thing, as Columbo would say.
Here's some scope shots I documented that might help.
6201

snuff28
- 27th December 2011, 17:46
The program does not go to NOACK at all, thats why i put the END in there to stop the program just to make sure. I can unplug the the I2C lcd controller and the led still blinks all the time. I do not have a scope. As far as the contrast and brightness, I just threw them in there but thanks for pointing it out. SCL and SDA lines are pulled high with a 4.7K resistor.

LinkMTech
- 27th December 2011, 20:51
Ahhh! I just checked out one of my programs and noticed that all the data had to be sent by their ASCII value.
Instead of:


I2CWRITE DPIN, CPIN, $4C, ["Hello"], NOACK

Send it:


I2CWRITE DPIN, CPIN, $4C, [$48,$65,$6C,$6C,$6F], NOACK ' Using HEX values

or

I2CWRITE DPIN, CPIN, $4C, [72,101,108,108,111], NOACK ' Using DEC values


My contrast and brightness values were also set a lot lower: Contrast= $1C or 28 and Brightness= $6 on a Newhaven serial display.

snuff28
- 29th December 2011, 15:49
I finally got the thing working!!! I realized that the address I was using $4C was only 7bits long, tried $98 for the address and it started working. Been working on this for about 5 days now, guess the important thing is that its working and I can now start my project as everything revolves around this lcd controller.

Thanks LinkMTech for all your input!!!!



SDA var PORTC.4
SCL var PORTC.3

I2CWRITE SDA,SCL,$98, ["HELLO"]

LinkMTech
- 29th December 2011, 16:12
Awesome!
Best of luck on the rest of your project!