C Program To Count Numbers and Display Using LEDS
C Program To Count Numbers and Display Using LEDS
In this lab exercise the c program has been compiled and examined the
execution of interrupt service routine.
Technical Discussion
Interrupts:
An interrupt is a signal generated by the software or hardware which
indicates an event that requires an attention by the processor. To service an
interrupt, interrupt service routine (ISR) is called.
Interrupt Service Routine (ISR):
Value
0x0000000
1
0x08000C
E9
0x0000000
0
0x0000000
7
0x4002000
0
0x2000004
4
0x0000000
0
0x0000000
0
Regis
ter
R8
R9
R10
R11
R12
R13
(SP)
R14
(LR)
R15
(PC)
Value
0x000000
00
0x000000
00
0x080011
20
0x000000
00
0x200000
84
0x200006
38
0x080007
F1
0x08000C
E8
Register/S
tate
xPSR
Value
PRIMASK
0x410000
16
0x200006
38
0x000000
00
0
CONTROL
0x00
Mode
Handler
Privilege
Privileged
Stack
MSP
MSP
PSP
2)
Address
0x20000638
0x2000063C
0x20000640
0x20000644
0x20000648
0x2000064C
0x20000650
Value
00000000
FFFFFFE9
00000000
2000002C
40020000
00000000
20000084
Description
Saved R0
Saved R1
Saved R2
Saved R3
Saved R12
Saved LR
Saved PC
3)
Regis
ter
R0
R1
R2
R3
R4
R5
R6
R7
Value
0x0000000
1
0x08000C
E9
0x0000000
0
0x0000000
7
0x0000000
1
0x2000004
4
0x0000000
0
0x0000000
0
Regis
ter
R8
R9
R10
R11
R12
R13
(SP)
R14
(LR)
R15
(PC)
Value
0x000000
00
0x000000
00
0x000000
00
0x200000
84
0x200000
84
0x200006
30
0x080007
F1
0x08000C
EC
Register/S
tate
xPSR
Value
PRIMASK
0x410000
16
0x200006
30
0x000000
00
0
CONTROL
0x00
Mode
Handler
Privilege
Privileged
Stack
MSP
MSP
PSP
4)
Address
0x20000630
0x20000634
0x20000638
0x2000063C
0x20000640
0x20000644
0x20000648
Value
40020000
080007F1
00000000
FFFFFFE9
00000000
2000002C
40020000
Description
Saved R4
Saved LR
Saved R0
Saved R1
Saved R2
Saved R3
Saved R12
5) The return address is same after servicing the interrupt. The return
address is 0x080007F1.
The address before servicing interrupt is 0x0800007F1
2) Software Project
The C program is developed using keil compiler. Interrupt service routines
has been used as per the requirements provided. First all the leds are off,
then blue led switched on after random amount of time. Then the program
checks for push button continuously and also increments the counter value.
If the button is pressed the ISR gets activates and the flag isr_status is
updated in the interrupt service routine. After that the count value is
displayed using 3 leds for 5ms. The following is the flowchart.
Flowchart:
Results:
Conclusion
The C program is compiled and analyzed the execution of an interrupt. I am
able to understand how an interrupt works in real time applications.
Appendix:
Code:
#include
#include
#include
#include
#include
<platform.h>
<gpio.h>
<stdlib.h>
"leds.h"
"delay.h"
count=0;
// resetting the flag
isr_status=0;
// delay for random time
randomTime=(rand()%10);
delay_ms(randomTime*30);
// Turn on one LED - blue
leds_set(0,0,1);
while(isr_status==0){
gpio_toggle(P_DBG_MAIN);
pdbgMAIN = gpio_get(P_DBG_MAIN);
pSW = gpio_get(P_SW);
count++;
}
leds_set(count & 1, count & 2, count & 4);
//saving to memory
*address=count;
delay_ms(5000);
}
}