Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Can help check my P16F876A coding for display on LCD in assembly language

Status
Not open for further replies.

tee_say

Newbie level 4
Joined
Sep 30, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,482
My LCD still cant display the word. My LCD is HD44780 16x2. This my code.
;------------------------------------------------------------------------------------------------------------------------
; Source code for the PIC16F876A based interfacing with HD44780 based 16x2 LCD.
;------------------------------------------------------------------------------------------------------------------------
;
; **LCD Pinouts
; 1 Vss - Ground, 3rd pin of the potentiometer
; 2 Vcc - 5V DC, 1st pin of the potentiometer
; 3 Vee - Middle pin of the potentiometer
; 4 RS - RC0 (Data - 1, Instruction - 0)
; 5 R/W - RC1 (R - 1, W - 0)
; 6 E - RC2 (Enable Pulse)
; 7 DB0 - RB0 (LSB)
; 8 DB1 - RB1
; 9 DB2 - RB2
; 10 DB3 - RB3 (Lower 4 bits)
; 11 DB4 - RB4 (Upper 4 bits)
; 12 DB5 - RB5
; 13 DB6 - RB6
; 14 DB7 - RB7 (MSB)
;
; Instruction Cycle Time = 1 / (4MHz / 4) = 1us per instruction
;------------------------------------------------------------------------------------------------------------------------

LIST P=16F876A
INCLUDE "p16f876A.inc"
; ERRORLEVEL -302
__CONFIG _PWRTE_OFF & _HS_OSC & _WDT_OFF & _LVP_OFF & _BODEN_OFF; configuration switches

N EQU 0x20
FIXDELAY EQU 0x21 ; General Purpose Registers.

org 0x00
movlw 0x00
movwf PCLATH
nop ; Reserved for ICD II.
goto start

start call initports ; Initialize Ports as output/inputs.
call INITLCD ; Initialize LCD.

main call testmsg ; Print test message on LCD

forever goto $

;------------------------------------------------------------------------------------------------------------------------
; Subroutine to initialize the PORTs as Inputs or Outputs.
;------------------------------------------------------------------------------------------------------------------------

initports
banksel PORTB
clrf PORTB ; Clear PORTs.
clrf PORTC

banksel TRISB
movlw 0x00 ; Define PORTB and PORTC as Outputs.
movwf PORTB
movwf PORTC

return

;------------------------------------------------------------------------------------------------------------------------
; Initialize the LCD.
;------------------------------------------------------------------------------------------------------------------------

INITLCD
BANKSEL PORTB ; Select Bank for PORTB.

MOVLW 0xE6 ; Call for 46ms delay
CALL NDELAY ; Wait for VCC of the LCD to reach 5V

BCF PORTC, 0 ; Clear RS to select Instruction Reg.
BCF PORTC, 1 ; Clear R/W to write

MOVLW B'00111011' ; Function Set to 8 bits, 2 lines and 5x7 dot matrix
MOVWF PORTB
CALL ENABLEPULSE
CALL DELAY50
CALL ENABLEPULSE
CALL DELAY50
CALL ENABLEPULSE
CALL DELAY50 ; Call 50us delay and wait for instruction completion

MOVLW B'00001000' ; Display OFF
MOVWF PORTB
CALL ENABLEPULSE
CALL DELAY50 ; Call 50us delay and wait for instruction completion

MOVLW B'00000001' ; Clear Display
MOVWF PORTB
CALL ENABLEPULSE
MOVLW 0x09 ; Call 1.8ms delay and wait for instruction completion
CALL NDELAY

MOVLW B'00000010' ; Cursor Home
MOVWF PORTB
CALL ENABLEPULSE
MOVLW 0x09 ; Call 1.8ms delay and wait for instruction completion
CALL NDELAY

MOVLW B'00001111' ; Display ON, Cursor ON, Blinking ON
MOVWF PORTB
CALL ENABLEPULSE
CALL DELAY50 ; Call 50us delay and wait for instruction completion

