timer - asian institute of technology

142
1 TIMER

Upload: others

Post on 09-May-2022

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TIMER - Asian Institute of Technology

1

TIMER

Page 2: TIMER - Asian Institute of Technology

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

Page 3: TIMER - Asian Institute of Technology

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

Page 4: TIMER - Asian Institute of Technology

Advanced control timer block diagram

4

Page 5: TIMER - Asian Institute of Technology

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

Page 6: TIMER - Asian Institute of Technology

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

Page 7: TIMER - Asian Institute of Technology

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

Page 8: TIMER - Asian Institute of Technology

Counter timing diagram with prescale

division changes from 1 to 2

8

Page 9: TIMER - Asian Institute of Technology

Counter timing diagram with prescale

division changes from 1 to 4

9

Page 10: TIMER - Asian Institute of Technology

Counter timing diagram (divided by 1)

10

Page 11: TIMER - Asian Institute of Technology

Counter timing diagram (divided by 2)

11

Page 12: TIMER - Asian Institute of Technology

Counter timing diagram (divided by 4)

12

Page 13: TIMER - Asian Institute of Technology

Counter timing diagram (divided by N)

13

Page 14: TIMER - Asian Institute of Technology

Counter modes

DIR bit in TIMx_CR1 register:

• Upcounting mode

• Downcounting mode

• Up/Down counting mode

14

Page 15: TIMER - Asian Institute of Technology

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

Page 16: TIMER - Asian Institute of Technology

Counter timing diagram when ARPE=0

16

Page 17: TIMER - Asian Institute of Technology

Counter timing diagram when ARPE=1

17

Page 18: TIMER - Asian Institute of Technology

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

Page 19: TIMER - Asian Institute of Technology

Down counter timing diagram (divided by 1)

19

Page 20: TIMER - Asian Institute of Technology

Down counter timing diagram (divided by 1)

20

Page 21: TIMER - Asian Institute of Technology

Down counter timing diagram (divided by 2)

21

Page 22: TIMER - Asian Institute of Technology

Down counter timing diagram (divided by 4)

22

Page 23: TIMER - Asian Institute of Technology

Down counter timing diagram (divided by 4)

23

Page 24: TIMER - Asian Institute of Technology

Down counter timing diagram (divided by N)

24

Page 25: TIMER - Asian Institute of Technology

Counter timing diagram with

auto-reload register update

25

Page 26: TIMER - Asian Institute of Technology

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

Page 27: TIMER - Asian Institute of Technology

Counter timing diagram with clock divided by 1

27

Page 28: TIMER - Asian Institute of Technology

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

Page 29: TIMER - Asian Institute of Technology

TIM1_RCR update

29

Page 30: TIMER - Asian Institute of Technology

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

Page 31: TIMER - Asian Institute of Technology

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

Page 32: TIMER - Asian Institute of Technology

Edge aligned PWM waveform

32

Page 33: TIMER - Asian Institute of Technology

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

Page 34: TIMER - Asian Institute of Technology

Center align PWM waveform

34

Page 35: TIMER - Asian Institute of Technology

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

Page 36: TIMER - Asian Institute of Technology

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

Page 37: TIMER - Asian Institute of Technology

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

Page 38: TIMER - Asian Institute of Technology

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

Page 39: TIMER - Asian Institute of Technology

