Google

May 8, 2008

Interfacing pic microcontroller with LCD

Here is the code to interface the pic 16F877A PROCESSOR to 16x2 LCD
this is a assembly code and it is properly working just you have to modify some of the routines as per your need.
As this is in assembly it doesn't take any space and also it use 8 bit mode.







; Program Module : Debug board
; Purpose : to show the content of data bus and adress bus
; Programmed By : Kamal Kant Singh
; Date of Start : 26th june,2007
;_________________________________________________________________________



PROCESSOR 16F877A
#include"p16F877A.inc"

__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON & _BODEN_OFF & _LVP_OFF & _DEBUG_OFF

cblock 0x0021
a1
a2
a3 ; used in sapr
a4
a5
a6 ;for macro PRINT
l1
l2
l3
w_temp
w_temp1
endc


PRINT macro label
local endom
local next_char
clrf a6
next_char
movf a6,w
call label
iorlw 0
btfsc STATUS,Z
goto endom
call send_char
incf a6,f
goto next_char
endom
endm

COMMA macro
movlw ','
call send_char
endm



en equ 2
rs equ 0
rw equ 1



org 0x0000
goto main
org 0x0004
goto main

main
movlw .20
call delayxms
banksel ADCON1
movlw 0x06
movwf ADCON1

banksel TRISA
movlw 0xFF
movwf TRISD ; port B for data bus contents while ports C,D are for address bus contents
movwf TRISB
movwf TRISC
movlw 0x30
movwf TRISA
clrf TRISE

banksel PORTB
clrf PORTB
clrf PORTD
clrf PORTC
clrf PORTE
clrf PORTA


; lcd operation
movlw 0x02
call send_cmd
call init_lcd
;entrance
movlw 0x82
call send_cmd
PRINT eklavya
check_again
movlw 0xC0
call send_cmd
PRINT address
call print_add
movlw 0xca
call send_cmd
PRINT dat
call print_dat
movlw 0xC5
call send_cmd


goto check_again




goto endop






init_lcd
movlw 0x28
call send_cmd
movlw 0x0c
call send_cmd
movlw 0x06
call send_cmd
;movlw 0x1c
;call send_cmd
return





send_cmd
movwf w_temp1
movlw 0x05
call delayxms
swapf w_temp1,w
bcf PORTE,rs
bcf PORTE,rw
movwf PORTA
bsf PORTE,en
nop
nop
bcf PORTE,en
movf w_temp1,w
movwf PORTA
bsf PORTE,en
nop
nop
bcf PORTE,en
movlw 0x03
call delayxms
return

send_char
movwf w_temp1
movlw 0x05
call delayxms
swapf w_temp1,w
bsf PORTE,rs
bcf PORTE,rw
movwf PORTA
bsf PORTE,en
nop
nop
bcf PORTE,en
movf w_temp1,w
movwf PORTA
bsf PORTE,en
nop
nop
bcf PORTE,en
movlw 0x03
call delayxms
return
print_add
movf PORTC,w
call sapr
call asc2
call send_char
movf a3,w
call asc2
call send_char
movf PORTD,w
call sapr
call asc2
call send_char
movf a3,w
call asc2
call send_char
return

print_dat
movf PORTB,w
call sapr
call asc2
call send_char
movf a3,w
call asc2
call send_char
return


sapr
movwf w_temp
andlw 0x0F
movwf a3
swapf w_temp,w
andlw 0x0F
return


delay3xms
movwf w_temp1
call delayxms
movf w_temp1,w
call delayxms
movf w_temp1,w
call delayxms
return


delayxms
movwf l1
up
call delay1ms
decfsz l1,f
goto up
return

delay1ms
movlw .62
movwf l2
delay4us
nop
decfsz l2,f
goto delay4us
return


asc2
addwf PCL,f
retlw '0'
retlw '1'
retlw '2'
retlw '3'
retlw '4'
retlw '5'
retlw '6'
retlw '7'
retlw '8'
retlw '9'
retlw 'A'
retlw 'B'
retlw 'C'
retlw 'D'
retlw 'E'
retlw 'F'

address
addwf PCL,f
retlw 'A'
retlw 'D'
retlw 'D'
retlw ':'
retlw 0

dat
addwf PCL,f
retlw 'D'
retlw 'T'
retlw ':'
retlw 0

eklavya
addwf PCL,f
retlw ' '
retlw 'E'
retlw 'k'
retlw 'l'
retlw 'a'
retlw 'v'
retlw 'y'
retlw 'a'
retlw ' '
retlw '1'
retlw '.'
retlw '0'
retlw ' '
retlw 0



endop
goto endop
end

You might be also interested in:
:: 8051 based project for electrical students
:: free and open source 8086 Microprocessor Emulator
:: Invoke function

No comments: