4 Timer
4 Timer
com
BIHE university
Timer
• SysTick Timer
• STM32 Timers
• Up-counter
• Down counter
• Event Counter
• Timer
SysTick->LOAD
SysTick->VAL
void delay() {
SysTick->LOAD = 9;
SysTick->CTRL = 5; /*Enable the timer and choose system clock as the
clock source */
Solution:
Solution:
delay = (N + 1) / clk ➔ (N + 1) = delay × clk = 0.001 sec × 72 MHz =
72,000 ➔ N = 72,000 – 1 = 71999
void delay1ms(void)
{
SysTick->LOAD = 71999;
SysTick->CTRL = 0x5; /* Enable the timer and choose sysclk as the clock
source */
11
STM32 Timers
www. Micro Digital Ed. com
BIHE university
CMS (Center-aligned
•◼ CEN (Counter Enable) Mode Selection)
◼ DIR
– 0:(Direction)
counter disabled
– 1: count
• OPM (One
CMS Pulse
DIRMode)
Counting mode
00 0 Counting up
– 0: the
00
counter
1
counts continuously
Counting down
– 1: the
01 counter
X stops
Countatupthe
andnext
downupdate event.
10 X Count up and down
11 X Count up and down
TIM2, 3, and 4
TIM1
void delay()
{
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
TIM2->ARR = 71;
TIM2->SR = 0; /* clear the UIF flag */
TIM2->CR1 = 1; /* up counting */
while((TIM2->SR & 1) == 0); /* wait until the UIF flag is set */
TIM2->CR1 = 0; /*stop counting */
}
Solution:
void delay()
{
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
TIM2->ARR = 3599;
TIM2->CR1 = 1; /* up counting */
while((TIM2->SR & 1) == 0); /* wait until the UIF flag is set */
TIM2->CR1 = 0; /*stop counting */
TIM2->SR = 0; /* clear the UIF flag */
}
void delay() {
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
TIM2->PSC = 7200-1; /* PSC = 7199 */
TIM2->ARR = 500-1;
TIM2->SR = 0; /* clear the UIF flag */
TIM2->CR1 = 1; /* up counting */
while((TIM2->SR & 1) == 0); /* wait until the UIF flag is set */
TIM2->CR1 = 0; /*stop counting */
}
#include <stm32f10x.h>
void delay(void);
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable GPIO clocks */
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
GPIOC->CRH = 0x44344444; /* PC13 as output */
while(1) {
GPIOC->ODR ^= (1<<13); /* toggle PC13 */
delay();
}
}
void delay() {
TIM2->PSC = 7200-1; /* PSC = 7199 */
TIM2->ARR = 10000-1;
TIM2->SR = 0; /* clear the UIF flag */
TIM2->CR1 = 1; /* up counting */
while((TIM2->SR & 1) == 0); /* wait until the UIF flag is set */
TIM2->CR1 = 0; /*stop counting */
}
Electronics Department, HCMUT 25
Down Counting
www. Micro Digital Ed. com
BIHE university
void delay()
{
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
TIM2->ARR = 999;
TIM2->CR1 = 1; /* up counting */
while((TIM2->SR & 1) == 0); /* wait until the UIF flag is set */
TIM2->CR1 = 0; /*stop counting */
TIM2->SR = 0; /* clear the UIF flag */
}
Solution:
28
The Channels of TIMx
www. Micro Digital Ed. com
BIHE university
TIM3
TIM2
TIM4
OCnM Mode
000 Frozen
001 Active on match
010 Inactive on match
011 Toggle on match
100 Force inactive
101 Force active
110 PWM 1
111 PWM 2
(a)
OC2CE OC2M OC2PE OC2FE CC2S OC1CE OC1M OC1PE OC1F CC1S
TIMx_CCMR1: E
0 001 0 0 00 0 000 0 0 00
TIMx->CCMR1 = 0x1000;
(b)
OC2CE OC2M OC2PE OC2FE CC2S OC1CE OC1M OC1PE OC1FE CC1S
TIMx_CCMR1:
0 011 0 0 00 0 000 0 0 00
TIMx->CCMR1 = 0x3000;
(c)
OC4CE OC4M OC4PE OC4FE CC4S OC3CE OC3M OC3PE OC3FE CC3S
TIMx_CCMR2:
0 000 0 0 00 0 011 0 0 00
TIMx->CCMR2 = 0x0030;
#include <stm32f10x.h>
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable GPIO clocks */
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
GPIOA->CRL = 0x44444B44; /* PA2: alternate func. output */
TIM2->CCR3 = 200;
TIM2->CCER = 0x1 << 8; /* CC3P = 0, CC3E = 1 */
TIM2->CCMR2 = 0x0030; /* toggle channel 3 */
TIM2->ARR = 10000-1;
TIM2->CR1 = 1; /* start counting up */
while(1) {}
}
#include <stm32f10x.h>
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable GPIO clocks */
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
GPIOA->CRL = 0x44444BB4; /* PA2(CH3), PA1(CH2): alternate func. output */
TIM2->CCR2 = 1000;
TIM2->CCR3 = 3000;
TIM2->CCER = (0x1<<8)|(0x1<<4); /*CC3P = 0, CC3E = 1, CC2E = 1 */
TIM2->CCMR1 = 0x3000; /* toggle ch.2 */
TIM2->CCMR2 = 0x0030; /* toggle ch. 3 */
TIM2->PSC = 7200-1;
TIM2->ARR = 10000-1;
TIM2->CR1 = 1;
while(1) { }
}
Solution:
TIMx_CCM IC2F IC2PSC CC2S IC1F IC1PSC CC1S
R1: 0000 00 00 0010 00 01
TIMx->CCMR1=0x0021;
TIMx_CC Res. CC4P CC4E Res. CC3P CC3E Res. CC2P CC2E Res. CC1P CC1E
ER 00 0 0 00 0 0 00 0 0 00 0 1
TIMx->CCER=0x0001;
#include <stdio.h>
void usart1_init(void);
void usart1_sendByte(unsigned char c);
void usart1_sendInt(unsigned int i);
void usart1_sendStr(char *str);
int main( ) {
RCC->APB2ENR |= (0xFC| (1<<14)); /* enable GPIO clocks and USART1 clock */
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
47
Timers Clock Sources
www. Micro Digital Ed. com
BIHE university
SMCR.TS:
000 to 011: Internal Triggers
SMS Mode Description
0 Timer mode clocked by the internal clock (TI1F_ED)
100: TI1 Edge detector
1 Encoder mode 1 101:counts
counter Filtered Timer
up/down onInput 1 (TI1FP1)
TI2FP1 from TIMx_CH1
edge depending on TI1FP2 level.
2 Encoder mode 2 110:counts
counter Filtered TimeronInput
up/down 2 (TI2FP2)
TI1FP2 from TIMx_CH2
edge depending on TI2FP1 level.
3 Encoder mode 3 111:counts
counter External Trigger
up/down Input
on both (ETRF)
TI1FP2 andfrom TIMx_ETR
TI2FP2 edges pin
depending on the level of the other.
4 Reset mode The counter is reset and the registers are updated on each rising
edge of TRGI signal. (See the manual.)
5 Gated mode As long as the TRGI signal is high, the timer counts. The timer stops
SMCR (Slave
counting Mode
when Control
the TRGI Register)
signal is low.
6 Trigger mode at the rising edge of the TRGI signal, it starts counting.
7 External clock mode 1 Counts the rising edges of TRGI signal. 48
Electronics Department, HCMUT
TIMx_CH1 and TIMx_CH2 External Clock
www. Micro Digital Ed. com
Circuit BIHE university
(a)
TIMx_S ETP ECE ETPS ETF MSM TS Res. SMS
MCR 0 0 00 0000 0 101 0 111
TIMx_SMCR = 0x57;
(b)
TIMx_S ETP ECE ETPS ETF MSM TS Res. SMS
MCR 0 0 00 0000 0 110 0 111
TIMx_SMCR = 0x67;
int main( ) {
RCC->APB2ENR |= (0xFC| (1<<14)); // enable GPIO clocks and USART1 clock
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
while(1) {
usart1_sendInt(TIM2->CNT); /* send the counter value through serial */
usart1_sendStr("\n\r"); /* go to new line */
delay_ms(100);
}
}
Electronics Department, HCMUT 51
Example: A clock pulse is fed into pin TIM2_CH1(PA0). Write a
program that toggles PC13 every 100 pulses.
www. Micro Digital Ed. com
BIHE university
#include <stm32f10x.h>
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable GPIO clocks and USART1 clock */
RCC->APB1ENR |= (1<<0); /* enable TIM2 clock */
GPIOA->CRL = 0x44444448; /* PA0(CH1): input pull-up */
GPIOA->ODR |= (1<<0);
GPIOC->CRH = 0x44344444; /* PC13 as output */
TIM2->CCMR1 = 0x0000; /* no filter */
TIM2->CCER = 0x1 << 1; /* CC0P = 1 (falling) */
TIM2->SMCR = 0x57; /* TIM2_CH1 as clock source */
TIM2->ARR = 100-1;
TIM2->CR1 = 1;
while(1) {
if((TIM2->SR&1) != 0) {
TIM2->SR = 0;
GPIOC->ODR ^= (1<<13);
}
}
}
Electronics Department, HCMUT 52
ETR (External clock) input Block
www. Micro Digital Ed. com
BIHE university