180 likes | 294 Views
ECE 353 Introduction to Microprocessor Systems. Discussion 10. Topics. Switch Debounce Software Timers ConcepTest Q&A. Switch Debounce - problem.
E N D
ECE 353Introduction to Microprocessor Systems Discussion 10
Topics • Switch Debounce • Software Timers • ConcepTest • Q&A
Switch Debounce - problem • Your success at writing the hardware timing module for the new fish finder has landed you the task of designing another ISR module for reading a switch. This switch is used to allow the user to store a brief log of the fish finder’s readings for later viewing. The log starts when the switch is pressed and continues until it is released. Your module must monitor the switch state and initiate/terminate the data logging process accordingly. • Assume that you are writing a timer ISR (timer 2) switchISR that is invoked at a 1 kHz rate. In the ISR, you are to read the state of the switch connected to P1.0, and then perform de-bouncing of that switch. The switch is rated to bounce for at most 4.5 ms. • If you determine a switch press event has occurred, simply call a subroutine savelog, with a 1 in R0 if it is a switch press event, and a 0 in R0 if it is a switch release event. (You do not have to write savelog). Your software is only to generate one event for each time the switch is pressed (i.e. pressing and holding it should only result in 1 event). • You do not have to write the main program nor do any of the timer interrupt configuration. Assume that this has been done for you, and just write the ISR.
Switch De-bounce - Analysis • We have a 1 kHz timer, so the ISR will run every 1/1000 = 1 ms. • The switch is rated to bounce for at most 4.5 ms, so we will need six consecutive readings all at the same value before we can declare a switch event. • If we store the readings in a register by shifting left, we need to look for the following bit patterns in the least significant digits: • 1000000 = switch press • 0111111 = switch release • You do not have to write the main program nor do any of the timer interrupt configuration. Assume that this has been done for you, and just write the ISR.
Switch De-bounce – Program flow • Read switch (P1.0) and mask • Put value in switch history • Test switch history for valid events: • 1000000 = switch press • 0111111 = switch release • If valid event occurred, call savelog with 0 or 1 in R0
Switch De-bounce – Program ; Filename: switchISR.s ; Author: ECE 353 Staff ; Description: Implementation of fish finder switch function ARM ;use ARM instruction set EXPORT switchISR INCLUDE aduc7026.inc EXTERN savelog EXTERN SwHistory AREA FLASH, CODE, READONLY ; Name: switchISR ; Description: Implements fish finder switch function ; using register parameter passing ; Assumes: None ; Returns: R0 = 0 , switch release ; R0 = 1 , switch press ; Modifies: None ; switchISR PUSH {R1-R4} ;context save POP {R1-R4} ;context restore SUBS PC, LR, #4 ;interrupt return END
Switch De-bounce – Program switchISR PUSH {R1-R4} ;context save LDR R3, SwHistory LDRH R2, [R3] ;get switch history info MOV R2, R2, LSL#1 ;make room for this switch reading LDR R4, =(GPIO_MMR_BASE) ;MMR base address LDR R1, [R4, #GP1DAT] ;get P1 pin states ANDS R1, R1, #1 ;mask off all but lsb, set flags ADDNE R2, R2, #1 ;switch state is '1' STRH R2, [R3] ;save switch history info
Switch De-bounce – Program ; check for an event AND R2, R2, #0x7F ;mask off bits not needed CMP R2, #0x40 ;pattern 1000000 -> valid press MOVEQ R0, #1 BLEQ savelog CMP R2, #0x3F ;pattern 0111111 -> valid release MOVEQ R0, #0 BLEQ savelog
Switch De-bounce – Program ;interrupt stuff LDR R4, =(TIMER_MMR_BASE) ;timer MMR base address MOV R1, #0xFF STRB R1, [R4, #T2CLRI] ;reset timer2 interrupt POP {R1-R4} ;context restore SUBS PC, LR, #4 ;interrupt return END
Software Timers • Why would we need/want software timers? Aren’t hardware timers good enough? • We may need many timers with different timeout values. (We only have a few hardware timers.) • We may need a longer timeout than the hardware timers can handle. • We may want timer functions at a higher level of code – isolated from the hardware.
Software Timers Problem • Assume that a timer interrupt is being generated at a 10.0 kHz rate. We would like to use the timer interrupt in order to create a procedure that will provide precise delays. In order to do this, the timer ISR will handle the decrementing of the timer variable used in the delay procedure. Write the timer ISR (ignore EOI and context save/restore) and a procedure DelayMS that is passed the desired delay in milliseconds in register R0. • Although not required here, you would normally need to configure the timer to generate such an interrupt, to enable interrupts in main code and to setup the call to the timer ISR in the IRQ interrupt routine.
Answer • The procedure and the ISR will modify a counter that holds the number of interrupts that must occur to give us the desired delay. • The procedure sets the counter to a desired value and waits for it to go to zero. • The interrupt will decrement the counter if it is greater than zero.
Answer • For DelayMS, we will: • Calculate the number of timer interrupts necessary to measure the given delay and save it in variable timercnt • Wait for timercnt to go to zero • Exit
Answer Code: delayMS STMFD R13!, {R1-R2, LR} ;context save MOV R1, #10 MUL R1, R0, R1 ;10 timer ISRs per millisec. LDR R2, =(DelayCnt) STR R1, [R2] ;store interrupt count delay_loop LDR R1, [R2] ;get current count CMP R1, #0 ;done when count is zero BNE delay_loop LDMFD R13!, {R1-R2, PC} ;context restore/return END
Answer • For TIMERISR, we will: • Decrease the value in timercnt if it is positive • Else do nothing • Clear timer interrupt
Answer Code: TimerISR PUSH {R1-R3} ;context save LDR R2, =(DelayCnt) LDR R1, [R2] ;get current count MOVS R1, R1 ;decrement if > zero SUBHI R1, R1, #1 STRHI R1, [R2] ;store updated count ;interrupt stuff LDR R3, =(TIMER_MMR_BASE) ;timer MMR base address MOV R1, #0xFF STRB R1, [R3, #T1CLRI] ;reset timer1 interrupt POP {R1-R3} ;context restore/return SUBS PC, LR, #4 ;interrupt return END
ConcepTest • How accurate is this software timer? • Resolution • 1.0 ms (0.1 ms for the hardware timer)