MBES - Lab 7-8 PDF
MBES - Lab 7-8 PDF
LABORATORY SESSION # 7
7 Hardware Delay Using STM32F100xx Timers
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
7.3 PROCEDURE
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
1
Student Workbook EE-07310: Microprocessor based Embedded Systems
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
3
Student Workbook EE-07310: Microprocessor based Embedded Systems
(c) GPIOC
- Hardware pictures
4
Student Workbook EE-07310: Microprocessor based Embedded Systems
- Oscilloscope screenshot
5
Student Workbook EE-07310: Microprocessor based Embedded Systems
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
100ms:_______________________________________________________________
_____________________________________________________________________
1s:__________________________________________________________________
_____________________________________________________________________
10s:_________________________________________________________________
_____________________________________________________________________
- Modify the program such that two LEDs (PC8 and PC9) toggles after
pressing the switch at 1Hz frequency
6
Student Workbook EE-07310: Microprocessor based Embedded Systems
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
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
8
Student Workbook EE-07310: Microprocessor based Embedded Systems
9
Student Workbook EE-07310: Microprocessor based Embedded Systems
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
10
Student Workbook EE-07310: Microprocessor based Embedded Systems
LABORATORY SESSION # 8
8 PWM Generation using STM32F100xx Timer
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
- __________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
8.3 PROCEDURE
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
11
Student Workbook EE-07310: Microprocessor based Embedded Systems
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
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
13
Student Workbook EE-07310: Microprocessor based Embedded Systems
#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
void PWM_Init(void);
void Duty_Vary_Ch_1(int Dc);
#endif
14
Student Workbook EE-07310: Microprocessor based Embedded Systems
15
Student Workbook EE-07310: Microprocessor based Embedded Systems
- Hardware pictures
- Oscilloscope screenshots
16
Student Workbook EE-07310: Microprocessor based Embedded Systems
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
#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
#include "main.h"
#include "PWM.h"
void PWM_Init(void)
{
RCC_APB2ENR |= 0x0000080C; // Enable clock to GPIO port A, B & Timer 1
#ifndef _PWM_H_
#define _PWM_H_
void PWM_Init(void);
void Duty_Vary_Ch_1(int Dc);
#endif
18
Student Workbook EE-07310: Microprocessor based Embedded Systems
- Generate PWM with different duty cycles (10%, 20%, 30% … 90%).
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();
}
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%
}
21
Student Workbook EE-07310: Microprocessor based Embedded Systems
22
Student Workbook EE-07310: Microprocessor based Embedded Systems
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
23