Книга: Advanced PIC Microcontroller Projects in C

PROJECT 10.1 — 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.


Figure 10.7: Circuit diagram of the project

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().

////////////////////////////////////////////////////////////////////////
//
//                       SIMPLE RTOS EXAMPLE
//                ----------------------------------
//
// This is a simple RTOS example. 4 LEDs are connected to lower half of
// PORTB of a PIC18F452 microcontroller. The program consists of 4
// tasks:
//
// Task task_B0 flashes the LED connected to port RB0 every 250ms.
// Task task_B1 flashes the LED connected to port RB1 every 500ms.
// Task task_B2 flashes the LED connected to port RB2 every second
// Task task_B3 flashes the LED connected to port RB3 every 2 seconds.
//
// The microcontroller is operated from a 4MHz crystal
//
// Programmer: Dogan Ibrahim
// Date: September, 2007
// File: RTOS1.C
//
////////////////////////////////////////////////////////////////////////
#include "C:NEWNESPROGRAMSrtos.h"
#use delay (clock=4000000)
//
// Define which timer to use and minor_cycle for RTOS
//
#use rtos(timer=0, minor_cycle=10ms)
//
// Declare TASK 1 - called every 250ms
//
#task(rate=250ms, max=10ms)
void task_B0() {
 output_toggle(PIN_B0); // Toggle RB0
}
//
// Declare TASK 2 - called every 500ms
//
#task(rate=500ms, max=10ms)
void task_B1() {
 output_toggle(PIN_B1); // Toggle RB1
}
//
// Declare TASK 3 - called every second
//
#task(rate=1s, max=10ms)
void task_B2() {
 output_toggle(PIN_B2); // Toggle RB2
}
//
// Declare TASK 4 - called every 2 seconds
//
#task(rate=2s, max=10ms)
void task_B3() {
 output_toggle(PIN_B3); // Toggle RB3
}
//
// Start of MAIN program
//
void main() {
 set_tris_b(0); // Configure PORTB as outputs
 rtos_run();    // Start RTOS
}


Figure 10.8: Program listing of the project

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 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.

Оглавление книги


Генерация: 1.018. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз