Trying to use CGRAM of LCD with PIC16F877A


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Location
    Charlottesville, VA
    Posts
    31


    Did you find this post helpful? Yes | No

    Default Re: Trying to use CGRAM of LCD with PIC16F877A

    Mackrackit,

    You were right again sir... I missed that part lower down. I discovered that what I was trying to do would not work because it would require 12 locations rather than 8. So I changed it a little and had it display a spade instead. I'm posting my code below in case you want to play with it or something. Once again I thank you for your help.

    Code:
    '****************************************************************
    '*  Name    : LCDTEST.BAS                                      *
    '*  Author  : Morris C. Beasley, Jr.                            *
    '*  Notice  : Copyright (c) 2011 Morris C. Beasley, Jr.         *
    '*          : All Rights Reserved                               *
    '*  Date    : 7/30/2011                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    DEFINE OSC 20               ' Define 20MHz Oscillator
    
    DEFINE LCD_RSREG PORTB      ' Set LCD Register Select port
    DEFINE LCD_RSBIT 1          ' Set LCD Register Select bit - pin 4 of LCD
    DEFINE LCD_RWREG PORTB      ' Set LCD Read Write port
    DEFINE LCD_RWBIT 2          ' Set LCD Read Write bit - pin 5 of LCD
    DEFINE LCD_EREG PORTB       ' Set LCD Enable port
    DEFINE LCD_EBIT 3           ' Set LCD Enable bit - pin 6 of LCD
    DEFINE LCD_DREG PORTB       ' Set LCD Data port
    DEFINE LCD_DBIT 4           ' Set starting bit (0 or 4) if 4-bit bus
    DEFINE LCD_BITS 4           ' 4-bit LCD data bus - pins 11 - 14 of LCD
    DEFINE LCD_LINES 4          ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2000   ' Set command delay time in us
    DEFINE LCD_DATAUS 50        ' Set data delay time in us
    
    DEFINE ADC_BITS 10          ' Defines number of adc bits (o - 255 output)
    DEFINE ADC_CLOCK 3          ' ADC clock speed
    DEFINE ADC_SAMPLEUS 1       ' How often the adc samples inputs
    PAUSE 1000
    
    '*******************************************************************************
    ' List the programmable CGRAM locations.  They are commented out because they  *
    ' are used as reference only.  All should know that commands are sent to the   *
    ' LCD by sending an $FE followed by the command itself.  In this situation, the*
    ' command is going to be the CGRAM address.  For example, LCDOUT $FE, $40 is   *
    ' writing to the 0 location of CGRAM.  There are 8 locations in total (0-7).   *
    ' That command is followed by the 8 bytes representing each row in the matrix. *
    ' In a 5x8 matrix, the 3 MSB are not used, therefore, xxxLLLLL is your byte,   *
    ' where x is not used and L is the logic of which you wish that box to be.     *
    ' As an example, the following command darkens an entire matrix at the given   *
    ' location:  LCDOUT $FE,$40,$1F,$1F,$1F,$1F,$1F,$1F,$1F,$1F.  The first byte   *
    ' is the top line while the last byte is the bottom line.                      *
    ' Constants are created to indicate the beginning of each line on a 20x4 LCD.  *
    '*******************************************************************************
    'LOCATION 0 = $40 = 64
    'LOCATION 1 = $48 = 72
    'LOCATION 2 = $50 = 80
    'LOCATION 3 = $58 = 88
    'LOCATION 4 = $60 = 96
    'LOCATION 5 = $68 = 104
    'LOCATION 6 = $70 = 112
    'LOCATION 7 = $78 = 120
    ROW1 CON 128                '$80 - LINE 1
    ROW2 CON 192                '$C0 - LINE 2
    ROW3 CON 148                '$94 - LINE 3
    ROW4 CON 212                '$D4 - LINE 4
    
    BATVOLTAGE VAR WORD         ' Variable for battery voltage
    BAT_DIGIT VAR BYTE          ' Variable for voltage digits before decimal
    BAT_DEC VAR BYTE            ' Variable for voltage digits after decimal
    ADCON1 = %10000010          ' ADC configuration
    TRISA = %11111111           ' Configure PortA
    TRISB = %00000000           ' Configure PortB
    
    MAIN:
        LCDOUT $FE, 1       'Clear the display
        LCDOUT $FE, 2       'Turn the cursor off
        LCDOUT $FE,$40,$00,$00,$00,$00,$00,$04,$0E,$1F  'Writes top of spade to CGRAM address 0
        LCDOUT $FE,$48,$01,$03,$07,$0F,$1F,$1F,$1F,$1F  'Write upper left of spade to CGRAM address 1
        LCDOUT $FE,$50,$1F,$1F,$1F,$1F,$1F,$1F,$1F,$1F  'Write upper center of spade to CGRAM address 2
        LCDOUT $FE,$58,$10,$18,$1C,$1E,$1F,$1F,$1F,$1F  'Write upper right of spade to CGRAM address 3
        LCDOUT $FE,$60,$1F,$0F,$07,$00,$00,$01,$03,$07  'Write lower left of spade to CGRAM address 4
        LCDOUT $FE,$68,$1F,$1F,$0E,$0E,$1F,$1F,$1F,$1F  'Write lower center of spade to CGRAM address 5
        LCDOUT $FE,$70,$1F,$1E,$1C,$00,$00,$10,$18,$1C  'Write lower right of spade to CGRAM address 6
    
        LCDOUT $FE, ROW1+2, 0
        LCDOUT $FE, ROW1+5, "ISH ELECTRONICS"
        LCDOUT $FE, ROW2+1, 1
        LCDOUT $FE, ROW2+2, 2
        LCDOUT $FE, ROW2+3, 3
        LCDOUT $FE, ROW2+11, "BY"
        LCDOUT $FE, ROW3+1, 4
        LCDOUT $FE, ROW3+2, 5
        LCDOUT $FE, ROW3+3, 6
        LCDOUT $FE, ROW3+9, "MORRIS"
        LCDOUT $FE, ROW4+9, "BEASLEY"
        PAUSE 2000
        GOSUB CHECK_VOLTAGE
        PAUSE 100
        
        GOTO MAIN
    
    CHECK_VOLTAGE:
        ADCIN 0, BATVOLTAGE     ' Store the 10-bit conversion in variable
        BAT_DIGIT = (BATVOLTAGE * 5) / 1000
        BAT_DEC = (BATVOLTAGE * 5) - (BAT_DIGIT * 1000)
        LCDOUT $FE, ROW1+2, 0
        LCDOUT $FE, ROW1+5, "    VOLTAGE    "
        LCDOUT $FE, ROW2+1, 1
        LCDOUT $FE, ROW2+2, 2
        LCDOUT $FE, ROW2+3, 3
        LCDOUT $FE, ROW2+5, "    MEASURED   "
        LCDOUT $FE, ROW3+1, 4
        LCDOUT $FE, ROW3+2, 5
        LCDOUT $FE, ROW3+3, 6
        LCDOUT $FE, ROW3+5, "       AT      "
        LCDOUT $FE, ROW4+5, DEC BAT_DIGIT, ".", DEC BAT_DEC, "DC Volts"
        PAUSE 2000
        RETURN
    Last edited by mcbeasleyjr; - 6th November 2011 at 02:13.

    Morris Beasley
    Manufacturing Test Technician
    B.S. Industrial Technology (2007)

    For more information on our robotics, please visit:
    http://www.ish-electronics.webs.com

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts