0% found this document useful (0 votes)
99 views23 pages

MBES - Lab 7-8 PDF

This document outlines a laboratory experiment on using timers on the STM32F100xx microcontroller to generate hardware delays. It provides the equipment needed, pre-lab preparations, procedures, code to program the microcontroller, expected outputs, and questions related to modifying the code and identifying hazards. The goal is to use timers to toggle LEDs on and off after a button press with delays of 500ms and 1Hz frequency. Students will observe output on an oscilloscope and learn about using timers instead of for loops for delays.

Uploaded by

Durraiz Shuaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views23 pages

MBES - Lab 7-8 PDF

This document outlines a laboratory experiment on using timers on the STM32F100xx microcontroller to generate hardware delays. It provides the equipment needed, pre-lab preparations, procedures, code to program the microcontroller, expected outputs, and questions related to modifying the code and identifying hazards. The goal is to use timers to toggle LEDs on and off after a button press with delays of 500ms and 1Hz frequency. Students will observe output on an oscilloscope and learn about using timers instead of for loops for delays.

Uploaded by

Durraiz Shuaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Student Workbook EE-07310: Microprocessor based Embedded Systems

LABORATORY SESSION # 7
7 Hardware Delay Using STM32F100xx Timers

7.1 EQUIPMENT & MATERIAL REQUIRED

- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________

7.2 PRE-LAB PREPARATIONS

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

7.3 PROCEDURE

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

1
Student Workbook EE-07310: Microprocessor based Embedded Systems

7.4 OBSERVATION & RESULTS

- Complete ‘main.c’ code

#define RCC_APB1ENR ( * ((volatile unsigned long*) 0x4002101C))


#define RCC_APB2ENR ( * ((volatile unsigned long*) 0x40021018))
#define GPIOA_IDR ( * ((volatile unsigned long*) 0x40010808))
#define GPIOC_BSRR ( * ((volatile unsigned long*) 0x40011010))
#define GPIOC_CRH ( * ((volatile unsigned long*) 0x40011004))
#define GPIOA_CRL ( * ((volatile unsigned long*) 0x40010800))
#define TIM2_CR1 ( * ((volatile unsigned long*) 0x40000000))
#define TIM2_SR ( * ((volatile unsigned long*) 0x40000010))
#define TIM2_PSC ( * ((volatile unsigned long*) 0x40000028))
#define TIM2_ARR ( * ((volatile unsigned long*) 0x4000002C))
#define TIM2_CNT ( * ((volatile unsigned long*) 0x40000024))

//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’
}
void SystemInit (void)
{
}
int main (void)
{
unsigned short Port_data;
RCC_APB2ENR |= 0x00000014; //enable clock to GPIO port A and C
GPIOA_CRL &= 0xFFFFFFF0; //PA0 connected with on-board switch
GPIOA_CRL |= 0x00000004; //PA0 as in input floating mode
GPIOC_CRH &= 0xFFFFFFF0; //clear previous config for port C pin 8
GPIOC_CRH |= 0x00000001; //config port C pin 8 as 10Mhz push pull
//general purpose output

2
Student Workbook EE-07310: Microprocessor based Embedded Systems

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


Cyclic_Start (500);
while(1)
{
Port_data = GPIOA_IDR; //copying the data from IDR register
if ((Port_data& 0x0001)==1) //IDR is a 16 bit register
{
GPIOC_BSRR = 0x00000100; //PC8 is set HIGH
Cyclic_Wait(); //Delay
GPIOC_BSRR = 0x01000000; //PC8 is set LOW
Cyclic_Wait(); //Delay
}
else
GPIOC_BSRR = 0x01000000; //PC8 is set LOW
}
return(1);
}

Figure 7.1:Complete code from Keil µVision

- Successful creation of HEX file

Figure 7.2:0 Errors, 0 Warnings

- Software screenshots in debugging session

3
Student Workbook EE-07310: Microprocessor based Embedded Systems

(a) Timer 2 (b) GPIOA

(c) GPIOC

Figure 7.3:(a) Timer2, (b) GPIOA and (c) GPIOC peripherals

- Hardware pictures

4
Student Workbook EE-07310: Microprocessor based Embedded Systems

Figure 7.4:Hardware setup

