Download - Κλήσεις συναρτήσεων και προγραμματισμός με μηχανές καταστάσεων

Transcript

1

Κλήσεις συναρτήσεων και προγραμματισμός με μηχανές

καταστάσεων

2

Functions, Subroutines and Interrupts

• Όταν καλείται μια υπορουτίνα-συνάρτηση• Οι «πρόχειροι καταχωρητές» (Scratch Registers) R12-R15

χρησιμοποιούνται για την μεταβίβαση παραμέτρων και έτσι συνήθως δεν διατηρούνται τα περιεχόμενά τους με την κλήση της υπορουτίνας-συνάρτησης.

• Οι καταχωρητές R4-R11 χρησιμοποιούνται για μεταβλητές και αποθήκευση προσωρινών αποτελεσμάτων και κατά την κλήση τα δεδομένα τους πρέπει να διατηρούνται με αποθήκευση και ανάκληση των δεδομένων τους συνήθως με την χρήση της στοίβας (stack).

3

Functions, Subroutines and Interrupts

4

Functions, Subroutines and Interrupts

• Listing 6.1: Subroutine from substk0.s43, which now saves and restores• R4 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 overwritten• jmp LoopTest ; Start with test in case R12 = 0• OuterLoop:• mov.w #DELAYLOOPS ,R4 ; Initialize loop counter• DelayLoop: ; [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 delays• LoopTest:• cmp.w #0,R12 ; Finished number of 0.1s delays?• jnz OuterLoop ; No: go around delay loop again• pop.w R4 ; Yes: restore R4 before returning• ret ; Return to caller

5

Functions, Subroutines and Interrupts

6

Αποθήκευση τοπικών μεταβλητών μιας ρουτίνας-συνάρτησης

• Καταχωρητές του μικροελεγκτή (μέγιστη ταχύτητα)

• Σταθερές θέσεις στην RAM

• Χρήση της στοίβας (stack).

7

• Listing 6.2: Subroutine from substk1.s43, whose delay loop uses two local• variables 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 130• LITTLELOOPS EQU 100• ;-----------------------------------------------------------------------• DelayTenths:• sub.w #4,SP ; Allocate 2 words (4 bytes) on stack• jmp LoopTest ; Start with test in case R12 = 0• OuterLoop:• mov.w #BIGLOOPS ,2(SP) ; Initialize big loop counter• BigLoop:• mov.w #LITTLELOOPS ,0(SP) ; Initialize little loop counter• LittleLoop: ; [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 delays• LoopTest:• cmp.w #0,R12 ; Finished number of 0.1s delays?• jnz OuterLoop ; No: go around delay loop again• add.w #4,SP ; Yes: finished , release space on stack• ret ; Return to caller

8

Αποθήκευση τοπικών μεταβλητών μιας ρουτίνας-συνάρτησης

9

• Listing 6.3: Subroutine and calling code from substk2.s43, where the stack is used• for passing the parameter and for local variables.• InfLoop: ; Loop forever• push.w #5 ; Push delay parameter on to stack• call #DelayTenths ; Call subroutine: don't forget the #!• incd.w SP ; Release space used for parameter• xor.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 130• LITTLELOOPS EQU 100• ;-----------------------------------------------------------------------• DelayTenths:• sub.w #4,SP ; Allocate 2 words (4 bytes) on stack• jmp LoopTest ; Start with test in case R12 = 0• OuterLoop:• mov.w #BIGLOOPS ,2(SP) ; Initialize big loop counter• BigLoop:• mov.w #LITTLELOOPS ,0(SP) ; Initialize little loop counter• LittleLoop: ; [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 delays• LoopTest:• cmp.w #0,6(SP) ; Finished number of 0.1s delays?• jnz OuterLoop ; No: go around delay loop again• add.w #4,SP ; Yes: finished , release space on stack• ret ; Return to caller

10

Αποθήκευση τοπικών μεταβλητών μιας ρουτίνας-συνάρτησης

11

Αποθήκευση τοπικών μεταβλητών μιας ρουτίνας-συνάρτησης

12

Μηχανές καταστάσεων (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.

13

Μηχανές καταστάσεων

14

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<16;i++) // Copy the string txBuffer[2+i] = kTableStr[i]; switch (j) { case 1://unit 1 txBuffer[16] = '0'; // Table #, first digit txBuffer[17] = '1'; break; case 2://unit 2 txBuffer[16] = '0'; // Table #, first digit txBuffer[17] = '2'; break; case 3://unit 3 txBuffer[16] = '0'; // Table #, first digit txBuffer[17] = '3'; break; case 14://unit 14 txBuffer[16] = '0'; // Table #, first digit txBuffer[17] = 'E'; break; case 15://unit 15 txBuffer[16] = '0'; // Table #, first digit txBuffer[17] = 'F'; break; }

Tόσο στην εντολή Switch όσο και στην case o τύπος μπορεί να είναι int ή char μόνο!

Η εντολή break μεταφέρει τον έλεγχο του προγράμματος μετά την switch

Αν προστεθεί στο τέλος η εντολή default εκτελείται σε περίπτωση που καμία case δεν είναι αληθής

15

Προγραμματισμός με Μηχανές καταστάσεων

16

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;}

17

Προγραμματισμός με Μηχανές καταστάσεων

18

Chronos State diagram programming

19

Chronos State diagram programming

• IAR visualSTATE6.3

• Download: http://supp.iar.com/Download/SW/?item=VS-EVAL

• Choronos example projects: http://www.iar.com/website1/1.0.1.0/1867/1/#chronosproject

20

21

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

22

www.quantum-leaps.com/downloads/

23

24

25