Timer Peripherals.

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
More fun with Timer/Counters
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
Microcontroller Programming II MP6-1
EET 2261 Unit 10 Enhanced Capture Timer  Read Almy, Chapter 20.  Homework #10 and Lab #10 due next week.  Quiz next week.
Oscilloscope Watch Teardown. Agenda History and General overview Hardware design: – Block diagram and general overview – Choice of the microcontroller.
Introduction of Holtek HT-46 series MCU
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
1.  8051 Timers “count up,” incrementing the Timer’s respective “count register” each time there is a triggering clock pulse. 2  When the “count register”
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
T IMERS - 2. O UTPUT U NIT Each capture/compare block contains an output unit. The output unit is used to generate output signals such as PWM signals.
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
8254 Programmable Interval Timer
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Lecture 29: LM3S9B96 Microcontroller – Pulse Width Modulator (PWM)
ARM Timers.
System Clocks.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
A Few Words From Dilbert
MS_uC / fue1 / V TIMx - Timer Programming Microcontroller TIMx - Timer Autumn term 2012.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Timers and Event Counters
Chapter 4 TIMER OPERATION
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
AT91 Embedded Peripherals
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
1 ARM University Program Copyright © ARM Ltd 2013 Timer Peripherals.
Microcontroller based system design Asst. Prof. Dr. Alper ŞİŞMAN.
ECE 447 Fall 2009 Lecture 10: TI MSP430 Timers and Capture Modes.
SIGMA-DELTA ADC SD16_A Sigma-Delta ADC Shruthi Sujendra.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose This course provides an overview of the standard peripheral.
Embedded Systems Design: A Unified Hardware/Software Introduction 1 Chapter 4 Standard Single Purpose Processors: Peripherals.
1 ARM University Program Copyright © ARM Ltd 2013 Using Direct Memory Access to Improve Performance.
The Cortex-M3 Embedded Systems: LM3S9B96 Microcontroller – Pulse Width Modulator (PWM) Refer to Chapter 21 in the reference book “Stellaris® LM3S9B96 Microcontroller.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
1 Timers and Event Counters Lecture In These Notes... We learn the basics of the Timer/Counter peripheral –Called timers by Renesas We examine how.
Counters.
TIMERS AND INTERRUPTS AVI SINGH KEVIN JOSE PIYUSH AWASTHI.
Lecture 4 General-Purpose Input/Output NCHUEE 720A Lab Prof. Jichiang Tsai.
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
Networked Embedded Systems Pengyu Zhang & Sachin Katti EE107 Spring 2016 Lecture 4 Timers and Interrupts.
1. PIC ADC  PIC18F877 has 8 analog input channels i.e. port A pins(RA0 to RA5) and port E pins(RE1 and RE2). These pins are used as Analog input pins.
Why are Timer Functions Important?
Application Case Study Security Camera Controller
Timers and Event Counters
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
PWM and DC Motor Control
Timer and Interrupts.
AVR Addressing Modes Subject: Microcontoller & Interfacing
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
8-bit Timer/Counter0 with PWM
ECE 3430 – Intro to Microcomputer Systems
Programmable Interval timer 8253 / 8254
STM Arm Timer Programming
Programmable Interval timer 8253 / 8254
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
Presentation transcript:

Timer Peripherals

STM32F4 Clock tree Various Clock sources Highly configurable Can be controlled independently Possible to prescale Can output clock from some pins To achieve the balance between performance and power consumption

Timer/Counter Peripheral Introduction Current Count Reload Value Presettable Binary Counter Events Reload or ÷2 or RS PWM Clock Interrupt Common peripheral for microcontrollers Based on presettable binary counter, enhanced with configurability Count value can be read and written by MCU Count direction can often be set to up or down Counter’s clock source can be selected Counter mode: count pulses which indicate events (e.g. odometer pulses) Timer mode: clock source is periodic, so counter value is proportional to elapsed time (e.g. stopwatch) Counter’s overflow/underflow action can be selected Generate interrupt Reload counter with special value and continue counting Toggle hardware output signal Stop!

STM32F4 Timer Peripherals Advanced Control Timer TIM1 and TIM8 Input capture, output compare, PWM, one pulse mode 16-bit auto-reload register Additional control for driving motor or other devices General Purpose Timer TIM2 to TIM5 16-bit or 32-bit auto-reload register TIM9 to TIM14 Only 16-bit auto-reload register Basic Timer (Simple timer) TIM6 and TIM7 Can be generic counter and internally connected to DAC 16-bit auto reload register Also a 24 bit system timer(SysTick)

General Purpose Timer Block Diagram

