Posts

Multi-Tasking and Real-Time Operating Systems:Voltmeter with RS232 Serial Output

Image
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 an LED connected to port RD7 of the microcontroller to indicate that the system is working. • Task Get_voltage reads channel 0 of the A/D converter where the voltage to be measured is connected. The read value is formatted and then stored in a variable. This task runs every two seconds. • Task To_RS232 reads the formatted voltage and sends it over the RS232 line to a PC every second. Figure 10.12 shows the block diagram of the project. The circuit diagram is given in Figure 10.13. A PIC18F8520-type microcontroller with a 10MHz crystal is used in this project (though any PIC18F-series microcontroller can be used). The voltage to be measured is connected to analog port...

Multi-Tasking and Real-Time Operating Systems:Random Number Generator

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

Multi-Tasking and Real-Time Operating Systems:CCS PIC C Compiler RTOS

Image
CCS PIC C Compiler RTOS The CCS PIC C compiler is one of the popular C compilers for the PIC16 and PIC18 series of microcontrollers. In addition to their PIC compilers, Customer Computer Services offers PIC in-circuit emulators, simulators, microcontroller programmers, and various development kits. The syntax of the CCS C language is slightly different from that of the mikroC language, but readers who are familiar with mikroC should find CCS C easy to use. CCS C supports a rudimentary multi-tasking cooperative RTOS for the PIC18 series of microcontrollers that uses their PCW and PCWH compilers. This RTOS allows a PIC microcontroller to run tasks without using interrupts. When a task is scheduled to run, control of the processor is given to that task. When the task is complete or does not need the processor any more, control returns to a dispatch function, which gives control of the processor to the next scheduled task. Because the RTOS does not use interrupts and is not preemptive,...

Multi-Tasking and Real-Time Operating Systems:LEDs

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

Multi-Tasking and Real-Time Operating Systems:The Real-Time Operating System (RTOS)

Image
The Real-Time Operating System (RTOS) Real-time operating systems are built around a multi-tasking kernel which controls the allocation of time slices to tasks. A time slice is the period of time a given task has for execution before it is stopped and replaced by another task. This process, also known as context switching, repeats continuously. When context switching occurs, the executing task is stopped, the processor registers are saved in memory, the processor registers of the next available task are loaded into the CPU, and the new task begins execution. An RTOS also provides task-to-task message passing, synchronization of tasks, and allocation of shared resources to tasks. The basic parts of an RTOS are: • Scheduler • RTOS services • Synchronization and messaging tools The Scheduler A scheduler is at the heart of every RTOS, as it provides the algorithms to select the tasks for execution. Three of the more common scheduling algorithms are: • Cooperative...

Multi-Tasking and Real-Time Operating Systems:State Machines

Image
State Machines State machines are simple constructs used to perform several activities, usually in a sequence. Many real-life systems fall into this category. For example, the operation of a washing machine or a dishwasher is easily described with a state machine construct. Perhaps the simplest method of implementing a state machine construct in C is to use a switch-case statement. For example, our temperature monitoring system has three tasks, named Task 1, Task 2, and Task 3 as shown in Figure 10.1. The state machine implementation of the three tasks using switch-case statements is shown in Figure 10.2. The starting state is 1, and each task increments the state number by one to select the next state to be executed. The last state selects state 1, and there is a delay at the end of the switch-case statement. The state machine construct is executed continuously inside an endless for loop. State machines, although easy to implement, are primitive and have limited applicatio...

Advanced PIC18 Projects—CAN Bus Projects:CAN Bus Programming

Image
CAN Bus Programming To operate the PIC18F258 microcontroller on the CAN bus, perform the following steps: • Configure the CAN bus I/O port directions (RB2 and RB3) • Initialize the CAN module (CANInitialize) • Set the CAN module to CONFIG mode (CANSetOperationMode) • Set the mask registers (CANSetMask) • Set the filter registers (CANSetFilter) • Set the CAN module to normal mode (CANSetOperationMode) • Write/read data (CANWrite/CANRead) PROJECT 9.1—Temperature Sensor CAN Bus Project The following is a simple two-node CAN bus–based project. The block diagram of the project is shown in Figure 9.15. The system is made up of two CAN nodes. One node (called DISPLAY node) requests the temperature every second and displays it on an LCD. This process is repeated continuously. The other node (called COLLECTOR node) reads the temperature from an external semiconductor temperature sensor. The project’s circuit diagram is given in Figure 9.16. Two CAN nodes are connected to...