TIMER - Asian Institute of Technology

Post on 09-May-2022

9 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

1

TIMER

TIM feature

• 16 bit up, down, up/down, and auto-reload

counter

• 16 bit programmable prescaler allowing

dividing the counter clock by any number

between 1 and 65535

• Up to 4 independent channel for:

– Input, Output, PWM, and One-pulse mode

output

• Synchronize circuit with other timers2

Time feature

• Repetition counter to update timer

registers only after a given number of

cycles of the counter

• Interrupt/DMA generation

• Support incremental encoder and hall

sensor circuitry for positioning purpose

• Trigger input for external clock

3

Advanced control timer block diagram

4

Timer-base unit

• Counter register (TIMx_CNT)

for counting purpose

• Prescaler register (TIMx_PSC)

for clock division

• Auto-reload register (TIMx_ARR) [period]

setting the maximum/minimum count value

to preload register

• Repetition counter register (TIMx_RCR)

the number of repetition5

Auto-reload register

• Writing or reading from the auto-reload

register will access to preload register

• The content of preload register are

transferred to the shadow register at each

update event (UEV) when ARPE register

=1

• When ARPE register =0, the update will

happen immediately

• The update of UEV is sent when the

counter reach overflow6

Prescaler description

• The prescaler can divide the counter clock

frequency by any factor between 1 and

65536

• It can be updated on the fly, but the

change will happen at the next update

event

• The value of prescaler has to be added

with 1

7

Counter timing diagram with prescale

division changes from 1 to 2

8

Counter timing diagram with prescale

division changes from 1 to 4

9

Counter timing diagram (divided by 1)

10

Counter timing diagram (divided by 2)

11

Counter timing diagram (divided by 4)

12

Counter timing diagram (divided by N)

13

Counter modes

DIR bit in TIMx_CR1 register:

• Upcounting mode

• Downcounting mode

• Up/Down counting mode

14

Upcounting

• The counter counts from 0 to auto-

reloaded valued

• Then, it restart from 0 and generate a

counter overflow event

• If the repetition counter is used, the update

event (UEV) is generated and will repeat

up to the number of times programmed in

repetition counter register

15

Counter timing diagram when ARPE=0

16

Counter timing diagram when ARPE=1

17

Down counting

• Counter count from auto-reload value

down to 0

• After reaching 0, it will restart from auto-

reload value and generate a count

underflow event

• If the repetition counter is used, the update

event (UEV) is generated, and will repeat

for the number of times programmed in

repetition register18

Down counter timing diagram (divided by 1)

19

Down counter timing diagram (divided by 1)

20

Down counter timing diagram (divided by 2)

21

Down counter timing diagram (divided by 4)

22

Down counter timing diagram (divided by 4)

23

Down counter timing diagram (divided by N)

24

Counter timing diagram with

auto-reload register update

25

Up/Down counting mode

• The counter counts from 0 to the auto-

reload value -1 then generate a counter

overflow event

• Then, it counts from auto reload value to

0, then generate a counter underflow

event

• Then, it restarts from 0

26

Counter timing diagram with clock divided by 1

27

Repetition counter

• It mainly uses in PWM

• It controls how the update event (UEV) or interrupt is

generated

• It will actually generated with TIM1_RCR (repetition

counter) is counted to 0

• TIM1_RCE will decrement when

– At each counter overflow in upcounting mode

– At each counter underflow in downcounting mode

– At each counter overflow and then underflow in up-

down counting mode

28

TIM1_RCR update

29

PWM mode

• It can generate a signal with a frequency

determined by the value of TIMx_ARR and

a duty cycle determined by TIMx_CCRx

register

• PWM mode can be selected

independently for each channel

• Timer is generated PWM in edge-aligned

mode or center aligned mode

• Polarity output (active high or active low)30

PWM edge-aligned mode

• Mode 1: set OCxM bit to “110”. During the

up count, PWM will be 1 when TIMx_CNT

< TIMx_CCRx

• Mode 2: set OCxM bit to “111”. During the

up count, PWM will be 0 when TIMx_CNT

< TIMx_CCRx

31

Edge aligned PWM waveform

32

PWM center aligned mode

• Mainly used in Up-Down counting mode

• To enable this mode, CMS bits are set to

any value except “00”

33

Center align PWM waveform

34

Example of ARM codeint flag=0;

TIM_OCInitTypeDef TIM_OCInitStructure_c1;

TIM_OCInitTypeDef TIM_OCInitStructure_c2;

TIM_OCInitTypeDef TIM_OCInitStructure_c3;

#define PWM_Period 2000

