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

• Task Get_voltage reads the analog voltage from channel 0 (pin RA0 or AN0) of the microcontroller. The value is then converted into millivolts by multiplying by 5000 and dividing by 1024 (in a 10-bit A/D there are 1024 quantization levels, and when working with a reference voltage of +5V, each quantization level corresponds to 5000/1024mV). The voltage is stored in a global variable called Volts.

• Task To_RS232 reads the measured voltage from common variable Volts and sends it to the RS232 port using the C printf statement. The result is sent in the following format:

 Measured voltage = nnnn mV

The HyperTerminal program is run on the PC to get an output from the program. A typical screen output is shown in Figure 10.15.

Figure 10.15: Typical output from the program

Using a Semaphore

The program given in Figure 10.14 is working and displays the measured voltage on the PC screen. This program can be improved slightly by using a semaphore to synchronize the display of the measured voltage with the A/D samples. The modified program (RTOS4.C) is given in Figure 10.16. The operation of the new program is as follows:

• The semaphore variable (sem) is set to 1 at the beginning of the program.

• Task Get_voltage decrements the semaphore (calls rtos_wait) variable so that task To_RS232 is blocked (semaphore variable sem=0) and cannot send data to the PC. When a new A/D sample is ready, the semaphore variable is incremented (calls rtos_signal) and task To_RS232 can continue. Task To_RS232 then sends the measured voltage to the PC and increments the semaphore variable to indicate that it had access to the data. Task Get_voltage can then get a new sample. This process is repeated forever.

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

//

//              SIMPLE RTOS EXAMPLE - VOLTMETER WITH RS232 OUTPUT

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

//

// This is a simple RTOS example. Analog voltage to be measured (between 0V

// and +5V) is connected to analog input AN0 of a PIC18F8520 type

// microcontroller. The microcontroller is operated from a 10MHz crystal. In

// addition, an LED is connected to port in RD7 of the microcontroller.

//

// RS232 serial output of the mirocontroller (RC6) is connected to a MAX232

// type RS232 voltage level converter chip. The output of this chip can be

// connected to the serial input of a PC (e.g., COM1) so that the measured

// voltage can be seen on the PC screen.

//

// The program consists of 3 tasks called "live", "Get_voltage", and

// "To_RS232".

//

// Task "Live" runs every 200ms and it flashes the LED connected to port RD7

// of the microcontroller to indicate that the program is running and is

// ready to measure voltages.

//

// task "Get_voltage" reads analog voltage from port AN0 and then converts the

// voltage into millivolts and stores in a variable called Volts.

//

// Task "To_RS232" gets the measured voltage and then sends to the PC over

// the RS232 serial line. The serial line is configured to operate at 2400

// Baud (higher Baud rates can also be used if desired).

//

// In this modified program, a semaphore is used to synchronize

// the display of the measured value with the A/D samples.

//

// Programmer: Dogan Ibrahim

// Date: September, 2007

// File: RTOS4.C

//

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

#include <18F8520.h>

#device adc=10

#use delay (clock=10000000)

#use rs232(baud=2400,xmit=PIN_C6,rcv=PIN_C7)

unsigned int16 adc_value;

unsigned int32 Volts;

int8 sem;

//

// Define which timer to use and minor_cycle for RTOS

//

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

//

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

// This task flashes the LED on port RD7

//

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

void Live() {

 output_toggle(PIN_D7); // Toggle RD7 LED

}

//

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

//

#task(rate=2s, max=100ms)

void Get_voltage() {

 rtos_wait(sem);         // decrement semaphore

 adc_value = read_adc(); // Read A/D value

 Volts = (unsigned int32)adc_value*5000;

 Volts = Volts / 1024;  // Voltage in mV

 rtos_signal(sem);      // increment semaphore

}

//

// Declare TASK "To_RS232" - called every millisecond

//

#task(rate=2s, max=100ms)

void To_RS232() {

 rtos_wait(sem);                               // Decrement semaphore

 printf("Measured Voltage = %LumV\n\r",Volts); // Send to RS232

 rtos_signal(sem);                             // Increment semaphore

}

//

// Start of MAIN program

//

void main() {

 set_tris_d(0);               // PORTD all outputs

 output_d(0);                 // Clear PORTD

 set_tris_a(0xFF);            // PORTA all inputs

 setup_adc_ports(ALL_ANALOG); // A/D ports

 setup_adc(ADC_CLOCK_DIV_32); // A/D clock

 set_adc_channel(0);          // Select channel 0 (AN0)

 delay_us(10);

 sem = 1;                     // Semaphore is 1

 rtos_run();                  // Start RTOS

}

Figure 10.16: Modified program listing