
; @ 11.0592mhz
;d4 - p2.0
;d5 - p2.1
;d6 - p2.2
;d7 - p2.3
;en - p2.7
;rs - p2.5

var1 equ r0		;var1 uses regiser r0 of bank 0
temp equ r1		;temp uses regiser r1 of bank 0
delay equ r2		;delay uses regiser r2 of bank 0
temp1 equ r3		;temp1 uses regiser r3 of bank 0
temp2 equ r4		;temp2 uses regiser r4 of bank 0
counter1 equ r5         ;counter1 uses regiser r5 of bank 0
counter2 equ r6         ;counter2 uses regiser r6 of bank 0

lcd_port equ p2         ;lcd connected to port2
en equ p2.7             ;enable connected to p2.7
rs equ p2.5             ;register select to p2.5

;  r/w pin connected to ground

;*******************************************************
;-------------------- "main program" -------------------
;*******************************************************

org 00h        ; program start here when power up
jmp main

org 03h        ; program start here when external interrupt 0 occur
;jmp one
mov A, #1
mov A, #2
reti

org 13h        ; program start here when external interrupt 1 occur
jmp two


main:
mov ie,#85h          ; enable external interrupts
call lcd_init        ; initialize lcd
mov dptr,#str_enter  ; point to string 1
acall lcd_str        ; display string
mov dptr,#str_enter1 ; point to string 2
mov a,#0c0h          ; lcd 2nd line address
acall lcd_cmd        ; comand to lcd
acall lcd_str        ; display string 
mov dptr,#str_enter2 ; point to string 3
mov a,#0c8h          ; lcd 2nd line location address
acall lcd_cmd        ; comand to lcd
acall lcd_str        ; display string 

jmp $




;*******************************************************
;-----------------" reseting the lcd "------------------
;*******************************************************

lcd_reset:                  ;lcd reset sequence
        mov lcd_port, #0ffh
        mov delay,#20           ;20ms delay
        acall delayms
        mov lcd_port, #83h      ;data = 30h, en = 1, first init
        mov lcd_port, #03h      ;data = 30h, en = 0
        mov delay,#15           ;delay 15ms
        acall delayms
        mov lcd_port, #83h      ;second init, data = 30h, en = 1
        mov lcd_port, #03h      ;data = 30h, en = 0
        mov delay,#5            ;delay 5ms
        acall delayms
        mov lcd_port, #83h      ;third init
        mov lcd_port, #03h
        mov delay,#5            ;delay 5ms
        acall delayms
        mov lcd_port, #82h      ;select data width (20h for 4bit)
        mov lcd_port, #02h      ;data = 20h, en = 0
        mov delay,#5            ;delay 5ms
        acall delayms
        ret

;*******************************************************
;-----------------" lcd initilizing "-------------------
;*******************************************************


lcd_init:
        acall lcd_reset         ;call lcd reset sequence
        mov a,#28h              ;4-bit, 2 line, 5x7 dots
        acall lcd_cmd           ;call lcd command
        mov a,#0eh              ;display on cursor off
        acall lcd_cmd           ;call lcd command
        mov a,#06h              ;set entry mode (auto increment)
        acall lcd_cmd           ;call lcd command
        mov a,#80h              ;bring cursor to line 1
        acall lcd_cmd           ;call lcd command
        ret

;*******************************************************
;----------------" lcd command write "------------------
;*******************************************************

lcd_cmd:                  ;lcd command routine
        mov temp,a            ;save a copy of command to temp
        swap a                ;swap to use higher nibble
        anl a,#0fh            ;mask the first four bits
        add a,#80h            ;enable = 1, rs = 0
        mov lcd_port,a        ;move it to lcd port
        anl a,#0fh            ;enable = 0, rs = 0
        mov lcd_port,a        ;move to lcd port

        mov a,temp            ;reload the command from temp
        anl a,#0fh            ;mask first four bits
        add a,#80h            ;enable = 1
        mov lcd_port,a        ;move to port
        anl a,#0fh            ;enable = 0
        mov lcd_port,a        ;move to lcd port

        mov delay,#2          ;delay 2 ms
        acall delayms
        ret

;*******************************************************
;------------------" lcd data write "-------------------
;*******************************************************

lcd_dat:                  ;lcd data routine
        mov temp,a            ;keep copy of data in temp
        swap a                ;we need higher nibble
        anl a,#0fh            ;mask first four bits
        add a,#0a0h           ;enable = 1, rs = 1
        mov lcd_port,a        ;move to lcd port
        nop
        clr en                ;enable = 0

        mov a,temp            ;reload the data from temp
        anl a,#0fh            ;we need lower nibble now
        add a,#0a0h           ;enable = 1, rs = 1
        mov lcd_port,a        ;move to lcd port
        nop
        clr en                ;enable = 0

        mov delay,#2          ;delay 2ms
        acall delayms
        ret


;*******************************************************
;--------------" delay of milli seconds "---------------
;*******************************************************

delayms:
mov var1,#230
d:
nop
nop
djnz var1,d
djnz delay,delayms
ret

;*******************************************************
;--------------" displaying string data "---------------
;*******************************************************

lcd_str:
clr a
movc a,@a+dptr
jz lcd_str_end
inc dptr
acall lcd_dat
sjmp lcd_str
lcd_str_end:
ret


one:
inc counter1
mov delay,#250           ;delay 250ms
acall delayms
mov a,counter1          ; converting decimal no into ascii to display on lcd
mov b,#10
div ab
mov temp2,b
mov b,#10
div ab
add a,#30h
mov temp1,a
mov a,#0c4h
acall lcd_cmd
mov a,temp1
acall lcd_dat
mov a,b
add a,#30h
acall lcd_dat
mov a,temp2
add a,#30h
acall lcd_dat
reti

two:
inc counter2
mov delay,#250           ;delay 250ms
acall delayms
mov a,counter2           ; converting decimal no into ascii to display on lcd
mov b,#10
div ab
mov temp2,b
mov b,#10
div ab
add a,#30h
mov temp1,a
mov a,#0cch
acall lcd_cmd
mov a,temp1
acall lcd_dat
mov a,b
add a,#30h
acall lcd_dat
mov a,temp2
add a,#30h
acall lcd_dat
reti

;*******************************************************
;------------------" look up tables "-------------------
;*******************************************************

str_enter:
db "hello world", 41, 0C5h, 41, 11000000b, 0h

str_enter1:
db "C1= ",0h

str_enter2:
db "C2= ",0h

end