MOVLW B'00000110' ; Entry Mode Set, Increment & No display shift
MOVWF PORTB
CALL ENABLEPULSE
CALL DELAY50 ; Call 50us delay and wait for instruction completion

BSF PORTC, 0 ; Set RS to select Data Reg.
BCF PORTC, 1 ; Clear R/W to write

RETURN

;------------------------------------------------------------------------------------------------------------------------
; Enable Pulse for writing or reading instructions or data
;------------------------------------------------------------------------------------------------------------------------

ENABLEPULSE BCF PORTC, 2 ; 2us LOW followed by 3us HIGH Enable Pulse and 2us LOW.
NOP
NOP
BSF PORTC, 2
NOP
NOP
NOP
BCF PORTC, 2
NOP
NOP
RETURN

;------------------------------------------------------------------------------------------------------------------------
; N DELAY SUBROUTINE, delay in multiples of 200us up to 200us*255 = 51ms (or more)
;------------------------------------------------------------------------------------------------------------------------

NDELAY
MOVWF N ; N is delay multiplier
NOTOVER CALL DELAY200 ; Call for 200us
DECFSZ N, 1 ; Decrease N by 1
GOTO NOTOVER ; The delay isn't done
RETURN

;------------------------------------------------------------------------------------------------------------------------
; FIXED 200us DELAY (Possibly more due to execution time of the DECFSZ instruction.)
;------------------------------------------------------------------------------------------------------------------------

DELAY200
MOVLW 0x42 ; 66 LOOPS
MOVWF FIXDELAY ; 200us fixed delay
NOTDONE200 DECFSZ FIXDELAY, 1 ; Decrement of FIXDELAY
GOTO NOTDONE200 ; If 200us isn't up go back to NOTDONE200
RETURN ; If 200us is up then return to instruction.

;------------------------------------------------------------------------------------------------------------------------
; FIXED 50us DELAY (Possibly more due to execution time of the DECFSZ instruction.)
;------------------------------------------------------------------------------------------------------------------------

DELAY50
MOVLW 0x10 ; 16 LOOPS
MOVWF FIXDELAY ; 50us fixed delay
NOTDONE50 DECFSZ FIXDELAY, 1 ; Decrement of FIXDELAY
GOTO NOTDONE50 ; If 50us isn't up go back to NOTDONE50
RETURN ; If 50us is up then return to instruction.

;------------------------------------------------------------------------------------------------------------------------
; FIXED 20us DELAY (Possibly more due to execution time of the DECFSZ instruction.)
;------------------------------------------------------------------------------------------------------------------------

DELAY20
MOVLW 0x7 ; 7 LOOPS
MOVWF FIXDELAY ; 50us fixed delay
NOTDONE20 DECFSZ FIXDELAY, 1 ; Decrement of FIXDELAY
GOTO NOTDONE20 ; If 50us isn't up go back to NOTDONE50
RETURN ; If 50us is up then return to instruction.

;------------------------------------------------------------------------------------------------------------------------
; Fast Directive to write characters to LCD.
;------------------------------------------------------------------------------------------------------------------------

PUTCHAR
MOVWF PORTB ; A quicker way of writing characters to LCD.
CALL ENABLEPULSE
CALL CHKBUSY
RETURN

;------------------------------------------------------------------------------------------------------------------------
; Subroutine to check for the BUSY flag. Mostly used for instructions that follows up a character write.
;------------------------------------------------------------------------------------------------------------------------

CHKBUSY
bcf PORTC, 0 ; Clear RS to select Instruction Reg.
bsf PORTC, 1 ; Set R/W to read.

banksel TRISB ; Select Bank for TRISC.
movlw 0xFF ; Define all PORTC Pins as Inputs.
movwf PORTB

banksel PORTC ; Select Bank for PORTC.
bsf PORTC, 2 ; I tried to write my own code for this part initially but I wasn't successful.
movf PORTB, w ; Therefore, I implemented a portion of Peter Ouwehand's LCD Code.
bcf PORTC, 2 ; Will look more into the BUSY flag of the LCD.
andlw 0x80 ; Credits to Peter Ouwehand for his code here. :)
btfss STATUS, Z
goto CHKBUSY

