SHOW:
|
|
- or go back to the newest paste.
1 | /* | |
2 | * File: main.c | |
3 | * Author: abdullah | |
4 | * | |
5 | * Created on 10 Haziran 2012 Pazar, 14:43 | |
6 | */ | |
7 | #include <xc.h> // Include the header file needed by the compiler | |
8 | __CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & IOSCFS_4MHZ & BOREN_ON); | |
9 | ||
10 | #define numOfTasks 4 | |
11 | unsigned char currentTask; // This TCB pointer will point to the current task's TCB. | |
12 | unsigned char tasks[4]; | |
13 | ||
14 | void fTask1(void); // Prototype the function for task1. | |
15 | void fTask2(void); // Prototype the function for task2. | |
16 | ||
17 | void main(void) | |
18 | { | |
19 | TRISA = 0; // Set all of the PORTA pins as outputs. | |
20 | TRISC = 0; // Set all of the PORTC pins as outputs. | |
21 | ANSEL = 0; // Set all of the analog input pins as digital i/o. | |
22 | PORTA = 0; // Clear PORTA bits. | |
23 | PORTC = 0; // Clear PORTC bits. | |
24 | ||
25 | currentTask = 0; | |
26 | fTask1(); | |
27 | - | currentTask++; |
27 | + | currentTask+=2; // was "currentTask++;" before. |
28 | fTask2(); | |
29 | ||
30 | #asm | |
31 | _taskswitcher | |
32 | movlw 0x02 ; W = 2 | |
33 | addwf _currentTask, f ; Add 2 to currentTask, store it in currentTask. | |
34 | movlw numOfTasks ; W = numOfTasks | |
35 | subwf _currentTask, w ; w= f - w | |
36 | btfsc status, 0 ; If currentTask >= numOfTasks | |
37 | clrf _currentTask ; Clear currentTask | |
38 | ||
39 | movlw _tasks ; Store the address of tasks, which is the start address of our task "array". | |
40 | addwf _currentTask, w ; Add current task's index to the start address. | |
41 | ; For example; task1's index is 2: [task0_1][task0_2][task1_1][task1_2].... | |
42 | ; 0 1 2 3 | |
43 | movwf fsr ; We have the index of current task in W. Copy it to FSR | |
44 | movf indf, w ; Copy the contents of current task's first item to W | |
45 | movwf pclath ; Copy W to PCLATH. As a result, current task's PCLATH will be in PCLATH register. | |
46 | ||
47 | incf fsr, f ; Increment index, so that we will point to the next item of current task. | |
48 | movf indf, w ; Copy the contents of current task's second item to W. | |
49 | movwf pcl ; Copy W to PCL. Finally, current task's PCL will be in PCL register. | |
50 | #endasm | |
51 | } | |
52 | ||
53 | void fTask1(void) | |
54 | { | |
55 | #asm | |
56 | movlw _tasks ; Store the address of tasks, which is the start address of our task "array". | |
57 | addwf _currentTask, w ; Add current task's index to the start address. | |
58 | ||
59 | movwf fsr ; We have the index of current task in W. Copy it to FSR | |
60 | movf pclath, w ; Copy PCLATH register's contents to W register. | |
61 | movwf indf ; Copy W to current task's first item. We now store PCLATH. | |
62 | ||
63 | incf fsr,f ; Increment index, so that we will point to the next item of current task. | |
64 | movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the start of the task. | |
65 | movwf indf ; Copy W to task's next item. With that, we will initialize the current task. | |
66 | return ; We have gathered our initialazation information. Return back to main. | |
67 | #endasm | |
68 | ||
69 | while (1) | |
70 | { | |
71 | PORTA = 0xAA; // Toggle PORTA.0 | |
72 | ||
73 | #asm | |
74 | movlw _tasks ; Store the address of tasks, which is the start address of our task "array". | |
75 | addwf _currentTask, w ; Add current task's index to the start address. | |
76 | ||
77 | movwf fsr ; We have the index of current task in W. Copy it to FSR | |
78 | movf pclath, w ; Copy PCLATH register's contents to W register. | |
79 | movwf indf ; Copy W to current task's first item. We now store PCLATH of the current state of the task. | |
80 | ||
81 | incf fsr, f ; Increment index, so that we will point to the next item of current task. | |
82 | movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the current state of the task. | |
83 | movwf indf ; Copy W to task's next item. With that, we will initialize the current task. | |
84 | ||
85 | goto _taskswitcher ; Yield the CPU to the awaiting task by going to task switcher. | |
86 | #endasm | |
87 | ||
88 | PORTA = 0x55; // Toggle PORTA.0 | |
89 | } | |
90 | } | |
91 | ||
92 | void fTask2(void) | |
93 | { | |
94 | #asm | |
95 | movlw _tasks ; Store the address of tasks, which is the start address of our task "array". | |
96 | addwf _currentTask, w ; Add current task's index to the start address. | |
97 | ||
98 | movwf fsr ; We have the index of current task in W. Copy it to FSR | |
99 | movf pclath, w ; Copy PCLATH register's contents to W register. | |
100 | movwf indf ; Copy W to current task's first item. We now store PCLATH. | |
101 | ||
102 | incf fsr,f ; Increment index, so that we will point to the next item of current task. | |
103 | movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the start of the task. | |
104 | movwf indf ; Copy W to task's next item. With that, we will initialize the current task. | |
105 | return ; We have gathered our initialazation information. Return back to main. | |
106 | #endasm | |
107 | ||
108 | while (1) | |
109 | { | |
110 | PORTA = 0xAA; // Toggle PORTA.0 | |
111 | ||
112 | #asm | |
113 | movlw _tasks ; Store the address of tasks, which is the start address of our task "array". | |
114 | addwf _currentTask, w ; Add current task's index to the start address. | |
115 | ||
116 | movwf fsr ; We have the index of current task in W. Copy it to FSR | |
117 | movf pclath, w ; Copy PCLATH register's contents to W register. | |
118 | movwf indf ; Copy W to current task's first item. We now store PCLATH of the current state of the task. | |
119 | ||
120 | incf fsr, f ; Increment index, so that we will point to the next item of current task. | |
121 | movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the current state of the task. | |
122 | movwf indf ; Copy W to task's next item. With that, we will initialize the current task. | |
123 | ||
124 | goto _taskswitcher ; Yield the CPU to the awaiting task by going to task switcher. | |
125 | #endasm | |
126 | ||
127 | PORTA = 0x55; // Toggle PORTA.0 | |
128 | } | |
129 | } |