- Oscilloscope screenshot

PC8 Channel 1 & PA0 Channel 2

Figure 7.4:Oscilloscope screenshot

7.5 LEARNING OUTCOMES

5
Student Workbook EE-07310: Microprocessor based Embedded Systems

What do you learn from this lab?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

7.6 EXERCISE QUESTIONS

- Evaluate value of TIMx_PSC for 100ms, 1s and 10s delay.

100ms:_______________________________________________________________
_____________________________________________________________________
1s:__________________________________________________________________
_____________________________________________________________________
10s:_________________________________________________________________
_____________________________________________________________________

- Modify the program such that two LEDs (PC8 and PC9) toggles after
pressing the switch at 1Hz frequency

GPIOC_CRH &= 0xFFFFFF00; //clear previous config for port C pin 8


GPIOC_CRH |= 0x00000011; //configure port C pin 8 and 9 as 10Mhz push pull
Cyclic_Start (500); // for 500 ms(Two time delay given so is equal to 1s or 1Hz)
Rest Shown in fig 7.8

Figure 7.5:Modified code block

6
Student Workbook EE-07310: Microprocessor based Embedded Systems

PC8 Channel 1 & PA0 Channel 2

Figure 7.6:Oscilloscope screenshot of signals at PA0 and PC8

PC9 Channel 1 & PA0 Channel 2

Figure 7.7:Oscilloscope screenshot of signals at PA0 and PC9

7
Student Workbook EE-07310: Microprocessor based Embedded Systems

- Point-out the changes in the code section to toggle the two LEDs
alternatively.

