LCD Module Confusion

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Mark_R
Posts: 18
Joined: Tue Nov 03, 2009 6:56 pm
Location: USA

LCD Module Confusion

Post by Mark_R » Sat Nov 21, 2009 11:39 pm

Hi,

For hours I've been trying to understand the workings of the LCD module. I get most of it but a few key things are baffling me. I've read the documentation and help, searched the forum but can't find the answer.
Consider the following section of the module:

Code: Select all

****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a single byte to the LCD                                 *
****************************************************************************
}
Sub WriteItem(pData As Byte)
   #ifdef _RW_USED
   WaitFor
   #endif
   
   RS = 1
   #ifdef _LCD_INTERFACE_08
   SetData(pData)
   #else
      #ifdef _LCD_UPPER 
      SetData(pData)
      SetData(pData << 4)
      #else
      SetData(pData >> 4)
      SetData(pData)
      #endif
   #endif   
   
   #ifndef _RW_USED
   DelayUS(DataUS) 
   #endif
   ClrWDT
End Sub
I understand the basic concept; write the byte data to the port and strobe the EN pin to write it to the LCD. Pretty straightforward.

But I'm totally lost on this one;

Code: Select all

{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a string to the LCD                                      *
****************************************************************************
}
Sub WriteItem(pText As String)
   Dim TextPtr As POSTINC0
   Dim Text As INDF0
   FSR0 = AddressOf(pText)
   While Text <> 0
      WriteItem(TextPtr)
   Wend
End Sub
{
I see that the special function register "POSTINC0" ($0FEE) is being used to step through the string, but don't understand the details at all. Is the routine to convert an ASCII character built into the PIC?

Could someone please explain how this works?

Thanks in advance.

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Nov 22, 2009 7:24 am

This is an overloaded function that will process a string when passed to WriteItem. It uses the POSTINC0 and INDF0 registers to access the string in memory indirectly - you must set the FSR0 pointer to the start address of the string, then step through the string with the while loop. The loop tests if the next item in memory (obtained using INDF0) is not 0 (end of string), and if not, sends the next character in the string to another overloaded version of WriteItem. This time POSTINC0 is used to fetch the character from memory - this also has the function of moving the pointer on one after the value has been fetched (hence POST INC). Text and TextPtr are just aliases to the two registers. Once you have got to grips with using the indirect addressing registers they are very useful for quickly iterating through strings or arrays.

Mark_R
Posts: 18
Joined: Tue Nov 03, 2009 6:56 pm
Location: USA

Post by Mark_R » Sun Nov 22, 2009 3:08 pm

Hi Steven,

Thanks, I think I understand that, what's confusing me is where is the ASCII character being converted to binary? ie: "M" = 01001101 or $04D? I was expecting some sort of look up table.
Maybe I'm over complicating this, is the conversion native to the language? In other words, any time a a string is passed to a sub, it's automatically converted and placed into memory without us having to do anything? We just have to go read it back using the associated SFR?

I have a feeling that I've missed a rudimentary concept along the way.

Thanks.

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Nov 22, 2009 4:00 pm

A string is stored as a series of bytes in ascii, so no conversion is needed. As far as the PIC is concerned, it is never a letter, it's just the ascii code for the letter, which the LCD also recognises. If you ask the compiler to print a string, when it is compiled, it will be stored in program code as a series of bytes in ascii also.

Mark_R
Posts: 18
Joined: Tue Nov 03, 2009 6:56 pm
Location: USA

Post by Mark_R » Sun Nov 22, 2009 6:35 pm

Steven wrote:A string is stored as a series of bytes in ascii, so no conversion is needed. As far as the PIC is concerned, it is never a letter, it's just the ascii code for the letter, which the LCD also recognises. If you ask the compiler to print a string, when it is compiled, it will be stored in program code as a series of bytes in ascii also.
OK, Simple enough.

Considering the line;
FSR0 = AddressOf(pText)

Am I understanding correctly that the input passed to the sub (pText) could end up anywhere at run time, so the above line tells us its starting memory location on the fly? And furthermore, we are stetting FSR0 to that address?

As a side note, while I'm asking obvious questions; In all the sample code / library's, I see that the value being passed to a sub is always represented (local to the sub) with an identifier beginning with lower case "p" (pData as byte, pKey as longword, etc.) Is that mandatory of just convention of the author?

Thanks for the help.

Post Reply