banksel TRISB ; Select Bank for TRISB.
movlw 0x00 ; Define all PORTC Pins as Outputs.
movwf PORTB

banksel PORTC ; Select Bank for PORTA, B, and C.
bsf PORTC, 0 ; Set RS to select Data Register.
bcf PORTC, 1 ; Clear R/W to write.

return

;------------------------------------------------------------------------------------------------------------------------
; Position Cursor to the next line.
;------------------------------------------------------------------------------------------------------------------------

nextline
banksel PORTC
bcf PORTC, 0 ; Select Instructions Register.
bcf PORTC, 1 ; Select Write.

movlw b'11000000' ; Shift cursor to second line at 0x40 RAM address on LCD.
call PUTCHAR

return

;------------------------------------------------------------------------------------------------------------------------
; Clear screen and Cursor home.
;------------------------------------------------------------------------------------------------------------------------

clrscreen
banksel PORTC
bcf PORTC, 0 ; Clear RS to select Instructions Register.
bcf PORTC, 1 ; Clear R/W to select Write.

banksel PORTB
MOVLW B'00000001' ; Clear Display
call PUTCHAR

return

;------------------------------------------------------------------------------------------------------------------------
; Position Cursor to home position.
;------------------------------------------------------------------------------------------------------------------------

cursorhome
banksel PORTC
bcf PORTC, 0 ; Select Instructions Register.
bcf PORTC, 1 ; Select Write.

movlw b'00000010' ; Position cursor to home position.
call PUTCHAR

return

;------------------------------------------------------------------------------------------------------------------------
; A test message.
;------------------------------------------------------------------------------------------------------------------------

testmsg movlw D'49' ; Displays "I m working fine"
call PUTCHAR ; "Line 2 is alrite"
movlw A' '
call PUTCHAR
movlw A'm'
call PUTCHAR
movlw A' '
call PUTCHAR
movlw A'w'
call PUTCHAR
movlw A'o'
call PUTCHAR
movlw A'r'
call PUTCHAR
movlw A'k'
call PUTCHAR
movlw A'i'
call PUTCHAR
movlw A'n'
call PUTCHAR
movlw A'g'
call PUTCHAR
movlw A' '
call PUTCHAR
movlw A'f'
call PUTCHAR
movlw A'i'
call PUTCHAR
movlw A'n'
call PUTCHAR
movlw A'e'
call PUTCHAR

call nextline

movlw A'L'
call PUTCHAR
movlw A'i'
call PUTCHAR
movlw A'n'
call PUTCHAR
movlw A'e'
call PUTCHAR
movlw A' '
call PUTCHAR
movlw A'2'
call PUTCHAR
movlw A' '
call PUTCHAR
movlw A'i'
call PUTCHAR
movlw A's'
call PUTCHAR
movlw A' '
call PUTCHAR
movlw A'a'
call PUTCHAR
movlw A'l'
call PUTCHAR
movlw A'r'
call PUTCHAR
movlw A'i'
call PUTCHAR
movlw A't'
call PUTCHAR
movlw A'e'
call PUTCHAR

return

;------------------------------------------------------------------------------------------------------------------------
; End of programme.
;------------------------------------------------------------------------------------------------------------------------

end
 

Hi,

Have just built the code ok and it ran straight away on a simulator.

Must assume you have something wrong with your simulator or hardware build.

Although that routine works fine its more usual to use the lcd in 4 bit mode so you save the 4 i/o lines for other uses.
 

Attachments

  • lcd.jpg
    lcd.jpg
    30.3 KB · Views: 112
Thank you, for help me simulate it .... i think is my hardware build problem ... can i know using what software to simulate the coding?

---------- Post added at 11:15 ---------- Previous post was at 10:16 ----------

Can help me check this for 4-bit interface for the lcd?


