;------------------------------------------------------------------------------- ; FILE: Serial4x40LCD ; AUTH: Ed's Projects ; DATE: 06/09/2016 ; DESC: Asynchronous Serial 4x40 Character LCD ;------------------------------------------------------------------------------- LIST P=16F690, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. __config _FCMEN_OFF & _IESO_OFF & _BOREN_ON & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDTE_OFF & _HS_OSC ;------------------------------------------------------------------------------- ;----------------------------- Port Definitions ------------------------------ ;------------------------------------------------------------------------------- ; PortA - Output - RA1 - Pin 18 - RS Register select for LCD ; PortA - Output - RA2 - Pin 17 - R/W Read / Write for LCD ; PortC - Output - RC0 - Pin 16 - E1 Enable for LCD ; PortA - Output - RA0 - Pin 19 - E2 Enable for LCD ; PortC - Output - RC1 - Pin 15 - DB4 Dataline for LCD ; PortC - Output - RC2 - Pin 14 - DB5 Dataline for LCD ; PortB - Output - RB4 - Pin 13 - DB6 Dataline for LCD ; PortB - Output - RB6 - Pin 11 - DB7 Dataline for LCD ; PortB - Input - RB5 - Pin 12 - Serial Data In ; PortC - Input - RC3 - Pin 7 - Baud Control 0 ; PortC - Input - RC6 - Pin 8 - Baud Control 1 ; PortC - Input - RC7 - Pin 9 - Baud Control 2 ; PortC - Input - RC5 - Pin 5 - Baud Control 3 ; Baud - 0001 = 110 ; Baud - 0010 = 300 ; 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000, 57600, 115200, 128000, 153600 ; Command 143 - Lines 1 and 2 ; Command 159 - Lines 3 and 4 ; Command 254 - Instruction ;------------------------------------------------------------------------------- ;-------------------------------- Variables ---------------------------------- ;------------------------------------------------------------------------------- cblock h'20' temp temp2 temp3 Tmp baudctrl Enind endc ;------------------------------------------------------------------------------- ;----------------------------- Define Symbols -------------------------------- ;------------------------------------------------------------------------------- #define LCD4 PORTC, 1 #define LCD5 PORTC, 2 #define LCD6 PORTB, 4 #define LCD7 PORTB, 6 #define RS PORTA, 1 #define RW PORTA, 2 #define E1 PORTC, 0 #define E2 PORTA, 0 #define BD0 PORTC, 3 #define BD1 PORTC, 6 #define BD2 PORTC, 7 #define BD3 PORTC, 5 ;------------------------------------------------------------------------------- ;------------------------------- Program Code -------------------------------- ;------------------------------------------------------------------------------- ORG 0 ;------------------------------------------------------------------------------- ;-------------------------- Initialisation of Ports -------------------------- ;------------------------------------------------------------------------------- Init bsf STATUS,RP0 bcf STATUS,RP1 ; Bank 1 Selected movlw b'00000000' movwf TRISA movlw b'10100000' movwf TRISB movlw b'11101000' movwf TRISC bcf STATUS,RP0 bsf STATUS,RP1 ; Bank 2 Selected clrf VRCON ; Set Vref to off clrf CM1CON0 ; Turn off comparator 1 clrf CM2CON0 ; Turn off comparator 2 clrf ANSEL ; Set analogue inputs to digital clrf ANSELH ; Set analogue inputs to digital bsf STATUS,RP0 ; Bank 3 Selected clrf SRCON ; Disable SR latch bcf STATUS,RP0 bcf STATUS,RP1 ; Bank 0 Selected clrf PORTA clrf PORTB clrf PORTC ;------------------------------------------------------------------------------- ;--------------------- Asynchronous Serial Initialisation -------------------- ;------------------------------------------------------------------------------- bsf STATUS, RP0 ; Bank 1 selected bsf BAUDCTL, BRG16 bsf TXSTA, BRGH ; Set high baud rate bcf STATUS, RP0 ; Bank 0 Selected clrf baudctrl btfsc BD0 bsf baudctrl, 0 btfsc BD1 bsf baudctrl, 1 btfsc BD2 bsf baudctrl, 2 btfsc BD3 bsf baudctrl, 3 movf baudctrl, w bsf STATUS, RP0 ; Bank 1 selected call baudhigh movwf SPBRGH bcf STATUS, RP0 ; Bank 0 selected movf baudctrl, w bsf STATUS, RP0 ; Bank 1 selected call baudlow movwf SPBRG goto serialinit baudhigh addwf PCL, f retlw d'177' retlw d'65' retlw d'32' retlw d'16' retlw d'8' retlw d'4' retlw d'2' retlw d'1' retlw d'1' retlw d'0' retlw d'0' retlw d'0' retlw d'0' retlw d'0' retlw d'0' retlw d'0' baudlow addwf PCL, f retlw d'141' retlw d'25' retlw d'140' retlw d'69' retlw d'34' retlw d'16' retlw d'8' retlw d'90' retlw d'3' retlw d'172' retlw d'129' retlw d'88' retlw d'86' retlw d'42' retlw d'38' retlw d'31' serialinit bcf TXSTA, SYNC ; Set to asynchronous mode bcf TXSTA, TXEN ; Disable Transmit bcf STATUS, RP0 ; Bank 0 Selected bsf RCSTA, SPEN ; Enable Serial Port bsf RCSTA, CREN ; Turns on receive movf RCREG, w movf RCREG, w movf RCREG, w ;------------------------------------------------------------------------------- ;-------------------------------- Initialisaton ------------------------------ ;------------------------------------------------------------------------------- skip movlw d'255' movwf temp initloop goto $+1 goto $+1 goto $+1 goto $+1 decfsz temp, f goto initloop ;------------------------------------------------------------------------------- ;--------------------------- LCD Initialisation ------------------------------ ;------------------------------------------------------------------------------- bcf RW ; Set write mode bcf RS ; Set Instruction mode movlw b'00000011' movwf Enind bsf LCD5 bsf LCD4 call Enable call ms2delay call ms2delay call Enable call ms2delay call Enable call ms2delay bcf LCD4 call Enable call ms2delay ;Function Set movlw b'00101000' ;4-bit mode, 2 lines, 5x7 matrix call SendLCD call ms2delay ;Display on / off movlw b'00001100' ;Display on, Cursor off, Blink off call SendLCD call ms2delay ;Entry Mode Set movlw b'00000110' ;Increment, Display shifted call SendLCD call ms2delay ;Cursor or display shift movlw b'00010000' ;Display Shift call SendLCD call ms2delay ;Clear Screen movlw b'00000001' ; Clear screen call SendLCD call ms2delay call ms2delay bsf RS ; Set data mode movlw b'00000001' movwf Enind ;------------------------------------------------------------------------------- ;------------------------------- Main Program -------------------------------- ;------------------------------------------------------------------------------- Start btfss PIR1, RCIF goto Start movf RCREG, w movwf temp xorlw d'143' ; Select display 1? btfsc STATUS, Z goto disp1com movf temp, w xorlw d'159' ; Select display 2? btfsc STATUS, Z goto disp2com movf temp, w xorlw d'254' ; Command? btfsc STATUS, Z goto Command movf temp, w call SendLCD bsf RS ; Data mode goto Start disp1com movlw b'00000001' ; Select display 1 movwf Enind goto Start disp2com movlw b'00000010' ; Select display 2 movwf Enind goto Start Command bcf RS goto Start ;------------------------------------------------------------------------------- ;----------------------------- Send Data to LCD ------------------------------ ;------------------------------------------------------------------------------- SendLCD movwf Tmp bcf LCD7 bcf LCD6 bcf LCD5 bcf LCD4 btfsc Tmp, 7 bsf LCD7 btfsc Tmp, 6 bsf LCD6 btfsc Tmp, 5 bsf LCD5 btfsc Tmp, 4 bsf LCD4 call Enable bcf LCD7 bcf LCD6 bcf LCD5 bcf LCD4 btfsc Tmp, 3 bsf LCD7 btfsc Tmp, 2 bsf LCD6 btfsc Tmp, 1 bsf LCD5 btfsc Tmp, 0 bsf LCD4 call Enable return ;------------------------------------------------------------------------------- ;--------------------------------- Enable ------------------------------------ ;------------------------------------------------------------------------------- Enable btfss Enind, 0 ; Display 1? goto Enable2 bsf E1 goto $+1 goto $+1 bcf E1 goto $+1 Enable2 btfss Enind, 1 ; Display 2? return bsf E2 goto $+1 goto $+1 bcf E2 goto $+1 return ;------------------------------------------------------------------------------- ;------------------------------- 2ms Delay ----------------------------------- ;------------------------------------------------------------------------------- ms2delay movlw d'8' movwf temp label1 movlw d'250' movwf temp2 label2 nop ;0.2us nop ;0.2us decfsz temp2,f ;0.2us goto label2 ;0.4us ;total 250us decfsz temp,f ;0.2us goto label1 ;0.4us return ;return from call end