Multi-Tasking and Real-Time Operating Systems:LEDs

PROJECT 10.1—LEDs

In the following simple RTOS-based project, four LEDs are connected to the lower half of PORTB of a PIC18F452-type microcontroller. The software consists of four tasks, where each task flashes an LED at a different rate:

• Task 1, called task_B0, flashes the LED connected to port RB0 at a rate of 250ms.

• Task 2, called task_B1, flashes the LED connected to port RB1 at a rate of 500ms.

• Task 3, called task_B2, flashes the LED connected to port RB2 once a second.

• Task 4, called task_B3, flashes the LED connected to port RB3 once every two seconds.

Figure 10.7 shows the circuit diagram of the project. A 4MHz crystal is used as the clock. PORTB pins RB0–RB3 are connected to the LEDs through current limiting resistors.

Multi-Tasking and Real-Time Operating Systems-0229

The software is based on the CCS C compiler, and the program listing (RTOS1.C) is given in Figure 10.8. The main program is at the end of the program, and inside the main program PORTB pins are declared as outputs and RTOS is started by calling function rtos_run().

The file that contains CCS RTOS declarations should be included at the beginning of the program. The preprocessor command #use delay tells the compiler that we are using

Multi-Tasking and Real-Time Operating Systems-0230

Multi-Tasking and Real-Time Operating Systems-0231

a 4MHz clock. Then the RTOS timer is declared as Timer 0, and minor_cycle time is declared as 10ms using the preprocessor command #use rtos.

The program consists of four similar tasks:

• task_B0 flashes the LED connected to RB0 at a rate of 250ms. Thus, the LED is ON for 250ms, then OFF for 250ms, and so on. CCS statement output_toggle is used to change the state of the LED every time the task is called. In the CCS compiler PIN_B0 refers to port pin RB0 of the microcontroller.

• task_B1 flashes the LED connected to RB1 at a rate of 500ms as described.

• task_B2 flashes the LED connected to RB2 every second as described.

• Finally, task_B3 flashes the LED connected to RB3 every two seconds as described.

The program given in Figure 10.8 is a multi-tasking program where the LEDs flash independently of each other and concurrently.