while(1)
{
Port_data = GPIOA_IDR; //copying the data from IDR register
if ((Port_data& 0x0001)==1) //IDR is a 16 bit register
{
GPIOC_BSRR = 0x02000100; //PC8 is set HIGH and PC9 LOW
Cyclic_Wait(); //Delay
GPIOC_BSRR = 0x01000200; //PC8 is set LOW and PC9 HICH
Cyclic_Wait(); //Delay

Figure 7.8:Code section modified for blinking of LEDs alternatively

- Identify the hazards of using for loop instead of STM32F100xx timers

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

8
Student Workbook EE-07310: Microprocessor based Embedded Systems

GENERALIZED LAB RUBRICS


Component Above Meeting Approaching Below Weight / Score
Domain with Expectation Expectation Expectation Expectation Used 100
(1–4)
Taxonomy (4) (3) (2) (1) (Optional)

Is able to build a given


setup neatly and timely Is able to assemble Is not able to
Psychomotor

Is only able to copy


Building using correct hardware a given setup using assemble a given
(Hardware) components and / or correct hardware
a given setup using
setup using 
correct hardware
P4 can reorganize / adapt components after correct hardware
components
to new / special minor revisions components
requirements
Recording
Is able to record Is only able to
Measurements Is able to record Is unable to
(Hardware / accurate measurements
accurate record accurate
record accurate ☐
measurements most measurements on
Software) all the time measurements
of the time some occasions
C3
Is able to formulate
Is partially able to
/develop theories in Is able to evaluate
Investigation evaluate /conclude Is unable to
addition to evaluating /conclude correctly
(Hardware /
/concluding correctly about investigation
correctly about comprehend 
Software) investigation investigation
about investigation parameters by
C5 parameters by parameters
parameters by assessing assessing data
assessing data
data
Design /
Is unable to
Development Is able to design / Is able to partially
Is able to design / partially design /
Cognitive

of Solution develop the solution of design / develop the ☐


develop the solution develop the
(Hardware / a given problem and solution of a given
of a given problem solution of a
Software) add features to it problem
given problem
C6
Is able to use the
Software Is adept in the use of Is able to use the
software tool
Usage software tool and can
effectively by
software tool but Is unable to use 
(Software) access advanced cannot access all the the software tool
accessing all the
C3 features required features
required features
Is able to efficiently
Is able to complete
complete a given task
a given task using
Programming using advanced Is unable to
required Is able to partially
Language programming language
programming complete a given
partially 
(Software) constructs / methods / complete a given
language constructs task
C3–C6 commands and/or add task
/ methods /
features to the original
commands
task

9
Student Workbook EE-07310: Microprocessor based Embedded Systems

GENERALIZED LAB RUBRICS

Component Above Approaching Below


Meeting Expectation Use Weight Score
Domain with Expectation Expectation Expectation / 100
(3) d (1–4)
Taxonomy (4) (2) (1) (Optional)

Safety Assesses and Assesses and complies Assesses and complies Assesses and
Instructions complies with all with most EHS with some EHS complies with ☐
(PLO6) EHS instructions instructions while in instructions while in few EHS
A4 while in lab lab lab instructions in lab
Does not exhibit
Exhibits exemplary Makes an effort to professional
Professional Exhibits professional
professional ethics exhibit professional ethics while
Ethics ethics while dealing
while dealing with
with fellow students,
ethics while dealing dealing with ☐
(PLO8) fellow students, lab with fellow students, fellow students,
A3 lab staff and instructor
staff and instructor lab staff and instructor lab staff and
all the time
all the time all the time instructor all the
time
Consistently shows
Shows some
full preparation by
Consistently shows full preparation which is Shows very little
Contribution completing all
preparation by mostly at superficial or no preparation
(PLO9) agreed tasks and
completing all agreed level in completing a in completing a 
A5 provides additional
tasks and work requires task and work requires task and work
resources for the
Affective

little or no revisions much revisions and quality is poor


group and work
editing
quality is excellent
Internalized positive
Attitude Consistent positive Neither helpful nor Discouraging
behavior and
(PLO9) encourages and
behavior most of the damaging and shows behavior towards ☐
A5 time towards other disinterest in the other team
helps other team
team members performance of others members
members
Report on all Report on all relevant Report on all relevant
relevant sections sections related to the sections related to the
Report related to the lab lab tasks is completed lab tasks is completed Report on all
Writing tasks is completed but few deficiencies but many deficiencies relevant sections
accurately, meeting are present in terms of are present in terms of related to the lab ☐
(PLO10)
A2 the requirements, in accuracy / meeting the accuracy / meeting the tasks is not
prescribed time and requirements / requirements / completed
with good language prescribed time / good prescribed time / good
skills language skills language skills
Lab Task Does not manage
Management Manages tasks well
within given
Manages tasks within Manages tasks in an tasks even in ☐
(PLO11) given timeframe extended timeframe extended
A3 timeframe
timeframes

10
Student Workbook EE-07310: Microprocessor based Embedded Systems

LABORATORY SESSION # 8
8 PWM Generation using STM32F100xx Timer

8.1 EQUIPMENT & MATERIAL REQUIRED

- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________

8.2 PRE-LAB PREPARATIONS

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

8.3 PROCEDURE

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

11
Student Workbook EE-07310: Microprocessor based Embedded Systems

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

8.4 OBSERVATION & RESULTS


- Complete code

Main.c:
#include "main.h"
#include "PWM.h"
void SystemInit(void)
{}
void Cyclic_Start(const unsigned short PERIOD)
{ RCC_APB1ENR |= 0x00000001;
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
if(PERIOD > 1)
{
TIM2_ARR = (PERIOD-1);
}
else
{
TIM2_ARR = 1;
}
TIM2_CR1 = 0x0001;
}
void Cyclic_Wait(void)
{
while((TIM2_SR & 0x00000001)==0)
{
}
TIM2_SR = 0;
}
// delay function end
int main(void)
{
int Dc=0;
PWM_Init();
Cyclic_Start(10);
while(1)
{
Duty_Vary_Ch_1(10000);
Cyclic_Wait();
}
return(1);

12
Student Workbook EE-07310: Microprocessor based Embedded Systems

}
Main.h
#ifndef _MAIN_H
#define _MAIN_H

#define RCC_APB2ENR ( * ((volatile unsigned long*) 0x40021018))


#define RCC_APB1ENR ( * ((volatile unsigned long*) 0x4002101C))
#define GPIOA_CRL ( * ((volatile unsigned long*) 0x40010800))
#define GPIOA_CRH ( * ((volatile unsigned long*) 0x40010804))
#define GPIOA_IDR ( * ((volatile unsigned long*) 0x40010808))
#define GPIOA_ODR ( * ((volatile unsigned long*) 0x4001080C))
#define GPIOA_BSRR ( * ((volatile unsigned long*) 0x40010810))
#define GPIOA_BRR ( * ((volatile unsigned long*) 0x40010814))
#define GPIOA_LCKR ( * ((volatile unsigned long*) 0x40010818))

#define GPIOB_CRL ( * ((volatile unsigned long*) 0x40010C00))


#define GPIOB_CRH ( * ((volatile unsigned long*) 0x40010C04))
#define GPIOB_IDR ( * ((volatile unsigned long*) 0x40010C08))
#define GPIOB_ODR ( * ((volatile unsigned long*) 0x40010C0C))
#define GPIOB_BSRR ( * ((volatile unsigned long*) 0x40010C10))
#define GPIOB_BRR ( * ((volatile unsigned long*) 0x40010C14))
#define GPIOB_LCKR ( * ((volatile unsigned long*) 0x40010C18))

#define GPIOC_CRL ( * ((volatile unsigned long*) 0x40011000))


#define GPIOC_CRH ( * ((volatile unsigned long*) 0x40011004))
#define GPIOC_IDR ( * ((volatile unsigned long*) 0x40011008))
#define GPIOC_ODR ( * ((volatile unsigned long*) 0x4001100C))
#define GPIOC_BSRR ( * ((volatile unsigned long*) 0x40011010))
#define GPIOC_BRR ( * ((volatile unsigned long*) 0x40011014))
#define GPIOC_LCKR ( * ((volatile unsigned long*) 0x40011018))

#define TIM1_CR1 ( * ((volatile unsigned long*) 0x40012C00))


#define TIM1_CR2 ( * ((volatile unsigned long*) 0x40012C04))
#define TIM1_CNT ( * ((volatile unsigned long*) 0x40012C24))
#define TIM1_PSC ( * ((volatile unsigned long*) 0x40012C28))
#define TIM1_ARR ( * ((volatile unsigned long*) 0x40012C2C))
#define TIM1_CCMR1 ( * ((volatile unsigned long*) 0x40012C18))
#define TIM1_CCER ( * ((volatile unsigned long*) 0x40012C20))
#define TIM1_CCR1 ( * ((volatile unsigned long*) 0x40012C34))
#define TIM1_CCR2 ( * ((volatile unsigned long*) 0x40012C38))
#define TIM1_CCR3 ( * ((volatile unsigned long*) 0x40012C3C))
#define TIM1_EGR ( * ((volatile unsigned long*) 0x40012C14))
#define TIM1_BDTR ( * ((volatile unsigned long*) 0x40012C44))
#define TIM1_SR ( * ((volatile unsigned long*) 0x40012C10))

#define TIM2_CR1 ( * ((volatile unsigned long*) 0x40000000))


#define TIM2_CNT ( * ((volatile unsigned long*) 0x40000024))
#define TIM2_PSC ( * ((volatile unsigned long*) 0x40000028))
#define TIM2_ARR ( * ((volatile unsigned long*) 0x4000002C))
#define TIM2_SR ( * ((volatile unsigned long*) 0x40000010))

#define TIM3_CR1 ( * ((volatile unsigned long*) 0x40000400))


#define TIM3_CR2 ( * ((volatile unsigned long*) 0x40012C04))
#define TIM3_CNT ( * ((volatile unsigned long*) 0x40000424))
#define TIM3_PSC ( * ((volatile unsigned long*) 0x40000428))
#define TIM3_ARR ( * ((volatile unsigned long*) 0x4000042C))
#define TIM3_CCMR1 ( * ((volatile unsigned long*) 0x40000418))
#define TIM3_CCMR2 ( * ((volatile unsigned long*) 0x4000041C))

13
Student Workbook EE-07310: Microprocessor based Embedded Systems

#define TIM3_CCER ( * ((volatile unsigned long*) 0x40000420))


#define TIM3_CCR1 ( * ((volatile unsigned long*) 0x40000434))
#define TIM3_CCR2 ( * ((volatile unsigned long*) 0x40000438))
#define TIM3_CCR3 ( * ((volatile unsigned long*) 0x4000043C))
#define TIM3_EGR ( * ((volatile unsigned long*) 0x40000414))
#define TIM3_SR ( * ((volatile unsigned long*) 0x40000410))

#endif
Pwm.c:
#include "main.h"
#include "PWM.h"

void PWM_Init(void)
{
RCC_APB2ENR |= 0x0000080C; // Enable clock to GPIO port A, B & Timer 1

GPIOA_CRH &= 0xFFFFFFF0; /* Clear previous config for PA8


(PA8 is connected to TIM1_CH1)
- user manual page 15*/
GPIOA_CRH |= 0x00000009; // 10Mhz push pull alternate output.

TIM1_CR1 = 0x0080; /* Enable Auto Reload Preload Enabled


-pg248(also check DIR register-page248)*/
TIM1_CNT = 0; /* Initialise counter from 0*/
TIM1_PSC = 7; /* Divide the default 8M Hz into
0.1M Hz clock(1us) ----- resolution*/
TIM1_ARR = 20000-1; /* 50Hz - 100 counts-after every 1us increase 1 in
the counter (counts x resolution = 1/PWM Frequency)*/
TIM1_CCMR1 = 0x0068; /* Output compare 1(preload enable &PWM mode 1
enabled &configured as output) - page 259 ref manual*/
/*TIM1_CCMR1 = 0x6868; Output compare 1+2 (preload enable & PWM mode
1enabled & configured as output) - page 259 ref manual*/
/*TIM1_CCMR2 = 0x0068; Output compare 3 (preload enable & PWM mode
1 enabled& configured as output)- page 259 ref manual*/
TIM1_CCER = 0x0001; /* Signal is output on the corresponding pin
- page 262 ref manual*/

TIM1_EGR = 0x0001; /* Reinitialize the couner& gen updates of the registers*/


TIM1_BDTR = 0x8000; /* MOE - Main output enabled - page 268 ref manual*/
TIM1_CR1 |= 1; /* Timer starts - counter enabled*/
}

void Duty_Vary_Ch_1(int Dc)


{
TIM1_CCR1 = Dc; /* Capture/Compare register for channel 1; defines duty cycle*/
/* Simillarly we can have 4 different duty cycles
by using CCR1 to CCR4 (freq will be same)*/
}
Pwm.h:
#ifndef _PWM_H_
#define _PWM_H_

void PWM_Init(void);
void Duty_Vary_Ch_1(int Dc);

#endif

Figure 8.1:Complete code from Keil µVision

14
Student Workbook EE-07310: Microprocessor based Embedded Systems

- Successful creation of HEX file

Figure 8.2:0 Errors, 0 Warnings

- Software screenshots in debugging session

Figure 8.3:Debugger session

15
Student Workbook EE-07310: Microprocessor based Embedded Systems

- Hardware pictures

Figure 8.4:Hardware setup

- Oscilloscope screenshots

Figure 8.5:Oscilloscope screenshot

16
Student Workbook EE-07310: Microprocessor based Embedded Systems

8.5 LEARNING OUTCOMES


What do you learn from this lab?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

8.6 EXERCISE QUESTIONS


- Modify the program so that the value of TIMx_CCRx is assigned through
passing a variable in the function Duty_Cycle()[Hint: Modify the function
in ‘main.c’].

#include "main.h"
#include "PWM.h"
void SystemInit(void)
{}
void Cyclic_Start(const unsigned short PERIOD)
{ RCC_APB1ENR |= 0x00000001;
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
if(PERIOD > 1)
{
TIM2_ARR = (PERIOD-1);
}
else
{
TIM2_ARR = 1;
}
TIM2_CR1 = 0x0001;
}
void Cyclic_Wait(void)
{
while((TIM2_SR & 0x00000001)==0)
{
}
TIM2_SR = 0;
}
// delay function end
int main(void)
{
int Dc=0;
PWM_Init();
Cyclic_Start(10);
while(1)
{
Duty_Vary_Ch_1(10000);
Cyclic_Wait();
}
return(1);
}

17
Student Workbook EE-07310: Microprocessor based Embedded Systems

Figure 8.6:‘main.c’ code

#include "main.h"
#include "PWM.h"

void PWM_Init(void)
{
RCC_APB2ENR |= 0x0000080C; // Enable clock to GPIO port A, B & Timer 1

GPIOA_CRH &= 0xFFFFFFF0; /* Clear previous config for PA8


(PA8 is connected to TIM1_CH1)
- user manual page 15*/
GPIOA_CRH |= 0x00000009; // 10Mhz push pull alternate output.

TIM1_CR1 = 0x0080; /* Enable Auto Reload Preload Enabled


-pg248(also check DIR register-page248)*/
TIM1_CNT = 0; /* Initialise counter from 0*/
TIM1_PSC = 7; /* Divide the default 8M Hz into
0.1M Hz clock(1us) ----- resolution*/
TIM1_ARR = 20000-1; /* 50Hz - 100 counts-after every 1us increase 1 in
the counter (counts x resolution = 1/PWM Frequency)*/
TIM1_CCMR1 = 0x0068; /* Output compare 1(preload enable &PWM mode 1
enabled &configured as output) - page 259 ref manual*/
/*TIM1_CCMR1 = 0x6868; Output compare 1+2 (preload enable & PWM mode
1enabled & configured as output) - page 259 ref manual*/
/*TIM1_CCMR2 = 0x0068; Output compare 3 (preload enable & PWM mode
1 enabled& configured as output)- page 259 ref manual*/
TIM1_CCER = 0x0001; /* Signal is output on the corresponding pin
- page 262 ref manual*/
TIM1_EGR = 0x0001; /* Reinitialize the couner& gen updates of the registers*/
TIM1_BDTR = 0x8000; /* MOE - Main output enabled - page 268 ref manual*/
TIM1_CR1 |= 1; /* Timer starts - counter enabled*/
}
void Duty_Vary_Ch_1(int Dc)
{
TIM1_CCR1 = Dc; /* Capture/Compare register for channel 1; defines duty cycle*/
/* Simillarly we can have 4 different duty cycles
by using CCR1 to CCR4 (freq will be same)*/
}

Figure 8.7:‘pwm.c’ code

#ifndef _PWM_H_
#define _PWM_H_

void PWM_Init(void);
void Duty_Vary_Ch_1(int Dc);

#endif

Figure 8.8:‘pwm.h’ code

18
Student Workbook EE-07310: Microprocessor based Embedded Systems

- Generate PWM with different duty cycles (10%, 20%, 30% … 90%).

DC Code section modified Oscilloscope screenshot


10% while(1)
{

Duty_Vary_Ch_1(2000);

Cyclic_Wait();
}

20% while(1)
{

Duty_Vary_Ch_1(4000);

Cyclic_Wait();
}

30% while(1)
{

Duty_Vary_Ch_1(6000);

Cyclic_Wait();
}

40% while(1)
{

Duty_Vary_Ch_1(8000);

Cyclic_Wait();
}

19
Student Workbook EE-07310: Microprocessor based Embedded Systems

50% while(1)
{

Duty_Vary_Ch_1(10000);

Cyclic_Wait();
}

60% while(1)
{

Duty_Vary_Ch_1(12000);

Cyclic_Wait();
}

70% while(1)
{

Duty_Vary_Ch_1(14000);

Cyclic_Wait();
}

80% while(1)
{

Duty_Vary_Ch_1(16000);

Cyclic_Wait();
}

90% while(1)
{

Duty_Vary_Ch_1(18000);

Cyclic_Wait();
}

Figure 8.9:PWM with different duty cycles

20
Student Workbook EE-07310: Microprocessor based Embedded Systems

- Re-write the program to generate a PWM with variable duty cycle, the duty
cycle automatically changes after a specific interval of time (say 3 sec).

Main.c:
while(1)
{
Duty_Vary_Ch_1(10000);//+50%
Cyclic_Wait();
break;
}
Pwm.c:
void Duty_Vary_Ch_1(int Dc)
{
volatile unsigned int i;
TIM1_CCR1 = 10000;//+50%
for (i=0; i<2400000; i++)
{
}
TIM1_CCR1 = 18000; //+90%
}

Figure 8.10:Code block

Figure 8.11:Oscilloscope screenshot

21
Student Workbook EE-07310: Microprocessor based Embedded Systems

GENERALIZED LAB RUBRICS


Component Above Meeting Approaching Below Weight / Score
Domain with Expectation Expectation Expectation Expectation Used 100
(1–4)
Taxonomy (4) (3) (2) (1) (Optional)

Is able to build a given


setup neatly and timely Is able to assemble Is not able to
Psychomotor

Is only able to copy


Building using correct hardware a given setup using assemble a given
(Hardware) components and / or correct hardware
a given setup using
setup using 
correct hardware
P4 can reorganize / adapt components after correct hardware
components
to new / special minor revisions components
requirements
Recording
Is able to record Is only able to
Measurements Is able to record Is unable to
(Hardware / accurate measurements
accurate record accurate
record accurate ☐
measurements most measurements on
Software) all the time measurements
of the time some occasions
C3
Is able to formulate
Is partially able to
/develop theories in Is able to evaluate
Investigation evaluate /conclude Is unable to
addition to evaluating /conclude correctly
(Hardware /
/concluding correctly about investigation
correctly about comprehend 
Software) investigation investigation
about investigation parameters by
C5 parameters by parameters
parameters by assessing assessing data
assessing data
data
Design /
Is unable to
Development Is able to design / Is able to partially
Is able to design / partially design /
Cognitive

of Solution develop the solution of design / develop the ☐


develop the solution develop the
(Hardware / a given problem and solution of a given
of a given problem solution of a
Software) add features to it problem
given problem
C6
Is able to use the
Software Is adept in the use of Is able to use the
software tool
Usage software tool and can
effectively by
software tool but Is unable to use 
(Software) access advanced cannot access all the the software tool
accessing all the
C3 features required features
required features
Is able to efficiently
Is able to complete
complete a given task
a given task using
Programming using advanced Is unable to
required Is able to partially
Language programming language
programming complete a given
partially 
(Software) constructs / methods / complete a given
language constructs task
C3–C6 commands and/or add task
/ methods /
features to the original
commands
task

22
Student Workbook EE-07310: Microprocessor based Embedded Systems

GENERALIZED LAB RUBRICS

Component Above Approaching Below


Meeting Expectation Use Weight Score
Domain with Expectation Expectation Expectation / 100
(3) d (1–4)
Taxonomy (4) (2) (1) (Optional)

Safety Assesses and Assesses and complies Assesses and complies Assesses and
Instructions complies with all with most EHS with some EHS complies with ☐
(PLO6) EHS instructions instructions while in instructions while in few EHS
A4 while in lab lab lab instructions in lab
Does not exhibit
Exhibits exemplary Makes an effort to professional
Professional Exhibits professional
professional ethics exhibit professional ethics while
Ethics ethics while dealing
while dealing with
with fellow students,
ethics while dealing dealing with ☐
(PLO8) fellow students, lab with fellow students, fellow students,
A3 lab staff and instructor
staff and instructor lab staff and instructor lab staff and
all the time
all the time all the time instructor all the
time
Consistently shows
Shows some
full preparation by
Consistently shows full preparation which is Shows very little
Contribution completing all
preparation by mostly at superficial or no preparation
(PLO9) agreed tasks and
completing all agreed level in completing a in completing a 
A5 provides additional
tasks and work requires task and work requires task and work
resources for the
Affective

little or no revisions much revisions and quality is poor


group and work
editing
quality is excellent
Internalized positive
Attitude Consistent positive Neither helpful nor Discouraging
behavior and
(PLO9) encourages and
behavior most of the damaging and shows behavior towards ☐
A5 time towards other disinterest in the other team
helps other team
team members performance of others members
members
Report on all Report on all relevant Report on all relevant
relevant sections sections related to the sections related to the
Report related to the lab lab tasks is completed lab tasks is completed Report on all
Writing tasks is completed but few deficiencies but many deficiencies relevant sections
accurately, meeting are present in terms of are present in terms of related to the lab ☐
(PLO10)
A2 the requirements, in accuracy / meeting the accuracy / meeting the tasks is not
prescribed time and requirements / requirements / completed
with good language prescribed time / good prescribed time / good
skills language skills language skills
Lab Task Does not manage
Management Manages tasks well
within given
Manages tasks within Manages tasks in an tasks even in ☐
(PLO11) given timeframe extended timeframe extended
A3 timeframe
timeframes

23

You might also like