Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: abdullah
- *
- * Created on 10 Haziran 2012 Pazar, 14:43
- */
- #include <xc.h> // Include the header file needed by the compiler
- __CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & IOSCFS_4MHZ & BOREN_ON);
- #define numOfTasks 4
- unsigned char currentTask; // This TCB pointer will point to the current task's TCB.
- unsigned char tasks[4];
- void fTask1(void); // Prototype the function for task1.
- void fTask2(void); // Prototype the function for task2.
- void main(void)
- {
- TRISA = 0; // Set all of the PORTA pins as outputs.
- TRISC = 0; // Set all of the PORTC pins as outputs.
- ANSEL = 0; // Set all of the analog input pins as digital i/o.
- PORTA = 0; // Clear PORTA bits.
- PORTC = 0; // Clear PORTC bits.
- currentTask = 0;
- fTask1();
- currentTask+=2; // was "currentTask++;" before.
- fTask2();
- #asm
- _taskswitcher
- movlw 0x02 ; W = 2
- addwf _currentTask, f ; Add 2 to currentTask, store it in currentTask.
- movlw numOfTasks ; W = numOfTasks
- subwf _currentTask, w ; w= f - w
- btfsc status, 0 ; If currentTask >= numOfTasks
- clrf _currentTask ; Clear currentTask
- movlw _tasks ; Store the address of tasks, which is the start address of our task "array".
- addwf _currentTask, w ; Add current task's index to the start address.
- ; For example; task1's index is 2: [task0_1][task0_2][task1_1][task1_2]....
- ; 0 1 2 3
- movwf fsr ; We have the index of current task in W. Copy it to FSR
- movf indf, w ; Copy the contents of current task's first item to W
- movwf pclath ; Copy W to PCLATH. As a result, current task's PCLATH will be in PCLATH register.
- incf fsr, f ; Increment index, so that we will point to the next item of current task.
- movf indf, w ; Copy the contents of current task's second item to W.
- movwf pcl ; Copy W to PCL. Finally, current task's PCL will be in PCL register.
- #endasm
- }
- void fTask1(void)
- {
- #asm
- movlw _tasks ; Store the address of tasks, which is the start address of our task "array".
- addwf _currentTask, w ; Add current task's index to the start address.
- movwf fsr ; We have the index of current task in W. Copy it to FSR
- movf pclath, w ; Copy PCLATH register's contents to W register.
- movwf indf ; Copy W to current task's first item. We now store PCLATH.
- incf fsr,f ; Increment index, so that we will point to the next item of current task.
- movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the start of the task.
- movwf indf ; Copy W to task's next item. With that, we will initialize the current task.
- return ; We have gathered our initialazation information. Return back to main.
- #endasm
- while (1)
- {
- PORTA = 0xAA; // Toggle PORTA.0
- #asm
- movlw _tasks ; Store the address of tasks, which is the start address of our task "array".
- addwf _currentTask, w ; Add current task's index to the start address.
- movwf fsr ; We have the index of current task in W. Copy it to FSR
- movf pclath, w ; Copy PCLATH register's contents to W register.
- movwf indf ; Copy W to current task's first item. We now store PCLATH of the current state of the task.
- incf fsr, f ; Increment index, so that we will point to the next item of current task.
- movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the current state of the task.
- movwf indf ; Copy W to task's next item. With that, we will initialize the current task.
- goto _taskswitcher ; Yield the CPU to the awaiting task by going to task switcher.
- #endasm
- PORTA = 0x55; // Toggle PORTA.0
- }
- }
- void fTask2(void)
- {
- #asm
- movlw _tasks ; Store the address of tasks, which is the start address of our task "array".
- addwf _currentTask, w ; Add current task's index to the start address.
- movwf fsr ; We have the index of current task in W. Copy it to FSR
- movf pclath, w ; Copy PCLATH register's contents to W register.
- movwf indf ; Copy W to current task's first item. We now store PCLATH.
- incf fsr,f ; Increment index, so that we will point to the next item of current task.
- movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the start of the task.
- movwf indf ; Copy W to task's next item. With that, we will initialize the current task.
- return ; We have gathered our initialazation information. Return back to main.
- #endasm
- while (1)
- {
- PORTA = 0xAA; // Toggle PORTA.0
- #asm
- movlw _tasks ; Store the address of tasks, which is the start address of our task "array".
- addwf _currentTask, w ; Add current task's index to the start address.
- movwf fsr ; We have the index of current task in W. Copy it to FSR
- movf pclath, w ; Copy PCLATH register's contents to W register.
- movwf indf ; Copy W to current task's first item. We now store PCLATH of the current state of the task.
- incf fsr, f ; Increment index, so that we will point to the next item of current task.
- movlw low($+3) ; Copy PCL+3 to W register. This will let us save the PCL of the current state of the task.
- movwf indf ; Copy W to task's next item. With that, we will initialize the current task.
- goto _taskswitcher ; Yield the CPU to the awaiting task by going to task switcher.
- #endasm
- PORTA = 0x55; // Toggle PORTA.0
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement