In case anyone is interested here is a display driver I developed for a
project
I am working on. The code is placed in the public domain so use it anyway
you want.

Cheers !

\ LCD 16x2 Display Driver for Arduino Uno
\ Based on Hitachi HD44780 Controller
\ Written for AmForth
\ By: Craig A. Lindley
\ Last Update: 11/25/2013
\ Must have bitnames.frt and marker.frt loaded
\ Uses 4 bit data interface with the following connections
\
\     LCD Pin Number  LCD Signal Name     Arduino Pin Number Arduino Signal
Name
\          1               Gnd                   Gnd                Gnd
\          2                5V                   5V                  5V
\          3                Vl  Arm of 10K pot connected between 5V and Gnd
\          4                RS                    8              PortB Pin 0
\          5                RW  This signal connected to Gnd (no reading
possible)
\          6                EN                    9              PortB Pin 1
\          11               D4                    10             PortB Pin 2
\          12               D5                    11             PortB Pin 3
\          13               D6                    12             PortB Pin 4
\          14               D7                    13             PortB Pin 5
\          15              +LED (backlight)       3              PortD Pin 3
\          16              -LED This signal connected to Gnd

marker _LCD_

\ Port and Bit definitions

\ PORTB
&37 constant PORTB    \ Port B Data Register
&36 constant DDRB    \ Port B Data Direction Register

\ PORTD
&43 constant PORTD    \ Port D Data Register
&42 constant DDRD    \ Port D Data Direction Register

\ Control line definitions
PORTB 0 portpin: rs    \ High to set data : Low to set command/control
PORTB 1 portpin: en    \ Enable strobe is active low
PORTD 3 portpin: bl    \ Backlight control

\ Write 4 data bits
: write4Bits        ( data --- )
    $000F and
    2 lshift
    PORTB c@
    $03 and or
    PORTB c!

    \ Pulse en low - high - low
    en low
    en high
    en low
;

\ Write 8 bit data to the LCD
: doIO            ( data --- )
    $FF and        ( data --- )
    dup            ( data --- data data )
    4 rshift
    write4Bits        \ Most significant 4 bits
    write4Bits        \ Least significant 4 bits
;

\ Write data to the LCD
: writeData
    rs high
    doIO
;

\ Write command to the LCD
: writeCommand
    rs low
    doIO
;

variable displayControl

\ Clear display
: clearDisplay
    $01 writeCommand
    2 ms        \ This command takes a while
;

\ Home display
: homeDisplay
    $02 writeCommand
    2 ms        \ This command takes a while
;

\ Display On/Off
: displayMode        ( f --- )
  if
      displayControl @ $04 or displayControl !
  else
      displayControl @ $FB and displayControl !
  then
  displayControl @ writeCommand
;

\ Cursor On/Off
: cursorMode        ( f --- )
  if
      displayControl @ $02 or displayControl !
  else
      displayControl @ $FD and displayControl !
  then
  displayControl @ writeCommand
;

\ Blink On/Off
: blinkMode        ( f --- )
  if
      displayControl @ $01 or displayControl !
  else
      displayControl @ $FE and displayControl !
  then
  displayControl @ writeCommand
;

\ Set the position at which the next char will be written
: setCharPosition    ( col row --- )
    dup            ( col row --- col row row )

    \ Row must be 0 or 1
    0 2 within        ( col row row --- col row f )
    rot dup        ( col row f --- row f col col )

    \ Col must be between 0 and 15
    0 16 within        ( row f col col --- row f col f )
    rot            ( row f col f --- row col f f )
    and          ( row col f f --- row col f )
    if            ( row col f --- row col )
        swap        ( row col --- col row )
        $40 * +
        $80 or
        writeCommand

    else
        drop drop
    then
;

\ Scroll display left
: scrollLeft
    $18 writeCommand
;

\ Scroll display right
: scrollRight
    $1C writeCommand
;

\ Initialize the LCD display for 4 bit operation
: initLCD
    \ Set output bit direction
    $3F DDRB c!        \ 6 bits of PORTB to output
    $FF DDRD c!        \ 8 bits of PORTD to output

    $00 PORTB c!    \ All bits low
    $00 PORTD c!    \ All bits low

    50 ms

    \ Set the control bits for writing commands
    rs low
    en low

    1ms

    $03 write4Bits
    5 ms
    $03 write4Bits
    5 ms
    $03 write4Bits
    1ms
    $02 write4Bits    \ Select 4 bit interface
    2 ms

    $28 writeCommand    \ Set to 2 line mode
    2 ms
    $08 writeCommand    \ Display off
    2 ms
    $01 writeCommand    \ Clear display
    10 ms
    $06 writeCommand

    \ Setup content of display control
    $08 displayControl !

    \ Turn backlight on
    bl high
;

\ Type RAM based string at char position
: typeString        ( addr count --- )
    0
    do
        dup
        i + c@ writeData
    loop
    drop
;

\ Type FLASH based string at char position
\ FLASH cells usually contain 2 characters
variable addr
variable count
variable count2
variable flag

: iTypeString        ( addr count --- )
    count ! addr !
    count @ 1 rshift count2 !
    count @ 2 mod 0 <> flag !
    count2 @
    dup         ( --- count2 count2 )
    0 <>        ( count2 count2 --- count2 f )
    if            ( count2 f --- count2 )
        0
        do
            addr @ @i dup
            $FF and  writeData
            8 rshift writeData
            1 addr +!
        loop
    else
        drop
    then
    flag @
    if
        addr @ @i $FF and writeData
    then
;

\ Perform setup and display test message
: test
    initLCD        \ Initialize the LCD
    true displayMode    \ Turn display on
    5 1 setCharPosition \ Set position of next character write
    s" Hello World"    \ String compiled into FLASH
    iTypeString        \ Type the FLASH string
;

Enjoy

Craig Lindley

If you’re one in a million, there are now seven thousand people exactly
like you.
------------------------------------------------------------------------------
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing 
conversations that shape the rapidly evolving mobile landscape. Sign up now. 
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
_______________________________________________
Amforth-devel mailing list for http://amforth.sf.net/
Amforth-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/amforth-devel

Reply via email to