LAB09 Report
LAB09 Report
Batch: BSEE22
Total
Name Roll number
(out of 35)
Signature: ____________________________
TASK#1
#include "stm32f407xx.h"
#include "rcc_config.h" // Assuming this header file contains system clock configuration
void Timer_Config(void);
void GPIO_Config(void);
void Delay_ms(void);
void Timer_Config(void){
RCC->APB1ENR |= (1<<0); // Enable Clock for Timer 2
TIM2->PSC = 83; // Set Prescaler to achieve desired clock frequency
TIM2->ARR = 9999; // Set Auto-Reload Register value for desired timer
period
TIM2->CR1 |= (1<<0); // Enable Timer 2
while (!(TIM2->SR&(1<<TIM_SR_UIF))); // Wait for the timer update event
TIM2->SR &= ~(uint32_t)(1<< TIM_SR_UIF); // Clear the update flag
void GPIO_Config(void){
RCC->AHB1ENR |= (1<<3); // Enable Clock for GPIO Port D
GPIOD->MODER |= (1<<26); // Set pin 13 of PORT D as output
GPIOD->MODER |= (1<<24); // Set pin 12 of PORT D as output
}
int main(void){
SysClockConfig(); // Configure system clock (Assumed from rcc_config.h)
Timer_Config(); // Configure timers
GPIO_Config(); // Configure GPIO ports
int i=0,j=0;
TIM2->CNT = 0; // Reset Timer 2 counter
TIM5->CNT = 0; // Reset Timer 5 counter
while(1){
// Toggle pin 13 of Port D every 20 cycles
if(i==20){
GPIOD->ODR ^= (uint32_t)(1<<13);
i=0;
}
// Toggle pin 12 of Port D every 200 cycles
if(j==200){
GPIOD->ODR ^= (uint32_t)(1<<12);
j=0;
}
// Increment i and reset Timer 2 on timer overflow
if((TIM2->SR&(1<<TIM_SR_UIF))){
i++;
TIM2->SR &= ~(uint32_t)(1<< TIM_SR_UIF); // Clear the update flag
TIM2->CNT = 0; // Reset Timer 2 counter
}
// Increment j and reset Timer 5 on timer overflow
if((TIM5->SR&(1<<TIM_SR_UIF))){
j++;
TIM5->SR &= ~(uint32_t)(1<< TIM_SR_UIF); // Clear the update flag
TIM5->CNT = 0; // Reset Timer 5 counter
}
}
}
The code sets up two timers (Timer 2 and Timer 5) and configures GPIO pins on a STM32
microcontroller. Timer 2 is used for delays, Timer 5 for periodic tasks. It heavily relies on bitwise
operations to manipulate hardware registers.
Task 2:
Code:
#include "stm32f407xx.h"
#include "rcc_config.h" // Assuming this header file contains system clock configuration
void Timer_Config(void);
void GPIO_Config(void);
void Delay_ms(void);
void TIM2_IRQHandler(void);
void TIM5_IRQHandler(void);
int i=0,j=0;
void Timer_Config(void){
RCC->APB1ENR |= (1<<0);
TIM2->PSC = 83;
TIM2->ARR = 9999;
TIM2->DIER |= (1<<0);
TIM2->CR1 |= (1<<0);
NVIC_EnableIRQ(TIM2_IRQn);
NVIC_SetPriority(TIM2_IRQn, 1);
RCC->APB1ENR |= (1<<3);
TIM5->ARR = 9999;
TIM5->DIER |= (1<<0);
TIM5->CR1 |= (1<<0);
NVIC_EnableIRQ(TIM5_IRQn);
NVIC_SetPriority(TIM5_IRQn, 1);
void TIM2_IRQHandler(void){
if(i==20){
GPIOD->ODR ^= (uint32_t)(1<<13);
i=0;
if((TIM2->SR&(1<<TIM_SR_UIF))){
i++;
TIM2->CNT = 0;
void TIM5_IRQHandler(void){
GPIOD->ODR ^= (uint32_t)(1<<12);
j=0;
if((TIM5->SR&(1<<TIM_SR_UIF))){
j++;
TIM5->CNT = 0;
void GPIO_Config(void){
RCC->AHB1ENR |= (1<<3);
GPIOD->MODER |= (1<<26);
GPIOD->MODER |= (1<<24);
int main(void){
while(1){
// Infinite loop to keep the program running
void Delay_ms(void){
OBSERVATION:
In this task, Timer 2 and Timer 5, and configures their interrupts to handle periodic tasks. Timer 2 is
used to toggle Pin 13 of Port D every 20 cycles, and Timer 5 is used to toggle Pin 12 of Port D every
200 cycles. It demonstrates interrupt-driven programming for handling time-sensitive tasks
efficiently on STM32 microcontrollers. Additionally, GPIO pins for Port D are configured as outputs
for LED control.
Task 3:
Code:
#include "stm32f407xx.h"
#include "rcc_config.h" // Assuming this header file contains system clock configuration
void Timer_Config(void);
void GPIO_Config(void);
void TIM2_IRQHandler(void);
void EXTI_Config(void);
void EXTI_Config(void){
RCC->AHB1ENR |= (1<<0);
RCC->APB2ENR |= (1<<14);
EXTI->IMR |= (1<<0);
EXTI->RTSR |= 1<<0;
NVIC_SetPriority(EXTI0_IRQn, 1);
NVIC_EnableIRQ (EXTI0_IRQn);
if(j>=100){
j=10;
EXTI->PR |= 1;
void Timer_Config(void){
RCC->APB1ENR |= (1<<0);
TIM2->PSC = 83;
TIM2->ARR = 9999;
TIM2->DIER |= (1<<0);
TIM2->CR1 |= (1<<0);
NVIC_EnableIRQ (TIM2_IRQn);
NVIC_SetPriority(TIM2_IRQn, 2);
void TIM2_IRQHandler(void){
if(i>=j){
GPIOD->ODR ^= (uint32_t)(1<<13);
i=0;
if((TIM2->SR&(1<<TIM_SR_UIF))){
i++;
TIM2->CNT = 0;
void GPIO_Config(void){
RCC->AHB1ENR |= (1<<3);
GPIOD->MODER |= (1<<26);
int main(void){
while(1){
OBSERVATION:
This task configures EXTI0 (External Interrupt 0) to respond to a rising edge on GPIOA Pin 0. When
this interrupt occurs, the value of 'j' is incremented by 10, resetting if it exceeds 100. Timer 2 is set
up to toggle Pin 13 of Port D when 'i' reaches or exceeds 'j'. This demonstrates a scenario where an
external event triggers a change in the behavior of a periodic task controlled by a timer.
Analysis:
1. Justify your choice of timers and their parameters for tasks 1 and 2.
Answer:
Justification for Timer Selection: We opted for Timer 2 & 5 due to their general-purpose
nature, connected to ABP1. Their clock sources match the Sys Clk/2, ensuring synchronization.
Moreover, their 32-bit resolution provides precise time counting, helpful for the tasks at hand.
Answer:
Approach for Task 3: Task 3 involves utilizing external interrupts for both timer and user
interaction. We assigned a higher priority to the user interrupt and a lower priority to the timer
interrupt. Leveraging the timer interrupt, we toggle an LED, while the user interrupt updates the PSC
register of the timer, allowing dynamic adjustments in timing behavior. This approach ensures
efficient handling of time-sensitive tasks while accommodating user inputs.
Assessment rubric for Lab 9
Performance CLO Able to complete the Able to complete the Tasks completion below Marks
tasks over 80% tasks 50 - 80% 50%
(4 – 5) (2 – 3) (0 – 1)
1. Realization of 1 Conceptually understands Needs guidance to Incapable of understanding
experiment the topic under study and understand the purpose of the purpose of the
develops the experimental the experiment and to experiment and
setup accordingly develop the required setup consequently fails to
develop the required setup
2. Conducting 1 Sets up the relevant Needs assistance in setting Unable to set up the
experiment hardware/software, writes up the software/hardware, hardware/software and to
and executes assembly/C makes minor errors in write and execute program
programs according to the writing programs according to task
requirement of tasks and according to the task requirements
examines the output requirements
rigorously
3. Data collection 1 Interprets program output Completes data collection Fails at observing output
and completes data with minor errors and states of experimental
collection as required in enters data in lab report setup and collecting data,
the lab task, ensures that with slight deviations from unable to fill the lab report
the data is presented in the provided guidelines properly
lab report according to the
specified instructions
4. Data analysis 1 Analyzes the data obtained Analyzes data with minor Unable to establish a
from experiment error and correlates it with relationship between
thoroughly and accurately theoretical values practical and theoretical
verifies it with theoretical reasonably. Attempts to results and lacks in-depth
understanding, accounts account for any understanding to justify the
for any discrepancy in data discrepancy in data from results or to explain any
from theory with sound theory discrepancy in data
explanation, where asked
5. Computer use 1 Possesses sufficient hands- Is able to work on relevant Cannot operate IDE
on ability to work with the IDE with some assistance without significant
relevant IDE assistance
6. Teamwork 3 Actively engages and Cooperates with other Distracts or discourages
cooperates with other group members in a other group members from
group members in an reasonable manner conducting the
effective manner experiments
7. Lab safety and 3 Observes lab safety rules; Observes safety rules and Disregards lab safety and
disciplinary rules and adheres to the lab disciplinary guidelines disciplinary rules
disciplinary guidelines with minor deviations
aptly
Total
(out of 35)