»®ƒµ¹‚...

download »®ƒµ¹‚ ƒ…½±„®ƒµ‰½ ±¹ €³±¼¼±„¹ƒ¼Œ‚ ¼µ ¼·‡±½­‚

of 25

  • date post

    02-Jan-2016
  • Category

    Documents

  • view

    25
  • download

    0

Embed Size (px)

description

Κλήσεις συναρτήσεων και προγραμματισμός με μηχανές καταστάσεων. Functions , Subroutines and Interrupts. Όταν καλείται μια υπορουτίνα - συνάρτηση - PowerPoint PPT Presentation

Transcript of »®ƒµ¹‚...

  • Functions, Subroutines and Interrupts - (Scratch Registers) R12-R15 -. R4-R11 (stack).

  • Functions, Subroutines and Interrupts

  • Functions, Subroutines and InterruptsListing 6.1: Subroutine from substk0.s43, which now saves and restoresR4 correctly.; Subroutine to give delay of R12 *0.1s; Parameter is passed in R12 and destroyed; R4 used for loop counter , stacked and restored;-----------------------------------------------------------------------DelayTenths:push.w R4 ; Stack R4: will be overwrittenjmp LoopTest ; Start with test in case R12 = 0OuterLoop:mov.w #DELAYLOOPS ,R4 ; Initialize loop counterDelayLoop: ; [clock cycles in brackets]dec.w R4 ; Decrement loop counter [1]jnz DelayLoop ; Repeat loop if not zero [2]dec.w R12 ; Decrement number of 0.1s delaysLoopTest:cmp.w #0,R12 ; Finished number of 0.1s delays?jnz OuterLoop ; No: go around delay loop againpop.w R4 ; Yes: restore R4 before returningret ; Return to caller

  • Functions, Subroutines and Interrupts

  • - ( ) RAM (stack).

  • Listing 6.2: Subroutine from substk1.s43, whose delay loop uses two localvariables on the stack.; Subroutine to give delay of R12 *0.1s; Parameter is passed in R12 and destroyed; Space for two loop counters is created on stack , after which; 0(SP) is innermost (little) loop , 2(SP) is big loop counter;-----------------------------------------------------------------------; Iterations of delay loop for about 0.1s (6 cycles/iteration ):BIGLOOPS EQU 130LITTLELOOPS EQU 100;-----------------------------------------------------------------------DelayTenths:sub.w #4,SP ; Allocate 2 words (4 bytes) on stackjmp LoopTest ; Start with test in case R12 = 0OuterLoop:mov.w #BIGLOOPS ,2(SP) ; Initialize big loop counterBigLoop:mov.w #LITTLELOOPS ,0(SP) ; Initialize little loop counterLittleLoop: ; [clock cycles in brackets]dec.w 0(SP) ; Decrement little loop counter [4]jnz LittleLoop ; Repeat loop if not zero [2]dec.w 2(SP) ; Decrement big loop counter [4]jnz BigLoop ; Repeat loop if not zero [2]dec.w R12 ; Decrement number of 0.1s delaysLoopTest:cmp.w #0,R12 ; Finished number of 0.1s delays?jnz OuterLoop ; No: go around delay loop againadd.w #4,SP ; Yes: finished , release space on stackret ; Return to caller

  • -

  • Listing 6.3: Subroutine and calling code from substk2.s43, where the stack is usedfor passing the parameter and for local variables.InfLoop: ; Loop foreverpush.w #5 ; Push delay parameter on to stackcall #DelayTenths ; Call subroutine: don't forget the #!incd.w SP ; Release space used for parameterxor.b #LED1 ,& P2OUT ; Toggle LEDjmp InfLoop ; Back around infinite loop;-----------------------------------------------------------------------; Subroutine to give delay of n*0.1s; Parameter n is passed on stack; Space for two loop counters created on stack. After this:; 0(SP) is innermost (little) loop counter; 2( SP) is big loop counter; 4(SP) is return address; 6(SP) is parameter n passed on stack;-----------------------------------------------------------------------; Iterations of delay loop for about 0.1s (6 cycles/iteration ):BIGLOOPS EQU 130LITTLELOOPS EQU 100;-----------------------------------------------------------------------DelayTenths:sub.w #4,SP ; Allocate 2 words (4 bytes) on stackjmp LoopTest ; Start with test in case R12 = 0OuterLoop:mov.w #BIGLOOPS ,2(SP) ; Initialize big loop counterBigLoop:mov.w #LITTLELOOPS ,0(SP) ; Initialize little loop counterLittleLoop: ; [clock cycles in brackets]dec.w 0(SP) ; Decrement little loop counter [4]jnz LittleLoop ; Repeat loop if not zero [2]dec.w 2(SP) ; Decrement big loop counter [4]jnz BigLoop ; Repeat loop if not zero [4]dec.w 6(SP) ; Decrement number of 0.1s delaysLoopTest:cmp.w #0,6(SP) ; Finished number of 0.1s delays?jnz OuterLoop ; No: go around delay loop againadd.w #4,SP ; Yes: finished , release space on stackret ; Return to caller

  • -

  • -

  • (State Machines) UML

    (tasks), .

    (State machines) .

    There is a formal foundation for state machines, which is often based on the unified modeling language (UML)

    More complicated systems may be designed using state charts, which are hierarchical extensions of state machines and share some ofthe properties of object-oriented languages.

  • Switch case and State diagram programming for (j = 1; j < 12 ; j++) { // Build packet txBuffer[0] = 17; // Pkt length (not inc. len byte) txBuffer[1] = j; // Pkt address for(i=0;i
  • Listing 8.18: Interrupt service routine for channel 0 of Timer_A from butstmc1.c.The state machine lights the LED while the button is pressed#pragma vector = TIMERA0_VECTOR__interrupt void TA0_ISR (void) // Acknowledged automatically{static enum {LedOff ,LedOn} LedState = LedOff; // State variable , initializedMonitor = ON;switch (LedState) {case LedOff: // LED currently offif (Button == ON) { // Button pressed?LED = ON; // Yes: light LEDLedState = LedOn; // Change state} else {// No action needed}break;case LedOn: // LED currently onif (Button == OFF) { // Button released?LED = OFF; // Yes: extinguish LEDLedState = LedOff; // Change state}break;default: // Should never happenLedState = LedOff; // Reset to initial statebreak;}Monitor = OFF;}

  • Chronos State diagram programming

  • Chronos State diagram programmingIAR visualSTATE6.3Download: http://supp.iar.com/Download/SW/?item=VS-EVALChoronos example projects: http://www.iar.com/website1/1.0.1.0/1867/1/#chronosproject

  • Quantum Leaps QP-nano is a generic, portable, ultra-lightweight, event-driven infrastructure designed specifi-cally for low-end 8- and 16-bit MCUs, such as 8051, PICmicro, AVR, 68H(S)08, MSP430, M16C/R8C, Cypress PSoC. RTOS

  • www.quantum-leaps.com/downloads/