Lecture 21 - Time Keeping I 10:10 Introduction This week, we will talk about the timer module, Timer_A. It's a new module, different from the Timer32 module we discussed earlier. Time Keeping is very, very important for micro-controller applications. In fact, time keeping in general is a critical function in many practical applications such as travel and position determination. Let's take a look at historical examples. (1) What enabled transatlantic and ocean travel? - In order to travel, you have to be able to tell _where_ you are. That allows you to estimate how many days you have left, whether you have sufficient supplies to reach the next port, and so on. - The essential tool is not the compass. The compass only gives you an idea of direction. It does not tell where you are. - We need to be a bit more accurate when we're saying 'where are we?'. What determines our position on earth? Longitude and Latitude. * Latitude is the angular distance of a location north or south to the equator, with the poles at 90 degrees. * Longitude is the angular distance of a location east or west to the standard meridian of greenwich. - OK, so what do we need to measure latitude and longitude. Think like a 16th century sailor ... Latitude can be found by looking at the sky, and by looking for the highest position that a star will reach. That gives you your position with respect to the equator. Longitude, on the other hand, is not so easy. You could measure sunrise and sunset, but you have to be able to measure time for it. Earliest clocks were wildly inaccurate, and even an error of a few minutes per day results in large navigational errors. (2) What enables GPS? - GPS uses a system of satellites that are precisely synchronized to the same time base - Each satellite emits a unique signal (a sequence of bits) - A GPS receiver captures this signal and determines exactly the position in the sequence. A GPS receiver then determines precisely how long the signal took to travel from the satellite to the GPS receiver. That translates to a distance between the satellite and the GPS receiver. - With three satellites, we can determine our unique position in a plane. - With four satellites, we can determine our unique position in three dimensions. 10:20 Properties of a periodic signal +-------+ +-------+ | | | | -----+ +----------------+ +------------ |<----->| | | Ton | |<---------------------->| T Period = T Frequency = (T)^-1 Duty Cycle = (Ton/T) x 100% Pulse Width Modulation PWM A PWM signal has two parameters (T, d) A pulse-width modulated signal is a periodic digital signal with period T and duty cycle d In PWM, the duty cycle is varied while the period is kept constant. This allows to control the relative on-time. PWM has important applications in motor control, and in power-conversion applications. 10:22 Measuring time by digital means It is common for micro-controller systems to measure time by counting clock cycles. We have already examined Timer applications in-depth while studying Timer32 Timer32 runs at a clock frequency f In periodic mode, it counts N, N-1, .., 1, 0, N, N-1, ..., 1, 0, N, .. so that the time span of one period is equal to: tspan = (N+1) / f METHOD I: We can now measure time by counting clock cycles. a/ We capture the number of cycles at a given time t1 -> N1 b/ We capture the number of cycles at a later time t2 -> N2 c/ We compute the modulo-difference between N2 and N1. Modulo-difference means: we cannot detect a difference greater than N cycles. METHOD II: We have also used the timer-overflow itself as a timer period marker. In this case, we count the number of overflows in software, so the precision limit is defined by the resolution of the software counting. The timer-overflow is counted: - By repeatedly checking the value of the timer getValue() and detecting when it wraps around - By using interrupts 10:28 The Timer_A peripheral The MSP432P401R has four 16-bit timer modules called Timer_A. Unlike Timer32, they are not integrated in the Cortex processor, but they are peripherals in their own right. Each of these timer modules as five 'capture/compare/PWM' channels, which will be defined as as below. The heart of the Timer_A peripheral is a 16-bit timer called TAxR. x can be 0, 1, 2, or 3 depending on what timer module is being used. The clock of the timer can be taken from several different sources, and the timer clock can be prescaled (by p=1/2/4/8) and divided by q=1..8. The resulting divider is set as p.q. The timer has a period register, TAxCCR0, and can be run in three different modes: - As an upcounter, going from 0 to TAxCCR0 - As a continuous counter, going from 0 to 0xFFFF - As an updown counter, going from 0 to TAxCCR0 and back down again CLK ---> Divider ---> TAxR (count mode) || || TAxCCR0 10:33 Input Capture/ Output Compare Hardware For each 16-bit counter TAx, there are 5 Input Capture/Output Compare hardware. The USER GUIDE of MSP432 on page 784 shows the details. Note that the schematic shows 7 Input Capture/Output Compare hardware; our chip only has five. Notation: TAx.y means: Timer_A x, input capture/output compare hardware y For example TA2.0 means: Timer_A 2, input capture/output compare hardware 0 Each IC/OC hardware has an input and an output. These two are both wired to a chip pin, which can be used as an input or an output. For example, if you look on the chip package for port P2.6, you can find that pin is also labeled as PM_TA0.3. The important part is the TA0.3, that's timer TA 0, IC/OC hardware 3. Now what does the IC/OC hardware do? - Each IC/OC hardware has a register called TAxCCRy. That is the capture/compare register. It can either be compared to the timer register, or it will capture the value of the timer register. - When used as OC, the IC/OC hardware allows to generate a signal transition at a precisely programmed moment. The comparator register is compared with the timer register (TAxR) until both are equal. At that moment, the output signal can be generated (this can be set to 1 or to 0). - When used as IC, the IC/OC hardware allows to capture the value of the timer register TAxR into TAxCCRy. This means that it can record precisely when an input event happens. In both cases, the IC/OC hardware is a fully dedicated hardware module, hence it gives a very strict control on timing. When you work with timing in software, there is always some uncertainty. - For example, the cyclic executive takes some time to read all inputs events. Therefore, there is some uncertainty on exactly when on input event is read. - Even an interrupt service routine is not instantaneous. After an interrupt, there is some latency before the software can respond. Therefore, there is again some uncertainty on exactly when an input event is read. In contrast, IC/OC hardware is always exact (to within a Timer TAxR clock cycle). We will now discuss several applications of Timer_A using IC/OC hardware. 10:40 Continuous Mode - precise frequency generation Continuous mode in combination with output compare hardware allows to generate precise frequencies. In continuous mode, the timer increments continuously from 0 to 0xFFFF and then wraps around. We can then select an output compare register, OC, to detect a given value in the timer register. The output compare register also needs to set an output compare mode. There are 7 output compare modes Activity on CCR Activity on 0xFFFF --------------- ------------------ 1. Set Set / 2. Toggle/Reset toggle (to 1) clear 3. Set/Reset set clear 4. Toggle toggle toggle 5. Reset reset / 6. Toggle/Set toggle (to 0) set 7. Reset/Set reset set Hence, this mode allows you to generate a pulse waveform without any interaction from software. Just set to proper period and output compare mode, and everything else is handled by the hardware. 10:45 Timer Pins on BOOSTXL The following devices can be controlled through the Timer IC/OC hardware J4.39 Red Led TA0.3 P2.6 J4.38 Green Led TA0.1 P2.4 J4.37 Blue Led TA2.1 P5.6 J4.35 not used TA2.3 P6.6 J4.36 not used TA2.4 P6.7 J4.40 Buzzer TA0.4 P2.7 In our examples, we make use of the Red Led 10:47 Example Code: msp432-continuous First, let's configure the configure the timer A in continuous mode. We're going to run timer A at 3MHz. This means that it will wrap around in 3M / 64K ~ 21 ms. Timer_A_ContinuousModeConfig contConfig = { TIMER_A_CLOCKSOURCE_SMCLK, TIMER_A_CLOCKSOURCE_DIVIDER_1, // 3 MHz TIMER_A_TAIE_INTERRUPT_DISABLE, TIMER_A_SKIP_CLEAR }; Timer_A_configureContinuousMode (TIMER_A0_BASE, &contConfig); Next, we have to set up the compare mode. We will do a toggle_set output compare when the timer reaches 0x1000. This will turn on the LED 1/16th of the time. Timer_A_CompareModeConfig cmpConfig = { TIMER_A_CAPTURECOMPARE_REGISTER_3, TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, TIMER_A_OUTPUTMODE_TOGGLE_SET, 0x1000 }; Timer_A_initCompare (TIMER_A0_BASE, &cmpConfig); Finally, to enable the timer hardware, we need to - Make sure the IC/OC hardware is connected to the IC pin - The timer is started GPIO_setAsPeripheralModuleFunctionOutputPin( GPIO_PORT_P2, GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION); Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_CONTINUOUS_MODE); Interestingly, no software needs to be running once everything is configured - it goes by itself. 10:50 Pulse Width Modulation The previous function - continuous mode in combination with output compare can be configured directly as 'Pulse Width Modulation' mode. PWM is extensively used for motor control, and as a 1-bit DAC. Indeed, by low-pass filtering a PWM signal using an RC (resistor and cap), we can generate an analog voltage that is proportional to the duty cycle. Example: Driverlib has function support for PWM Timer_A_PWMConfig pwmConfig = { TIMER_A_CLOCKSOURCE_SMCLK, TIMER_A_CLOCKSOURCE_DIVIDER_1, // 3 MHz (int) (3000000 / 1000), // divider for 1KHz period TIMER_A_CAPTURECOMPARE_REGISTER_3, TIMER_A_OUTPUTMODE_SET_RESET, (int) (3000000 / 3000) // 33% duty cucle }; GPIO_setAsPeripheralModuleFunctionOutputPin( GPIO_PORT_P2, GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION); Timer_A_generatePWM(TIMER_A0_BASE, &pwmConfig); As with the previous example, no software support is needed after initialization. The hardware does this automatically. 10:55 Conclusions Time keeping: - Timer32 - Software Timer - TimerA peripheral Input Capture/Output Compare - Examples of Output Compare - Next lecture: Input Capture