list P=P16F876A
INCLUDE "P16F876A.INC"
; ERRORLEVEL -302
__CONFIG _PWRTE_ON & _HS_OSC & _WDT_OFF & _LVP_OFF & _BODEN_OFF; configuration switches
;*******************************************
; 4 BIT LCD interface with PIC
; only port B is implemented
; RB7-RB4 = DB7-DB4 ; RB3=E ; RB2=RW ; RB1=RS
;*******************************************
;decleration
BIT_E EQU 3
BIT_RW EQU 2
BIT_RS EQU 1

TEMP EQU 0x020 ;TEMP STORAGE

V_DLY EQU 0x021 ; Variable for DLY
V_DLY1 EQU 0x022 ; for DLY1


org 0h ;start of program at
movlw 0x00
movwf PCLATH
goto START

;initialise
INITPIC
bsf STATUS,RP1 ;sellect bank 1
movlw b'00000000'
movwf PORTB ;set as output
bcf STATUS,RP1 ;select bank 0
clrf PORTB
return

INITLCD ;INITIALIZE LCD
MOVLW 0xFE ;WAIT LONG
CALL DLY1 ;254 * 0.5 = DELAY OF 127ms

;BUSY FLAG CAN'T BE CHECKED RIGHT NOW

MOVLW B'00111000' ;FUNCTION SET - 8 BIT, BIT_E = HIGH
MOVWF PORTB
BCF PORTB,BIT_E ; BIT_E = LOW

MOVLW 0x0A ;wait for about 5ms
CALL DLY1


MOVLW B'00111000' ;FUNCTION SET - 8 BIT, BIT_E=HIGH
MOVWF PORTB
BCF PORTB,BIT_E

MOVLW 0x02 ;
CALL DLY1


MOVLW B'00111000' ;FUNCTION SET - 8 BIT, BIT_E = high
MOVWF PORTB
BCF PORTB,BIT_E ;BIT_E=LOW
;above i have done same command 3 times, this may be not necessary, i just followed datasheet

CALL LCDBUSY ;BUSY FLAG CAN BE CHECKED NOW!

MOVLW B'00101000' ;FUNCTION SET - 4 BIT ,BIT_E = HIGH
MOVWF PORTB
BCF PORTB,BIT_E
;..........EVERY INSTRUCTION WILL BE OF 2 CYCLE FROM HERE
CALL LCDBUSY ;..FUNCTION SET..
MOVLW B'00101000'
MOVWF PORTB
BCF PORTB,BIT_E ;1 CYCLE COMPLETE
MOVLW B'10001000'
MOVWF PORTB
BCF PORTB,BIT_E ;2 CYCLE

CALL LCDBUSY ;DISPLAY CONTROL -
MOVLW B'00001000' ; 1ST NIBBLE AND BIT_E=1,BIT_RW=0,BIT_RS=0
MOVWF PORTB
BCF PORTB,BIT_E ;1 CYCLY
MOVLW B'11111000' ;DISPLAY CONTROL NIBBLE (DISP=ON,CURSOR=ON,BLINK=ON) AND BIT_E=1,BIT_RW=0,BIT_RS=0
MOVWF PORTB
BCF PORTB,BIT_E ;2 CYCLE

CALL LCDBUSY ;CLEAR DISPLAY AND HOME CURSOR
MOVLW B'00001000'
MOVWF PORTB
BCF PORTB,BIT_E
MOVLW B'00011000'
MOVWF PORTB
BCF PORTB,BIT_E

CALL LCDBUSY ;ENTRY SET -
MOVLW B'00001000' ; BIT_E=1,BIT_RW=0,BIT_RS=0
MOVWF PORTB
BCF PORTB,BIT_E ;1 CYCLE
MOVLW B'01101000' ;ENTRY SET - INCREMENT,NO DISP SHIFT(CUR SHIFT),BIT_E=1,BIT_RW=0,BIT_RS=0
MOVWF PORTB
BCF PORTB,BIT_E ;2 CYCLE

RETURN

