TIMER - Asian Institute of Technology
Transcript of TIMER - Asian Institute of Technology
![Page 1: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/1.jpg)
1
TIMER
![Page 2: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/4.jpg)
Advanced control timer block diagram
4
![Page 5: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/8.jpg)
Counter timing diagram with prescale
division changes from 1 to 2
8
![Page 9: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/9.jpg)
Counter timing diagram with prescale
division changes from 1 to 4
9
![Page 10: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/10.jpg)
Counter timing diagram (divided by 1)
10
![Page 11: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/11.jpg)
Counter timing diagram (divided by 2)
11
![Page 12: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/12.jpg)
Counter timing diagram (divided by 4)
12
![Page 13: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/13.jpg)
Counter timing diagram (divided by N)
13
![Page 14: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/14.jpg)
Counter modes
DIR bit in TIMx_CR1 register:
• Upcounting mode
• Downcounting mode
• Up/Down counting mode
14
![Page 15: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/16.jpg)
Counter timing diagram when ARPE=0
16
![Page 17: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/17.jpg)
Counter timing diagram when ARPE=1
17
![Page 18: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/19.jpg)
Down counter timing diagram (divided by 1)
19
![Page 20: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/20.jpg)
Down counter timing diagram (divided by 1)
20
![Page 21: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/21.jpg)
Down counter timing diagram (divided by 2)
21
![Page 22: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/22.jpg)
Down counter timing diagram (divided by 4)
22
![Page 23: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/23.jpg)
Down counter timing diagram (divided by 4)
23
![Page 24: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/24.jpg)
Down counter timing diagram (divided by N)
24
![Page 25: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/25.jpg)
Counter timing diagram with
auto-reload register update
25
![Page 26: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/27.jpg)
Counter timing diagram with clock divided by 1
27
![Page 28: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/29.jpg)
TIM1_RCR update
29
![Page 30: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/32.jpg)
Edge aligned PWM waveform
32
![Page 33: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/34.jpg)
Center align PWM waveform
34
![Page 35: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/39.jpg)
/* 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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/40.jpg)
Simulink: basic PWM
• This block can be used to generate PWM
signal.
40
![Page 41: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/41.jpg)
Configuration
41
![Page 42: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/42.jpg)
Example: Basic PWM
42
![Page 43: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/43.jpg)
Setup
43
![Page 44: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/44.jpg)
Output
44
![Page 45: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/45.jpg)
Active Low example
45
![Page 46: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/46.jpg)
Output
46
![Page 47: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/47.jpg)
Simulink: Advanced PWM
• To control advanced PWM
47
![Page 48: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/48.jpg)
Configuration
48
![Page 49: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/49.jpg)
Example
49
![Page 50: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/50.jpg)
Output
50
![Page 51: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/51.jpg)
Delay
51
![Page 52: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/52.jpg)
Configuration
52
![Page 53: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/53.jpg)
Example
53
![Page 54: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/54.jpg)
Simulink: Encoder read
54
![Page 55: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/55.jpg)
Configuration
55
![Page 56: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/56.jpg)
Example
56
![Page 57: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/57.jpg)
Com port setup
57
![Page 58: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/58.jpg)
Hardware
58
![Page 59: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/59.jpg)
Timer IRQ
59
![Page 60: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/60.jpg)
Configuration
60
![Page 61: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/61.jpg)
Configuration
61
![Page 62: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/62.jpg)
Example
62
![Page 63: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/63.jpg)
Stepper Motor Example
• External and Internal structure of Bi-polar
Stepper Motor
63
![Page 64: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/64.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/65.jpg)
Stepper motors
![Page 66: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/66.jpg)
Stepper motor
![Page 67: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/67.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/68.jpg)
Two Phase Rotation Control
• This technique provides more current and
hence more power.
68
![Page 69: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/69.jpg)
Half Step Rotation Control
69
![Page 70: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/70.jpg)
Waijun: Single phase
70
![Page 71: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/71.jpg)
Simulink Model
71
![Page 72: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/72.jpg)
Subsystem for motor control
72
![Page 73: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/73.jpg)
Full System Model
73
![Page 74: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/74.jpg)
Two-Phase Model
74
![Page 75: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/75.jpg)
Half Phase Model
75
![Page 76: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/76.jpg)
Subsystem for Up/Down Counter
76
![Page 77: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/77.jpg)
Full System Block
77
![Page 78: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/78.jpg)
Trigger Subsystem block
78
![Page 79: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/79.jpg)
Rotation Speed Adjustment with DIP Switch
79
![Page 80: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/80.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/81.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/82.jpg)
Desired Step
• Change CW1 with our desired step
82
![Page 83: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/83.jpg)
RC Servo
83
![Page 84: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/84.jpg)
Servo Motor
84
![Page 85: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/85.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/86.jpg)
PWM Block
86
![Page 87: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/87.jpg)
Servo Control
87
![Page 88: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/88.jpg)
Results
88
![Page 89: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/89.jpg)
Servo with Potentiometer
89
![Page 90: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/90.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/91.jpg)
Results
91
![Page 92: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/92.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/93.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/94.jpg)
Timer Example
94
![Page 95: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/95.jpg)
Timer with Prescale
95
![Page 96: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/96.jpg)
Time Module
96
![Page 97: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/97.jpg)
PWM Duty Cycle
97
![Page 98: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/98.jpg)
Duty Cycle Adjust to the Edge Align mode
98
![Page 99: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/99.jpg)
Adjust PWM for the Center Align mode
99
![Page 100: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/100.jpg)
Basic PWM Block
100
![Page 101: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/101.jpg)
Adjusting the Duty Cycle
101
![Page 102: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/102.jpg)
Adjusting the Brightness of LED
102
![Page 103: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/103.jpg)
Advanced PWM Block
103
![Page 104: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/104.jpg)
Simulink Model for Generating PWM Signal
104
![Page 105: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/105.jpg)
PWM Signal at Channel 1
105
![Page 106: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/106.jpg)
PWM Signal at Channel 2
106
![Page 107: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/107.jpg)
PWM Signal at Channel 3
107
![Page 108: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/108.jpg)
Setting Timer IRQ Block
108
![Page 109: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/109.jpg)
Time IRQ Block
109
![Page 110: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/110.jpg)
Function Call Subsystem
110
![Page 111: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/111.jpg)
Sample Time Window
111
![Page 112: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/112.jpg)
Ultrasonic Module
112
![Page 113: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/113.jpg)
Operating Mode
113
![Page 114: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/114.jpg)
PWM Capture Block
114
![Page 115: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/115.jpg)
Comparing the Measured Signal
115
![Page 116: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/116.jpg)
The Experiment Setup
116
![Page 117: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/117.jpg)
Simulink Model
117
![Page 118: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/118.jpg)
Results
118
![Page 119: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/119.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/120.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/121.jpg)
H-Bridge - DC Motor Driver Circuit
![Page 122: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/122.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/123.jpg)
H-Bridge Example - Forward
HIGH LOW
![Page 124: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/124.jpg)
H-Bridge Example - Reverse
LOW HIGH
![Page 125: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/125.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/126.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/127.jpg)
Using Potentio Meter to control speed of DC motor
127
![Page 128: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/128.jpg)
Simulink Model
128
![Page 129: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/129.jpg)
Motor Direction Control
129
![Page 130: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/130.jpg)
Simulink Model
130
![Page 131: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/131.jpg)
Encoder Reading
• Structure of Rotary Encoder
131
![Page 132: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/132.jpg)
Detecting Direction
• If the signal A is leading signal B, the
motor is moving clockwise
132
![Page 133: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/133.jpg)
Signal Encoder Interface
133
![Page 134: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/134.jpg)
Waijung Encoder Read
134
![Page 135: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/135.jpg)
Reading Rotation Angle
135
![Page 136: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/136.jpg)
Simulink Model
136
![Page 137: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/137.jpg)
Converting Encoder Reading to Integer
137
![Page 138: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/138.jpg)
Results
138
![Page 139: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/139.jpg)
Waveform Results
139
![Page 140: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/140.jpg)
Question?
• Calculate speed (period) of each PWM?
140
![Page 141: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/141.jpg)
Answer:
• Period = 72 MHz / (2000*72) = 500 Hz
141
![Page 142: TIMER - Asian Institute of Technology](https://reader034.fdocuments.in/reader034/viewer/2022051217/62792349e45ca74866054b98/html5/thumbnails/142.jpg)
Questions?
142