Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LCD library for 16x2

Status
Not open for further replies.

edeca

Active Member
From assembly, do most people here use an LCD library? Or something they have written themselves? I've just hooked up mine for the first time, I'll probably write some simple code to teach myself the basics but I don't want to reinvent the whole wheel for real projects.

Also, how do most people handle strings on <18F devices that lack the neat table lookup tricks? Sending each byte at a time is fine for HELLO WORLD, but not practical after that ;)
 
One method I use for printing string tables on 18F' devices allows me to store the string tables in-line with my code. It's a reasonably efficient method because it uses a single subroutine to setup the TBLPTR registers (pulling them from top of stack). This can save a lot of memory compared to other methods which setup TBLPTR registers for every single string table.

Code:
;
;  example macro usage
;
        PutStr  "Menu\r\n"      ;
        PutStr  "<1> Config\r\n"
        PutStr  "<2> Set AOS\r\n"
        PutStr  "<X> Exit menu"
Code:
;
;  the PutStr macro
;
PutStr  macro   str
        rcall   PutString       ;
        db      str,0           ; inline null terminated string table
        endm
Code:
;******************************************************************
;
;  PutString - print in-line string via Stack and TBLPTR (18F)
;
;  string must be terminated with a 00 byte and does not need
;  to be word aligned
;
PutString
        movff   TOSL,TBLPTRL    ; copy return address into TBLPTR
        movff   TOSH,TBLPTRH    ;
        clrf    TBLPTRU         ; assume PIC with < 64-KB
PutNext
        tblrd   *+              ; get in-line string character
        movf    TABLAT,W        ; last character (00)?
        bz      PutExit         ; yes, exit, else
        rcall   Put232          ; print character  [COLOR=Magenta]<-- change this to PutLCD[/COLOR]
        bra     PutNext         ; and do another
PutExit
        btfsc   TBLPTRL,0       ; odd address?
        tblrd   *+              ; yes, make it even (fix PC)
        movf    TBLPTRH,W       ; setup new return address
        movwf   TOSH            ; 
        movf    TBLPTRL,W       ;
        movwf   TOSL            ;
        return                  ;
I'm pretty new at using a character LCD on the PIC too.

I used the code above as a basis for adding an str type to my PutLCD command. I guess I'm trying to make my PutLCD commands look a little bit like a high level language.

Here are some of the things I'm doing with my home-made assembly language library so far (lots more to do, grin);
Code:
;******************************************************************
;                                                                 *
;  init 2x16 LCD using the 44780 procedure for a 4 bit interface  *
;                                                                 *
InitLCD
        DelayMS(15)             ; wait 15 msecs after power up    |B0
        PutLCD  nyb, m8bits     ; step 1, 8 bit reset (160 usec)  |B0
        DelayMS(4)              ; wait 4.1 msecs total, then      |B0
        PutLCD  nyb, m8bits     ; step 2, 8 bit reset (160 usec)  |B0
        PutLCD  nyb, m8bits     ; step 3, 8 bit reset (160 usec)  |B0
        PutLCD  nyb, m4bits     ; set 4 bit mode from 8 bit mode  |B0
        PutLCD  cmd, m4bits|m2line|fon5x7    ; "function set"     |B0
        PutLCD  cmd, DispOff    ; display, cursor, blink all off  |B0
        PutLCD  cmd, Clear      ; clear display (1.53 msecs)      |B0
        PutLCD  cmd, CursInc    ; cursor inc, shift off           |B0
        PutLCD  cmd, DispOn     ; display on, leave cursor off    |B0
        return                  ;                                 |B0

;******************************************************************
Code:
        call    InitLCD         ; initialize LCD subsystem        |B0
        PutLCD  cmd,cgram+0     ; cgram addr + 0..63              |B0
        PutLCD  dat,b'00100'    ; upload char 0 "Up Arrow"        |B0
        PutLCD  dat,b'01110'    ;                                 |B0
        PutLCD  dat,b'11111'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00000'    ;                                 |B0
        PutLCD  dat,b'00100'    ; upload char 1 "Dn Arrow"        |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'11111'    ;                                 |B0
        PutLCD  dat,b'01110'    ;                                 |B0
        PutLCD  dat,b'00100'    ;                                 |B0
        PutLCD  dat,b'00000'    ;                                 |B0
        PutLCD  cmd,line1       ;                                 |B0
        PutLCD  dat,0           ; the <up> arror char             |B0
        PutLCD  cmd,line1+3     ; ddram addr line 1, htab 3       |B0
        PutLCD  str,"K8LH Klock"
        PutLCD  cmd,line2       ;                                 |B0
        PutLCD  dat,1           ; the <dn> arrow char             |B0
        PutLCD  cmd,line2+4     ; ddram addr line 2, htab 4       |B0
        PutLCD  str,"HH:MM:SS"
Loop
        btfss   flags,0         ; clock update?                   |B0
        goto    Loop            ; no, branch, else                |B0
        bcf     flags,0         ; clear the flag                  |B0
        PutLCD  cmd,line2+4     ; ddram addr line 2, htab 4       |B0
        PutLCD  hex,hour        ; print 'HH' hour digits          |B0
        PutLCD  dat,":"         ; print ':' char                  |B0
        PutLCD  hex,mins        ; print 'MM' minute digits        |B0
        PutLCD  dat,":"         ; print ':' char                  |B0
        PutLCD  hex,secs        ; print 'SS' second digits        |B0
        goto    Loop            ; do it all over again            |B0
Have fun. Mike
 
That's pretty neat and has other applications too (e.g. RS232) as your code shows. Thanks!

I am fairly interested by neat ways to do it on the 16F devices, but I'm not sure if I'll worry about learning them at all. The 18F devices are fairly cheap and have decent hardware.. it would seem like a step back. Perhaps others would take a different view and suggest I play with them too.

I've decided to do "hello world" from scratch with a datasheet & a few examples tonight. If anybody can suggest a decent library (for 4 bit operation) it would save me writing my own.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top