R8C Program Demo4
R8C Program Demo4
Introduction:
Sixteen keys are connected in 4 X 4 matrix format. A One Millisecond interrupt is generated
using Timer X and the keyboard is scanned in the interrupt and the pressed key is identified. The
pressed key is displayed in 2 digit 7-segment display.
Demo Hardware:
In this demo, the row lines of the keyboard are connected to the port lines P14 to P17 and the
column lines are interfaced with the port lines P10 to P13. Hence row lines can be used as
output lines and column lines are defined as input lines.
2 digits of seven segment LED displays are connected in multiplexed mode with seven segment
data input. The seven segments are connected to the port P0 sequentially and the Port lines P30
and P31 are conneced to the digit selection lines.
1
www.MightyMicons.com
+5V
C1 0.1uF
C2 0.1uF
7
23
VCC
IVCC
Circuit Connection:
JP1 32
P00/AN7/TXD11
31
P01/AN6
1
2
3
www.MightyMicons.com
C NC 30
C3 33PF 6 P02/AN5
XIN/P46 29
XTAL1 P03/AN4
27
P04/AN3
20MHZ 26
P05/AN2 C D E F
C4 33PF 4 25
XOUT/P47 P06/AN1
C NC 24
Column0
Column1
Column3
P07/AN0
Column2
1
2
3
+5V
JP2
Row3 8 9 A B
12
R2 P13/KI3
4K7 13
PROGRAM P12/KI2
28 14
1 MODE P11/KI1
JP4 2 15 Row2 4
3 P10/KI0
5 6 7
2
+5V CNVSS 8
NORMAL P17/INT1/CNTR0
R1 4K7
9
P16/CLK0
D1 10
P15/RXD0 1 3
R1 IN4148 Row1 0 2
10K 11
P14/TXD0
U2
1 2 3 4 3 18
RESET P32/INT2/CNTR1
17
P33/INT3/TCIN Row0
CD40106 CD40106
SW1 U2
Keyboard
+ 16
+5V P45/INT0
C6 +5V 1
RESET 4.7uF P37/TXD10/RXD1
1 19
JP3 2 AVCC/VREF 22
3 P30/CNTR0
EX 20
P31/TZOUT
AVSS
VSS
5
21
U1 R8C11/13
2
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
+5V
C1 0.1uF
C2 0.1uF
7
23
4 3 2 1
1
3
5
9
U20A U20B U20C U20D
VCC
IVCC
R29 R30 R31 R32
74LS07 74LS07 74LS07 74LS07
JP1 16
P45/INT0
10K 10K 10K 10K
www.MightyMicons.com
22
2
4
6
8
P30/CNTR0
1
2
3
T1 T2 T3 T4
C NC 20 BC 557 BC 557 BC 557 BC 557
C3 33PF 6 P31/TZOUT 1K R33 1K R34 1K R35 1K R36
XIN/P46 18
XTAL1 P32/INT2/CNTR1 U8 U9 U10 U11
17
3
8
3
8
3
8
3
8
P33/INT3/TCIN
20MHZ 1
C
C
C
C
C
C
C
C
P37/TXD10/RXD1
C4 33PF 4 A 7 A 7 A 7 A 7
XOUT/P 47 6 A 6 A 6 A 6 A
B B B B
B B B B
C NC 32 C 4 C 4 C 4 C 4
P00/AN7/TXD11 C C C C
D 2 D 2 D 2 D 2
1
2
3
KLS 563 G
KLS 563 G
KLS 563 G
KLS 563 G
+5V 31 1 D 1 D 1 D 1 D
E E E E
P01/AN6 9 E 9 E 9 E 9 E
F F F F
F F F F
JP2 30 G 10 G 10 G 10 G 10
P02/AN5 G G G G
D.P 5 D.P 5 D.P 5 D.P 5
R2 29 D.P D.P D.P D.P
4K7 P03/AN4
PROGRAM 27
28 P04/AN3
1 MO DE 26
JP4 2 P05/AN2 +5V
3 2 25
+5V CNVSS P06/AN1
NORMAL
R1 4K7 24
P07/AN0
1
20
D1
R1 IN4148 15
DIR
a
VCC
P10/KI0
10K 2 18 R37 56E A
A1 B1
14 b
P11/KI1
U2 3 17 R38 56E B
1 2 3 4 3 13 A2 B2
RESET P12/KI2 c
4 16 R39 56E C
A3 B3
12 d
CD40106 P13/KI3 5 15 R40 56E
CD40106 D
SW 1 11 A4 B4
U2 e
P14/TXD0 6 14 R41 56E
+ E
+5V 10 A5 B5
P15/RXD0 f
C6 +5V 7 13 R42 56E F
RESET 4.7uF 9 +5V A6 B6
1 P16/CLK0 g
19 8 12 R43 56E G
JP3 2 AVCC/VREF 8 A7 B7
3 P17/INT1/CNTR0 h
R48 9 11 R44 56E D.P
A8 B8
EX
+5V 10K
AVSS
VSS
G
G ND
JP6
5
1 2
21
19
10
1K
3
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
• For Keyboard
4
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Functional Description:
The keys are connected with the microcontroller in many ways. The most simple way is to
assign a port line to each of these switches to sense the closure of the key. Because of this
arrangement, this interface requires as many port lines as the keys. When more keys are re-
quired for the application, this interface arrangement demands more port lines.
To manage this resource drain, the keys can be arranged in a matrix form, where each of these
keys is connected at the intersection of row and column lines of the matrix.
These row and column lines of the key matrix are connected with the port lines. When a key is
pressed, it connects one row with a column and this connection is sensed by the controller. In
this way, the port line requirement is kept as the lowest level. For a 16 key matrix arranged in a
4X 4 matrix requires only 8 port lines for usage.
Similarly, for a key assembly of 64 keys arranged in a 8 X 8 matrix, only 16 port lines are
required during interfacing.
In this application, an interrupt is used to scan the keyboard for the key closure. A timer is used
to generate a periodical time delay of 1 millisecond to find out the key closure. When servicing
the interrupt routine, the key scanning task is taking place.
Registers used:
5
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
To set a port line as output line, load the corresponding bit of the direction register with a value
of 1 level and for the input line the value should be 0 level. After the reset, all the port lines are
set to input mode, which indicates that all direction registers are already set with a value 0.
6
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
The Port data register keeps data meant for both input and output operations. During output
operation, the corresponding output data should be written in this register. Similarly, during input
operations, reading this register is sufficient to get the data from that particular port.
Set a value of 0x00 in the timer X mode register to select timer mode operation for Timer X.
7
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Software Description:
Timer X is used to generate a one millisecond delay. The clock frequency for the Timer X is fosc.
The timer is initialized to generate an interrupt for every millisecond. The PREX register is loaded
with 99. The timer TX is loaded with a value 199.
(n+1) (m+1)
= ----------------------------
Fosc
n - Prescalar value
m - Primary register
8
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
(100) * (200)
= -------------------------------
20000000
= 1 millisecond.
Timer X overflows for every millisecond and an interrupt is generated and also the timer registers
are reloaded with the initial set values. For every millisecond lapsed, the interrupt is serviced
and the keyboard is scanned. If any key is pressed, the key value will be stored in a variable and
the control returns back to main program.
The column lines are provided with individual pull-up resistors. When they are read, column data
may contain high level from all the columns.
The rows are activated one by one and the selected row is supplied with a low level. Then the
column data is read. If any key is pressed then that particular column will be at low level. Thus
the pressed key is identified and KeyboardFlag is set. The key code of the pressed key is
stored in the KeyboardCode variable.
If no key has been pressed in that row, the next row is activated and checked for any keypress.
This process is repeated for the remaining rows.
The keyboard scanning routine provides two variables for processing. The keyboard scanning is
done in the Timer X Interrupt service routine. The user application checks for level 1 in the
variable KeyboardFlag. If this flag is at 1 level, then the keycode is read from the variable
KeyboardCode after clearing KeyboardFlag variable.
Another routine, “WaitForKeypress”, when used, can wait for a key press and return the key
value when the key press is sensed.
9
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
One more option is to read the status of the keyboard ( Whether any key is pressed or not) using
the routine “ReadKeyboardStatus”. After this routine, the pressed key value is read using
“ReadKeyCode”.
Files Description
Keyboard routines to initialize
Keyboard, WaitForKeyPress,
Demo4.C
ReadKeyboardStatus, Display
routines for 7-segment display etc.
Declarations of functions in
Keyboard.H
keybaord
Declarations of functions in 7-
LEDDisplay.H
segement display
10
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Files Description
11
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Program Flow.
Start
Initialize Clock,
keyboard,LED
No
Is KeyboardFlag =1
Yes
Clear KeyboardFlag
Read KeyboardCode
Display it on 7-segment
LED
12
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
No Any Column
value = 0
Yes
Return Interrupt
Execute Application:
13
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Open the project Demo4 in the R8C/Tiny System Simulator using Open Project option from
Project menu. The project window opens up along with the Demo4.c file. Use Build option from
Build menu to compile the project. An output window captures the compiler ouput.
Use Project -> Download Project from main menu to download the Demo4.mot file into the
simulator’s memory for simulation.
14
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Open the LED Module settings window and do the settings to the 7-segment LED module as
shown. Connect 7 segments of the display to the port lines P00 to P07 and the 2 digit selection
lines to P30 and P31 respectively using radio buttons.
15
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Open the Keybaord Module settings window and do the settings. Connect the rows to P14 to
P17 and columns to P10 to P13.
16
www.MightyMicons.com
Demo 4 - 4 X 4 Matrix Keyboard Using Timer Interrupt
Then open the LED window usign the option View -> External Modules -> LED as shown
below and the Program Window. Open the Keybaord window usign the option View -> External
Modules -> Keyboard as shown below and the Program Window.
Run the program using Go from the Run menu. The program will display the pressed keyvalue on
the 7-segment display.
17
www.MightyMicons.com