Microcontroller › 8051 › How to Editing Keypad alphabetic coding in hangman program
- This topic has 1 reply, 2 voices, and was last updated 13 years, 12 months ago by Anonymous.
-
AuthorPosts
-
December 11, 2010 at 3:31 pm #492byuParticipant
hello friends…
I wanna ask , how to make keypad to be alphabetic keypad from hangman game using atmel microcontroller
this is my program:
#include “m16def.inc”
.def temp =r22
.def dly =r23
.def dly1 =r24
.def dly2 =r25
.equ col1 =0b11101111 ;Column 1
.equ col2 =0b11011111 ;Column 2
.equ col3 =0b10111111 ;Column 3
.cseg
.org 0x0000rjmp main
main:
ldi r16,low(RAMEND) ;stack pointer
out SPL,r16
ldi r16,high(RAMEND)
out SPH,r16
ldi r16,0xff
out ddrc,r16
rcall InitLCD
ldi r16,0xff ; inisialisasi 7 segment, port A sebagai output
out ddra,r16
ldi r16,0b00111000
out ddrb,r16 ; set portB as input
sbi PORTB,3
sbi PORTB,4
sbi PORTB,5
rcall delay_hold
rcall delay_hold
rcall delay_hold
rjmp LED1LED1:
cbi PORTB,3
rcall delay_hold
rcall delay_hold
rcall delay_hold
rjmp LED2
LED2:
cbi PORTB,4
rcall delay_hold
rcall delay_hold
rcall delay_hold
rjmp LED3
LED3:
cbi PORTB,5
Start:
ldi r17,0x01 ;lcd clear
rcall SLcd
ldi r16,0xf0 ;PD[7:4]=output PD[3:0]=input init keypadS
out ddrd,r16
ldi r16,0x05
out TCCR0,r16 ; set timer with 1024 prescaler
ldi r16,0x00
sts hscore,r16
main_menu:
ldi r17,0x01 ; lcd clear
rcall SLcd
ldi ZL,low(2*welcome) ; welcome to
ldi ZH,high(2*welcome)
rcall WM1LCD
ldi ZL,low(2*text3) ; memory game
ldi ZH,high(2*text3)
rcall WM2LCD
rcall delay_hold
rcall delay_hold
ldi ZL,low(2*text4) ; select menu
ldi ZH,high(2*text4)
rcall WM1LCD
;
; Main Menu
;
check_menu:
sbic PINB,0
rjmp menu2
rcall new_game ; if true = new game
rcall delay_hold
rjmp check_menu
menu2:
sbic PINB,1
rjmp menu3
rcall high_score
rcall delay_hold ; if true = high score
rjmp check_menu
menu3:
sbic PINB,2
rjmp loop
rcall credits ; if true = credit
rcall delay_hold
loop:
rjmp check_menu;
; New Game
;
new_game:
ldi r17,0x01 ;lcd clear
rcall SLcd
rcall reseed ; menentukan angka baru
ldi ZL,low(2*text) ;write “new game”
ldi ZH,high(2*text)
rcall WM1LCD
ldi r16,0x00
sts score,r16
out portA,r16
ldi r21,0x03level1:
rcall next_level
rcall print_number
rcall delay_hold
ldi r17,0x01 ; lcd clear
rcall SLcd
rcall loopx
rcall benar
dec r21
cpi r21,0x00
brne level1
ldi ZL,low(2*right1) ; write next…
ldi ZH,high(2*right1)
rcall WM2LCD
ldi r21,0x03level2:
rcall next_level
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+5,r25
mov r17,r24
rcall print_number
lds r19,number+5
rcall WLcd
rcall delay_hold
ldi r17,0x01 ; lcd clear
rcall SLcd
rcall loopx
rcall check_keys
lds r20,number+5
cp r19,r20
brne salah1
rcall benar
dec r21
cpi r21,0x0
brne level2
ldi ZL,low(2*right1) ; write next…
ldi ZH,high(2*right1)
rcall WM2LCD
ldi r21,0x03level3:
rcall next_level
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+5,r25
mov r17,r24
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+6,r25
mov r17,r24
rcall print_number
lds r19,number+5
rcall WLcd
lds r19,number+6
rcall WLcd
rcall delay_hold
ldi r17,0x01 ; lcd clear
rcall SLcd
rcall loopx
rcall check_keys
lds r20,number+5
cp r19,r20
brne salah1
rcall check_keys
lds r20,number+6
cp r19,r20
brne salah1
rcall benar
dec r21
cpi r21,0x0
brne level3ldi r17,0x01 ; lcd clear
rcall SLcd
ldi ZL,low(2*right2) ; write next…
ldi ZH,high(2*right2)
rcall WM1LCD
ldi ZL,low(2*right3) ; write next…
ldi ZH,high(2*right3)
rcall WM2LCD
rcall delay_hold
rcall delay_hold
rjmp main_menucompare1:
mov r17,r16
swap r17
sts hscore,r17salah1:
lds r17,hscore
lds r16,score
sts score,r16
cp r16,r17 (*)(&
brge compare1
ldi ZL,low(2*text2) ; cupu, kamu kalah
ldi ZH,high(2*text2)
rcall WM2LCD
rcall delay_hold
rcall delay_hold
rcall delay_hold
ldi r16,0x00
out portA,r16
sts score,r16
rjmp main_menucmpr_akhir:
lds r17,hscore
lds r16,score
sts score,r16
cp r16,r17 (*)(&
brge compare1
retnext_level:
rcall delay_hold
ldi r17,0x01 ; lcd clear
rcall SLcd
ldi r16,3 ; set nilai lcprng a_{n+1} = (j a_n + k) mod m.
lds r17,seed ; r16 = j, r17= a_n= seed, m=256, r18=k
ldi r18,1
rcall lcprng ; count lcprng
rcall clamp
subi r25,-0x30
sts number+0,r25
mov r17,r24
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+1,r25
mov r17,r24
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+2,r25
mov r17,r24
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+3,r25
mov r17,r24
rcall lcprng
rcall clamp
subi r25,-0x30
sts number+4,r25
mov r17,r24
retprint_number:
lds r19,number+0
rcall WLcd
lds r19,number+1
rcall WLcd
lds r19,number+2
rcall WLcd
lds r19,number+3
rcall WLcd
lds r19,number+4
rcall WLcd
retloopx:
rcall check_keys ; check keys per number
lds r20,number+0
cp r19,r20
brne salah
rcall check_keys
lds r20,number+1
cp r19,r20
brne salah
rcall check_keys
lds r20,number+2
cp r19,r20
brne salah
rcall check_keys
lds r20,number+3
cp r19,r20
brne salah
rcall check_keys
lds r20,number+4
cp r19,r20
brne salah
retbenar:
lds r16,score
inc r16
sts score,r16
swap r16
out PORTA,r16
ldi ZL,low(2*right) ; write next…
ldi ZH,high(2*right)
rcall WM2LCD
retcompare:
mov r17,r16
swap r17
sts hscore,r17
retsalah:
lds r17,hscore
lds r16,score
sts score,r16
cp r16,r17 (*)(&
brge compare
ldi ZL,low(2*text2) ; cupu, kamu kalah
ldi ZH,high(2*text2)
rcall WM2LCD
rcall delay_hold
rcall delay_hold
rcall delay_hold
ldi r16,0x00
out portA,r16
sts score,r16
rjmp main_menuclamp:
mov r25,r24 ; membuat nilainya jadi antara 0-9
andi r25,0xF
cpi r25,10
brlt clamp_end
subi r25,10clamp_end:
ret;
; High Score
;
high_score:
ldi r17,0x01
rcall SLcd
ldi ZL,low(2*text5)
ldi ZH,high(2*text5)
rcall WM1LCD
lds r19,hscore
out portA,r19
rcall delay_hold
ret;
; Credits
;
credits:
ldi r17,0x01
rcall SLcd
ldi ZL,low(2*text6)
ldi ZH,high(2*text6)
rcall WM1LCD
rcall delay_hold
ldi ZL,low(2*credit1)
ldi ZH,high(2*credit1)
rcall WM1LCD
ldi ZL,low(2*credit2)
ldi ZH,high(2*credit2)
rcall WM2LCD
jalan:
ldi r17,0x18
rcall SLcd
rcall delay_hold
rjmp jalanrcall delay_hold
ldi r17,0x01
rcall SLcd
ret
;
; Check keypad sub routine
;
check_keys:
ldi temp,col1 ;Enable column1
out PORTD,temp
rcall delay
sbic PIND,PD0 ; Pressed key No1 ?
rjmp keyB ; if Not, check next key
keyA:
ldi r19,’A’
rcall WLcd
rcall delay_hold
ret
keyB:
sbic PIND,PD0
rcall delay
sbic PIND,PD0
rjmp keyC
ldi r19,’B’ ; if yes…
rcall WLcd
rcall delay_hold
retkeyC:
sbic PIND,PD0
rcall delay
sbic PIND,PD0
rcall delay
sbic PIND,PD0
rjmp key4
ldi r19,’C’ ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x01
key4:
sbic PIND,PD1 ; Pressed key No4 ?
rjmp key7 ; if Not, check next key
rcall reseed
ldi r19,0x34 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x04key7:
sbic PIND,PD2 ; Pressed key No7 ?
rjmp key_bintang ; if Not, check next key
rcall reseed
ldi r19,0x37 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x07key_bintang:
sbic PIND,PD3 ; Pressed key bintang ?
rjmp key2 ; if Not, check next key
rcall reseed
ldi r19,’*’ ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x0Akey2:
ldi temp,col2 ; Disable the first column…
out PORTD,temp ; and enable the second
rcall delay
sbic PIND,PD0 ; Pressed key No2 ?
rjmp key5 ; if Not…
rcall reseed
ldi r19,0x32 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x02key5:
sbic PIND,PD1 ; Pressed key No5 ?
rjmp key8 ; if Not…
rcall reseed
ldi r19,0x35 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x05key8:
sbic PIND,PD2 ; Pressed key No8 ?
rjmp key0 ; if Not…
rcall reseed
ldi r19,0x38 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x08key0:
sbic PIND,PD3 ; Pressed key No0 ?
rjmp key3 ; if Not…
rcall reseed
ldi r19,0x30 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x00key3:
ldi temp,col3 ; Disable 2nd column…
out PORTD,temp ; and enable the 3th
rcall delay
sbic PIND,PD0 ; Pressed key No3 ?
rjmp key6 ; if Not…
rcall reseed
ldi r19,0x33 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x03key6:
sbic PIND,PD1 ; Pressed key No6 ?
rjmp key9 ; if Not…
rcall reseed
ldi r19,0x36 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x06key9:
sbic PIND,PD2 ; Pressed key No9 ?
rjmp key_pagar ; if Not…
rcall reseed
ldi r19,0x39 ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x09key_pagar:
sbic PIND,PD3 ; Pressed key ENT ?
rjmp check_keys ; if Not…
rcall reseed
ldi r19,’#’ ; if yes…
rcall WLcd
rcall delay_hold
ret ; key=0x0B;
; Delay Keypad sub routine
;
delay :
ldi dly,0x10dl1 :
ldi dly1,0xffdl2 :
dec dly1
cpi dly1,0x00
brne dl2
dec dly
cpi dly,0x00
brne dl1
retdelay1:
ldi r19, 0x01
ldi r19, 0x1C
ldi r19, 0x01
ldi r19, 0x01
delay_hold:
ldi r16, 0x00
ldi r17, 0x00
ldi r18, 0x20
delay_hold2:
dec r16
brne delay_hold2
dec r17
brne delay_hold2
dec r18
brne delay_hold2
ret;
; Write Message to LCD
;
WM1LCD:
ldi r17,0x80 ;1st line, 1st col
rcall SLcd
rjmp Wr1
WM2LCD:
ldi r17,0xC0
rcall SLcd
Wr1: clr r1
loopWr1: lpm ;load program memory
mov r19,r0
cpi r19,0
brne NextCHR
ret
NextCHR: rcall WLCD ;write character
adiw ZL,1 ;get next char
rjmp LoopWr1
;
; Inisialisasi LCD
;
InitLCD: ldi r20,8
LoopDelay:
rcall delay_LCD
dec r20
cpi r20,0
brne LoopDelay
ldi r17,0x30 ;Function set
out PortC,r17
sbi PortC,2 ;E=1
cbi PortC,2 ;E=0
rcall delay_LCD
ldi r17,0x30 ;Function set
out PortC,r17
sbi PortC,2 ;E=1
cbi PortC,2 ;E=0
rcall delay_LCD
ldi r17,0x20 ;Function set:4 bit operation
out PortC,r17
sbi PortC,2 ;E=1
cbi PortC,2 ;E=0
ldi r17,0x28 ;Function set:4 bit op;2lines
rcall SLcd
ldi r17,0x08 ;Turn Off LCD
rcall SLcd
ldi r17,0x0C ;Turn On LCD
rcall SLcd
ldi r17,0x01 ;Clear Display
rcall SLcd
ldi r17,0x06 ;Entry mode set; Inc addr
rcall SLcd
ret
;
; Rutin kirim instruksi
;
SLcd:
cbi PortC,2 ;E=0
mov r18,r17
andi r18,$F0 ;Get high nibble
out PortC,r18
cbi PortC,0 ;RS=0
sbi PortC,2 ;E=1
rcall delay_LCD
cbi PortC,2 ;E=0
rcall delay_LCD
mov r18,r17
andi r18,$0F ;Get low nibble
swap r18
out PortC,r18
cbi PortC,0 ;RS=0
sbi PortC,2 ;E=1
rcall delay_LCD
cbi PortC,2 ;E=0
ret
;
; Rutin kirim karakter
;
WLcd:
cbi PortC,2 ;E=0
mov r18,r19
andi r18,$F0 ;Get high nibble
out PortC,r18
sbi PortC,0 ;RS=1
sbi PortC,2 ;E=1
rcall delay_LCD
cbi PortC,2 ;E=0
rcall delay_LCD
mov r18,r19
andi r18,$0F ;Get low nibble
swap r18
out PortC,r18
sbi PortC,0 ;RS=1
sbi PortC,2 ;E=1
rcall delay_LCD
cbi PortC,2 ;E=0
ret;
;
; Rutin delay init LCD ;
;
;delay_LCD:
ldi dly , 0x00
ldi dly1, 0x00
ldi dly2, 0x01loop_delay:
dec dly
brne loop_delay
dec dly1
brne loop_delay
dec dly2
brne loop_delay
rettext: .db “new game”,0,0
text2: .db “cupu,kamu kalah”, 0
text3: .db “memory game!!”,0
text4: .db “select menu”,0
text5: .db “high score:”,0
text6: .db “credits:”,0,0
welcome: .db “welcome to”,0,0
credit1: .db “wisnu-ari-pungki-syifa-tommy-ainul”,0, 0
credit2: .db “aland-fajar-icha-pipit-pandu-yunan”,0,0
right: .db “next…”,0
right1: .db “next level…”,0
right2: .db “Congrats Gan.!!”,0
right3: .db “Main lagi ya…”,0;
;
; Rutin LCPRNG ;
;
;reseed:
push r16
push r17
in r16, TCNT0
lds r17, seed
add r16,r17
sts seed,r16
pop r17
pop r16
ret; Linear Congruential Pseudorandom Number Generator
; a_{n+1} = (j a_n + k) mod m.
; r16 j (multiplier)
; r17 a_n (seed)
; r18 k (offset)
; m=256.
; r24 tempat penyimpanan hasil.lcprng:
clr r19
mul r16, r17
add r0,r18
mov r24,r0
ret.DSEG
seed: .byte 1
number: .byte 8
score: .byte 1
hscore: .byte 1December 18, 2010 at 1:30 pm #5225AnonymousGuestwill you please elobrate a bit what is hungman game???
-
AuthorPosts
- You must be logged in to reply to this topic.