Cl Clocks, s, Co Counters, s, and Ti Timers 01204322 Embedded System Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Kasetsart Unive versity Revised 2020-01-07 Materials from RM0394: Reference Manual for STM32L41xx/2xx/3xx/4xx/5xx/6xx
Ou Outline • Clock sources • Basics of counters and timers • Clocks, counters, and timers in ARM Cortex-M chips • Hands-on activity with NUCLEO-L432KC board 2
Cl Clock k Signals • Clock signals are required for a lot of stuff ◦ CPU execution ◦ Internal communications, e.g., peripheral bus ◦ External communications, e.g., USB, USART ◦ Analog-to-digital conversion ◦ Tick generation for timers 3
Co Counters s and Ti Timers • Counters and timers are ones of the most important components of MCUs • A counter keeps track of how many event ticks have occurred http://clipart-library.com/images/rcnRnRocR.png • A timer is a counter that counts deterministic clock cycles • Two terms are often used interchangeably http://clipart-library.com/image_gallery/92121.png 4
Ty Typical Uses of Counters/Timers • Scheduling future events ◦ One-shot events ◦ Periodic events • Input capture ◦ Measuring input pulse length • Output waveform generation Image by Hebi B. from Pixabay ◦ Output compare – flags or toggles output pin when the counter reaches a certain value ◦ Pulse-width modulation (PWM) – generates analog output encoded as pulse length https://www.makerlab-electronics.com 5
Ba Basi sic Co Counter/ r/Ti Timer r Operations clock counter ticks - Counter reset when overflows or reaches auto-reload value - A flag is raised; interrupt is triggered 6
Co Counter r Mo Modes • Up-counting counter ticks • Down-counting counter ticks • Center aligned (up/down) counter ticks 7
Pr Prescaler • Divides clock frequency; increases timer period clock (prescaler = 1) counter prescaler = 2 counter prescaler = 4 counter 8
Input Input C Captur ure • Measures input pulse lengths • Applications: decoding remote control signals, distance measurement clock counter ticks input pulses current counter’s values recorded as captured input 9
Ou Output Com ompare • Generates an event when counter matches a certain value clock counter compare value ticks event event event • Possible MCU’s behavior when receiving a compare event: ◦ Sets a flag ◦ Generates an interrupt ◦ Sets, resets, or toggles output pin 10
Pu Pulse-Wi Width th Modulati tion (PWM WM) • PWM utilizes duty cycle control to simulate analog output • Applications: motor speed control, servo position control, LED brightness control 25% 50% 75% duty cycle duty cycle duty cycle Analog data PWM output https://en.wikipedia.org/wiki/Pulse-width_modulation 11
PW PWM Wavefor orm Generation on • Typical MCU behavior ◦ Auto-reload event sets output pin high ◦ Output-compare event sets output pin low clock counter compare value ticks PWM Output • Output compare value determines PWM duty 12
NU NUCLEO-L4 L432KC C Develo lopme ment t Bo Boar ard • STM32L432KC target chip ◦ ARM 32-bit Cortex-M4 up to 80MHz with single-precision FPU ◦ 256KB Flash, 64KB SRAM ◦ 1x 12-bit ADC (10 channels), 2x 12-bit DAC • 32.768 kHz external crystal oscillator • On-board user LED and reset button • On-board ST-LINK/V2-1 debugger ◦ Serial Wire Debug (SWD) ◦ Virtual COM port (VCP) ◦ Mass storage interface • Pin-compatible with Arduino Nano Source: ST’s UM1956: STM32 Nucleo-32 Boards 13
Cl Clock k Options s for r STM3 TM32L4xx xx • Clock sources for the system clock (SYSCLK) ◦ High-speed internal (HSI 16): 16 MHz RC oscillator ◦ Multispeed internal (MSI): 100 kHz to 48 MHz RC oscillator ◦ High-speed external (HSE): from 4 to 48 MHz crystal oscillator ◦ Phase-locked loop (PLL) • Additional clock sources (for USB, real-time clock, etc.) ◦ Low-speed internal (LSI) 32 kHz RC oscillator ◦ Low-speed external (LSE): 32.768 kHz crystal oscillator ◦ High-speed internal (HSI 48): 48 MHz RC oscillator • MSI can be automatically trimmed by LSE using PLL ◦ Achieves accuracy better than +/- 0.25% 14
Cl Clock k Settings s for r NUCL CLEO-L4 L432KC • External 32.768 kHz crystal oscillator ◦ Used for real-time clock (RTC) and low-power operations ◦ Also used for trimming MSI clock • System clock: 80 MHz ◦ MSI at 4 MHz, multiplied by PLL ◦ Auto-trimmed by LSE 15
ST STM32L4xx’s Timers • Basic timers: TIM6/TIM7 • General-purpose timers: TIM2/TIM3 and TIM15/TIM16 • Advanced timers: TIM1 • Low-power timers: LPTIM 16
STM32L4xx’s General-Pu ST Purpose Timers 17
Impo Important T Timer mer-Re Related Re Registers • TIM x ->PSC (16 bits) ◦ Prescaler for TIMx ◦ PSC = 0 à no prescaler (prescaler = 1) ◦ PSC = n à prescaler = n +1 • TIM x ->ARR (32 bits) ◦ Auto-reload register for TIM x ◦ Counter counts up to and including ARR ◦ Therefore, reload occurs every ARR+1 clock ticks • TIM x ->CCR y (32 bits) ◦ Capture/compare register for TIM x , channel y 18
Ca Calculating Ti Timer r Peri riod/Frequency • Let ◦ PSC = Prescaler ( TIM x ->PSC ) ◦ ARR = Auto-Reload Register ( TIM x ->ARR ) • Then 𝑔𝑠𝑓𝑟 +,-+. 𝑔𝑠𝑓𝑟 %&'() = 𝑄𝑇𝐷 + 1 ×(𝐵𝑆𝑆 + 1) or 𝑞𝑓𝑠𝑗𝑝𝑒 %&'() = 𝑞𝑓𝑠𝑗𝑝𝑒 +,-+. × 𝑄𝑇𝐷 + 1 ×(𝐵𝑆𝑆 + 1) 19
Ex Example: ample: Ti Timer er Per eriod • What is the timer’s period in milliseconds with the following configuration? ◦ Up-counting configuration ◦ Input clock: 80 MHz ◦ Prescaler (PSC): 999 ◦ Auto-reload register (ARR): 4999 20
PW PWM Mod odes 1 vs 2 clock counter CCR ticks PWM mode 1 (output cleared after match) PWM mode 2 (output set after match) 21
Ca Calculating PWM M Duty y Cy Cycle • Assume up-counting configuration and PWM mode 1 (output is clear when compare match) • Let ◦ ARR = Auto-Reload Register ( TIM x ->ARR ) ◦ CCR = Capture/Compare Register ( TIM x ->CCR y ) 𝐷𝐷𝑆 (𝐵𝑆𝑆 + 1) ×100% , 𝐷𝐷𝑆 ≤ 𝐵𝑆𝑆 𝑒𝑣𝑢𝑧 @A' = B 100% , 𝐷𝐷𝑆 > 𝐵𝑆𝑆 22
Ex Example: ample: PW PWM Duty Cycle • What is the PWM’s frequency and duty cycle with the following configuration? ◦ Up-counting configuration ◦ Input clock: 80 MHz ◦ Prescaler (PSC): 79 ◦ Auto-reload register (ARR): 4999 ◦ Capture/compare register (CRR): 2000 ◦ PWM mode 1 23
Ha Hands nds-on on: : LD LD3 B Brig rightness C Contro rol • Control brightness of on-board LED (LD3) using PWM • LD3 is attached to PB3, which is also output of TIM2, channel 2 • PWM frequency: 5 kHz • Initial brightness: 10% 24
Co Configuri ring PB3 B3 • Attach PB3 to TIM2_CH2 25
Co Configuri ring TI TIM2 M2, Ch Channel 2 26
Co Configuri ring PWM 𝑔𝑠𝑓𝑟 +,-+. • Input clock freq = 80 MHz 𝑔𝑠𝑓𝑟 %&'() = 𝑄𝑇𝐷 + 1 ×(𝐵𝑆𝑆 + 1) • Required PWM frequency: 5 kHz • Possible PSC/ARR configurations ◦ PSC = 7999, ARR = 1 ◦ PSC = 799, ARR = 19 ◦ PSC = 79, ARR = 199 ◦ : • Initial duty cycle: 10% ◦ CCR = 20 27
St Star art PW PWM • Start PWM before entering the infinite loop • Leave the infinite loop empty for now /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ 28
Ad Adju justing Brightness with Debugger • Try adjusting the value of CCR2 register in the debugger (SFRs tab) 29
Co Conclusi sion • MCUs require clock signals for CPU execution and operations of many components • Counters/timers are crucial MCU components for time- related tasks, such as ◦ Scheduling one-shot or periodic events ◦ Measuring input pulse lengths (input capture) ◦ Generating output PWM waveform for specific frequency and duty cycle 30
Recommend
More recommend