A serial interface for the Truly MTC-C162DPLY-2N
2 line X 16 char LCD display9600 baud RS-232 using AT90S2313 or equivalent.
Behavior of the receive and display code:
Upon the application of power, communications protocol and code date and revision letter are displayed. "9600" Refers to 9600 baud, "1" refers to 1 stop bit being required, and "N" means no parity bits are expected. The next 7 characters uniquely identify the firmware revision level.
Incoming characters are display on the LCD module on the lower of the two lines on the display, referred to here as line two. The upper line is referred to as line 1. the first 16 characters are displayed and any additional characters are not displayed. Control characters, defined as those represented by ASCII values below $1F are not displayed.
Linefeed characters cause the display to wait for the first non-control character following the linefeed before copying line 2 to line 1 (scrolling the display), clearing line 2 and positioning the cursor at the start of line 2.
The display responds to carriage return characters ($0D) by placing the cursor to the fist (leftmost) position of line two.
The display cursor is on.
Note that unused I/O pins are pulled up with the weak pull-up (direction bits set to inputs, data bits written with a logic "1") to keep unused pins from floating.
A note about how this code works
The display is two lines of 16 characters.
The maximum data rate for allowable operation is 9600 baud with 1 stop bit and no parity.
All display write operations are "open loop" timing. That is to say that delay loops are used to assure that display write operations don't proceed faster than the display can handle. Therefore, if the controller's clock rate is changed, the timing routines will have to be modified accordingly. Mr. Hosking has thoughtfully indicated the delay time expected for each routine.
Upon coming out of reset, the display is initialized and the data format and firmware revision are displayed.
Incoming characters cause an interrupt and each character is stored in an 8 character circular buffer. A 16 character buffer is used to store a copy of the line two (lower line) of the display so it can be copied to line one during scrolling. The circular buffer is necessary because scrolling of the display takes 1.8 character times at 9600 baud, and without the buffer, a character would be lost each the time display content is scrolled.
The main loop of the program, named "forever" continuously checks to see if new characters have been written to the circular received character buffer and processes new characters. Displayable characters, defined as those with ASCII codes above $1F, are written to display line 2 and to a 16 character buffer in RAM. Linefeed and carriage return are the only control characters that are recognized. Carriage returns cause the line buffer to be erased and its pointer set to the start of the buffer. Linefeeds cause a flag to be set, which will cause display scrolling when the first displayable character after the linefeed is read from the circular buffer
Display scrolling, in response to the first character read from the circular buffer after a linefeed calls the routine "linefeed". Linefeed scrolls the display upward one line, leaving line two clear with the cursor positioned at the start of the line. In scrolling the display linefeed copies the line buffer, which contains a copy of the contents of line two of the display to line one, resets the line buffer by clearing it and setting the pointer to the start, then it clears line two and sets the cursor to the start of line two.
The second one worked the same as the first. This was not a surprise, but it was reassuring.
2 line X 16 char LCD display9600 baud RS-232 using AT90S2313 or equivalent.
DOWNLOADS
AT90S2313 AVRStudio assembler source, LDCbutons040904Ca.htm
Truly LCD driver include file, 2x16lcdinc.htm
tlcd309Ba.htm AT90S8515/ATMega8515 AVRStudio assembler source by Rafael A. Alcocer.
Original version of posted AT90S2313 code in zip archive tlcd030929A.zip
The 040904C version of the code has a larger UART receive buffer and supports two pushbuttons that send either and ASCII "R" ($52) or an ASCII carriage return ($0D) via the UART when pressed.
Note: A potential bug that prevents the code from working on AVR controllers that support 16 bit addressing(specifically, the AT90S2313 only has an 8 bit stack pointer, while some others have 16 bit stack pointers) found by Rafael A. Alcocer has been corrected in version AT90S2313 version 030929A as well as being incorporated into the AT90S8515 version. .
The Truly MTC-C162DPLY-2N-LCD is an inexpensive two line LCD display and seems to be ideal as a display for some of my projects that have serial data outputs. Before building this interface, I used to either have a computer on the workbench to serve as a terminal or run an RS-232 cable from the desk with the computer on it to the workbench. When I saw the backlit version of displays being offered for less than US$6.00, I thought "Why not?".
The Truly display uses the Samsung KS0070B controller, which is (reportedly) compatible with the Hitachi HD44780 controller. I found a set of initialization and control routines that were written and placed on the on the web by Richard Hosking which only required a minor adjustment to get the Truly display working. Mr. Hosking's code was used in the assembler code that was originally posted and is in the ATMega8515 version. Mr. Hosking's code served as a model for the .include file driver that is used in the current AT90S2313 version.
The main minor adjustment I mentioned in the paragraph above was the addition of a second "Function Set" command that was needed to get the Truly displays to work. Mr. Peter Coppens has reported that he had to remove the second "Function Set" operation in the initialization routine in the Truly LCD Include file. This is noted in the include file by labeling the two lines with the comment "(OMIT FOR HITACHI)". the affected code fragment is shown immediately below (Some white space removed for to better fit on this page)..ldi temp,0b00101000 ;Function set 4 bit mode, 2 lines 5X7 pixels. rcall SendCommand ;Write to display -first write sets 4 bit mode. ldi temp,0b00101000 ;Function set 4 bit mode, 2 lines 5X7 pixels (OMIT FOR HITACHI) rcall SendCommand ;write to display -second write to set N and F (OMIT FOR HITACHI)
With this arrangement, the Truly display has a 4 bit parallel interface. The AT90S2313 controller converts incoming serial data to the appropriate 4 bit parallel commands and data. Incoming characters are written to the bottom line of the display and when a displayable character, that is a character that is not a control character, is received after a linefeed is received, the bottom line is copied to the top line, leaving a blank line on the bottom with the cursor set to the first column of the bottom line.
This delayed implementation of the linefeed until after a displayable character is received keeps the display from scrolling at the end of a line until there is something to display on the next line, thus two lines of information are always displayed, even when using it with hardware that automatically sends a carriage return and linefeed at the end of each line. This is important when you only have two lines on the display
It should be noted that the LCD display interface code is copyrighted by Mr. Hoskin and on his web page, we are admonished to use the code for personal use only. The rest of the code is hereby placed in the public domain.
When using the circuit (below) with 0 to 5V level serial connections, doide D1 and capacitor C1 may be omitted and the lower end of R1 connected directly to ground (see the schematic, below).
Upon the application of power, communications protocol and code date and revision letter are displayed. "9600" Refers to 9600 baud, "1" refers to 1 stop bit being required, and "N" means no parity bits are expected. The next 7 characters uniquely identify the firmware revision level.
Incoming characters are display on the LCD module on the lower of the two lines on the display, referred to here as line two. The upper line is referred to as line 1. the first 16 characters are displayed and any additional characters are not displayed. Control characters, defined as those represented by ASCII values below $1F are not displayed.
Linefeed characters cause the display to wait for the first non-control character following the linefeed before copying line 2 to line 1 (scrolling the display), clearing line 2 and positioning the cursor at the start of line 2.
The display responds to carriage return characters ($0D) by placing the cursor to the fist (leftmost) position of line two.
The display cursor is on.
Note that unused I/O pins are pulled up with the weak pull-up (direction bits set to inputs, data bits written with a logic "1") to keep unused pins from floating.
A note about how this code works
The display is two lines of 16 characters.
The maximum data rate for allowable operation is 9600 baud with 1 stop bit and no parity.
All display write operations are "open loop" timing. That is to say that delay loops are used to assure that display write operations don't proceed faster than the display can handle. Therefore, if the controller's clock rate is changed, the timing routines will have to be modified accordingly. Mr. Hosking has thoughtfully indicated the delay time expected for each routine.
Upon coming out of reset, the display is initialized and the data format and firmware revision are displayed.
Incoming characters cause an interrupt and each character is stored in an 8 character circular buffer. A 16 character buffer is used to store a copy of the line two (lower line) of the display so it can be copied to line one during scrolling. The circular buffer is necessary because scrolling of the display takes 1.8 character times at 9600 baud, and without the buffer, a character would be lost each the time display content is scrolled.
The main loop of the program, named "forever" continuously checks to see if new characters have been written to the circular received character buffer and processes new characters. Displayable characters, defined as those with ASCII codes above $1F, are written to display line 2 and to a 16 character buffer in RAM. Linefeed and carriage return are the only control characters that are recognized. Carriage returns cause the line buffer to be erased and its pointer set to the start of the buffer. Linefeeds cause a flag to be set, which will cause display scrolling when the first displayable character after the linefeed is read from the circular buffer
Display scrolling, in response to the first character read from the circular buffer after a linefeed calls the routine "linefeed". Linefeed scrolls the display upward one line, leaving line two clear with the cursor positioned at the start of the line. In scrolling the display linefeed copies the line buffer, which contains a copy of the contents of line two of the display to line one, resets the line buffer by clearing it and setting the pointer to the start, then it clears line two and sets the cursor to the start of line two.
The second one worked the same as the first. This was not a surprise, but it was reassuring.
No comments:
Post a Comment