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

C Program To Count Numbers and Display Using LEDS

This document summarizes an experiment examining the execution of an interrupt service routine (ISR) in a C program on a microcontroller. It discusses how interrupts work by triggering an ISR in response to an event. The C program is compiled and run, toggling LEDs and incrementing a counter when a button is pressed. The ISR sets a flag to indicate when it is called. Register values and stack contents are examined before and after the ISR to understand its execution. A flowchart outlines the program flow of turning LEDs on/off, checking for the button press, and displaying the counter value.

Uploaded by

anssandeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

C Program To Count Numbers and Display Using LEDS

This document summarizes an experiment examining the execution of an interrupt service routine (ISR) in a C program on a microcontroller. It discusses how interrupts work by triggering an ISR in response to an event. The C program is compiled and run, toggling LEDs and incrementing a counter when a button is pressed. The ISR sets a flag to indicate when it is called. Register values and stack contents are examined before and after the ISR to understand its execution. A flowchart outlines the program flow of turning LEDs on/off, checking for the button press, and displaying the counter value.

Uploaded by

anssandeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction:

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):

An interrupt service routine is a routine that invokes the hardware in


response to an interrupt occurred by an event. ISR handles the interrupt and
returns the status to the processor.
Answers:
1)
Regis
ter
R0
R1
R2
R3
R4
R5
R6
R7

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

The address after servicing interrupt is 0x0800007F1, which is same.

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"

static int count = 0;


int pdbgISR = 0;
int red_on = 1;
int pdbgMAIN, pSW;
int *address = (int*)0x40001000;
// flag indicating ISR implemetation
static int isr_status=0;
int randomTime;
void button_press_isr(int sources) {
pSW = 1;
gpio_set(P_DBG_ISR, 1);
pdbgISR = 1;
gpio_set(P_DBG_ISR, 0);
pdbgISR = 0;
pSW = gpio_get(P_SW);
isr_status=1;
}
int main(void) {
// initialising peripherals
leds_init();
gpio_set_mode(P_DBG_ISR, Output);
gpio_set_mode(P_DBG_MAIN, Output);
// Set up on-board switch.
gpio_set_trigger(P_SW, Rising);
gpio_set_callback(P_SW, button_press_isr);
pSW = gpio_get(P_SW);
__enable_irq();
while (1) {
//Turning off Leds
leds_set(0, 0, 0);
//clearing the counter

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);
}
}

You might also like