Sample code for I2C text LCD needed


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    68

    Default Sample code for I2C text LCD needed

    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

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Sample code for I2C text LCD needed

    Looks like you forgot the defines. Sorry I am not in a position to guide with that, but look at the manual.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Aug 2011
    Location
    Virginia
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Sample code for I2C text LCD needed

    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

  4. #4
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    68


    Did you find this post helpful? Yes | No

    Default Re: Sample code for I2C text LCD needed

    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

  5. #5
    Join Date
    Aug 2011
    Location
    Virginia
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Sample code for I2C text LCD needed

    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

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts