Proton BASIC Compiler - Using PDS with SPI GLCD based on ST7565R Controller

  • Pic® Basic

  • Using PDS with SPI GLCD based on ST7565R Controller

    Graphic LCDs based on the ST7565 are cheaper then GLCDs with other controllers. SPI requires only four pins. If the circuit uses other SPI peripherals, adding an LCD requires only two more I/O’s. It’s simple to code functions that will control an ST7565 based GLCD but that would mean giving up compatibility with PDS GLCD library commands. The idea is to use PDS GLCD commands by redirecting PDS GLCD library commands to subroutines that can drive the ST7565R controller.

    In most SPI LCD’s, the SPI is MOSI only. It’s not possible to read the LCD RAM. To stay on track with the display memory, especially for a set/clear pixel function, the code includes a byte array with 1024 elements which mirrors the LCD RAM. Writing data to the LCD also update the array. Though it takes a lot of RAM on the PIC, it can be useful for refreshing the display, i.e., occasionally resetting it, or for fast uploading of bitmaps. With GLCD it’s all about the custom fonts. The code handles two types of fonts:
    1. Internal font: using the file with fixed size (8x6), single table for ASCII character values 0 – 127. For speed and simplicity, the internal fonts are handled by a dedicated routine based on the single table in and fixed font size. Selection of the internal font is done via macro Glcd_IntFont(Invert), if Invert is 0, the characters are normal black on white and if 1, the characters are white on black.
    2. Custom fonts: Font tables are created by FontConverter (for PDS) and must have specific structure: Header Cdata table containing pointers to the actual font table The first element in the header table is the font height in pixels. Each character has a dedicated Cdata table with the number of columns for the specific character in the first element, followed by the bitmap bytes for the character. Custom FontConverter fonts are processed by separate routine and selected by a Macro that I hacked from Les: Glcd_SetFont(TableName,ASCII-OFFSET,Invert). TableName is the name of FontConverter's header table, ASCII-OFFSET is 32 For a full table _0 to _95. For modified tables i.e., only numbers it is possible to use other offset values.
    When changing to a different font, a font selection macro must be executed. For example to use a custom font and then the internal font:
    Glcd_SetFont(ARIAL_BOLD11,0x20,1) ' select custom font and invert   
    Print At 0,30, "ABC"
    Glcd_IntFont(0)    ' back to internal font, normal
    Print At 7,0, "XYZ"
    Graphic commands:
    the arguments format for PDS library commands Plot, UnPlot, Line, Circle, Box and LCDWrite is as per the PDS manual. The Line function is a Bresenham's algorithm and the Circle is a Midpoint Circle algorithm both were “translated” from pseudo or C code on Wikipedia.The Box command is using the DrawRectangle sub to draw a square.

    Other functions:
    • GoSub DrawRectangle[Set_clear,x0,y0,width,hight] can be used directly to draw rectangles
    • GoSub FillRectangle[set_clear,x0,y0,width,hight] can be used to fill rectangle shaped areas. This is done in a very simplistic way by first loading all the pixels to the memory mirror array and then dumping the entire array to the display
    • GoSub BlankDisplay clears the display but not the memory mirror array
    • GoSub GlcdBuffDump restores the display to the contents of the memory image array
    • GoSub OffRecordPrint[charbyte] prints a character without updating the memory mirror array
    • GoSub GlcdScroll[LineNum] takes values in the range 0-XRES (0-63 for a 128x64 display) to scroll up. Invert the value of LineNum to scroll down.
    • GoSub BuffSetPixel[y,x] sets or clears pixels in the memory image array. Note this function also requires that pix_byte will be assigned a value of 1=set or 0=clear. This is usefull when large bitmaps are needed. The bitmap can be quickly created in the array and then the lcdBuffDump can be used to dump the entire array to the display.
    When PDS Print command sends the same character again, it does not update the WREG. The Print Redirect routine will most likely use the WREG and the second character will not print. For example: Print “999”, will only print 9. Therefore, before returning from the PrintRedirect routine, recover the WREG by adding:
    @Movf strbyte, W
    The code does not include any mechanism to verify the arguments are in correct range. I used PIC18F25K20 and later moved to PIC46K22 (tons of memory) to develop the code. However, this code has not been tested on a 16F device.

    Sharing this code exposed it to other applications which also led to finding more problems. Recently, Less took a look at the code and made an important modification that made it possible to display floats. There potentially other issues with this code which at this point I am not planning to address. For more info see:

    An updated demo '' ncluding Less's fix for displaying float numbers, is attached. Before compiling, open ST7565_DEF.Inc and comment/uncomment to select Preprocessor and HW Interface defines for the LCD used in your hardware.