/* 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

Page 40: TIMER - Asian Institute of Technology

Simulink: basic PWM

• This block can be used to generate PWM

signal.

40

Page 41: TIMER - Asian Institute of Technology

Configuration

41

Page 42: TIMER - Asian Institute of Technology

Example: Basic PWM

42

Page 43: TIMER - Asian Institute of Technology

Setup

43

Page 44: TIMER - Asian Institute of Technology

Output

44

Page 45: TIMER - Asian Institute of Technology

Active Low example

45

Page 46: TIMER - Asian Institute of Technology

Output

46

Page 47: TIMER - Asian Institute of Technology

Simulink: Advanced PWM

• To control advanced PWM

47

Page 48: TIMER - Asian Institute of Technology

Configuration

48

Page 49: TIMER - Asian Institute of Technology

Example

49

Page 50: TIMER - Asian Institute of Technology

Output

50

Page 51: TIMER - Asian Institute of Technology

Delay

51

Page 52: TIMER - Asian Institute of Technology

Configuration

52

Page 53: TIMER - Asian Institute of Technology

Example

53

Page 54: TIMER - Asian Institute of Technology

Simulink: Encoder read

54

Page 55: TIMER - Asian Institute of Technology

Configuration

55

Page 56: TIMER - Asian Institute of Technology

Example

56

Page 57: TIMER - Asian Institute of Technology

Com port setup

57

Page 58: TIMER - Asian Institute of Technology

Hardware

58

Page 59: TIMER - Asian Institute of Technology

Timer IRQ

59

Page 60: TIMER - Asian Institute of Technology

Configuration

60

Page 61: TIMER - Asian Institute of Technology

Configuration

61

Page 62: TIMER - Asian Institute of Technology

Example

62

Page 63: TIMER - Asian Institute of Technology

Stepper Motor Example

• External and Internal structure of Bi-polar

Stepper Motor

63

Page 64: TIMER - Asian Institute of Technology

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

Page 65: TIMER - Asian Institute of Technology

Stepper motors

Page 66: TIMER - Asian Institute of Technology

Stepper motor

Page 67: TIMER - Asian Institute of Technology

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

Page 68: TIMER - Asian Institute of Technology

Two Phase Rotation Control

• This technique provides more current and

hence more power.

68

Page 69: TIMER - Asian Institute of Technology

Half Step Rotation Control

69

Page 70: TIMER - Asian Institute of Technology

Waijun: Single phase

70

Page 71: TIMER - Asian Institute of Technology

Simulink Model

71

Page 72: TIMER - Asian Institute of Technology

Subsystem for motor control

72

Page 73: TIMER - Asian Institute of Technology

Full System Model

73

Page 74: TIMER - Asian Institute of Technology

Two-Phase Model

74

Page 75: TIMER - Asian Institute of Technology

Half Phase Model

75

Page 76: TIMER - Asian Institute of Technology

Subsystem for Up/Down Counter

76

Page 77: TIMER - Asian Institute of Technology

Full System Block

77

Page 78: TIMER - Asian Institute of Technology

Trigger Subsystem block

78

Page 79: TIMER - Asian Institute of Technology

Rotation Speed Adjustment with DIP Switch

79

Page 80: TIMER - Asian Institute of Technology

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

Page 81: TIMER - Asian Institute of Technology

Position Control with Push Button

• Rotate 90 degree CW when SW1 is pushed

• Rotate 90 degree CCW when SW2 is pushed

81

Page 82: TIMER - Asian Institute of Technology

Desired Step

• Change CW1 with our desired step

82

Page 83: TIMER - Asian Institute of Technology

RC Servo

83

Page 84: TIMER - Asian Institute of Technology

Servo Motor

84

Page 85: TIMER - Asian Institute of Technology

Servo Motor Turning

Standard for Servo motor control 1.5 ms

over 20ms will move to neutral position

85

Page 86: TIMER - Asian Institute of Technology

PWM Block

86

Page 87: TIMER - Asian Institute of Technology

Servo Control

87

Page 88: TIMER - Asian Institute of Technology

Results

88

Page 89: TIMER - Asian Institute of Technology

Servo with Potentiometer

89

Page 90: TIMER - Asian Institute of Technology

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

Page 91: TIMER - Asian Institute of Technology

Results

91

Page 92: TIMER - Asian Institute of Technology

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

Page 93: TIMER - Asian Institute of Technology

Answer

• Signal = 60 Hz, period = 16.67 ms

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

93

Page 94: TIMER - Asian Institute of Technology

Timer Example

94

Page 95: TIMER - Asian Institute of Technology

Timer with Prescale

95

Page 96: TIMER - Asian Institute of Technology

Time Module

96

Page 97: TIMER - Asian Institute of Technology

PWM Duty Cycle

97

Page 98: TIMER - Asian Institute of Technology

Duty Cycle Adjust to the Edge Align mode

98

Page 99: TIMER - Asian Institute of Technology

Adjust PWM for the Center Align mode

99

Page 100: TIMER - Asian Institute of Technology

Basic PWM Block

100

Page 101: TIMER - Asian Institute of Technology

Adjusting the Duty Cycle

101

Page 102: TIMER - Asian Institute of Technology

Adjusting the Brightness of LED

102

Page 103: TIMER - Asian Institute of Technology

Advanced PWM Block

103

Page 104: TIMER - Asian Institute of Technology

Simulink Model for Generating PWM Signal

104

Page 105: TIMER - Asian Institute of Technology

PWM Signal at Channel 1

105

Page 106: TIMER - Asian Institute of Technology

PWM Signal at Channel 2

106

Page 107: TIMER - Asian Institute of Technology

PWM Signal at Channel 3

107

Page 108: TIMER - Asian Institute of Technology

Setting Timer IRQ Block

108

Page 109: TIMER - Asian Institute of Technology

Time IRQ Block

109

Page 110: TIMER - Asian Institute of Technology

Function Call Subsystem

110

Page 111: TIMER - Asian Institute of Technology

Sample Time Window

111

Page 112: TIMER - Asian Institute of Technology

Ultrasonic Module

112

Page 113: TIMER - Asian Institute of Technology

Operating Mode

113

Page 114: TIMER - Asian Institute of Technology

PWM Capture Block

114

Page 115: TIMER - Asian Institute of Technology

Comparing the Measured Signal

115

Page 116: TIMER - Asian Institute of Technology

The Experiment Setup

116

Page 117: TIMER - Asian Institute of Technology

Simulink Model

117

Page 118: TIMER - Asian Institute of Technology

Results

118

Page 119: TIMER - Asian Institute of Technology

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

Page 120: TIMER - Asian Institute of Technology

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.

Page 121: TIMER - Asian Institute of Technology

H-Bridge - DC Motor Driver Circuit

Page 122: TIMER - Asian Institute of Technology

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!

Page 123: TIMER - Asian Institute of Technology

H-Bridge Example - Forward

HIGH LOW

Page 124: TIMER - Asian Institute of Technology

H-Bridge Example - Reverse

LOW HIGH

Page 125: TIMER - Asian Institute of Technology

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

Images courtesy of Fairchild Semiconductor.

Page 126: TIMER - Asian Institute of Technology

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.

Page 127: TIMER - Asian Institute of Technology

Using Potentio Meter to control speed of DC motor

127

Page 128: TIMER - Asian Institute of Technology

Simulink Model

128

Page 129: TIMER - Asian Institute of Technology

Motor Direction Control

129

Page 130: TIMER - Asian Institute of Technology

Simulink Model

130

Page 131: TIMER - Asian Institute of Technology

Encoder Reading

• Structure of Rotary Encoder

131

Page 132: TIMER - Asian Institute of Technology

Detecting Direction

• If the signal A is leading signal B, the

motor is moving clockwise

132

Page 133: TIMER - Asian Institute of Technology

Signal Encoder Interface

133

Page 134: TIMER - Asian Institute of Technology

Waijung Encoder Read

134

Page 135: TIMER - Asian Institute of Technology

Reading Rotation Angle

135

Page 136: TIMER - Asian Institute of Technology

Simulink Model

136

Page 137: TIMER - Asian Institute of Technology

Converting Encoder Reading to Integer

137

Page 138: TIMER - Asian Institute of Technology

Results

138

Page 139: TIMER - Asian Institute of Technology

Waveform Results

139

Page 140: TIMER - Asian Institute of Technology

Question?

• Calculate speed (period) of each PWM?

140

Page 141: TIMER - Asian Institute of Technology

Answer:

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

141

Page 142: TIMER - Asian Institute of Technology

Questions?

142