LCDBUSY
BSF STATUS,RP0 ;SELLECT BANK 1
MOVLW B'11110000'
MOVWF PORTB ;SET RB7-RB4 INPUT
BCF STATUS,RP0 ;SELLECT BANK 0

BSF PORTB,BIT_RW
BCF PORTB,BIT_RS

BSF PORTB,BIT_E
MOVF PORTB,W ;READ
BCF PORTB,BIT_E ;1 CYCLE complete
BSF PORTB,BIT_RW
BCF PORTB,BIT_RS
BSF PORTB,BIT_E
NOP ;DO NOTTHING COZ BUSY FLAG IS IN FIRST NIBBLE
BCF PORTB,BIT_E ;2nd CYCLE complete

ANDLW 0x80
BTFSS STATUS,Z ;CHECK BUSY
GOTO LCDBUSY ;LOOP IF BUSY

BCF PORTB,BIT_RW ;**
BSF STATUS,RP0 ;NOT BUSY SO MAKE PORT B O/P
MOVLW 0x000
MOVWF PORTB
BCF STATUS,RP0

RETURN

;LCD COMMANDS--- i have written routines for only 2 basic commands,
; one has to write routines for shifting display/crussor, home crussor etc.
;it's easy just follow datasheet and i'll include it someday!

LINE2 ;by selecting DDRAM address = 0x40 in case of 16x4 line LCD, see datasheet of HD44780
MOVLW B'11001000'
MOVWF PORTB
BCF PORTB,BIT_E
MOVLW B'00001000'
MOVWF PORTB
BCF PORTB,BIT_E
RETURN

LCDWRITE ;Writes data/character in W register to sellected CG/DD RAM, see its use in START routine
MOVWF TEMP
CALL LCDBUSY
MOVF TEMP,W
ANDLW B'11110000'
IORLW B'00001010' ;BIT_E=1,BIT_RW=0,BIT_RS=1
MOVWF PORTB
BCF PORTB,BIT_E ;1 CYCLE COMPLETE
SWAPF TEMP,W ;SWAP NIBBLES AND STORE IN W REGISTER
ANDLW B'11110000'
IORLW B'00001010' ;BIT_E=1,BIT_RW=0,BIT_RS=1
MOVWF PORTB
BCF PORTB,BIT_E ;2 CYCLE COMPLETE

RETURN



;Delay routines
;500uS delay with 4MHz
DLY
MOVLW D'165'
MOVWF V_DLY
DLY_LOOP
DECFSZ V_DLY, F
GOTO DLY_LOOP
RETURN

; w times DLY
DLY1
MOVWF V_DLY1
DLY1_LOOP
CALL DLY
DECFSZ V_DLY1, F
GOTO DLY1_LOOP
RETURN


START
CALL INITPIC ;initialize PIC
CALL INITLCD ;initialize LCD

MOVLW D'72' ;H ;Write to DDRAM which is displayed in LCD, DDRAM already sellected at initialization
CALL LCDWRITE

MOVLW D'69' ;E
CALL LCDWRITE

MOVLW D'76' ;L
CALL LCDWRITE

MOVLW D'76' ;L
CALL LCDWRITE

MOVLW D'79' ;O
CALL LCDWRITE

MOVLW D'32' ; 0x20 space
CALL LCDWRITE

MOVLW D'87' ;W
CALL LCDWRITE

MOVLW D'79' ;O
CALL LCDWRITE

MOVLW D'82' ;R
CALL LCDWRITE

MOVLW D'76' ;L
CALL LCDWRITE

MOVLW D'68' ;D
CALL LCDWRITE

MOVLW D'33' ;!
CALL LCDWRITE

CALL LINE2 ;set crussor to line 2
MOVLW D'76' ;L
CALL LCDWRITE
MOVLW D'73' ;I
CALL LCDWRITE
MOVLW D'78' ;N
CALL LCDWRITE
MOVLW D'69' ;E
CALL LCDWRITE
MOVLW D'32' ;space
CALL LCDWRITE
MOVLW D'50' ;2
CALL LCDWRITE

LOOP NOP
GOTO LOOP ;endless loop

END
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top