ELE271 Mon. April 7, 2014 - Review LPM - Morse Code Lab - .ref and .def - Multiplication, shift 1 Low Power Modes Principles of Low-Power Apps • • • • Maximize the time in low-power modes. Sleep as long as possible and use interrupts to wakeup. Use the slowest clock while still meeting processing needs. Switch on peripherals only when needed (ie, switch off peripherals when not needed). • Use low-power integrated computer peripherals. – Timers: Timer_A and Timer_B for PWM – A/D convertors, flash, LCD’s • Use faster software algorithms / programming techniques – – – – Calculated branches instead of flag polling. Fast table look-ups instead of iterative calculations. Use in-line code instead of frequent subroutine / function calls. More single-cycle CPU register usage. 2 How are these concepts applied to microcontroller programming? - Low Power design isn’t just for low power applications. - Low Power/Interrupt-driven design helps with modular design. The following techniques are applied to low power Design: - Use of Interrupts. - Controlling the clock. 3 Low Power Modes MSP430 Clock Modes Average A method to reduce power consumption is to turn off some (or all) of the system clocks (and turning off the power to some circuits). A device is said to be sleeping when in low-power mode; waking refers to returning to active mode. 4 Processor Clocks Processor Clock Speeds • Often, the most important factor for reducing power consumption is slowing the clock down. – Faster clock = Higher performance, more power required – Slower clock = Lower performance, less power required Power Consumption Power (Watts) 300 250 200 Active Power 150 Leakage 100 50 0 0.25 0.18 0.13 0.09 0.065 Clock Speeds () 5 6 MSP430 5xx Power Modes 7 Low Power Modes MSP430 Clock Settings (r2) SCG1 SCG0 OSC OFF CPU OFF Active Mode 0 0 0 0 ~ 250 µA LPM0 0 0 0 1 ~ 35 µA LPM3 1 1 0 1 ~ 0.8 µA LPM4 1 1 1 1 ~ 0.1 µA Reserved SMCLK and ACLK Active Only ACLK Active ; V No Clocks! GIE N Z C Sleep Modes enable interrupts / enter low-power mode 0 bis.w #LPM0|GIE,SR ; LPM0 w/interrupts Can be combined 8 Two problems with this: - What if you want to stay in active mode after ISR? - What if you want to keep ISR small (so you don’t turn off other interrupts)? 9 Review of stack interaction with interrupts. 10 How to come out of LPM in active mode. A mildly tricky line of assembly language is required to clear the bits set for the low power mode PORT1_ISR bic.w #LPM4,0(SP) reti See Jimenez Ex. 7.4 11 12 Morse Code Lab Objective: to program the device using assembly code to toggle the LED to send a Morse Code message “SOS” each time S1 is pressed. This lab will illustrate the following concepts: - Interrupt-driven programming. - Multiple source files (.ref,.def directives) - Subroutines - Timers - Low Power Modes - Advanced data structures (pointers) - Exit an ISR in Active Mode (AM). 13 Assume one word. Assume only letters. 14 Morse code has a variable-length code for each character. Question: how to represent this in memory. Solution: use pointers or “relocatable expressions” in assembly: ; letters--->A_ptr---->DOT,DASH,END ;A ; ;B B_ptr---->DASH,DOT,DOT,DOT,END ,etc… 15 16 Relocatable expressions 17 Morse Code Data Structure A_ptr DOT .equ 1 DASH .equ 3 0x5c00 0x5c10 0x5c10 01h dot 03h dash end letters: 00h .word A_ptr 0x5c13 03h .word B_ptr 01h ,etc… 01h 01h 00h A_ptr: .byte DOT,DASH,END ;A B_ptr: .byte DASH,DOT,DOT,DOT,END ;B , etc… ; letters--->A_ptr---->DOT,DASH,END ;A ; ;B B_ptr---->DASH,DOT,DOT,DOT,END 18 Morse Code Algorithm Sleep; Get address of .cstring message. Get first character. While character != 0 , do normalize ASCII to 41h (ie, so “A”=1) Get address of letters Get first code ; code = dot or dash While code != 0, do Turn LED on Sleep(1|3) ; 1unit=dot,3 units=dash Turn LED off Sleep(1) ; 1 unit Get next code End While Sleep(3) Read next character. End while 19 RTN to Assembly: The While Statement Part 2 Usually a “while” statement implies a “cmp” “while” loops cause jmp while ( n > 0 ) { Loop: s1… backwards cmp #0,r6 } dec r6 jz End s1… jmp Loop End: Until condiion is met, then Jmp forward. sleep If(pushbutton) do R4<-#message ; get address of message R5<-(r4) ; get character pointed to by #message while (r5 != 0), do ; keep getting another char unless zero r5<-r5-41h ; normalize ASCII character to 40h (ie, A=0) r5<-r5*2 ; multiply by 2 for word boundary r7<-letters(r5) ; get first code for letter (dot or dash) while(r7 != 0), do ; get next code unless end (0) beep(r7) ; beep amount of code (1 or 2) short_delay ; short delay between beeps r5<-r5+1 ; increment address r7<-letters(r5) ; get next code endwhile r4<-r4+2 ; increment character address long_delay ; long delay between characters Endwhile End if 21 Write an interrupt-driven program to toggle the LED based on the Morse Code. • Learn how to use multiple source files with the .ref and .def directives. • Learn how to use subroutines. • Learn how use Timers. • Learn how use Low Power Modes. • Learn how use pointer data types. • Learn how to come out of an ISR in active mode. 22