;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ;/// CODE IN ASSEMBLER TO PRINT CHARACTERS IN AN LCD ;/// USING PIC16f877A ;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LIST P=16f873A ;Indica el tipo de procesador a programar INCLUDE "P16F873A.INC" ;Incluye en el programa el fichero de definiciones del uC seleccionado __CONFIG _CP_OFF & _BODEN_OFF & _HS_OSC & _WRT_OFF & _WDT_OFF & _PWRTE_ON & _DEBUG_OFF & _CPD_OFF & _LVP_OFF CBLOCK 0x35 Dly0 ;Stores 3 bytes of data for the delay count Dly1 ;Dly0 is the least significant byte Dly2 ENDC Dly24 MACRO DLY ;Take the delay value argument from the macro, precalculate ;the required 3 RAM values and load the RAM values Dly2,Dly1 ;and Dly0. banksel Dly0 movlw DLY & H'FF' movwf Dly0 movlw DLY >>D'08' & H'FF' movwf Dly1 movlw DLY >>D'16' & H'FF' movwf Dly2 ;Bytes are shifted and anded by the assembler to make user ;calculations easier. endm ORG 0x00 BCF STATUS,RP1 ; Selection of memory bank 1 BSF STATUS,RP0 CLRF TRISB ; Configuration of PORT B as output (Data pins LCD) CLRF TRISC ; Configuration of PORT D as output (RD0-R/S, RD1-E) MOVLW b'00000111' MOVWF OPTION_REG ; Configuration of Option Register (TMR0 Rate = 1:256) BCF STATUS,RP0 ; Selection of memory bank 0 CLRF PORTB ; Setting PORTB to "0000000" CLRF PORTC ; Setting PORTD to "0000000" CALL InitiateLCD LOOP2 CALL PRINTCHAR GOTO LOOP2 ;////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ;///// LCD RUTINA //////////////////////////////////////////////////////////////////////////////////////////////// ;////////////////////////////////////////////////////////////////////////////////////////////////////////////////// InitiateLCD BCF PORTC,RC0 ; Setting RS as 0 (Sends commands to LCD) CALL Delay_05_sec MOVLW b'00110000' ; Clearing display MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW b'00110000' ; Funtion set MOVWF PORTB CALL Enable CALL Delay_200_us MOVLW b'00110000' ; Display on off MOVWF PORTB CALL Enable CALL Delay_200_us MOVLW b'00111000' ; Entry mod set MOVWF PORTB CALL Enable CALL Delay_200_us MOVLW b'00001000' ; Entry mod set MOVWF PORTB CALL Enable CALL Delay_200_us MOVLW b'00000001' ; Entry mod set MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW b'00000110' ; Entry mod set MOVWF PORTB CALL Enable CALL Delay_200_us MOVLW b'00001100' ; Entry mod set MOVWF PORTB CALL Enable CALL Delay_200_us RETURN PRINTCHAR BCF PORTC,RC0 ; Setting RS as 0 (Sends commands to LCD) MOVLW b'00000010' ; Set cursor home MOVWF PORTB CALL Enable CALL DELAY_5ms BSF PORTC,RC0 ; Setting RS as 1 (Sends information to LCD) CALL DELAY_5ms MOVLW d'72' ; Print character "H" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'101' ; Print character "e" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'108' ; Print character "l" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'108' ; Print character "l" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'111' ; Print character "o" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'0' ; Print caracter " " MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'87' ; Print caracter "W" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'111' ; Print character "o" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'114' ; Print character "r" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'108' ; Print character "l" MOVWF PORTB CALL Enable CALL DELAY_5ms MOVLW d'100' ; Print character "d" MOVWF PORTB CALL Enable CALL DELAY_5ms RETURN Enable BSF PORTC,1 ; E pin is high, (LCD is processing the incoming data) Delay_500_ns BCF PORTC,1 ; E pin is low, (LCD does not care what is happening) RETURN Delay_5_ms MOVLW .5 ; Delay of 5 ms MOVWF TMR0 Delay_05_sec ;Pause 0.5 s Dly24 D'156249' ; 0.5/(4/20000000)/16=156250-1=156249 goto DoDly24 DELAY_5ms ; Pause 5 ms Dly24 D'1562' ; 0.005/(4/20000000)/16=1562.5=1562 goto DoDly24 Delay_200_us ; Pause 200 us Dly24 D'62' ; 0.0002/(4/20000000)/16=62.5=62 goto DoDly24 Delay_500_ns ; Pause 200 us Dly24 D'0.1562' ; 0.0002/(4/20000000)/16=62.5=62 goto DoDly24 DoDly24 ;16 Tcy per loop movlw H'FF' ;Start with -1 in W addwf Dly0,F ;LSB decrement btfsc STATUS,C ;was the carry flag set? clrw ;If so, 0 is put in W addwf Dly1,F ;Else, we continue. btfsc STATUS,C clrw ;0 in W addwf Dly2,F btfsc STATUS,C clrw ;0 in W iorwf Dly0,W ;Inclusive-OR all variables iorwf Dly1,W ;together to see if we have reached iorwf Dly2,W ;0 on all of them. btfss STATUS,Z ;Test if result of Inclusive-OR's is 0 goto DoDly24 return LOOP BTFSS INTCON,2 GOTO LOOP BCF INTCON,2 RETURN ;////////////////////////////////////////////////////////////////////////////////////////////////////////////////// END