Author: robhamerling
Date: Fri Oct 3 02:39:40 2008
New Revision: 343
Modified:
trunk/unvalidated/include/external/lcd/lcd_hd44780_4.jal
Log:
Removed hd44780_xxx procedures, renamed some procedure, fixed set_cursor()
for 3 and 4 line LCDs, added port+pin direction settings to lcd_setup(),
indents 2->3 positions, updated comments
Modified: trunk/unvalidated/include/external/lcd/lcd_hd44780_4.jal
==============================================================================
--- trunk/unvalidated/include/external/lcd/lcd_hd44780_4.jal (original)
+++ trunk/unvalidated/include/external/lcd/lcd_hd44780_4.jal Fri Oct 3
02:39:40 2008
@@ -13,56 +13,56 @@
-- Screens can range from 2x8 (2 lines, 8 chars) to 4x20.
-- Uses 4 bit wide datapath + 2 handshake lines (total 6 PIC
pins).
-- Expects: - 2 pins for handshake: 'lcd_rs' and 'lcd_en'
--- - a port nibble for data: 'data_port'
+-- - 1 port nibble for data: 'lcd_dataport'
--
-- Dependencies: delay_any_mc.jal
--
-- Code example:
--
--- The application program must declare the following variables (aliases):
--- var byte lcd_data_port is portA_low -- LCD data
--- var byte lcd_data_port_direction is portA_low_direction
+-- The application program must declare the following variables (aliases)
+-- _before_ the include of this library!
+--
+-- var byte lcd_dataport is portA_low -- LCD data
+-- var byte lcd_dataport_direction is portA_low_direction
-- var bit lcd_en is pin_A4 -- LCD data
trigger
-- var bit lcd_en_direction is pin_A4_direction
-- var bit lcd_rs is pin_A5 -- LCD cmd/data
select
--- var bit lcd_rd_direction is pin_A5_direction
---
--- (This is an example: The lower nibble of portA is used for data,
--- pins 4 and 5 of portA are used for handshake. Any other available
dataport
--- or handshake pins may be used, provided these are not input only).
+-- var bit lcd_rs_direction is pin_A5_direction
--
--- and initialize the port and pin directions:
---
--- lcd_dataport_direction = all_output -- make datapath output
--- lcd_en_direction = output -- )
--- lcd_rs_direction = output -- ) make handshake lines
output
---
--- _before_ the include of this library!
+-- Above is an example: Bits 0..3 of portA are used for data, pins 4 and 5
of
+-- portA are used for handshake. Any other available nibble and handshake
pins
+-- could be used (provided these are configurable for output).
--
-- Available functions for application programs:
--- lcd_setup()
--- lcd_clearscreen()
--- lcd_setcursor(<row>,<column>)
--- lcd_wrtchar(<byte>)
+-- lcd_setup() (called automatically)
+-- lcd_clearscreen() clear screen, cursor
home
+-- lcd_setcursor(<row>,<column>) position cursor
+-- lcd_writechar(<byte>) write single character
+-- at cursor position and
+-- shift cursor position 1
right
+-- A pseudo variable 'lcd' is available as alternative for
lcd_writechar(<byte>):
+-- var byte lcd
+-- So in stead of writing: lcd_writechar(<byte>)
+-- you may write: lcd = <byte>
--
--
-----------------------------------------------------------------------------
-include delay_any_mc -- delay procedures
+include delay_any_mc -- delay procedures
-- -----------------------------------------------------
-- Write nibble (lower order 4 bits of byte) to LCD.
--- Nibble is sent to data_port.
--- RS not touched: function can be used for commands and data
--- Required wait after write expected to be done by calling function,
--- in 4-bit mode only required after second nibble.
+-- Nibble is sent to dataport.
+-- lcd_rs is not touched: function can be used for commands and data.
+-- When wait is required after write then caller should wait
+-- (in 4-bit mode only required after second nibble).
-- -----------------------------------------------------
-procedure _lcd_wrtnibble(byte in data) is
-
- lcd_data_port = data -- replace low nibble
- LCD_EN = high -- trigger on
- asm nop -- wait (> 400 ns)
- LCD_EN = low -- trigger off
+procedure _lcd_writenibble(byte in data) is
+
+ lcd_dataport = data -- replace low nibble
+ lcd_en = HIGH -- trigger on
+ asm nop -- wait (> 400 ns)
+ lcd_en = LOW -- trigger off
end procedure
@@ -71,133 +71,98 @@
-- (and automatically set the cursor one position right)
-- ----------------------------------------------------------
-procedure lcd_wrtchar(byte in c) is
+procedure lcd_writechar(byte in c) is
- _lcd_wrtnibble(c >> 4) -- write high nibble
- _lcd_wrtnibble(c) -- write low nibble
- delay_10us(4) -- > 37 us
+ _lcd_writenibble(c >> 4) -- write high nibble
+ _lcd_writenibble(c) -- write low nibble
+ delay_10us(4) -- > 37 us
end procedure
--- -------------------------------------------------
--- Send byte to the LCD as 2 nibbles, compatible with oldskool JAL
--- ----------------------------------------------------------
-
-procedure hd44780'put(byte in c) is
-
- _lcd_wrtnibble(c >> 4) -- write high nibble
- _lcd_wrtnibble(c) -- write low nibble
- delay_10us(4) -- > 37 us
-
-end procedure
--- -------------------------------------------------
--- Send byte to the LCD as 2 nibbles,
+-- ----------------------------------------------------------
+-- Pseudo variable 'lcd' as alternative for lcd_writechar(<byte>)
-- ----------------------------------------------------------
procedure lcd'put(byte in c) is
-
- _lcd_wrtnibble(c >> 4) -- write high nibble
- _lcd_wrtnibble(c) -- write low nibble
- delay_10us(4) -- > 37 us
-
+ lcd_writechar(c)
end procedure
-- ------------------------------------------------------------
--- Clear screen LCD
+-- Clear screen
-- (and set the cursor to the upper left corner: row 0, column 0)
-- ------------------------------------------------------------
procedure lcd_clearscreen() is
- LCD_RS = low -- control character
follows
- lcd_wrtchar(0b0000_0001) -- clear screen, cursor
home
- delay_10us(150) -- 1.5 ms
- LCD_RS = high -- switch back to data
chars
+ lcd_rs = LOW -- set to control char mode
+ lcd_writechar(0b0000_0001) -- clear screen, cursor
home
+ delay_1ms(2) -- > 1.5 ms
+ lcd_rs = HIGH -- set to data char mode
end procedure
--- Note about cursor positions: the LCD is a 2x40 char device. So a 2x16
LCD has
--- the second line beginning at 40. Sometimes, an 1x16 LCD is really a 2x8
LCD,
--- with the second line *after* the first. This would show as: "Hello
Wo234 "
--- where 234 is the counter.
---
+
+-- About cursor positions: the LCDs are internally 2x40 char devices.
+-- The first line starts at offset 0, the second line at offset 64 (0x40).
+-- With a 4x16 or 4x20 LCD the third line starts at offset 20 and the
+-- fourth line at offset 84 (0x40 + 20).
+-- Note: Some 1x16 LCDs are implemented as 2x8 line LCDs, which means that
+-- the second half of the line has to be handled as the second line.
+--
-- ------------------------------------------------------------
-- Set cursor position
--- Specify row and column in base-0 notation.
+-- Specify row and column in base-0 notation (first line is 0).
-- ------------------------------------------------------------
-procedure lcd_set_cursor(byte in row, byte in col) is
+procedure lcd_setcursor(byte in row, byte in col) is
- col = col | 0b1000_0000 -- add offset top line
- if (row > 0) then -- 2nd line req'd
- col = col | 0b0100_0000 -- add offset 2nd line
- end if
- LCD_RS = low -- control character
follows
- lcd_wrtchar(col) -- new cursor position
- LCD_RS = high -- switch back to data
chars
+ col = col | 0b1000_0000 -- set to DRAM offset top
line
+ case row of
+ 1: col = col + 0x40 -- 2nd line of 2 or 4 line
displays
+ 2: col = col + 20 -- 3rd line \ 4x16 or
+ 3: col = col + 0x40 + 20 -- 4th line / 4x20
+ end case
+ lcd_rs = LOW -- set to control char mode
+ lcd_writechar(col) -- set new cursor position
+ lcd_rs = HIGH -- set to data char mode
end procedure
--- ----------------------------------------------------------
--- Oldskool line positions
--- ---------------------------------------------------------
-
-procedure hd44780_line0() is -- line 1 of max 4
- LCD_RS = low -- control character follows
- lcd_wrtchar(0b1000_0000) -- address zero in display
data RAM
- delay_10us(150) -- 1.5 ms
- LCD_RS = high
-end procedure
-
-procedure hd44780_line1() is -- line 2, position 40 on
4x20 display
- LCD_RS = low
- lcd_wrtchar(0b1010_1000) -- DDRAM addres = 40
- delay_10us(150)
- LCD_RS = high
-end procedure
-
-procedure hd44780_line2() is -- line 3, position 20 on
4x20 display
- LCD_RS = low
- lcd_wrtchar(0b1001_0100) -- DDRAM address = 20
- delay_10us(150)
- LCD_RS = high
-end procedure
-
-procedure hd44780_line3() is -- line 4, position 60 on
4x20 display
- LCD_RS = low
- lcd_wrtchar(0b1101_0100) -- DDRAM address = 60
- delay_10us(150)
- LCD_RS = high
-end procedure
-
-
-- -----------------------------------------------------
-- Initialize LCD
-- - global init to 4-bit data mode
--- - 2-line mode, no visible cursor
+-- - 2-line (/ 4-line) mode, shift right invisible cursor
-- -----------------------------------------------------
procedure lcd_setup() is
- LCD_RS = low -- set control mode
- delay_1ms(15) -- 15 ms power-up delay
- _lcd_wrtnibble(0b0000_0011) -- function set
- delay_1ms(5) -- > 4.1 milliseconds
- _lcd_wrtnibble(0b0000_0011) -- function set
- delay_10us(110) -- > 100 �s
- _lcd_wrtnibble(0b0000_0011) -- function set
- delay_10us(4) -- > 37 �s
- _lcd_wrtnibble(0b0000_0010) -- to 4-bit mode
- delay_10us(4) -- > 37 �s
- lcd_wrtchar(0b0010_1000) -- 4-bits, 2 lines, 5x8
font
- lcd_wrtchar(0b0001_1100) -- cursor move right
- lcd_wrtchar(0b0000_1100) -- display on,
cursor,blink off
- lcd_wrtchar(0b0000_0110) -- cursor->right, no shift
- lcd_clearscreen() -- clear screen
- -- incl switch to data mode
+-- Initialize the port and pin directions
+
+ lcd_dataport_direction = all_output -- make datapath output
+ lcd_en_direction = output -- \
+ lcd_rs_direction = output -- / make handshake lines
output
+
+-- Initialize HD44780 controller for 4-bit data mode
+
+ lcd_rs = LOW -- set to control char mode
+ delay_1ms(15) -- 15 ms power-up delay
+ _lcd_writenibble(0b0000_0011) -- function set
+ delay_1ms(5) -- > 4.1 milliseconds
+ _lcd_writenibble(0b0000_0011) -- function set
+ delay_10us(10) -- > 100 �s
+ _lcd_writenibble(0b0000_0011) -- function set
+ delay_10us(4) -- > 37 �s
+ _lcd_writenibble(0b0000_0010) -- to 4-bit mode
+ delay_10us(4) -- > 37 �s
+ lcd_writechar(0b0010_1000) -- 4-bits, 2 lines, 5x8
font
+ lcd_writechar(0b0001_1100) -- cursor move right
+ lcd_writechar(0b0000_1100) -- display on,
cursor,blink off
+ lcd_writechar(0b0000_0110) -- cursor->right, no shift
+ lcd_clearscreen() -- clear screen
+ -- (incl switch to data
char mode)
end procedure
@@ -205,7 +170,7 @@
-- actually initialize the LCD
-- -----------------------------------------------------
-lcd_setup() -- to get the LCD running
+lcd_setup() -- to get the LCD active
-- -----------------------------------------------------
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---