Выбрать главу

//

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

PROJECT 10.2 — Random Number Generator

In this slightly more complex RTOS project, a random number between 0 and 255 is generated. Eight LEDs are connected to PORTB of a PIC18F452 microcontroller. In addition, a push-button switch is connected to bit 0 of PORTD (RD0), and an LED is connected to bit 7 of PORTD (RD7).

Three tasks are used in this project: Live, Generator, and Display.

• Task Live runs every 200ms and flashes the LED on port pin RD7 to indicate that the system is working.

• Task Generator increments a variable from 0 to 255 continuously and checks the status of the push-button switch. When the push-button switch is pressed, the value of the current count is sent to task Display using a messaging queue.

• Task Display reads the number from the message queue and sends the received byte to the LEDs connected to PORTB. Thus, the LEDs display a random pattern every time the push button is pressed.

Figure 10.9 shows the project’s block diagram. The circuit diagram is given in Figure 10.10. The microcontroller is operated from a 4MHz crystal.

Figure 10.9: Block diagram of the project

Figure 10.10: Circuit diagram of the project

The program listing of the project (RTOS2.C) is given in Figure 10.11. The main part of the program is in the later portion, and it configures PORTB pins as outputs. Also, bit 0 of PORTD is configured as input and other pins of PORTD are configured as outputs.

/////////////////////////////////////////////////////////////////////////////

//

//               SIMPLE RTOS EXAMPLE - RANDOM NUMBER GENERATOR

// --------------------------------------------------------------------------

//

// This is a simple RTOS example. 8 LEDs are connected to PORTB

// of a PIC18F452 microcontroller. Also, a push-button switch is

// connected to port RC0 of PORTC, and an LED is connected to port

// RC7 of the microcontroller. The push-button switch is normally at logic 1.

//

// The program consists of 3 tasks called "Generator", "Display", and "Live".

//

// Task "Generator" runs in a loop and increments a counter from 0 to 255.

// This task also checks the state of the push-button switch. When the

// push-button switch is pressed, the task sends the value of the count to the

// "Display" task using messaging passing mechanism. The “Display” task

// receives the value of count and displays it on the PORTB LEDs.

//

// Task "Live" flashes the LED connected to port RC7 at a rate of 250ms.

// This task is used to indicate that the system is working and is ready for

// the user to press the push-button.

//

// The microcontroller is operated from a 4MHz crystal

//

// Programmer: Dogan Ibrahim

// Date:       September, 2007

// File:       RTOS2.C

//

//////////////////////////////////////////////////////////////////////////////

#include "C:\NEWNES\PROGRAMS\rtos.h"

#use delay (clock=4000000)

int count;

//

// Define which timer to use and minor_cycle for RTOS

//

#use rtos(timer=0, minor_cycle=1ms)

//

// Declare TASK "Live" - called every 200ms

// This task flashes the LED on port RC7

//

#task(rate=200ms, max=1ms)

void Live() {

 output_toggle(PIN_D7);

}

//

// Declare TASK "Display" - called every 10ms

//

#task(rate=10ms, max=1ms, queue=1)

void Display() {

 if (rtos_msg_poll() > 0)    // Is there a message ?

 {

  output_b(rtos_msg_read()); // Send to PORTB

 }

}

//

// Declare TASK "Generator" - called every millisecond

//

#task(rate=1ms, max=1ms)

void Generator() {

 count++;                       // Increment count

 if (input(PIN_D0) == 0)        // Switch pressed ?

 {

  rtos_msg_send(Display,count); // send a message

 }

}

//

// Start of MAIN program

//

void main() {

 set_tris_b(0); // Configure PORTB as outputs

 set_tris_d(1); // RD0=input, RD7=output

 rtos_run();    // Start RTOS

}

Figure 10.11: Program listing of the project

PROJECT 10.3 — Voltmeter with RS232 Serial Output

In this RTOS project, which is more complex than the preceding ones, the voltage is read using an A/D converter and then sent over the serial port to a PC. The project consists of three tasks: Live, Get_voltage, and To_RS232.

• Task Live runs every 200ms and flashes the LED connected to port pin RD7. This LED indicates that the system is working.