General Purpose Timer Current Count Reload Value TIMx_CNT TIMx_ARR Reload Clock TIMx_PSC ISR Resume Interrupt Timer can count down or up or up/down (up by reset) A prescaler can divide the counter clock 4 independent channels for the timer Input capture Output compare PWM generation One-pulse mode output Overflow or underflow will cause an update event (UEV), thus the interrupt and possibly the reload

General Purpose Timer Control Registers Highly Configurable

Periodic Interrupt

Timer as A periodic Interrupt Source Current Count Reload Value TIMx_CNT TIMx_ARR Reload Clock TIMx_PSC ISR Resume Interrupt STM32F4 families enjoy sophisticated and powerful timer One of the basic function of the timer is to cause independent and periodic interrupts Best for regularly repeating some certain small tasks

General Purpose Timer Control Registers Highly Configurable Only few need to be configure for periodic interrupt setup

General Purpose Timer Clock selection There are 4 possible sources of clock Internal clock(CK_INT) External clock mode1: external input pin(TIx) External clock mode2: external trigger input (ETR) (some timers only) Internal trigger inputs (ITRx) Access TIMx_SMCR (Bit 2:0 SMS) slave mode control register to select the clock source and mode For example, for the periodic interrupt, with SMS being 000, the counter will clocked by the internal clock (APB1) By default, the prescaler for APB1 is 4 (defined by the system_stm32f4xx.c), which means the CK_INT is 42Mhz (SYSCLK is 168Mhz)

Specify PSC and ARR Current Count Reload Value TIMx_CNT TIMx_ARR Reload Clock TIMx_PSC ISR Resume Interrupt TIMx_PSC prescale register stores the value which will be used to divide the clock input. In count up mode, overflow will occur if TIMx_CNT counter value reach the TIMx_ARR auto-reload value. And then the TIMx_CNT will be updated with 0. Both TIMx_ARR and TIMx_PSC are 16-bit register! So the total periodic time can be calculated as Tout=((ARR+1)×(PSC+1))÷Fclk When Fclk is 42MHz, setting ARR to 5999 and PSC to 13999 makes one second periodic time.

More on PSC and ARR Current Count Reload Value TIMx_CNT TIMx_ARR Reload Clock TIMx_PSC ISR Resume Interrupt Accessing the ARR only write or read from a preload register of ARR, there is another shadow register which is the register that actually performs the reloading. The content of the preload register will be transfer to the shadow register permanently if the APRE bit in TIMx_CR1 is clear or at each UEV if APRE bit is set. PSC, on the other hand, is always buffered. Which means though it can be changed on the fly, the new ratio will only be taken into account at the next UEV.

Initialize the TIM2 with CMSIS Enable clock to Timer2 RCC->APB1ENR|=RCC_APB1ENR_TIM2EN; Set the auto-reload TIM2->ARR=arr; Set the prescaler TIM2->PSC=psc; Enable the update interrupt TIM2->DIER|=TIM_DIER_UIE; Enable counting TIM2->CR1|=TIM_CR1_CEN

Interrupt Handler Enable the interrupt CMSIS ISR name: TIM2_IRQHandler __enable_irq() ; CMSIS ISR name: TIM2_IRQHandler NVIC_EnableIRQ(TIM2_IRQn); ISR activities Clear pending IRQ NVIC_ClearPendingIRQ(TIM2_IRQn); Do the ISR’s work Clear pending flag for timer TIM2->SR&=~TIM_SR_UIF;

SysTick as Periodic Interrupt Source Alternatively, we can use the SysTick provided by CortexM4 core to generate exactly the same periodic interrupt. The configuration of Systick is however much simpler. Four registers: Since this is part of the core, it is fully supported by CMSIS SysTick_Config(uint32_t ticks) Initialize the Systick SysTick_Config(SystemCoreClock / 1000) makes 1ms 168M/1000*(1/f)= 1 ms

Example: Stopwatch Measure time with 100 us resolution Display elapsed time, updating screen every 10 ms UUse Systick Counter increment every 100 us LCD Update every 10 ms Update LCD every nth periodic interrupt n = 10 ms/100us = 100 Don’t update LCD in ISR! Too slow. Instead set flag LCD_Update in ISR, poll it in main loop Usually the ISR is only for update the timer or for delaying (precise timing!)

Capture/compare mode

Capture/Compare Channels Different channels but same blocks Capture mode can be used to measure the pulse width or frequency Input stage includes digital filter, multiplexing and prescaler Output stage includes comparator and output control A capture register (with shadow register) Input Stage Example Input signal->filter->edge detector->slave mode controller or capture command

