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

PROJECT 10.2 — Random Number Generator

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

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


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