PDA

View Full Version : Sample code for I2C text LCD needed



Alexey
- 12th September 2011, 06:15
Hi Guys,


I am trying to make my first LCD project with I2C LCD (NHD-C0216CiZ-FSW-FBW-3V3 from Digikey) but not sure how to approach. Does anyone have a sample? I use PIC18f46J50 like this:

ANCON0=%11111111
ANCON1=%11111111

TRISA = 0
TRISB = %0110000
TRISC = 0

start:

I2CWRITE PORTA.0,PORTA.2,0,$7C,[$7C]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[0]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$00]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$31]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$14]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$25]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$56]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$6D]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$0C]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$06]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,[$01]
PAUSE 20
I2CWRITE PORTA.0,PORTA.2,0,$7C,["TEXT"]


IF PORTB.1 =0 THEN
PORTB.1 = 1
ELSE
PORTB.1=0
ENDIF
PAUSE 1000

GOTO START

--------------
LED blinking but i do not see any output on LCD

Thank you,
Alexey

cncmachineguy
- 13th September 2011, 21:10
Looks like you forgot the defines. Sorry I am not in a position to guide with that, but look at the manual.

Toadman
- 14th September 2011, 03:26
Hi,

I would like to assist if I can. Any chance for a schematic? And I have to ask the obvious, , you did check your clock and data line to be sure they were connected to the right pin on the uC. And you have the requisite pull up reisitors (one on each line)? If you have a bus analyzer check your I2C protocol is working. Check the manual and add the error messages after each write to be sure your writes are being accepted.

Is this your first go with I2C??

Toadman

Alexey
- 14th September 2011, 03:54
Toadman, Bert,

Thank you,

Yes, you were absolutely right, defines and the pull up resistors were the problem - it was my first attempt with I2C. The corrected code and schematic now works. I learnt from manual that control byte is different for control codes and for outputting text. It still did not wirk normally (display did not want to switch into two line mode using commands from manual) but it is fixed after adding some improper code found by trying different combinations.(see below)

Thanks again!

Alexey

ANCON0=%11111111
ANCON1=%11111111
TRISA = 0
TRISB = %0110000
TRISC = 0
RTCCFG=0
SDA var PORTA.0
SCL VAR PORTA.2
I2CDevice var byte
I2CDevice = $7C 'ADDRESS FROM MANUAL
dat var byte 'CONTROL BYTE FOR DATA
cont var byte 'CONTROL BYTE FOR CONTROL CODES
dat=%01000000 'USE TO display data
cont=0 'USE TO send a command

I2CWRITE SDA,SCL,I2CDevice,cont,[$7C] 'Slave
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$00] 'Comsend
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[%0000110000] 'Function [$31]
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$14] 'Internalosc
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$25] 'contrast
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$56] 'Powercontrol
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$6D] 'Followercontrol
PAUSE 250
I2CWRITE SDA,SCL,I2CDevice,cont,[%1100] '$0C isplay on cursor off
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$06] 'Entrymode
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$C0] 'Clear
PAUSE 10
I2CWRITE SDA,SCL,I2CDevice,cont,[3,"*"] '!THIS IS SOMETHING STUPID BUT IT does switch the display into two lines mode!
PAUSE 10
I2CWRITE SDA,SCL,I2CDevice,dat,["text one"] 'print on the first line
pause 2000
I2CWRITE SDA,SCL,I2CDevice,cont,[$01] 'Clear
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,dat,["text two"] 'print on the first line again
PAUSE 2000
I2CWRITE SDA,SCL,I2CDevice,cont,[$01]
PAUSE 1
I2CWRITE SDA,SCL,I2CDevice,cont,[$C0] 'cursor in line two line2
PAUSE 1
i2CWRITE SDA,SCL,I2CDevice,dat,["Line two"]

PORTB.1 = 1
STOP

Toadman
- 15th September 2011, 02:29
Alexey,

Super! Not only did you get it working but you did a very intelligent thing, , you did your best, got some tips, and then plowed ahead and solved it yourself and learned by doing so. Most excellent, glad to help. (And I bet others reading your work learned something too!)

Toadman