int main(){

int pulse1 = 1000/2;

int pulse1_change_flag=0;

flag = 0;

RCC_setup(); // RCC Configuration

GPIO_setup(); // GPIO Configuration

TIMER_setup(); // TIMER Configuration

while(1) {

TIM_OCInitStructure_c1.TIM_Pulse = pulse1; // set duty cycle

TIM_OCInit(TIM4, &TIM_OCInitStructure_c1);

}

} 35

void RCC_setup(){

ErrorStatus HSEStartUpStatus; // Keep error status

RCC_DeInit(); // RCC system reset(for debug purpose)

RCC_HSEConfig(RCC_HSE_ON); // Enable HSE

HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready

if(HSEStartUpStatus == SUCCESS)

{

RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK

RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK

RCC_PCLK1Config(RCC_HCLK_Div2); // PCLK1 = HCLK/2

RCC_ADCCLKConfig(RCC_PCLK2_Div4); // ADCCLK = PCLK2/4

FLASH_SetLatency(FLASH_Latency_2); // Flash 2 wait state

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

RCC_PLLCmd(ENABLE); // Enable PLL

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system

clock source

while(RCC_GetSYSCLKSource() != 0x08); // Wait till PLL is used as system clock

source

}

}

36

void GPIO_setup(){

GPIO_InitTypeDef GPIO_InitStructure; // Enable GPIOA GPIOB clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB

|RCC_APB2Periph_AFIO,ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_9 ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; // tim4_1,

tim4_2, tim4_3, tim4_4

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);

/* Disable the Serial Wire Jtag Debug Port SWJ-DP */

}37

void TIMER_setup(){

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); // Enable TIM4 clock

// Time base configuration

TIM_TimeBaseStructure.TIM_Period = 1999;//1999;

TIM_TimeBaseStructure.TIM_Prescaler = 71;//71;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

/* PWM1 Mode configuration: Channel1 */

TIM_OCInitStructure_c1.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure_c1.TIM_Channel = TIM_Channel_1;

TIM_OCInitStructure_c1.TIM_Pulse = (int)(PWM_Period / 2);

TIM_OCInitStructure_c1.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OCInit(TIM4, &TIM_OCInitStructure_c1);

38

/* PWM1 Mode configuration: Channel2 */

TIM_OCInitStructure_c2.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure_c2.TIM_Channel = TIM_Channel_2;

TIM_OCInitStructure_c2.TIM_Pulse = (int)(PWM_Period / 2);

TIM_OCInitStructure_c2.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OCInit(TIM4, &TIM_OCInitStructure_c2);

/* PWM1 Mode configuration: Channel3 */

TIM_OCInitStructure_c3.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure_c3.TIM_Channel = TIM_Channel_3;

TIM_OCInitStructure_c3.TIM_Pulse = (int)(PWM_Period / 2);

TIM_OCInitStructure_c3.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OCInit(TIM4, &TIM_OCInitStructure_c2);

TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM4, ENABLE);

TIM_Cmd(TIM4, ENABLE);

}

39

Simulink: basic PWM

• This block can be used to generate PWM

signal.

40

Configuration

41

Example: Basic PWM

42

Setup

43

Output

44

Active Low example

45

Output

46

Simulink: Advanced PWM

• To control advanced PWM

47

Configuration

48

Example

49

Output

50

Delay

51

Configuration

52

Example

53

Simulink: Encoder read

54

Configuration

55

Example

56

Com port setup

57

Hardware

58

Timer IRQ

59

Configuration

60

Configuration

61

Example

62

Stepper Motor Example

• External and Internal structure of Bi-polar

Stepper Motor

63

Stepper motor

• A synchronous electric motor that can divide a full rotation into a number of steps

• Motor position can be controlled precisely without any feedback system

• Doesn’t require feedback sensor

• Operate in DC power

• Used in many devices such as harddisk drives, and printers

• Can make motor spin by outputting the sequence like … 10,9,5,6,10,9,5,6….

• For 200 steps motor, each new output will cause the motor to rotate 1.8 degree

Stepper motors

Stepper motor

Single Phase Rotation Control

• The currents in the coils will have the

same direction. It will result in low drive

force and not recommended.

67

Two Phase Rotation Control

• This technique provides more current and

hence more power.

68

Half Step Rotation Control

69

Waijun: Single phase

70

Simulink Model

71

Subsystem for motor control

72

Full System Model

73

Two-Phase Model

74

Half Phase Model

75

Subsystem for Up/Down Counter

76

Full System Block

77

Trigger Subsystem block

78

