0% found this document useful (0 votes)
26 views

Programming 8051 Microcontroller: 8051 Assembly Program

The document describes code for reading a 4x4 keypad connected to an 8051 microcontroller. It includes assembly and C code to initialize the keypad ports, read the keypad columns, detect pressed keys, and return the key value. The code sets the rows as outputs one by one, reads the column pins to detect pressed keys, and uses the column number to return the corresponding key value from 1 to 16.

Uploaded by

Tập Bon Chen
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Programming 8051 Microcontroller: 8051 Assembly Program

The document describes code for reading a 4x4 keypad connected to an 8051 microcontroller. It includes assembly and C code to initialize the keypad ports, read the keypad columns, detect pressed keys, and return the key value. The code sets the rows as outputs one by one, reads the column pins to detect pressed keys, and uses the column number to return the corresponding key value from 1 to 16.

Uploaded by

Tập Bon Chen
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming 8051 Microcontroller

8051 Assembly Program CODE: keyport equ P2 col1 equ P2.0 col2 equ P2.1 col3 equ P2.2 col4 equ P2.3 keyval equ 30H pressed bit 0H key_init: mov keyport,#0FH as /p ret get_key: mov keyval,#0 mov keyport,#7FH acall read_col jb pressed, done mov keyval,#4 mov keyport,#0 FH acall read_col jb pressed, done mov keyval,#! mov keyport,#0"FH acall read_col jb pressed, done mov keyval,#12 mov keyport,#0#FH acall read_col done: ret read_col: clr pressed ;read columns rout ne ;reset the "lag ;reset the number ;make !ow1 low ;read columns ;check " "lag s set ;Keypad ;Column ;Column ;Column ;Column port connected here 1 2 3 4

;To store key number ;Flag ;Make rows as o/p and col

; " not then read ne#t row ;make !ow2 low ;read columns ;check " "lag s set

; " not then read ne#t row ;make row3 low ;read columns ;check " "lag s set

; " not read row4 ;make row4 low ;read columns

jb col1, ne$tcol pressed jnb col1,% release setb pressed ret ne$tcol: jb col2, ne$tcol1 pressed jnb col2,% release inc keyval setb pressed ret ne$tcol1: jb col3, ne$tcol2 pressed jnb col3,% release inc keyval inc keyval setb pressed ret ne$tcol2: jb col4, exit pressed jnb col4,% release inc keyval inc keyval inc keyval setb pressed ret exit: clr pressed clr keyval ret end

;check

" " rst key

; " yes then wa t "or key ;set the "lag ;read col2 ;check " second key

; " yes then wa t "or key ; ts key number 2 ;set the "lag ;read col3 ;check " th rd key

; " yes then wa t "or key ; ts key 3 ;set the "lag ;read column 4 ;check " "ourth key ; " yes then wa t "or key ; ts key 4 ;set the "lag ; " no key s pressed ;clr the "lag ;reset the number

C Program for Keypad CODE: #incl&de '()!*+,1.H#de0ine keyport P2 #de0ine col1 P2_0 #de0ine col2 P2_1 #de0ine col3 P2_2 #de0ine col4 P2_3 #de0ine )23# 1 #de0ine F(45# 0 ../ncl&de 0ile 0or !0,1 ..keypad connected to P2 ..col&1n 1 ..col&1n 2 ..col&1n 3 ..col&1n 4 ..so1e de0ines

/$ %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&% ' (rototype) *o d key+ n t,*o d-; ' ' !eturn Type) *o d ' ' .rguments) /one ' ' 0escr pt on) 1n t al 2e ports and ' ' Keypad3 ' %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&% $/ void key_init678 keyport 9:0$0F; //make !ows as o/p and cols are <

/p

/$ %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&% ' (rototype) uns gned char get+key,*o d-; ' ' !eturn Type) uns gned char ' ' .rguments) /one ' ' 0escr pt on) To read key "rom the keypad ' %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&% $/ &nsigned c=ar get_key678 &nsigned c=ar i,k,key:0; k:1; 0or6i:0;i'4;i>>78 //loop "or 4 rows keyport 9:?60$!0--i7; //to make rows low 1 by 1 i06@col178 //check " key1 s pressed key : k>0; //set key number A=ile6@col17; //wa t "or release ret&rn key; //return key number < i06@col278 //check " key2 s pressed key : k>1; //set key number

A=ile6@col27; ret&rn key; < i06@col378 key : k>2; A=ile6@col37; ret&rn key; < i06@col478 key : k>3; A=ile6@col47; ret&rn key; < k>:4; keyport B: 0$!0--i; < ret&rn F(45#; pressed <

//wa t "or release //return key number //check " key3 s pressed //set key number //wa t "or release //return key number //check " key4 s pressed //set key number //wa t "or release //return key number //ne#t row key number //make the row h gh aga n //return "alse " no key

You might also like