navneet_2040
Newbie level 3
lcd 4 bits
; plz tell me why this code is not working
;**********************************************************************
; This file is a basic code template for assembly code generation *
; on the PICmicro PIC16F72. This file contains the basic code *
; building blocks to build upon. *
; *
; If interrupts are not used all code presented between the ORG *
; 0x004 directive and the label main can be removed. In addition *
; the variable assignments for 'w_temp' and 'status_temp' can *
; be removed. *
; *
; Refer to the MPASM User's Guide for additional information on *
; features of the assembler (Document DS33014). *
; *
; Refer to the respective PICmicro data sheet for additional *
; information on the instruction set. *
; *
;**********************************************************************
; *
; Filename: lcd.asm *
; Date: 9-12-08 *
; File Version: *
; *
; Author: Navneet Kumar *
; Company: *
; *
; *
;**********************************************************************
; *
; Files required: *
; *
; *
; *
;**********************************************************************
; *
; Notes: *
; *
; *
; *
; *
;**********************************************************************
list p=16f72 ; list directive to define processor
#include <p16f72.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDTEN_OFF & _BODEN_OFF & _PWRTEN_ON & _RC_OSC
; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;***** VARIABLE DEFINITIONS
w_temp EQU 0x20 ; variable used for context saving
w_temp1 EQU 0xA0 ; reserve bank1 equivalent of w_temp
status_temp EQU 0x21 ; variable used for context saving
;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
bcf STATUS,RP0 ; ensure file register bank set to 0
movwf status_temp ; save off contents of STATUS register
; isr code can go here or be located as a call subroutine elsewhere
bcf STATUS,RP0 ; ensure file register bank set to 0
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
main
; remaining code goes here, main code is starting from here, 16x2 lcd
bsf STATUS,RP0 ; we select bank1 to put values in TRIS registers
movlw 0x00
movwf TRISB ; setting portb as output, b0 to b3 are connected to lcd data pins d4-d7
movlw 0x00
movwf TRISC ; setting portc as output
; RS is at PORtC,6
; EN is at PORTC,7
; R/W pin is at ground,so i am not using it
start:
bcf STATUS,RP0 ; we come in bank0 again
movlw 0x28 ; 28 is we want to send to lcd pin for 4 bit initialization
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay ; we call delay
movlw 0x01 ; 01 is we want to send to lcd
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay
movlw 0x0c ; 0c is we want to send to lcd
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay
movlw 0x80 ; 80 is we want to send to lcd,as starting address
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay
movlw #"A" ; character A is we want to send to lcd pin for 4 bit initialization
movwf 0x23 ; 0x23 is the memory location we use to store the data
call data1 ; we call data1 now because we are sending data now to lcd
call delay
movlw #"B" ; character B is we want to send to lcd pin for 4 bit initialization
movwf 0x23 ; 0x23 is the memory location we use to store the data
call data1 ; we call data1 now because we are sending data now to lcd
call delay
loop: ; endless loop,after coming here the lcd should show AB characters on display
goto loop
command1:
movlw 0x02 ; 2 is taken into 0x33 memory location for counter to go for 2 times
movwf 0x33
movf PORTB,w ; save to contents of portb to w
andlw 0xf0 ; mask upper 4 bits as we use lower 4 bits to send data on lcd
movwf 0x24 ; save that value to memory location 0x24
s1:
movf 0x23,w ; now put the value in 0x23 to w
andlw 0xf0 ; save the higher 4 bits,put in w,data in 0x23 reamins unchanged
movwf 0x29 ; put it in memory location 0x29
swapf 0x29,w ; swap it and put it in w
iorwf 0x24,w ; now OR it with the upper higher bits we saved of portb
movwf PORTB ; put that on portb, so lower 4 bits of portb have higher nibble of data
bcf PORTC,6 ; RS is cleared as to show that it is command
bsf PORTC,7 ; EN is set
call delay
call delay
bcf PORTC,7 ; EN is cleared
call delay
swapf 0x23,F ; now we get lower nibble of data
decf 0x33,F ; count is decremented
btfss STATUS,Z ; we check the count
goto s1 ; we go again to s1 and send the lower nibble of data on portb as we did for higher nibble
return
data1:
movlw 0x02 ; same as command1 , here RS is set high,other logic is same as command1
movwf 0x33
movf PORTB,w
andlw 0xf0
movwf 0x24
g1:
movf 0x23,w
andlw 0xf0
movwf 0x29
swapf 0x29,w
iorwf 0x24,w
movwf PORTB
bsf PORTC,6
bsf PORTC,7
call delay
call delay
bcf PORTC,7
call delay
swapf 0x23,F
decf 0x33,F
btfss STATUS,Z
goto g1
return
delay: ; this delay is enough that the lcd requires
movlw 0x0d
movwf 0x21
here:
decf 0x21,F
btfss STATUS,Z
goto here1
goto end1
here1:
movlw 0x0f
movwf 0x20
go1:
decf 0x20,F
btfss STATUS,Z
goto go1
goto here
end1: nop
return
END
; plz tell me why this code is not working
;**********************************************************************
; This file is a basic code template for assembly code generation *
; on the PICmicro PIC16F72. This file contains the basic code *
; building blocks to build upon. *
; *
; If interrupts are not used all code presented between the ORG *
; 0x004 directive and the label main can be removed. In addition *
; the variable assignments for 'w_temp' and 'status_temp' can *
; be removed. *
; *
; Refer to the MPASM User's Guide for additional information on *
; features of the assembler (Document DS33014). *
; *
; Refer to the respective PICmicro data sheet for additional *
; information on the instruction set. *
; *
;**********************************************************************
; *
; Filename: lcd.asm *
; Date: 9-12-08 *
; File Version: *
; *
; Author: Navneet Kumar *
; Company: *
; *
; *
;**********************************************************************
; *
; Files required: *
; *
; *
; *
;**********************************************************************
; *
; Notes: *
; *
; *
; *
; *
;**********************************************************************
list p=16f72 ; list directive to define processor
#include <p16f72.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDTEN_OFF & _BODEN_OFF & _PWRTEN_ON & _RC_OSC
; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;***** VARIABLE DEFINITIONS
w_temp EQU 0x20 ; variable used for context saving
w_temp1 EQU 0xA0 ; reserve bank1 equivalent of w_temp
status_temp EQU 0x21 ; variable used for context saving
;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
bcf STATUS,RP0 ; ensure file register bank set to 0
movwf status_temp ; save off contents of STATUS register
; isr code can go here or be located as a call subroutine elsewhere
bcf STATUS,RP0 ; ensure file register bank set to 0
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
main
; remaining code goes here, main code is starting from here, 16x2 lcd
bsf STATUS,RP0 ; we select bank1 to put values in TRIS registers
movlw 0x00
movwf TRISB ; setting portb as output, b0 to b3 are connected to lcd data pins d4-d7
movlw 0x00
movwf TRISC ; setting portc as output
; RS is at PORtC,6
; EN is at PORTC,7
; R/W pin is at ground,so i am not using it
start:
bcf STATUS,RP0 ; we come in bank0 again
movlw 0x28 ; 28 is we want to send to lcd pin for 4 bit initialization
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay ; we call delay
movlw 0x01 ; 01 is we want to send to lcd
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay
movlw 0x0c ; 0c is we want to send to lcd
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay
movlw 0x80 ; 80 is we want to send to lcd,as starting address
movwf 0x23 ; 0x23 is the memory location we use to store the data
call command1 ; the data on 0x23 is sent to lcd as command
call delay
movlw #"A" ; character A is we want to send to lcd pin for 4 bit initialization
movwf 0x23 ; 0x23 is the memory location we use to store the data
call data1 ; we call data1 now because we are sending data now to lcd
call delay
movlw #"B" ; character B is we want to send to lcd pin for 4 bit initialization
movwf 0x23 ; 0x23 is the memory location we use to store the data
call data1 ; we call data1 now because we are sending data now to lcd
call delay
loop: ; endless loop,after coming here the lcd should show AB characters on display
goto loop
command1:
movlw 0x02 ; 2 is taken into 0x33 memory location for counter to go for 2 times
movwf 0x33
movf PORTB,w ; save to contents of portb to w
andlw 0xf0 ; mask upper 4 bits as we use lower 4 bits to send data on lcd
movwf 0x24 ; save that value to memory location 0x24
s1:
movf 0x23,w ; now put the value in 0x23 to w
andlw 0xf0 ; save the higher 4 bits,put in w,data in 0x23 reamins unchanged
movwf 0x29 ; put it in memory location 0x29
swapf 0x29,w ; swap it and put it in w
iorwf 0x24,w ; now OR it with the upper higher bits we saved of portb
movwf PORTB ; put that on portb, so lower 4 bits of portb have higher nibble of data
bcf PORTC,6 ; RS is cleared as to show that it is command
bsf PORTC,7 ; EN is set
call delay
call delay
bcf PORTC,7 ; EN is cleared
call delay
swapf 0x23,F ; now we get lower nibble of data
decf 0x33,F ; count is decremented
btfss STATUS,Z ; we check the count
goto s1 ; we go again to s1 and send the lower nibble of data on portb as we did for higher nibble
return
data1:
movlw 0x02 ; same as command1 , here RS is set high,other logic is same as command1
movwf 0x33
movf PORTB,w
andlw 0xf0
movwf 0x24
g1:
movf 0x23,w
andlw 0xf0
movwf 0x29
swapf 0x29,w
iorwf 0x24,w
movwf PORTB
bsf PORTC,6
bsf PORTC,7
call delay
call delay
bcf PORTC,7
call delay
swapf 0x23,F
decf 0x33,F
btfss STATUS,Z
goto g1
return
delay: ; this delay is enough that the lcd requires
movlw 0x0d
movwf 0x21
here:
decf 0x21,F
btfss STATUS,Z
goto here1
goto end1
here1:
movlw 0x0f
movwf 0x20
go1:
decf 0x20,F
btfss STATUS,Z
goto go1
goto here
end1: nop
return
END