120 likes | 127 Views
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs. Lecture #16 Agenda Today Input Capture Pulse Accumulation Concept. Input Capture. Input Capture Function
E N D
ECE 3430 – Introduction to Microcomputer SystemsUniversity of Colorado at Colorado Springs Lecture #16 Agenda Today • Input Capture • Pulse Accumulation Concept ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function - Sometimes a microcontroller needs to measure time between internally or externally generated “events”. A feature of some microcontrollers, formally called input capture, allows the timer sub-system to measure these time periods. - The MSP430 (G2553) provides 3 input capture channels per Timer_A module. Each channel has an associated input pin on the microcontroller. Internal or external events are captured through these pins. - When an event occurs on a given input pin, the value of the main timer (TAR) is latched (or sampled) into a dedicated 16-bit register. Each input capture channel has a dedicated 16-bit capture register. - If another event occurs, a new TAR value is latched in over the previous and can be compared to the first event to obtain a relative time between events.Ex) Event 1 Event 2 (TAR = 0x0100) (TAR = 0x0200) If TAR pre-scale factor is 1 and TAR is clocked at 1 MHz--each timer tick is worth 1us of real-time.Time Between Events = (0x0200 - 0x0100) (time per TAR clocks) = (0x100) (1us) = (256) (1us) = 256us ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function Details - Input capture channels each use one of the port pins. Each channel allows the programmer to pick from one of two sources (CCIxA or CCIxB). Each port pin is bi-directional—so we need to make sure P1DIR is set properly. - There are two Timer_A modules in the G2553 part—each with 3 capture channels (Timer_A3). - The mapping from CCIxA and CCIxB to pins is specific to each channel and which Timer_A module is being used (see device-specific datasheet for these mappings). - The input CCIxA or CCIxB can be selected via the CCISx bits in the TACCTLx control register. - The event captured can either be a rising or falling edge of a digital signal (or both). The edge sensitivity can be defined independently for all channels via the CMx bits in the TACCTLx control register: Bit 15Bit 14 0 0 Capture disabled 0 1 Capture on rising edges 1 0 Capture on falling edges 1 1 Capture on any edge ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function Details - When the input capture triggers, the current “TAR” value is latched into dedicated 16-bit registers: Input Capture Channel 0 = TACCR0 Input Capture Channel 1 = TACCR1 - P1SEL (in addition to P1DIR) may additionally need to be set to select the right input. - During input capture function, these registers are treated read-only by software! - When the trigger occurs, a “flag bit” (CCIFG) will be set in the appropriate TACCTLx register This flag only indicates that an event of interest has been captured. It is set even if interrupts are disabled. - These flags can be cleared by writing a ‘0’ to the corresponding flag bit: CCIFG = 0 = flag cleared, waiting for input capture event (RESET state) CCIFG = 1 = flag set, input capture event occurred sometime in the past - When using any timer registers, the Timer_A instance number is specified. Ex) TA0CCR0 (instance 0, channel 0) , TA1CCR0 (instance 1, channel 0). - If the Timer_A instance number is not specified, the assumption is instance 0. Ex) TACCR0 == TA0CCR0 ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function (Optional Interrupt) - Just as with the timer overflow, successful capture of an event can generate an interrupt—however this is not a requirement! - Just like all MSP430 interrupts, each input capture channel has a relative priority. Input capture channel 0 has a higher priority than all the other input capture channels (see interrupt vector table in device-specific datasheet). - The timer overflow and capture channel 1 and 2 interrupts share an interrupt vector, so the source of the interrupt must be determined. The TAIV register can be used to do this expediently. TAIV is set to a distinct value based on the highest-priority, pending timer interrupt. The associated flag is automatically cleared by hardware when this register is read. - Just like many other MSP430 interrupts, there is both a global and local interrupt mask involved: Global mask = GIE flag in the SR (this is a maskable interrupt) -> OK for CPU to acceptLocal mask = CCIE bit in appropriate TACCTLx register -> OK for timer to generate ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function ApplicationsComparing Arrival Times - You can attach multiple wires to the MSP430 and monitor edges as they come in to see which is first. TACCR0 TACCR1 TACCR2Period Measurement - Measuring ‘rising edge’ to ‘rising edge’ of a signal is the definition of PERIOD. Can also do ‘falling edges’. Delta TCNT ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function ApplicationsPulse Width Measurements - ‘rising edge’ to ‘falling edge’ is the pulse width. Flip Flop Behavior - When an edge hits the MSP430, we will be notified. We can grab data off of any other pin when this occurs (in an ISR?) D Q ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function ApplicationsDuty Cycle Measurements - Measure of the “high time” with respect to total period. Typically expressed as a percent. Duty Cycle % = /T T ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function Watch out for timer rollover! - If TAR rolls over more than once between input capture events. Ex) Capture 1 = 0xFFF0 Capture 2 = 0x0005 - If required, we need to add code to track if TAR rolls over. We can use the Timer_A counter overflow for this. - The “timer overflow interrupt” will tell us when TAR rolls-over. We can add an ISR that will increment a counter every time this happens. We can read the counter each time an input capture event occurs to see if there was TAR roll-over. - It is possible to account for one or more timer overflows—but if we account for more than one timer overflow, our arithmetic will exceed 16-bit limits and we’ll have to use multi-precision arithmetic in the MSP430. ECE 3430 – Intro to Microcomputer Systems Fall 2014
Input Capture Input Capture Function Software needs to be fast enough! - If trying to take measurements between multiple captures, software must have enough time to copy the capture register elsewhere (by reading it) before the next capture occurs. - The MSP430 hardware sets the COV (capture overflow) bit in the TACCTLx register if a second capture occurs before the associated TACCRx register is read. The COV flag indicates data loss. - Always set the SCS bit in the TACCTLx register (asynchronous capture is not useful). - The state of the associated input at the time of the capture can be checked via the SCCI bit in the TACCTLx register. The current input can be checked via the CCI bit. - For input capture, the CAP bit should be set to 1. ECE 3430 – Intro to Microcomputer Systems Fall 2014
Pulse Accumulator Pulse Accumulator Applications Event Counting Anything that needs counting. - Assembly line. - Vending machine to monitor inventory. - Serial bit stream to know when 8-bits have arrived.Gated Accumulator Monitoring the time it takes for an event to come and go. - Seeing how fast an assembly line is moving. - Generating interrupts when a pulse exceeds a certain delta. -> Use gated time accumulation mode, pre-program counter, set up pulse accumulator counter overflow interrupt. ECE 3430 – Intro to Microcomputer Systems Fall 2014
Pulse Accumulator Pulse Accumulator- Sometimes it is desirable to have an external circuit count external events. The external events manifest themselves as digital pulses—either 0->1->0 pulses or 1->0->1 pulses. - Some microcontrollers provide a circuit to do just this. It is called a pulse accumulator. - Our MSP430 has no dedicated hardware for this—but we can use Timer_A features to do this. - A pulse accumulator can either count external events (event counting mode) or count the number of clock cycles that occur when the pulse accumulator input is asserted (gated time accumulation mode). - How could we make the MSP430 reliably count events? - How could we make the MSP430 accumulate the total time that a signal is asserted? ECE 3430 – Intro to Microcomputer Systems Fall 2014