;------------------------------------------------------------------------------- ; FILE: LCD character Display ; AUTH: Ed's Projects ; DATE: 24/06/2016 ; DESC: LCD character display in 4-bit mode. ;------------------------------------------------------------------------------- LIST P=16F628A, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. __config _CP_OFF & _CPD_OFF & _LVP_OFF & _BOREN_ON & _MCLRE_OFF & _PWRTE_ON & _WDTE_OFF & _HS_OSC ;------------------------------------------------------------------------------- ;----------------------------- Port Definitions ------------------------------ ;------------------------------------------------------------------------------- ; PortA - Output - RA0 - Pin 17 - RS Register select for LCD ; PortA - Output - RA1 - Pin 18 - E Enable for LCD ; PortB - Output - RB4 - Pin 10 - DB4 Dataline for LCD ; PortB - Output - RB5 - Pin 11 - DB5 Dataline for LCD ; PortB - Output - RB6 - Pin 12 - DB6 Dataline for LCD ; PortB - Output - RB7 - Pin 13 - DB7 Dataline for LCD ;------------------------------------------------------------------------------- ;-------------------------------- Variables ---------------------------------- ;------------------------------------------------------------------------------- cblock h'20' micro200f ms2delayf ms2delayg Varpcl dly0 Tmp endc ;------------------------------------------------------------------------------- ;----------------------------- Define Symbols -------------------------------- ;------------------------------------------------------------------------------- #define LCD4 PORTB, 4 #define LCD5 PORTB, 5 #define LCD6 PORTB, 6 #define LCD7 PORTB, 7 #define LCDRS PORTA, 0 #define LCDE PORTA, 1 ;------------------------------------------------------------------------------- ;------------------------------- Program Code -------------------------------- ;------------------------------------------------------------------------------- ORG 0 ;------------------------------------------------------------------------------- ;-------------------------- Initialisation of Ports -------------------------- ;------------------------------------------------------------------------------- Init bsf STATUS,RP0 ; Set RP0 to 1 bcf STATUS,RP1 ; Set RP1 to 0 ; Bank 1 Selected clrf VRCON ; Set Vref to off clrf TRISB ; Set all PortB as Output movlw b'00000000' ; Load 00111100 to working register movwf TRISA ; bcf STATUS,RP0 ; Set RP0 to 0 ; Bank 0 Selected movlw d'7' ; Load working register with "7" movwf CMCON ; Turn off comparators clrf PORTA clrf PORTB clrf micro200f clrf ms2delayf clrf ms2delayg clrf Tmp clrf Varpcl clrf dly0 ; Clear variables and output ports ;------------------------------------------------------------------------------- ;--------------------------- LCD Initialisation ------------------------------ ;------------------------------------------------------------------------------- movlw d'32' call delay bsf LCD5 bsf LCD4 call Enable call ms2delay call ms2delay call Enable call micro200 call Enable bcf LCD4 call Enable ;Function Set movlw b'00101000' ;4-bit mode, 2 lines, 5x7 matrix call SendLCD ;Display on / off movlw b'00001100' ;Display on, Cursor off, Blink off call SendLCD ;Entry Mode Set movlw b'00000110' ;Increment, Display shifted call SendLCD ;Cursor or display shift movlw b'00010000' ;Display Shift call SendLCD ;Clear Screen movlw b'00000001' ; Clear screen call SendLCD call ms2delay call ms2delay bsf LCDRS ; Set data mode goto Start ;------------------------------------------------------------------------------- ;----------------------------- Send Data to LCD ------------------------------ ;------------------------------------------------------------------------------- SendLCD ; Send data in two 4-bit nibbles 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 bsf LCDE nop nop nop nop bcf LCDE call micro200 call micro200 call micro200 return ;------------------------------------------------------------------------------- ;--------------------------------- LCD Clear --------------------------------- ;------------------------------------------------------------------------------- LCDClr bcf LCDRS movlw b'00000001' call SendLCD bsf LCDRS call ms2delay return ;------------------------------------------------------------------------------- ;----------------------------- LCD Cursor Shift ------------------------------ ;------------------------------------------------------------------------------- LCDShi bcf LCDRS iorlw b'10000000' call SendLCD bsf LCDRS call ms2delay return ;------------------------------------------------------------------------------- ;---------------------------- Loops 2ms Delay -------------------------------- ;------------------------------------------------------------------------------- delay movwf dly0 delayloop call ms2delay decfsz dly0, f goto delayloop return ;------------------------------------------------------------------------------- ;------------------------------- 2ms Delay ----------------------------------- ;------------------------------------------------------------------------------- ms2delay movlw d'8' movwf ms2delayg ms2delayc movlw d'250' movwf ms2delayf ms2delaya nop ;0.2us nop ;0.2us decfsz ms2delayf,f ;0.2us goto ms2delaya ;0.4us ;total 250us ms2delayb decfsz ms2delayg,f ;0.2us goto ms2delayc ;0.4us return ;return from call ;------------------------------------------------------------------------------- ;------------------------------- 200us Delay --------------------------------- ;------------------------------------------------------------------------------- micro200 movlw d'200' movwf micro200f micro200a nop ;0.2us nop ;0.2us decfsz micro200f,f ;0.2us goto micro200a ;0.4us return ;return from call ;------------------------------------------------------------------------------- ;--------------------------- Character Storage ------------------------------- ;------------------------------------------------------------------------------- Char1 clrf Varpcl char1a movf Varpcl, w call char1lookup ; fetch a character addlw 0 btfsc STATUS, Z return call SendLCD call micro200 incf Varpcl, f goto char1a char1lookup addwf PCL, f DT " Welcome to ", "4-bit LCD using the", " Ed's Projects ", " PIC16F628A ", d'0' ;------------------------------------------------------------------------------- ;------------------------- Main part of program ------------------------------ ;------------------------------------------------------------------------------- Start call Char1 Cats goto Cats end