Capture/Compare Channels Main Circuit The block is made of one preload register and a shadow register. In capture mode, captures are done in shadow register than copied into preload register In compare mode, the content of the preload register is copied into the shadow register which is compared to the counter

Capture/Compare Channels Output stage Generates an intermediate waveform which is then used for reference.

Wind Speed Indicator (Anemometer) Rotational speed (and pulse frequency) is proportional to wind velocity Two measurement options: Frequency (best for high speeds) Width (best for low speeds) Can solve for wind velocity v How can we use the Timer for this? Use Input Capture Mode to measure period of input signal

Input Capture Mode for Anemometer Operation: Repeat First capture - on rising edge Reconfigure channel for input capture on falling edge Clear counter, start new counting Second Capture - on falling edge Read capture value, save for later use in wind speed calculation Reconfigure channel for input capture on rising edge Solve the wind speed Vwind = K÷(Cfalling – Crising)×Freq

Registers for Anemometer TIMx_ARR (Refer to the periodic interrupt) TIMx_PSC (Refer to the periodic interrupt ) TIMx_CCMR1 Capture/compare mode register 1 (Channel 1 and 2) TIMx_CCMR2 (for channel 3 and 4) TIMx_CCER Capture/compare enable register TIMx_DIER DMA/interrupt enable register TIMx_CR1 Control register TIMx_CCRx Capture compare register TIMx_SR Status register

Capture/Compare Mode Register 1 [0:7] bits are for channel 1, [8:15] bits are for channel 2 Set CC1S (Capture/compare 1 selection) to 01, configure channel 1 as input and map IC1 to TI1 (Only writable when channel is OFF) IC1PSC (Input capture 1 prescaler) IC1F (Input capture 1 filter) decide sampling frequency and N events needed to validate a transition on the output For example, if set to 0001, and set to capture rising edge, when rising edge detected, sample the channel twice with the FCK_INT ,if they are both high then the capture is validated.

Capture/Compare Enable Register 3 Bits for each channel and 4 bits in total are reserved CC1NP/CC1P: 00 to be sensitive to rising edge, 01 to be sensitive to falling edge, different meaning in other modes CC1E, set to 1 to enable the capture Also needs to enable the interrupt on capture TIMx_DIER: set CC1IE Finally, enable the counting Also remember to clear the pending bit(write 0 to TIM_SR_CC1IF) in the ISR

Configure the GPIO - AF Refer to the user manual to make sure which pin is able to connect to the TIM Then configure the GPIO as AF mode, be careful with the pull up or down setting since it should match the setting of edge detection Configure the GPIO AF register

Output Compare Mode Control an output waveform or indicating when a period of time has elapsed When a Match occur (CCRx=CNT) Generate specific output on corresponding pin Set the CCxIF(Interrupt status) bit in the SR Generate Interrupt if configured Generate DMA request if configured Configure steps Select the counter clock Write the desired data in ARR and CCR registers Enable Interrupt or DMA request if needed Select the output mode Enable the counter

PWm Mode

Pulse-Width Modulation Uses of PWM Digital power amplifiers are more efficient and less expensive than analog power amplifiers Applications: motor speed control, light dimmer, switch-mode power conversion Load (motor, light, etc.) responds slowly, averages PWM signal Digital communication is less sensitive to noise than analog methods PWM provides a digital encoding of an analog value Much less vulnerable to noise PWM signal characteristics Modulation frequency – how many pulses occur per second (fixed) Period – 1/(modulation frequency) On-time – amount of time that each pulse is on (asserted) Duty-cycle – on-time/period Adjust on-time (hence duty cycle) to represent the analog value

Pulse-Width Modulation Timer also provides the PWM mode, generate a signal with frequency determined by the value of ARR and a duty cycle determined by the CCR. CCMRx_OCxM bits, 110 (PWM mode 1) 111(PWM Mode 2) (differs in polarity) Enable the corresponding preload register (OCxPE in TIMx_CCMRx) Enable the auto reload by setting ARPE bit in TIMxCR1 Enable the output Ocx in TIMx_CCER CCxE bit PWM done by comparing TIMx_CCR and TIMx_CNT, i.e., if TIMx_CCRx≤TIMx_CNT or TIMx_CNT≤TIMx_CCRx then output high or low. Also can be configured as input mode, so that it can measure external PWM.

Edge-Aligned CMS bits in TIMx_CR1 are 00 As long as TIMx_CNT<TIMx_CCRx then the OCXREF is high Select down-counting or up-counting using DIR bit in TIMx_CR1

Center-Aligned CMS bits in TIMx_CR1 are not 00: many possible wave forms

PWM to Drive Servo Motor Servo PWM signal 20 ms period 1 to 2 ms pulse width