0% found this document useful (0 votes)
10 views21 pages

ECEg5702 Lecture 06

Uploaded by

sewa kassie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

ECEg5702 Lecture 06

Uploaded by

sewa kassie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Embedded Systems (ECEg5702)

Lecture 6: CCS PIC C Compiler RTOS

BY ETEFA B
July 9, 2024
Outline of the Lecture

Introduction CCS PIC C compiler

CCS PIC C built-in RTOS functions

Before using built-in RTOS

Declaring a Task

Examples
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
Introduction CCS PIC C compiler

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, CCS 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.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
CCS PIC C built-in RTOS functions

The CCS language provides the following RTOS


functions in addition to the normal C functions:
rtos_run() initiates the operation of RTOS. All task control
operations are implemented after calling this function.
e.g. rtos_run();
rtos_terminate() terminates the operation of RTOS. Control
returns to the original program without RTOS. In fact, this
function is like a return from rtos_run().
e.g. rtos_teriminate();
rtos_enable(task) receives the name of a task as an argument.
The function enables the task so function rtos_run() can call
the task when its time is due.
e.g. rtos_enable(toggle_green);
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
CCS PIC C built-in RTOS functions
rtos_disable(taskname) receives the name of a task as an argument. The function disables
the task so it can no longer be called by rtos_run() unless it is re-enabled by calling
rtos_enable(taskname).
e.g. rtos_disable(toggle_green)
rtos_ yield(), when called from within a task, returns control to the dispatcher. All tasks
should call this function to release the processor so other tasks can utilize the processor
time.
e.g. rtos_yield();
rtos_msg_send(taskname, byte) receives a task name and a byte as arguments. The
function sends the byte to the specified task, where it is placed in the task’s message
queue.
e.g. rtos_msg_send(echo, getc());
rtos_msg_read() reads the byte located in the task’s message queue.
e.g. b=rtos_msg_read();
rtos_msg_ poll() returns true if there is data in the task’s message queue. This function
should be called before reading a byte from the task’s message queue.
e.g. if (rtos_msg_poll())
rtos_signal(sem) receives a semaphore name and increments that semaphore.
e.g. rtos_signal(uart_use)
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
CCS PIC C built-in RTOS functions

rtos_wait(sem) receives a semaphore name and waits for the


resource associated with the semaphore to become available. The
semaphore count is then decremented so the task can claim the
resource.
e.g. rtos_wait(uart_use);
rtos_await(expre) receives a logical expression as an argument,
and the task waits until the expression evaluates to true.
rtos_overrun() receives a task name as an argument, and the
function returns true if that task has overrun its allocated time.
e.g. rtos_overrun(); or rtos_overrun(taskname);
rtos_stats(task, &stat) returns the specified statistics about a
specified task. The statistics can be the minimum and maximum
task run times and the total task run time. The task name and the
type of statistics are specified as arguments to the function.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
Before using built-in RTOS
The #use rtos() preprocessor command must be
specified at the beginning of the program before
calling any of the RTOS functions.
The format of this preprocessor command is:
#use rtos(time=n, minor_cycle=m)
where timer is between 0 and 4 and specifies the processor
timer that will be used by the RTOS, and minor_cycle is the
longest time any task will run. The number entered here must
be followed by s, ms, us, or ns.

Hawassa University, Institute of Technology School


of Electrical and Computer Engineering
Declaring a Task
A task is declared just like any other C function, but tasks in a
multi-tasking application do not have any arguments and do
not return any values.
Before a task is declared, a #task preprocessor command is
needed to specify the task options.
The format of this preprocessor command is:
#task(rate=n, max=m, queue=p)
where
rate specifies how often the task should be called. The number specified must
be followed by s, ms, us, or ns.
max specifies how much processor time a task will use in one execution of the
task. The time specifed here must be equal to or less than the time specified by
minor_cycle.
queue is optional and if present specifies the number of bytes to be reserved for
the task to receive messages from other tasks. The default value is 0.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
Declaring a Task
In the following example, a task called my_ticks
is every 20ms and is expected to use no more than
10ms of processor time.
This task is specified with no queue option:
#task(rate=20ms, max=10ms)
void my_ticks()
{
...........
...........
}

Hawassa University, Institute of Technology School


of Electrical and Computer Engineering
Examples
Example 1: LEDs
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.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
Examples

Hawassa University, Institute of Technology School


of Electrical and Computer Engineering
/*
* Simple RTOS Example 1
* Microcontroller: PIC18F452
* Date: May 18, 2015
* Frequency: 4.0MHz
* 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.
*/
#include "C:\Program Files (x86)\PICC\Devices\18F452.h"
#use delay (clock=4000000)
// Define which timer to use and minor_cycle for RTOS
#use rtos(timer=0, minor_cycle=10ms)

Hawassa University, Institute of Technology School of Electrical and


Computer Engineering
// 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
}
Hawassa University, Institute of Technology School of Electrical and
Computer Engineering
// 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
}

Hawassa University, Institute of Technology School of Electrical and


Computer Engineering
Examples
Example 2: Random Number Generator
In this 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.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering
Hawassa University, Institute of Technology School of Electrical and
Computer Engineering
/*
* Simple RTOS Example 2
* Microcontroller: PIC18F452
* Date: May 18, 2015
* Frequency: 4.0 MHz
* 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 RD7 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.
*/
#include "C:\Program Files (x86)\PICC\Devices\18F452.h"
#use delay (clock=4000000)
// Define which timer to use and minor_cycle for RTOS
#use rtos(timer=0, minor_cycle=1ms)
Hawassa University, Institute of Technology School of Electrical and
Computer Engineering
// 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);
}
// 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
}
}

Hawassa University, Institute of Technology School of Electrical and


Computer Engineering
// 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
}

Hawassa University, Institute of Technology School of Electrical and


Computer Engineering
Examples
Example 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.

Hawassa University, Institute of Technology School


of Electrical and Computer Engineering
Hawassa University, Institute of Technology School of Electrical and
Computer Engineering

You might also like