MPBES Lab 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Laboratory Manual EE-07310: MP Based Embedded Systems

LABORATORY SESSION # 6
Hardware Delay Using STM32F100xx Timers

6.1 OBJECTIVES

To create time delays using STM32F100xx timers.


6.2 EQUIPMENT & MATERIAL REQUIRED

- Keil µVision 4 – IDE


- GCC C Compiler
- STM32 ST Link Utility
- STM32F100RB Discovery Board
- STM32F100xx USB Data Cable
- STM32F100xx Trainer Board
- Oscilloscope and it’s Connecting Leads
- Connecting Cables

6.3 INTRODUCTION & THEORY

The advanced-control timers of STM32F100xx consist of a 16-bit auto-reload counter


driven by a programmable prescaler. It may be used for a variety of purposes, including
measuring the pulse lengths of input signals (input capture) or generating output waveforms
(output compare, PWM, complementary PWM with dead-time insertion). Pulse lengths and
waveform periods can be modulated from a few microseconds.
The main block of the programmable advanced-control timer is a 16-bit counter with
its related auto-reload register. The counter can count up, down or both up and down. The
counter clock can be divided by a prescaler. The counter, the auto-reload register and the
prescaler register can be written or read by software. This is true even when the counter is
running.
The time-base unit includes:
● Counter register (TIMx_CNT)
● Prescaler register (TIMx_PSC)
● Auto-reload register (TIMx_ARR)
● Repetition counter register (TIMx_RCR)
The prescaler can divide the counter clock frequency by any factor between 1 and
65536. It is based on a 16-bit counter controlled through a 16-bit register (in the TIMx_PSC
register). It can be changed on the fly as this control register is buffered. The new prescaler
ratio is taken into account at the next update event.

38
Laboratory Manual EE-07310: MP Based Embedded Systems

6.4 PRE-LAB PREPARATIONS

Students should know about:


- Software architectures
- Timers peripherals of STM32F100xx
- Sandwich delay using timers
- Timer registers and its functionalities
- Timer peripheral from the architecture perspective

6.5 PROCEDURE

- Create new project with the name ‘Lab6’, following the steps in Lab Session 1.
- In the ‘main.c’ file, write the code for implementing delay function using hardware timer,
as skeleton shown below.

...
//Configure and start the timer
void Cyclic_Start(const unsigned short PERIOD)
{
RCC_APB1ENR |= 0x00000001; //Timer 2 clock enables
TIM2_CR1 = 0; //Counter Register-Counter disabled (stopped)
TIM2_CNT = 0; //Counter value reset to 0
TIM2_PSC = 7999; //Pre-scaler value calculated for 1ms delay

if(PERIOD > 1)
{
TIM2_ARR = (PERIOD-1);
}
/* If value in PERIOD is given ‘1’, ARR will be 0. ARR is the value up to
which counter increments */
else
{
TIM2_ARR = 1; //When PERIOD is 1ms
}
TIM2_CR1 = 0x0001; //Timer counter enabled (time starts)
}
//Wait for the set time to pass
void Cyclic_Wait(void)
{
while((TIM2_SR & 0x00000001)==0)
{
} //Wait until counter counts up to ARR value
/* Value of register SR is 0 if counter value is <ARR value and value of
register SR is 1 if the counter value is >ARR value */

TIM2_SR = 0; //When counter counts up to ARR, then reset ‘SR’


}
...

39
Laboratory Manual EE-07310: MP Based Embedded Systems

//Block of code for using inside main() function


while(1)
{
//Instruction for setting on-board LED HIGH
Cyclic_Wait(); //Call DELAY function
//Instruction for setting on-board LED LOW
Cyclic_Wait(); // Call DELAY function
}
return(1);
...

- Complete the ‘main.c’ such that the LED blinks at 1Hz when the switch is pressed once.
- Successfully create the HEX file.
- Open the debugger session and start the simulation after opening the Timer 2, GPIOA and
GPIOC peripherals.
- Burn the program and test the results on hardware by connecting the hardware with
oscilloscope.
- Turn on the ‘measure’ window on oscilloscope and verify the frequency of LED toggling.

6.6 OBSERVATIONS & RESULTS

Students are required to provide the following components in their lab reports.
- Complete ‘main.c’ code developed
- HEX file created successfully in Keil µVision 4
- Software tested in Debugger
- Software tested on the hardware

6.7 LEARNING OUTCOMES

By the end of this lab, students should be able to:


- Create hardware delays with STM32F100RB timers.
- Design program for the configuration of timers.

6.8 EXERCISE QUESTIONS

- Modify the program to create hardware delays of 100ms and 1s.


- Modify the program such that two on-board LEDs toggles at once after pressing the switch
at 1Hz frequency exactly.
- Modify the program such that two on-board LEDs toggles alternatively after pressing the
switch at 1Hz frequency exactly.

40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy