PCD8544 GLCD Code

2013-01-11
2 days ago
  • Clint Koehn
    Clint Koehn
    2013-01-11

    I am sure that this code can be improved on, but it works.

    ;=====================================================================
    ;             This program will run a PCD8544 GLCD
    ;
    ;          This requires that the chip is run at 3v
    ;
    ;  
    ;
    ;=====================================================================
    #chip 18F14K22lin,64            ;PIC18F14K22 running at 16 MHz x 4
    #config osc=irc
    OSCCON = 0b01110000         ;clock at 16MHz
    OSCTUNE = 0b01000000        ;enable the PLL
    ADCON1 = 0b00001000         ;FVR
    ADCON2 = 0b10111111         ;setup adc register
    VREFCON0 = 0b10110000       ;4.096v ref for adc readings
    ;************************ Settings for PCD8544 ************************
    ;       Sparkfun board: https://www.sparkfun.com/products/10168
    ;**********************************************************************
    #define VDDPin PORTC.5      ; 1 - power for GLCD  2 - GND
    #define SCEPin PORTC.0      ; 3 - SCE
    #define RSTPin PORTC.1      ; 4 - RST
    #define DCPin PORTC.2       ; 5 - DC
    #define CLKPin PORTB.6      ; 6 - CLK
    #define SDOPin PORTC.7      ; 7 - DN(MOSI)
    #define BKLPin PORTC.4      ; 8 - LED Backlight
    #define pxLED PORTC.6       ; an external LED
    dir VDDPin out
    dir SCEPin out
    dir RSTPin out
    dir CLKPin out
    dir DCPin out
    dir SDOPin out
    dir BKLPin out
    dir pxLED out
    ;LCD Command Sets
    ;Use in either Standard or Extended Command mode
    #define ANYUSECMD  = 0b00100000  ; 0010 0pvh - Use Standard Command Set
    #define ANYUSEEXT  = 0b00100001  ; 0010 0pvh - Use Extended Command Set
    ;Standard Command Set
    #define CMDBLANK   = 0b00001000  ; 0000 1d0e - Blank the display
    #define CMDNORMAL  = 0b00001100  ; 0000 1d0e - Set Normal Display
    #define CMDSETROW  = 0b01000000  ; 0100 0yyy - Set Line Position
    #define CMDSETCOL  = 0b10000000  ; 1xxx xxxx - Set Column Position
    ;Extended Command Set
    #define EXTTEMP    = 0b00000100  ; 0000 01tt - Set Temperature Coefficient
    #define EXTBIAS    = 0b00010000  ; 0001 0bbb - Set Bias
    #define EXTUSECMD  = 0b00100000  ; 0010 0pvh - Use Standard Command Set
    #define EXTVOP     = 0b10000000  ; 1vvv vvvv - Set Vop
    #define EEPROMCONTRAST    = 253 ; Contrast setting (Vop)
    #define EEPROMTEMPERATURE = 254 ; Temperature Compensation setting
    #define EEPROMBIAS        = 255 ; Bias setting
    ;Graphic Display Definition
    #define NUMROWS      = 6
    #define NUMCOLS      = 84
    #define MAXROWS      = NUMROWS - 1
    #define MAXCOLS      = NUMCOLS - 1
    ;----------- general variables -----------------------
    dim wnum1, wnum2, wnum3 as word
    col = 0
    row = 0
    txd = 0
    num1 = 0
    num2 = 0
    num3 = 0
    num4 = 0
    num5 = 0
    wnum1 = 0
    wnum2 = 0
    wnum3 = 0
    doublewide = 0      ;when 1 characters are doublewide
    inverttext = 0      ;when 1 characters are white on black
    ;--------- init glcd -------------------------------------
    set pxLED on
    set VDDPin on
    wait 50 us
    set RSTPin on
    wait 50 us
    set SCEPin on
    wait 50 us
    set RSTPin off
    wait 50 us
    set RSTPin on
    wait 50 us
    set SCEPin off
    wait 50 us
    set CLKPin off
    set DCPin on
    wait 50 ms
    SetContrast 47
    SetTemperature 0
    SetBias 4
    set BKLPin on
    SendCommand CMDNORMAL
    set pxLED off
    ClearDisplay
    wnum1 = 0
    SetRowCol 0, 0
    inverttext = 0
    PrintString "  GLCD Test    "
    inverttext = 0
    SetRowCol 1,0
    PrintString "PCD8544 Contrl"
    doublewide = 1
    inverttext = 0
    ;====================================================================
    main:
        SetRowCol 3,1
    
        sPrintNum wnum1,0b11111001  ;all char with 1 decimal place
        wait 100 ms
    
            wnum1 ++    ;inc every .1 seconds
        goto main
    ;====================================================================
    sub sPrintNum(in bite as byte,in cfg as byte)   ;cfg: 0bvvvvvddd
        num1 = (bite % 10) + 48                     ; v - use value,  d - decimal place
        bite = bite / 10                            ; 123, 0b00111001  = ' 12.3'
        num2 = (bite % 10) + 48                     ; 123, 0b00111010  = '1.23'
        bite = bite / 10
        num3 = (bite % 10) + 48
        if num3 = 48 then
            num3 = 32
            if cfg.1 = 1 then
                num3 = num3 + 16
            end if
        end if
        if num2 = 48 and num3 = 32 then
                num2 = 32
                if cfg.1 = 1 or cfg.0 = 1 then
                    num2 = num2 + 16
                end if
        end if
        if cfg.5 = 1 then       
            PrintChar num3              ;send to display
            if cfg.1 = 1 then
                PrintChar "."           ;send to display
            end if
        end if
        if cfg.4 = 1 then
            PrintChar num2              ;send to display
            if cfg.0 = 1 then
                PrintChar "."           ;send to display
            end if
        end if
        PrintChar num1                  ;send to display
    end sub
    sub sPrintNum(in bite as word,in cfg as byte)   ;cfg: 0bvvvvvddd
        num1 = (bite % 10) + 48                     ; v - use value,  d - decimal place
        bite = bite / 10                            ; 1234, 0b11111001  = ' 123.4'
        num2 = (bite % 10) + 48                     ; 1234, 0b01111010  = '12.34'
        bite = bite / 10
        num3 = (bite % 10) + 48
        bite = bite / 10
        num4 = (bite % 10) + 48
        bite = bite / 10
        num5 = bite + 48
        if num5 = 48 then
            num5 = 32
        end if
        if num4 = 48 and num5 = 32 then
            num4 = 32
            if cfg.2 = 1 then
                num4 = num4 + 16
            end if
        end if
        if num3 = 48 and num4 = 32 then
            num3 = 32
            if cfg.2 = 1 or cfg.1 = 1 then
                num3 = num3 + 16
            end if
        end if
        if num2 = 48 and num3 = 32 then
            num2 = 32
            if cfg.2 = 1 or cfg.1 = 1 or cfg.0 = 1 then
                num2 = num2 + 16
            end if
        end if
    
        if cfg.7 = 1 then
            PrintChar num5          ;send to display
        end if
        if cfg.6 = 1 then
            PrintChar num4          ;send to display
            if cfg.2 = 1 then
                PrintChar "."       ;send to display
            end if
        end if
        if cfg.5 = 1 then
            PrintChar num3          ;send to display
            if cfg.1 = 1 then
                PrintChar "."
            end if
        end if
        if cfg.4 = 1 then
            PrintChar num2          ;send to display
            if cfg.0 = 1 then
                PrintChar "."
            end if
        end if
        PrintChar num1              ;send to display
    end sub
    sub SetContrast(in contrast as byte)
      SendCommand ANYUSEEXT
      txd = contrast & 127 | EXTVOP
      SendCommand txd ;set contrat level
      SendCommand EXTUSECMD  
    end sub
    sub SetTemperature(in Temperature as byte)
        SendCommand ANYUSEEXT
        txd = Temperature & 3 | EXTTEMP
        SendCommand txd
        SendCommand EXTUSECMD
    end sub
    sub SetBias(in Bias as byte)
        SendCommand ANYUSEEXT
        txd = Bias & 7 | EXTBIAS
        SendCommand txd
        SendCommand EXTUSECMD
    end sub
    sub ClearDisplay
        row = 0
        col = 0
    
        SendCommand CMDSETROW
        SendCommand CMDSETCOL
    
        set pxLED on
        do
            ClearToEndOfLine
        loop while (row <> 0)
    
        set pxLED off
    end sub
    sub ClearToEndOfLine
        do
            SendData 0
            col = col + 1
        loop while (col < MAXCOLS)
        col = 0
        row = (row + 1) % NUMROWS
    end sub
    sub SetRowCol(in setrow as byte, in setcol as byte)
        row = setrow
        col = setcol
        txd = CMDSETROW or setrow
        SendCommand txd
        txd = CMDSETCOL or (setcol * 6)
        SendCommand txd
    end sub
    sub PrintString(in txt as string)   ;print a string to the display
        for cnt = 1 to txt(0)
            PrintChar txt(cnt)
        next
    end sub
    sub PrintChar(in bite as byte)
        if ((bite >= " ") and (bite <= "@")) then
            for cntr = 1 to 5
                ptr = (bite - 32) * 5 + cntr
                readtable NumSym, ptr, charbyte
                if inverttext = 1 then
                    charbyte = !charbyte
                end if
                SendData charbyte
                if doublewide = 1 then
                    SendData charbyte
                    col = col + 1
                end if
            next
            if inverttext = 1 then
                charbyte = 255
            else            
                charbyte = 0
            end if
            SendData charbyte
            if doublewide = 1 then
                SendData charbyte
                col = col +1
            end if
    
            col = col + 6
            if col > NUMCOLS then
                col = col - NUMCOLS
                row = (row + 1) % NUMROWS
            end if
    
        end if
        if (bite >= "A") and (bite <= "`") then
            for cntr = 1 to 5
                ptr = (bite - 65) * 5 + cntr
                readtable UpperAZ, ptr, charbyte
                if inverttext = 1 then
                    charbyte = !charbyte
                end if
                SendData charbyte
                if doublewide = 1 then
                    SendData charbyte
                    col = col + 1
                end if
            next
            if inverttext = 1 then
                charbyte = 255
            else            
                charbyte = 0
            end if
            SendData charbyte
            if doublewide = 1 then
                SendData charbyte
                col = col +1
            end if
    
            col = col + 6
            if col > NUMCOLS then
                col = col - NUMCOLS
                row = (row + 1) % NUMROWS
            end if
    
        end if
        if (bite >= "a") and (bite <= 127) then
            for cntr = 1 to 5
                ptr = (bite - 97) * 5 + cntr
                readtable LowerAZ, ptr, charbyte
                if inverttext = 1 then
                    charbyte = !charbyte
                end if
                SendData charbyte
                if doublewide = 1 then
                    SendData charbyte
                    col = col + 1
                end if
            next
            if inverttext = 1 then
                charbyte = 255
            else            
                charbyte = 0
            end if
            SendData charbyte
            if doublewide = 1 then
                SendData charbyte
                col = col +1
            end if
    
            col = col + 6
            if col > NUMCOLS then
                col = col - NUMCOLS
                row = (row + 1) % NUMROWS
            end if
    
        end if  
    end sub
    sub SendCommand(in txd as byte)
        set DCPin off                   ;set for command
        SendData txd
    end sub
    sub SendData(in txd as byte)
        set CLKPin off
        wait 1 us
    
        for i = 1 to 8
            if txd.7 = 0 then
                set SDOPin off
            else
                set SDOPin on
            end if
            rotate txd left ;msb first?
            wait 1 us
    
            set CLKPin on
            wait 1 us
            set CLKPin off
    
        next
    
        set DCPin on                    ;set for data by default
    end sub
    ;characters are in 5 bytes chunks
    table NumSym
    0
    0
    0
    0
    0
    0   
    0
    47
    0
    0
    0
    7
    0
    7
    0
    20
    127
    20
    127
    20
    36
    42
    127
    42
    18
    196
    200
    16
    38
    70
    54
    73
    85
    34
    80
    0
    5
    3
    0
    0
    0
    28
    34
    65
    0
    0
    65
    34
    28
    0
    20
    8
    62
    8
    20
    8
    8
    62
    8
    8
    0
    0
    80
    48
    0
    16
    16
    16
    16
    16
    0
    96
    96
    0
    0
    32
    16
    8
    4
    2
    62
    81
    73
    69
    62
    0
    66
    127
    64
    0
    66  ;2
    97
    81
    73
    70
    33
    65
    69
    75
    49
    24
    20
    18
    127
    16
    39
    69
    69
    69
    57
    60  
    74
    73
    73
    48
    1   
    113
    9
    5
    3
    54  
    73
    73
    73
    54
    6   
    73
    73
    41
    30
    0   
    54
    54
    0
    0
    0   
    86
    54
    0
    0
    8   
    20
    34
    65
    0
    20  
    20
    20
    20
    20
    0   
    65
    34
    20
    8
    2   
    1
    81
    9
    6
    50  
    73
    89
    81
    62
    end table
    table UpperAZ
    126
    17
    17
    17
    126
    127
    73
    73
    73
    54
    62
    65
    65
    65
    34
    127
    65
    65
    34
    28
    127
    73
    73
    73
    65
    127
    9
    9
    9
    1
    62
    65
    73
    73
    122
    127
    8
    8
    8
    127
    0
    65
    127
    65
    0
    32
    64
    65
    63
    1
    127
    8
    20
    34
    65
    127
    64
    64
    64
    64
    127
    2
    12
    2
    127
    127
    4
    8
    16
    127
    62
    65
    65
    65
    62
    127
    9
    9
    9
    6
    62
    65
    81
    33
    94
    127
    9
    25
    41
    70
    70
    73
    73
    73
    49
    1
    1
    127
    1
    1
    63
    64
    64
    64
    63
    31
    32
    64
    32
    31
    63
    64
    56
    64
    63
    99
    20
    8
    20
    99
    7
    8
    112
    8
    7
    97
    81
    73
    69
    67
    0
    127
    65
    65
    0
    2
    4
    8
    16
    32
    0
    65
    65
    127
    0
    4
    2
    1
    2
    4
    64
    64
    64
    64
    64
    0
    1
    2
    4
    0
    end table
    table LowerAZ
    32
    84
    84
    84
    120
    127
    72
    68
    68
    56
    56
    68
    68
    68
    32
    56
    68
    68
    72
    127
    56
    84
    84
    84
    24
    8
    126
    9
    1
    2
    12
    82
    82
    82
    62
    127
    8
    4
    4
    120
    0
    68
    125
    64
    0
    32
    64
    68
    61
    0
    127
    16
    40
    68
    0
    0
    65
    127
    64
    0
    124
    4
    24
    4
    120
    124
    8
    4
    4
    120
    56
    68
    68
    68
    56
    124
    20
    20
    20
    8
    8
    20
    20
    24
    124
    124
    8
    4
    4
    8
    72
    84
    84
    84
    32
    4
    63
    68
    64
    32
    60
    64
    64
    32
    124
    28
    32
    64
    32
    28
    60
    64
    48
    64
    60
    68
    40
    16
    40
    68
    12
    80
    80
    80
    60
    68
    100
    84
    76
    68
    0
    8
    54
    65
    0
    0
    0
    127
    0
    0
    0
    65
    54
    8
    0
    4
    2
    4
    8
    4
    28
    18
    17
    18
    28
    end table
    

    Later,
    Clint