SlideShare a Scribd company logo
HEX KEYPAD SCANNING PROGRAM
LOGICAL APPROACH
By
Sudheera Rao Y
spn_rao@yahoo.com
1
Interfacing a 4x4 keyboard to the ARM7 LPC2148 Microcontroller
and
displaying the key code pressed by the user
C D E F
8 9 A B
4 5 6 7
0 1 2 3
2
Program:
#include <LPC214X.H>
#define CR 0x0d
/* ----------------------------------Initialization of Serial Port -------------------------------------------*/
void init_serial (void)
{ /* Initialize Serial Interface */
PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL = 97; /* 9600 Baud Rate @ 12 MHz VPB Clock */
U1LCR = 0x03; /* DLAB = 0 */
}
int putkey(int ch) /* Send the character to UART */
{
if(ch == 'n')
{
while(!(U1LSR & 0x20));
U1THR = CR;
}
while(!(U1LSR & 0x20));
return (U1THR = ch);
}
3
void sendhex (int hex)
{ /* Write Hex Digit to Serial Port */
if (hex > 9)
putkey('A' + (hex - 10));
else
putkey('0' + hex);
}
void sendstr(unsigned char *p) /* Send the string of characters to UART */
{
while(*p)
{
putkey(*p++);
}
}
void delay()
{
unsigned int i,j;
for(i = 0; i <= 32100; i ++)
{
for(j = 0; j <= 10; j ++);
}
}
4
unsigned long rows,columns,result;
void KeyScan() /* Scan Routine for the key pad */
{
unsigned long temp = 0x00;
again:
columns = 0x00040000;
rows = 0x00000004;
result = 0x0000000C;
next:
IOSET0 = columns;
delay();
temp = IOPIN1;
temp = (temp & 0x000F0000);
if(temp != 0x00000000)
{
rot:
temp = temp >> 1;
if ((temp&0x000f8000) == 0x00008000)
{
IOCLR0=columns; //
return;
}
else
{
result += 1;
goto rot;
}
}
5
else
{
result -= 0x04;
rows --;
if (rows == 0x00)
{
IOCLR0=columns;
goto again;
} //closing if ( ) statements
else
{
IOCLR0= columns;
columns >>=1;
if(columns == 0x00004000)
columns = (columns & 0x0040000);
goto next;
} // closing else
} // closing else
} // closing of Keyscan function
void Display()
{
sendhex(result);
}
int main()
{
IODIR1 = 0x00000000; //Port1 as Input
IODIR0 = 0x007F80F3; //Port0 as Output
init_serial ();
while(1)
{
KeyScan();
sendstr("nThe KEY pressed is : ");
Display();
delay();
}
}
6
SCANNING OF HEX KEYPAD
0th Col 1st Col 2nd Col 3rd Col
3rdrow
C
result=0x0000000C
(i.e 12)
=1100
11-3rd row . 00 – 0th Col
D
Result+=1
(i.e 13)
=1101
E
Result+=1
(i.e 14)
=1110
F
Result+=1
(i.e 15)
=1111
2ndrow
8
result -=0x04
(i.e 8)
=1000
10 - 2nd row. 00 – 0th col
9
result+=1
A
result+=1
B
result+=1
1strow
4
result -=0x04
5
result+=1
6
result+=1
7
result+=1
0throw
0
result - =0x04
1
result+=1
2
result+=1
3
result+=1
7
0th Col 1st Col 2nd Col 3rd Col
3rdrow
C
result=0x0000000C
(i.e 12)
=1100
11-3rd row . 00 – 0th Col
D
Result+=1
(i.e 13)
=1101
E
Result+=1
(i.e 14)
=1110
F
Result+=1
(i.e 15)
=1111
2ndrow
8
result -=0x04
(i.e 8)
=1000
10 - 2nd row. 00 – 0th col
9
result+=1
A
result+=1
B
result+=1
1strow
4
result -=0x04
5
result+=1
6
result+=1
7
result+=1
0throw
0
result - =0x04
1
result+=1
2
result+=1
3
result+=1
SCANNING OF 3rd ROW done FIRST
SCANNING 2nd ROW done SECOND
SCANNING 1st ROW done THIRD
SCANNING 0th ROW done FOURTH
8
CONTROL SWITCH FOR SCANNING OF HEX KEYPAD COLUMNS
CONTROL SWITCH is defined in IOSET0 Register. The Control switch
for various series of Columns is as follows.
For SCANNING OF 3rd ROW -SET 18th BIT
Columns= 0x00040000
For SCANNING 2nd ROW -SET 17th BIT
Columns>>=1
For SCANNING 1st ROW -SET 16th BIT
Columns>>=1
For SCANNING 0th ROW -SET 15th BIT
Columns>>=1
19 18 17 16 15
IOSET0
In a row there will be 4 columns. From 2nd row onwards, At the Beginning
of row (i.e 0th col) ,the value of result will be result-=0x04. It keeps
increasing for every column in that row (result+=1). If the user has not
pressed any key in that row, then we go for next row by changing control
switch for the next row( Columns>>=1. When the control switch goes from
15th to 14th Bit, it ‘s value becomes 0x00004000. It means user has not
pressed any key. Hence we should start from top again by setting control
switch to 3rd row.
9
c D E F
19 18 17 16
PIN
Numbers
8 9 A B
19 18 17 16
PIN
Numbers
10
c D E F 0 0 0 1
19 18 17 16
PIN
Numbers
If the user chooses “C”
c D E F 0 0 0 0
19 18 17 16
“temp “ variable to store user selection. If the user does not selected any thing
PIN
Numbers
temp=IOPIN1
temp=
This is obtained
through IOPIN
Register
11
temp is not equal to zero. It means the user has pressed
some key and we have to find which key it is. It is the first
iteration. At this point the value of result is 0x0000000C. If
the user has pressed C then the value of temp will be 0001 0 0 0 1temp=
19 18 17 16If we do temp>>1, It shifts the set bit to 15th bit and value
becomes 0x00008000. . 15th bit is to exit from loop. 0x00008000
is to exit from keyscan function. If the condition (temp &
0x000f8000)==0x00008000 satisfies, The value of result is
returned to the main function. That will be “C”. (Note that I0SET
is set for 3rd row)
If the condition does not satiefies, it means user has pressed
some key, but it is not C. Then we should suspect for next
character in that row, i.e D. Hence we increase the value of result
(i.e. result+=1). Then again do rotation of “temp “ bits. If the user
has pressed D then value of temp will be 0010
0 0 1 0temp=
19 18 17 16
For the “D” to become true, 1st rotation temp>>1 becomes falls. Result value increases by 1 .
2nd rotation becomes true. The current value of result is returned. That will be “D”. (Note that I0SET is
set for 3rd row)
12
If the user has not pressed any key from 3rd row, scanning goes to the
next row (2nd row). When control was at 3rd row, value of result= 0x0000000C. It becomes
result-=0x04. i.e result=0x08. Number of rows decremented by 1. so rows--.
But rows number have not become zero. To scan the 2nd row, we should set the
17th bit of IOSET. This is done by columns >>=1
8
If the user has pressed this key
•temp=0001
•Row=2
•Result=8
•Temp>>1
•return
If user has not pressed this key
•Temp=0010 (If pressed key is 9)
•Row=2
•Result+=1
9
If the user has pressed this key
•temp=0010
•Row=2
•Result=9
•Temp>>1
•return
If user has not pressed this key
•Temp=0100 (If pressed key is A)
•Row=2
•Result+=1
13
14
Thank you

More Related Content

What's hot

Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Irfan Qadoos
 
Mux decod pld2_vs2
Mux decod pld2_vs2Mux decod pld2_vs2
Mux decod pld2_vs2
WanNurdiana
 
3 axis digital product manual
3 axis digital product manual3 axis digital product manual
3 axis digital product manual
Khunut Thi-ai
 
Rota qsig central 3
Rota qsig central 3Rota qsig central 3
Rota qsig central 3
zeu1507
 
Sequential Circuit
Sequential CircuitSequential Circuit
Sequential Circuit
Heman Pathak
 
Combinational logic 2
Combinational logic 2Combinational logic 2
Combinational logic 2
Heman Pathak
 
Digital logic Gates of Computer Science
Digital logic Gates of Computer ScienceDigital logic Gates of Computer Science
Digital logic Gates of Computer Science
Anil Kumar Prajapati
 
Basic Cisco Switch config
Basic Cisco Switch configBasic Cisco Switch config
Basic Cisco Switch config
Andrew Spiker
 
Dsys m2 u1
Dsys m2 u1Dsys m2 u1
Combinational logic 1
Combinational logic 1Combinational logic 1
Combinational logic 1
Heman Pathak
 

What's hot (10)

Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
 
Mux decod pld2_vs2
Mux decod pld2_vs2Mux decod pld2_vs2
Mux decod pld2_vs2
 
3 axis digital product manual
3 axis digital product manual3 axis digital product manual
3 axis digital product manual
 
Rota qsig central 3
Rota qsig central 3Rota qsig central 3
Rota qsig central 3
 
Sequential Circuit
Sequential CircuitSequential Circuit
Sequential Circuit
 
Combinational logic 2
Combinational logic 2Combinational logic 2
Combinational logic 2
 
Digital logic Gates of Computer Science
Digital logic Gates of Computer ScienceDigital logic Gates of Computer Science
Digital logic Gates of Computer Science
 
Basic Cisco Switch config
Basic Cisco Switch configBasic Cisco Switch config
Basic Cisco Switch config
 
Dsys m2 u1
Dsys m2 u1Dsys m2 u1
Dsys m2 u1
 
Combinational logic 1
Combinational logic 1Combinational logic 1
Combinational logic 1
 

Similar to Hex keypad

Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Sheila Sinclair
 
Combine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdfCombine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdf
forwardcom41
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDAC
nanocdac
 
Microcontroladores: El microcontrolador 8051 con LCD 16x2
Microcontroladores: El microcontrolador 8051 con LCD 16x2Microcontroladores: El microcontrolador 8051 con LCD 16x2
Microcontroladores: El microcontrolador 8051 con LCD 16x2
SANTIAGO PABLO ALBERTO
 
Microcontroller- An overview
Microcontroller- An overviewMicrocontroller- An overview
Microcontroller- An overview
PANIMALAR ENGINEERING COLLEGE
 
Switch Control and Time Delay - Keypad
Switch Control and Time Delay - KeypadSwitch Control and Time Delay - Keypad
Switch Control and Time Delay - Keypad
Ariel Tonatiuh Espindola
 
Jp
Jp Jp
DTMF Mobile Operated Robot using Atmega16
DTMF Mobile Operated Robot using Atmega16DTMF Mobile Operated Robot using Atmega16
DTMF Mobile Operated Robot using Atmega16
Prashant Saini
 
Basic standard calculator
Basic standard calculatorBasic standard calculator
Basic standard calculator
UVSofts Technologies
 
Analog to Digital .pptx
Analog to Digital .pptxAnalog to Digital .pptx
Analog to Digital .pptx
karanthakur846894
 
8051
80518051
Automation and Robotics 20ME51I_Week_2_Practicals.pdf
Automation and Robotics 20ME51I_Week_2_Practicals.pdfAutomation and Robotics 20ME51I_Week_2_Practicals.pdf
Automation and Robotics 20ME51I_Week_2_Practicals.pdf
Gandhibabu8
 
Lesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operationsLesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operations
PVS-Studio
 
Chapter5 dek3133
Chapter5 dek3133Chapter5 dek3133
Chapter5 dek3133
Hattori Sidek
 
Lab 9 D-Flip Flops: Shift Register and Sequence Counter
Lab 9 D-Flip Flops: Shift Register and Sequence CounterLab 9 D-Flip Flops: Shift Register and Sequence Counter
Lab 9 D-Flip Flops: Shift Register and Sequence Counter
Katrina Little
 
The IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programming
The IOT Academy
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
Abdul Haseeb
 
Attachment_ VHDL datasheet
Attachment_ VHDL datasheetAttachment_ VHDL datasheet
Attachment_ VHDL datasheet
jethro kimande
 
11. Lecture.pdf
11. Lecture.pdf11. Lecture.pdf
11. Lecture.pdf
Deependra35
 
Ceng232 Decoder Multiplexer Adder
Ceng232 Decoder Multiplexer AdderCeng232 Decoder Multiplexer Adder
Ceng232 Decoder Multiplexer Adder
gueste731a4
 

Similar to Hex keypad (20)

Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
Combine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdfCombine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdf
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDAC
 
Microcontroladores: El microcontrolador 8051 con LCD 16x2
Microcontroladores: El microcontrolador 8051 con LCD 16x2Microcontroladores: El microcontrolador 8051 con LCD 16x2
Microcontroladores: El microcontrolador 8051 con LCD 16x2
 
Microcontroller- An overview
Microcontroller- An overviewMicrocontroller- An overview
Microcontroller- An overview
 
Switch Control and Time Delay - Keypad
Switch Control and Time Delay - KeypadSwitch Control and Time Delay - Keypad
Switch Control and Time Delay - Keypad
 
Jp
Jp Jp
Jp
 
DTMF Mobile Operated Robot using Atmega16
DTMF Mobile Operated Robot using Atmega16DTMF Mobile Operated Robot using Atmega16
DTMF Mobile Operated Robot using Atmega16
 
Basic standard calculator
Basic standard calculatorBasic standard calculator
Basic standard calculator
 
Analog to Digital .pptx
Analog to Digital .pptxAnalog to Digital .pptx
Analog to Digital .pptx
 
8051
80518051
8051
 
Automation and Robotics 20ME51I_Week_2_Practicals.pdf
Automation and Robotics 20ME51I_Week_2_Practicals.pdfAutomation and Robotics 20ME51I_Week_2_Practicals.pdf
Automation and Robotics 20ME51I_Week_2_Practicals.pdf
 
Lesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operationsLesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operations
 
Chapter5 dek3133
Chapter5 dek3133Chapter5 dek3133
Chapter5 dek3133
 
Lab 9 D-Flip Flops: Shift Register and Sequence Counter
Lab 9 D-Flip Flops: Shift Register and Sequence CounterLab 9 D-Flip Flops: Shift Register and Sequence Counter
Lab 9 D-Flip Flops: Shift Register and Sequence Counter
 
The IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programming
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
Attachment_ VHDL datasheet
Attachment_ VHDL datasheetAttachment_ VHDL datasheet
Attachment_ VHDL datasheet
 
11. Lecture.pdf
11. Lecture.pdf11. Lecture.pdf
11. Lecture.pdf
 
Ceng232 Decoder Multiplexer Adder
Ceng232 Decoder Multiplexer AdderCeng232 Decoder Multiplexer Adder
Ceng232 Decoder Multiplexer Adder
 

Recently uploaded

How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 

Recently uploaded (20)

How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 

Hex keypad

  • 1. HEX KEYPAD SCANNING PROGRAM LOGICAL APPROACH By Sudheera Rao Y spn_rao@yahoo.com 1
  • 2. Interfacing a 4x4 keyboard to the ARM7 LPC2148 Microcontroller and displaying the key code pressed by the user C D E F 8 9 A B 4 5 6 7 0 1 2 3 2
  • 3. Program: #include <LPC214X.H> #define CR 0x0d /* ----------------------------------Initialization of Serial Port -------------------------------------------*/ void init_serial (void) { /* Initialize Serial Interface */ PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */ U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ U1DLL = 97; /* 9600 Baud Rate @ 12 MHz VPB Clock */ U1LCR = 0x03; /* DLAB = 0 */ } int putkey(int ch) /* Send the character to UART */ { if(ch == 'n') { while(!(U1LSR & 0x20)); U1THR = CR; } while(!(U1LSR & 0x20)); return (U1THR = ch); } 3
  • 4. void sendhex (int hex) { /* Write Hex Digit to Serial Port */ if (hex > 9) putkey('A' + (hex - 10)); else putkey('0' + hex); } void sendstr(unsigned char *p) /* Send the string of characters to UART */ { while(*p) { putkey(*p++); } } void delay() { unsigned int i,j; for(i = 0; i <= 32100; i ++) { for(j = 0; j <= 10; j ++); } } 4
  • 5. unsigned long rows,columns,result; void KeyScan() /* Scan Routine for the key pad */ { unsigned long temp = 0x00; again: columns = 0x00040000; rows = 0x00000004; result = 0x0000000C; next: IOSET0 = columns; delay(); temp = IOPIN1; temp = (temp & 0x000F0000); if(temp != 0x00000000) { rot: temp = temp >> 1; if ((temp&0x000f8000) == 0x00008000) { IOCLR0=columns; // return; } else { result += 1; goto rot; } } 5
  • 6. else { result -= 0x04; rows --; if (rows == 0x00) { IOCLR0=columns; goto again; } //closing if ( ) statements else { IOCLR0= columns; columns >>=1; if(columns == 0x00004000) columns = (columns & 0x0040000); goto next; } // closing else } // closing else } // closing of Keyscan function void Display() { sendhex(result); } int main() { IODIR1 = 0x00000000; //Port1 as Input IODIR0 = 0x007F80F3; //Port0 as Output init_serial (); while(1) { KeyScan(); sendstr("nThe KEY pressed is : "); Display(); delay(); } } 6
  • 7. SCANNING OF HEX KEYPAD 0th Col 1st Col 2nd Col 3rd Col 3rdrow C result=0x0000000C (i.e 12) =1100 11-3rd row . 00 – 0th Col D Result+=1 (i.e 13) =1101 E Result+=1 (i.e 14) =1110 F Result+=1 (i.e 15) =1111 2ndrow 8 result -=0x04 (i.e 8) =1000 10 - 2nd row. 00 – 0th col 9 result+=1 A result+=1 B result+=1 1strow 4 result -=0x04 5 result+=1 6 result+=1 7 result+=1 0throw 0 result - =0x04 1 result+=1 2 result+=1 3 result+=1 7
  • 8. 0th Col 1st Col 2nd Col 3rd Col 3rdrow C result=0x0000000C (i.e 12) =1100 11-3rd row . 00 – 0th Col D Result+=1 (i.e 13) =1101 E Result+=1 (i.e 14) =1110 F Result+=1 (i.e 15) =1111 2ndrow 8 result -=0x04 (i.e 8) =1000 10 - 2nd row. 00 – 0th col 9 result+=1 A result+=1 B result+=1 1strow 4 result -=0x04 5 result+=1 6 result+=1 7 result+=1 0throw 0 result - =0x04 1 result+=1 2 result+=1 3 result+=1 SCANNING OF 3rd ROW done FIRST SCANNING 2nd ROW done SECOND SCANNING 1st ROW done THIRD SCANNING 0th ROW done FOURTH 8
  • 9. CONTROL SWITCH FOR SCANNING OF HEX KEYPAD COLUMNS CONTROL SWITCH is defined in IOSET0 Register. The Control switch for various series of Columns is as follows. For SCANNING OF 3rd ROW -SET 18th BIT Columns= 0x00040000 For SCANNING 2nd ROW -SET 17th BIT Columns>>=1 For SCANNING 1st ROW -SET 16th BIT Columns>>=1 For SCANNING 0th ROW -SET 15th BIT Columns>>=1 19 18 17 16 15 IOSET0 In a row there will be 4 columns. From 2nd row onwards, At the Beginning of row (i.e 0th col) ,the value of result will be result-=0x04. It keeps increasing for every column in that row (result+=1). If the user has not pressed any key in that row, then we go for next row by changing control switch for the next row( Columns>>=1. When the control switch goes from 15th to 14th Bit, it ‘s value becomes 0x00004000. It means user has not pressed any key. Hence we should start from top again by setting control switch to 3rd row. 9
  • 10. c D E F 19 18 17 16 PIN Numbers 8 9 A B 19 18 17 16 PIN Numbers 10
  • 11. c D E F 0 0 0 1 19 18 17 16 PIN Numbers If the user chooses “C” c D E F 0 0 0 0 19 18 17 16 “temp “ variable to store user selection. If the user does not selected any thing PIN Numbers temp=IOPIN1 temp= This is obtained through IOPIN Register 11
  • 12. temp is not equal to zero. It means the user has pressed some key and we have to find which key it is. It is the first iteration. At this point the value of result is 0x0000000C. If the user has pressed C then the value of temp will be 0001 0 0 0 1temp= 19 18 17 16If we do temp>>1, It shifts the set bit to 15th bit and value becomes 0x00008000. . 15th bit is to exit from loop. 0x00008000 is to exit from keyscan function. If the condition (temp & 0x000f8000)==0x00008000 satisfies, The value of result is returned to the main function. That will be “C”. (Note that I0SET is set for 3rd row) If the condition does not satiefies, it means user has pressed some key, but it is not C. Then we should suspect for next character in that row, i.e D. Hence we increase the value of result (i.e. result+=1). Then again do rotation of “temp “ bits. If the user has pressed D then value of temp will be 0010 0 0 1 0temp= 19 18 17 16 For the “D” to become true, 1st rotation temp>>1 becomes falls. Result value increases by 1 . 2nd rotation becomes true. The current value of result is returned. That will be “D”. (Note that I0SET is set for 3rd row) 12
  • 13. If the user has not pressed any key from 3rd row, scanning goes to the next row (2nd row). When control was at 3rd row, value of result= 0x0000000C. It becomes result-=0x04. i.e result=0x08. Number of rows decremented by 1. so rows--. But rows number have not become zero. To scan the 2nd row, we should set the 17th bit of IOSET. This is done by columns >>=1 8 If the user has pressed this key •temp=0001 •Row=2 •Result=8 •Temp>>1 •return If user has not pressed this key •Temp=0010 (If pressed key is 9) •Row=2 •Result+=1 9 If the user has pressed this key •temp=0010 •Row=2 •Result=9 •Temp>>1 •return If user has not pressed this key •Temp=0100 (If pressed key is A) •Row=2 •Result+=1 13