'**************************************************************** '* Name : SERCLCD1.BAS * '* Author : Ranjit Diol * '* Notice : Copyright (c) 2003 COMPSys * '* : All Rights Reserved * '* Date : 2/15/03 * '* Version : 1.00 * '* Compiler: PICBasicPro Ver 2.43 * '* Notes : Designed for HD44780 type character LCDs * '* : Serial Mode configuration (No Read LCD available) * '* * '* MCU : PIC16F871/2/3/6 or 18F2x2 * '* * '* * '**************************************************************** ' NOT FOR COMMERCIAL USE * '**************************************************************** ' * '===============================================================* ' DISCLAIMER: This file is being released as non-commericial * ' software. It is being provided "AS IS", neither the author, * ' nor COMPSys shall be held liable for any damages caused * ' by its use. * '===============================================================* ' This code may be used in whole or in part as * ' needed as long as the author's name appears in the credits * ' Contact: rsdiol@compsys1.com for more information * '===============================================================* ' ' NOTE: Make sure that the LCD is compatible to a standard HD44780 display ' ' Standard Character LCD pin outs (either 1x14 or 2x7): ' 1 - GND ' 2 - Vdd (5v+ usually) ' 3 - Vo (LCD contrast voltage) ' 4 - RS (Data / Instruction) ' 5 - RW (Read/Write) ' 6 - E (Enable) ' 7 - 14 DB0 to DB7 Data bus ' ' 1x10 header pins 14-13-12-11-10-09-08-07-06-05-04-03-02-01 ' 2x14 header pins ' 01-02 ' 03-04 ' 05-06 ' 07-08 ' 09-10 ' 11-12 ' 13-14 ' ' Contrast is controlled by using an approx 20K pot, with the center to LCD PIN 3 ' and the legs to Vdd and Gnd (You may require a 5-10k resistor between Vdd and the POT) ' 20K pot ' | Pin 1 -----(optional 10K res)-----Vdd (5v+) ' LCDPin3 ---Center Pin 2 ' | Pin 3 ' 'NOTE: Extended Temp LCD displays may require a negative voltage ' '============================================================ ' HD44780 Control Codes '============================================================ ' CTRL Hex Dec ' $FE, $01 001 Clear display ' $FE, $02 002 Return home (beginning of first line) ' $FE, $08 008 Blank display (doesn't clear memory) ' $FE, $0C 012 Cursor off ' $FE, $0D 013 Block cursor ' $FE, $0E 014 Underline cursor on ' $FE, $0F 015 Blinking cursor on ' $FE, $10 016 Move cursor left one position ' $FE, $14 020 Move cursor right one position ' $FE, $18 024 Scroll all lines 1 char left ' $FE, $1C 028 Scroll all lines 1 char right ' $FE, $C0 192 Move cursor to beginning of second line ' $FE, $94 148 Move cursor to beginning of third line ' $FE, $D4 212 Move cursor to beginning of fourth line ' $FE, $80 128+addr DDRAM cursor position ' $FE, $40 064+addr CGRAM pointer '============================================================= ' 'Physical connection: 1 data line and common ground ' 'Usage Notes: ' 1. Send TWO Carriage Returns (ASCII decimal 13) to start the program ' 2. The program will loop checking the serial input pin for data ' 3. If a \ (backslash) CTL character is received it will expect ' it to be follwed by an LCD command (3 dec digits) Ex: 12 = 012 ' 4. If a ] (Right bracket) is received it expects a dec 3 digit value (000-255) ' which it will display as a ASCII character ' 5. If a [ (Left bracket) is received it will wait for a 2 digit decimal ' User Control number. ' ' USER CONTROL NUMBERS [nn ' [30 make PIN3 LOW [31 make PIN3 HIGH ' [40 make PIN4 LOW [41 make PIN4 HIGH ' [50 make PIN5 LOW [51 make PIN5 HIGH ' [60 make PIN6 LOW [61 make PIN6 HIGH ' [70 make PIN7 LOW [71 make PIN7 HIGH ' [30 make PIN3 LOW [31 make PIN3 HIGH ' [98 follwed by 8 bytes (each as a 3 digit dec number) of a custom CGRam character ' [99 Display the logo screen ' ' BAUD RATES (Set by using jumpers on the board) Are checked only once when the program starts. ' Possible Rates: 1200,2400,9600,19200 (19200 may not always be reliable) ' Data can be TRUE RS232 (like that from a PC's com port or a RS232 transceiver , ei: MAX232) ' or INVERTED TTL RS232 (direct mcu to mcu using 1 pin and GND). ' ' ' ' Include "Modedefs.bas" 'Include general definitions DEFINE OSC 20 'Set the Xtal frequency ADCON1 = 7 'Make all pins digital ' ** Declare LCDOUT Defines ** DEFINE LCD_DREG PortC ' Set Data to PortC (LCD Pins7-14) DEFINE LCD_DBIT 0 ' Set starting Data to Bit0 DEFINE LCD_RSREG PortB ' Set Register Select to PortB (LCD Pin 4) DEFINE LCD_RSBIT 1 ' Set RS line to PORTB.1 DEFINE LCD_EREG PortB ' Set Enable to PortB (LCD Pin 6) DEFINE LCD_EBIT 0 ' Set Enable line to PORTB.0 DEFINE LCD_BITS 8 ' Set for 8 bit Bus DEFINE LCD_LINES 4 ' Set number of lines to 4 ' ** Define LCD Control Constants ** CTL Con 254 ' Control Byte Clr Con 1 ' Clear the display HOME Con 2 'Beginning of the 1st line LINE1 Con 128 ' Point to beginning of line 1 LINE2 Con 192 ' Point to beginning of line 2 LINE3 Con 148 ' Point to beginning of line 3 LINE4 Con 212 ' Point to beginning of line 4 CGram Con 64 ' Point to Cgram within LCD DDram Con 128 ' Pont to DDRAM ShiftL Con 24 ' Shift display left ShiftR Con 28 ' Shift display right Blank Con 8 ' Blank display (don't clear memory) ULon Con 14 ' Underline ON BlinkCur Con 15 ' Blinking Cursor CurLeft Con 16 ' Move cursor left 1 position CurRight Con 20 ' Move cursor right 1 position BlockCur Con 13 ' Block cursor Space Con 32 'ASCII code for a space ' ** Declare Variables ** 'General use I/O pins the user can turn them 'ON/OFF via serial commands Pin7 var PortB.7 'Also used by ICSP Pin6 var PortB.6 'and this one Pin5 var PortB.5 Pin4 var PortB.4 Pin3 var PortB.3 SI Var PortB.2 ' Serial In Inverted VDD Var PortA.5 ' LCD VDD pin InByte1 Var byte ' The byte received InByte2 var byte ' Second byte recvd baud var word 'Baud rate bd var word 'Displayed baud number BP0 var PortA.0 'A0,A1,A3 Pins used to determine BP1 var PortA.1 'baud rate and polarity BP2 var PortA.3 ch1 var byte 'General variables ch2 var byte ch3 var byte bits var byte 'Used for baud calculations tmp var byte 'Temp var CGChar var byte[8] 'CGRam custom character CGAddr var byte 'CGRam address cnt var byte 'General use counter 'Initialize variables bits = 0 'Set to high high BP0 high BP1 high BP2 low SI 'Now make them input (they should read high) input BP0 input BP1 input BP2 input SI 'Start up LCD Low VDD ' Turn Off LCD Pause 500 ' Wait for Pic to Initialise 'Check pins to determine baud rate and polarity 'BITS var will end up having a value to 'determine baud and polarity if BP0 = 0 then bits.0=0 else bits.0=1 endif if BP1 = 0 then bits.1=0 else bits.1=1 endif if BP2 = 0 then bits.2=0 else bits.2=1 endif if bits > 3 then bits = bits - 4 ch1 = "N" 'Inverted TTL RS232 else ch1 = "T" 'True RS232 endif '19200 1200 2400 9600 lookup2 bits,[32,813,396,84],baud lookup2 bits,[19200,1200,2400,9600],bd if ch1 = "N" then baud.14 = 1 'Invert it '==== MAIN SECTION ======= 'Initialize the LCD gosub init InByte1 = " " 'The user must first send TWO CARRIAGE RETURNS as a start flag 'This was done for CLCD PCB Ver 1.0 which didn't have 'provisions to prevent 'floating' of the serial SI pin 'if no serial connection was present. This produced 'garbage' 'characters on the display. 'For TRUE the SI pin should be pulled slightly HIGH with 'a 100-200k resistor. Likewise, for INVERTED the SI pin 'should be pulled LOW using a 100-200k resistor. Pcb Ver 2 'will have jumpers to accomodate this. 'Wait for 2 carriage returns Serin2 SI,baud,[wait(13),tmp] MainLoop: Serin2 SI,baud,[InByte1] ' Receive the serial byte if InByte1 = "\" then goto LCDCtrl if InByte1 = "]" then goto DispChr if Inbyte1 = "[" then goto UserCtrl select case InByte1 case 008 'BackSpace Lcdout CTL,CurLeft Lcdout Space Lcdout CTL,CurLeft case 009 'Tab (5 spaces) for cnt = 1 to 5 Lcdout Space next cnt case 013 'Carriage return 'not implemented as yet case else Lcdout InByte1 end select Goto MainLoop LCDCtrl: Serin2 SI,baud,[dec3 InByte2] 'Receive the second serial byte select case InByte2 case CLR Lcdout CTL,CLR pause 30 case LINE1 Lcdout CTL,LINE1 case LINE2 Lcdout CTL,LINE2 case LINE3 Lcdout CTL,LINE3 case LINE4 Lcdout CTL,LINE4 case else Lcdout CTL,InByte2 end select goto Mainloop DispChr: Serin2 SI,baud,[dec3 InByte2] ' Receive the second serial byte Lcdout InByte2 ' If not then send out the two bytes goto Mainloop 'User Control of pins and loading CGRam custom characters UserCtrl: Serin2 SI,baud,[dec2 InByte2] ' Receive the second serial byte select case InByte2 case 30 low Pin3 case 31 high Pin3 case 40 low Pin4 case 41 high Pin4 case 50 low Pin5 case 51 high Pin5 case 60 low Pin6 case 61 high Pin6 case 70 low Pin7 case 71 high Pin7 case 98 'Create custom CGRam character 'Addr followed by 8 bytes Serin2 SI,baud,[dec3 CGAddr, dec2 CGChar(0),dec2 CGChar(1),_ dec2 CGChar(2),dec2 CGChar(3),dec2 CGChar(4),dec2 CGChar(5),_ dec2 CGChar(6),dec2 CGChar(7)] Lcdout CTL,CGAddr,_ CGChar[0],CGChar[1],CGChar[2],CGChar[3],_ CGChar[4],CGChar[5],CGChar[6],CGChar[7],CTL,128 case 99 gosub logo end select goto MainLoop '******End of Main Program ********* END '*********************************** 'Subroutines ClrLCD: Lcdout CTL,Clr:Pause 30 ' Clear the LCD Return Init: High VDD ' Switch On LCD VDD Trisc=0 ' Set Port to Output Pause 300 ' Wait for LCD to Initialize 'Load Custom CGRam characters LCDOut CTL,64,$00,$04,$02,$1F,$02,$04,$00,$00,CTL,128 LCDOut CTL,72,$00,$04,$08,$1F,$08,$04,$00,$00,CTL,128 LCDOut CTL,80,$00,$04,$0E,$15,$04,$04,$00,$00,CTL,128 LCDOut CTL,88,$00,$00,$04,$04,$15,$0E,$04,$00,CTL,128 LCDOut CTL,96,$1F,$11,$11,$11,$11,$11,$11,$1F,CTL,128 LCDOut CTL,104,$04,$0E,$1F,$00,$00,$1F,$0E,$04,CTL,128 LCDOut CTL,112,$02,$06,$0E,$1E,$0E,$06,$02,$00,CTL,128 LCDOut CTL,120,$08,$0C,$0E,$0F,$0E,$0C,$08,$00,CTL,128 logo: Lcdout CTL,Clr:Pause 30 ' Clear the LCD Lcdout CTL,LINE1,"Serial CLCD v1.0" Lcdout CTL,LINE2,"Baud: ", ch1,dec bd Lcdout CTL,HOME Return