emperror123
Member level 5
dear all
i am facing a problem on my coding, hopefully i can get ur help
lcd display hello and ask user key in password
then when user enter correct password, stepper motor will turn
when press any key, the stepper will turn back again
if user key in wrong password, LCD will display wrong password, try again
just would like to ask
how should i do, since i done LCD part and motor, but problem is how do i connect 4x4 keypad together with it
my sample code as below
lcd.asm
digibyte.inc
keypad.inc
lcd.inc
motor.inc
hopefully can get ur feedback soon
i am facing a problem on my coding, hopefully i can get ur help
lcd display hello and ask user key in password
then when user enter correct password, stepper motor will turn
when press any key, the stepper will turn back again
if user key in wrong password, LCD will display wrong password, try again
just would like to ask
how should i do, since i done LCD part and motor, but problem is how do i connect 4x4 keypad together with it
my sample code as below
lcd.asm
Code:
;*********************** Header ***********************************
; DEFINING VARIABLES IN PROGRAM
list p=16F877A
#include p16f877a.inc
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
CBLOCK 0x20 ; Block of variables starts at address 20h
HIcnt ; Belongs to macro "pausems"
LOcnt
LOOPcnt
LCDbuf ; Belongs to functions "LCDxxx"
LCDtemp
LCDportBuf ; LCD Port Buffer
Digtemp ; Belongs to macro "digbyte"
Dig0
Dig1
Dig2
Dig3
set1
set2
set3
set4
col1
col2
col3
col4
temp
temp1
temp2
temp3
temp4
D1
D2
D3
ENDC ; End of block
LCD1 EQU b'10000001'
LCDport EQU PORTD ; LCD is on PORTB (4 data lines on RB0-RB3)
RS EQU 4 ; RS line connected to RB4
EN EQU 5 ; EN line connected to RB5
;**********************************************************************
ORG 0x0000 ; Reset vector address
nop
goto main ; Go to beginning of the program (label "main")
;**********************************************************************
include "lcd.inc"
include "digbyte.inc"
include "keypad.inc"
include "motor.inc"
;**********************************************************************
main
bsf STATUS,RP0 ; Bank0 active only
bcf STATUS,RP1
clrf TRISA
movlw b'11110000'
movwf TRISB
bcf STATUS,RP1
;movlw .23
movwf temp ; Move arbitrary value to variable
; is to be displayed on LCD
lcdinit ; LCD initialization
counter
Loop
lcdcmd 0x01 ; Instruction to clear LCD
lcdtext 1, "Welcome" ; Write text from the begin
; ning of the first line
lcdtext 2, "Key in password" ; Write text from the beginning of
call delay
movlw .1
movwf set1
movlw .1
movwf set2
movlw .1
movwf set3
movlw .1
movwf set4
call check
movwf temp1
call check
movwf temp2
call check
movwf temp3
call check
movwf temp4
movf set1, 0
movwf temp
movf temp1, 0
subwf temp, 1
btfss STATUS, Z
goto wrong
movf set2, 0
movwf temp
movf temp2, 0
subwf temp, 1
btfss STATUS, Z
goto wrong
movf set3, 0
movwf temp
movf temp3, 0
subwf temp, 1
btfss STATUS, Z
goto wrong
movf set4, 0
movwf temp
movf temp4, 0
subwf temp, 1
btfss STATUS, Z
goto wrong
goto motor
;rotate the motor
;if the password is wrong,ask the user to key in again
; the second line
;pausems .2000 ; 2 sec. delay
wrong lcdcmd 0x01 ; Instruction to clear LCD
lcdtext 1, "Wrong password" ; Write text from the begin
; ning of the first line
lcdtext 2, "Key in again" ; Write text from the beginning of
; the second line
;lcdbyte temp ; Write variable (dec.)
;lcdtext 0, " C" ; Write text after cursor
;pausems .2000 ; 2 sec. delay
call delay
goto Loop
delay movlw D'10' ;
movwf D3 ;1 inst cycle
movlw D'20'
movwf D2 ;1 inst cycle
movlw D'100'
movwf D1 ;1 inst cycle
decfsz D1,1 ;when D1 = 0, skip next step
goto $-1 ;2 inst cycle
decfsz D2,1 ;when D2 = 0, skip next step
goto $-5 ;2 inst cycle
decfsz D3, 1 ;when D3 = 0, skip next step
goto $-9 ;2 inst cycle
return
;**********************************************************************
end ; End of program
digibyte.inc
Code:
;************************* Header *******************************************
;****************************************************************************
; DEFINING VARIABLES IN PROGRAM
w_temp EQU 0x7D ; Variable for saving W register
status_temp EQU 0x7E ; Variable for saving STATUS register
pclath_temp EQU 0x7F ; Variable for saving PCLATH register
CBLOCK 0x20 ; Block of variables starts at address 20h
Digtemp
Dig0 ; Variables for displaying digits - LSB
Dig1
Dig2
Dig3 ; Variables for displaying digits - MSB
Dval ; Counter value
One ; Auxiliary variable which determines which
; display is to be switched on
ENDC ; End of block of variables
poc_vr EQU .99 ; Initial counter value is 99
include "Digbyte.inc"
;****************************************************************************
ORG 0x0000 ; First instruction address
goto main ; Jump to label "main"
;****************************************************************************
ORG 0x0004 ; Interrupt vector address
movwf w_temp ; Move w register to w_temp register
movf STATUS,w ; Move STATUS register to status_temp
movwf status_temp ; register
movf PCLATH,w ; Move PCLATH register to pclath_temp
movwf pclath_temp ; register
; Start of interrupt routine...
BANKSEL TMR0
movlw .100
movwf TMR0
bcf INTCON, T0IF
bcf PORTA, 0
bcf PORTA, 1
btfsc One, 0
goto Lsdon
goto Msdon
Lsdon
incf One, f
movlw HIGH (Bcdto7seg)
movwf PCLATH
digbyte Dval
movf Dig1, w
call Bcdto7seg ; Place L1 mask on the PORTD
movwf PORTD
bsf PORTA, 1
goto ISR_end
Msdon
incf One, f
movlw HIGH (Bcdto7seg)
movwf PCLATH
digbyte Dval
movf Dig0, w
call Bcdto7seg ; Place LO mask on the PORTD
movwf PORTD
bsf PORTA, 0
goto ISR_end
; End of interrupt routine...
ISR_end
movf pclath_temp,w ; PCLATH register is given its original
movwf PCLATH ; state
movf status_temp,w ; STATUS register is given its original
movwf STATUS ; state
swapf w_temp,f ; W register is given its original
; state
swapf w_temp,w
retfie ; Return from interrupt routine
main
banksel ANSEL ; Selects bank containing ANSEL
clrf ANSEL ; All pins are digital
clrf ANSELH
BANKSEL TRISA
movlw b'11111100' ; RA0 and RA1 are configured as outputs and
; used for 7-segment display multiplexing
; RA2 is input push-button for initializa
; tion
movwf TRISA
clrf TRISD
BANKSEL OPTION_REG
movlw b'10000110' ; TMR0 is incremented each 32us (Fclk=8MHz)
movwf OPTION_REG
BANKSEL PORTA
movlw poc_vr
movwf Dval ; Dval contains counter value
movlw b'00000001' ; Initializes variable specifying display
movwf One ; to switch on
movwf PORTA
movlw .100
movwf TMR0 ; TMR0 interrupt appr.every 10ms
bsf INTCON, GIE ; Global interrupt enabled
bsf INTCON, T0IE ; Timer TMR0 interrupt enabled
bcf INTCON, T0IF
Loop
btfss One, 3 ; Falling edge encountered?
goto Dec ; Yes! Go to Dec
btfss PORTA, 2 ; Counter reset button pressed?
goto Reset ; Yes! Go to Reset
goto Loop
; Decrement Dval counter by 1
Dec
btfss One, 3
goto Dec
movf Dval, f
btfsc STATUS, Z ; Is Dval equal to 0?
goto Loop ; If it is, go to loop and wait for T2
decf Dval, f ; If Dval not equal to 0, decrement it by 1
goto Loop
Reset
btfss PORTA, 2 ; Wait for rising edge
goto Reset
movlw poc_vr
movwf Dval ; Write initial value to counter
goto Loop
;****************************************************************************
ORG 0x0300 ; Lookup table is at the top of third page, but
; can be placed at some other place, it is impor
; tant to have it all on one page
Bcdto7seg
addwf PCL, f
DT 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
;****************************************************************************
END ; End of programMacro "digbyte":
digbyte MACRO arg0
LOCAL Exit0
LOCAL Exit1
LOCAL Exit2
clrf Dig0
clrf Dig1
clrf Dig2
clrf Dig3
movf arg0, w
movwf Digtemp
movlw .100
Exit2
incf Dig2, f
subwf Digtemp, f
btfsc STATUS, C
goto Exit2
decf Dig2, f
addwf Digtemp, f
Exit1
movlw .10
incf Dig1, f
subwf Digtemp, f
btfsc STATUS, C
goto Exit1
decf Dig1, f
addwf Digtemp, f
Exit0
movf Digtemp, w
movwf Dig0
ENDM
keypad.inc
Code:
check bsf PORTB, 0
btfsc PORTB, 7
retlw .1
btfsc PORTB, 6
retlw .2
btfsc PORTB, 5
retlw .3
btfsc PORTB, 4
retlw .15
bsf PORTB, 1
btfsc PORTB, 7
retlw .4
btfsc PORTB, 6
retlw .5
btfsc PORTB, 5
retlw .6
btfsc PORTB, 4
retlw .14
bsf PORTB, 2
btfsc PORTB, 7
retlw .7
btfsc PORTB, 6
retlw .8
btfsc PORTB, 5
retlw .9
btfsc PORTB, 4
retlw .13
bsf PORTB, 3
btfsc PORTB, 7
retlw .10
btfsc PORTB, 6
retlw .0
btfsc PORTB, 5
retlw .11
btfsc PORTB, 4
retlw .12
retlw .16
lcd.inc
Code:
;**********************************************************************
; Initialization must be done by using macro lcdinit before access
; ing LCD
;**********************************************************************
lcdinit MACRO
bcf STATUS, RP0 ; Bank0
bcf STATUS, RP1
clrf LCDportBuf
movf LCDportBuf, w
movwf LCDport
bsf STATUS, RP0 ; Bank1
bcf STATUS, RP1
clrf TRISD ; LCDport with output LCD
bcf STATUS, RP0 ; Bank0
bcf STATUS, RP1
; Function set (4-bit mode change)
movlw b'00100000'
movwf LCDbuf
swapf LCDbuf, w
movwf LCDportBuf
bcf LCDportBuf, RS
movf LCDportBuf, w
movwf LCDport
bsf LCDportBuf, EN
movf LCDportBuf, w
movwf LCDport
bcf LCDportBuf, EN
movf LCDportBuf, w
movwf LCDport
call Delay1ms ; 1 ms delay
; Function set (display mode set)
lcdcmd b'00101100'
call Delay1ms ; 1 ms delay
; Display ON/OFF Control
lcdcmd b'00001100'
call Delay1ms ; 1 ms delay
; Entry Mode Set
lcdcmd b'00000110'
call Delay1ms ; 1 ms delay
; Display Clear
lcdcmd b'00000001'
call delay
; Function set (4-bit mode change)
movlw b'00100000'
movwf LCDbuf
swapf LCDbuf, w
movwf LCDportBuf
bcf LCDportBuf, RS
movf LCDportBuf, w
movwf LCDport
bsf LCDportBuf, EN
movf LCDportBuf, w
movwf LCDport
bcf LCDportBuf, EN
movf LCDportBuf, w
movwf LCDport
call Delay1ms ; 1 ms delay
; Function set (display mode set)
lcdcmd b'00101100'
call Delay1ms ; 1 ms delay
; Display ON/OFF Control
lcdcmd b'00001100'
call Delay1ms ; 1 ms delay
; Entry Mode Set
lcdcmd b'00000110'
call Delay1ms ; 1 ms delay
; Display Clear
lcdcmd b'00000001'
call delay
ENDM
;**********************************************************************
; lcdcmd sends command to LCD (see the table on the previous page)
; lcdclr is the same as lcdcmd 0x01
;**********************************************************************
lcdcmd MACRO LCDcommand ; Send command to LCD
movlw LCDcommand
call LCDcomd
ENDM
LCDcomd
movwf LCDbuf
bcf LCDportBuf, RS
movf LCDportBuf, w
movwf LCDport
goto LCDwr
LCDdata
movwf LCDbuf
bsf LCDportBuf, RS
movf LCDportBuf, w
movwf LCDport
goto LCDwr
LCDwr
swapf LCDbuf, w
call SendW
movf LCDbuf, w
call SendW
return
SendW
andlw 0x0F
movwf LCDtemp
movlw 0xF0
andwf LCDportBuf, f
movf LCDtemp, w
iorwf LCDportBuf, f
movf LCDportBuf, w
movwf LCDport
call Delay1ms
bsf LCDportBuf, EN
movf LCDportBuf, w
movwf LCDport
bcf LCDportBuf, EN
movf LCDportBuf, w
movwf LCDport
call Delay1ms
return
;**********************************************************************
; lcdtext writes text containing 16 characters which represents a
; macro argument. The first argument select selects the line in which
; text writing is to start. If select is 0, text writing starts from
; cursor current position.
;**********************************************************************
lcdtext MACRO select, text ; This macro writes text from cursor
; current position. Text is specified
; in argument consisting of 16 charac
; ters
local Message
local Start
local Exit
local i=0
goto Start
Message DT text ; Create lookup table from arguments
DT 0
Start
IF (select == 1)
lcdcmd b'10000000'
ELSE
IF (select == 2)
lcdcmd b'11000000'
ENDIF
ENDIF
WHILE (i<16) ; Repeat conditional program compiling 16 times
call Message+i ; Read lookup table and place value in W
addlw 0
bz Exit ; until 0 is read
call LCDdata ; Call routine displaying W on LCD
i=i+1
ENDW
Exit
ENDM
;**********************************************************************
; This macro writes value in size of 1 byte on LCD
; excluding leading zeros
;**********************************************************************
lcdbyte MACRO arg0
digbyte arg0 ; A hundred is in Dig2,
; A ten is in Dig1 and one in Dig0
movf Dig2, w
addlw 0x30
call LCDdata
movf Dig1, w ; If digit is 0 move cursor
addlw 0x30
call LCDdata
movf Dig0, w ; If digit is 0 move cursor
addlw 0x30
call LCDdata
ENDM
;**********************************************************************
; 1ms Delay
Delay1ms:
movlw .10
movwf LOOPcnt
Delay10us:
nop ;1us
nop ;1us
nop ;1us
nop ;1us
nop ;1us
nop ;1us
nop ;1us
decfsz LOOPcnt, f ;1us
goto Delay10us ;2us
return
motor.inc
Code:
motor
movlw D'120'
movwf counter
motor1 bsf PORTA,1
bcf PORTA,2
bcf PORTA,3
bsf PORTA,4
nop
bcf PORTA,1
bsf PORTA,2
bsf PORTA,3
bcf PORTA,4
nop
decfsz counter,1
goto motor1
hopefully can get ur feedback soon