Rotation Speed Adjustment with DIP Switch

79

Position Control with Stepper Motor

• From the properties of stepper motor in

the experiment, Step Angle: 5.625 degree

/ 64 means motor rotate 1 step the axis

will rotate 0.0879 degree, so if we want to

rotate 90 degree.

• Desired Step = 90 / 0.0879 = 1024 Step

80

Position Control with Push Button

• Rotate 90 degree CW when SW1 is pushed

• Rotate 90 degree CCW when SW2 is pushed

81

Desired Step

• Change CW1 with our desired step

82

RC Servo

83

Servo Motor

84

Servo Motor Turning

Standard for Servo motor control 1.5 ms

over 20ms will move to neutral position

85

PWM Block

86

Servo Control

87

Results

88

Servo with Potentiometer

89

Linear Relation Graph

• When resistor is connected to the ground, it will rotate to

-90 degree, when it is connected to Vdd, it will rotate 90

degree

90

Results

91

Question?

• If the user wants to set the RC servo to be

at 0 degree (Pulse Width =1.5ms), what

should be the duty cycle if signal

frequency = 60 Hz.

92

Answer

• Signal = 60 Hz, period = 16.67 ms

• The duty cycle = 1.5 x 100 / 16.67 = 9%

93

Timer Example

94

Timer with Prescale

95

Time Module

96

PWM Duty Cycle

97

Duty Cycle Adjust to the Edge Align mode

98

Adjust PWM for the Center Align mode

99

Basic PWM Block

100

Adjusting the Duty Cycle

101

Adjusting the Brightness of LED

102

Advanced PWM Block

103

Simulink Model for Generating PWM Signal

104

PWM Signal at Channel 1

105

PWM Signal at Channel 2

106

PWM Signal at Channel 3

107

Setting Timer IRQ Block

108

Time IRQ Block

109

Function Call Subsystem

110

Sample Time Window

111

Ultrasonic Module

112

Operating Mode

113

PWM Capture Block

114

Comparing the Measured Signal

115

The Experiment Setup

116

Simulink Model

117

Results

118

Driving Motors and Relays

• High current devices like motors, relays, solenoids, buzzers, and light bulbs can require more than 500mA of current

• Even though voltage levels may be the same, digital outputs from a GPIO (parallel) port typically drive only 5-20mA of current

• They cannot drive high current devices directly and trying to do so will likely blow out the output circuit

Driver Circuits

• A higher current driver circuit must be added after the digital output pin and before the device

• A driver circuit typically uses a discrete power transistor

• For DC motors, consider using an H-bridge circuit module. It contains four power transistors than can also reverse the motor.

• Diodes are often used for additional protection across the load on motors and relays. When you turn off the current in an inductive load it generates a reverse voltage spike that might damage the transistor (back EMF). The diode shorts it out.

H-Bridge - DC Motor Driver Circuit

H-Bridge Control FunctionsInput Function Operation

1 0 Forward DC Motor runs in the

forward direction

0 1 Reverse DC Motor runs in the

reverse direction

0 0 Stop Motor is not

connected – Coasts

1 1 Brake* or

Short Power Supply

(not allowed!)

Motor Terminals

Shorted or

Power Supply

Shorted!

*The Brake function requires a more complex decoder circuit to control the power transistors. Check the H-Bridge data sheet to make sure it is supported before using it. In some simple H-Bridge circuits, the fourth state must be avoided (i.e., illegal state) and it will short out the power supply!

H-Bridge Example - Forward

HIGH LOW

H-Bridge Example - Reverse

LOW HIGH

Figure 3.10 Fairchild FAN8100N Low Voltage Dual H-Bridge DC Motor Driver IC.

Images courtesy of Fairchild Semiconductor.

Higher current H-Bridge modules typically use discrete power transistors assembled on a board. This dual H-Bridge module switches up to 10 amps at 24V DC. The eight power transistors can be see arranged on the right side of the board. Photograph courtesy of RoboticsConnection.

Using Potentio Meter to control speed of DC motor

127

Simulink Model

128

Motor Direction Control

129

Simulink Model

130

Encoder Reading

• Structure of Rotary Encoder

131

Detecting Direction

• If the signal A is leading signal B, the

motor is moving clockwise

132

Signal Encoder Interface

133

Waijung Encoder Read

134

Reading Rotation Angle

135

Simulink Model

136

Converting Encoder Reading to Integer

137

Results

138

Waveform Results

139

Question?

• Calculate speed (period) of each PWM?

140

Answer:

• Period = 72 MHz / (2000*72) = 500 Hz

141

Questions?

142

top related