PICOS18 > Tutorial > Interruptions

PICos18 : Using drivers

You want to communicate through the serial port or the CAN interface ? Now you know how to write interrupts and tasks and you know how to manage alarms and events it will be easy to use and to write a real driver for PICos18.


> Driver features

A driver is often sompared to a library byt it's really something different. A library is a set of functions written in a language (like the C language for instance) and used to simplify the development phase. In the case of a RS232 transfer a library proposes for example a function to open or close the channel or to recieve and send data…

A driver is a layer dedicated to a specific peripheral management. Its goal is not to offer a set of functions to use a such peripheral because it's the job of the driver to be in charge of the peripheral usage...

A driver is in fact composed of an ISR (Interrupt Service Routine) and a task. We have already seen the ISR in the previous chapter through the InterruptVectorL() and InterruptVectorH() functions.

The ISR is in charge of catching the interrupt (IT) and to inform the driver task the interrupt occured. So the mission of the task is to do what is necessary to manage these IT.

 

> Software layer usage

PICos18 is delivered with the kernel and this tutorial. But in the DOWNLOAD page of the web site we will find also the drivers and libraries available for free. This is to let you develop your application like a puzzle, with a lot of free layers you can add to your project without wasting time to study how it works.

We're going to study how to use a software layer. More precisely we're going to use the RS232 driver of PICos18 in order to display our "software swatch" with the global variables "hour", "min", and "sec".

Download the RS232 driver from the DOWNLOAD of PICos18 :

The package has a text file (TXT link) that indicates how to include and use the driver in your application (how to set the baudrate for instance or how to register the task to the RS driver).

 ______________________________________________________________________
>                                                                      <
>                          RS232 driver v1.02                          <
>                                 for                                  <
>                         PICos18 release 2.00 (beta 10 or upper)      <
>                                                                      <
>             PICos18 - Real-time kernel for PIC18 family              <
>                                                                      <
>                                                                      <
> www.picos18.com                                    www.pragmatec.net <
>______________________________________________________________________

...

Now you can insert tasks to your projet, write ISR, setup your application thanks to the "taskdesc.c" file then follow the recommandations of the text file step by step to insert the driver in your application.

You don't need to add the "Example" task references like the alarm of the Alarm_list structure (chapter IV of the text file) or the EXAMPLE_ID definition in the define.h file. Do not reach the chapter VII, because this one will be use in the next paragraph.

> Driver usage

In order to use the driver create a new task called "TASK4" to replace the "Example" task of the text file. Do not forget to add the ALARM_TASK4 settings in the "taskdesc.c" file.

Here is what should be the TASK4 task :

#include "define.h"
#include "drv_rs.h"
#include <stdio.h>
#include <string.h>

#define ALARM_TSK4      1

int Printf (const rom char *fmt, ...);
extern unsigned char hour;
extern unsigned char min;
extern unsigned char sec;

/**********************************************************************
 * Definition dedicated to the local functions.
 **********************************************************************/
RS_message_t RS_msg; 
unsigned char  buffer[80];

/**********************************************************************
 * ------------------------------ TASK4 -------------------------------
 *
 * Fifth task of the tutorial.
 *
 **********************************************************************/
TASK(TASK4) 
{
  SetRelAlarm(ALARM_TSK4, 1000, 1000);

  Printf(" ______________________________________________________________________ \r\n");
  Printf(">                                                                      <\r\n");
  Printf(">                         PICos18 Tutorial                             <\r\n");
  Printf(">                                                                      <\r\n");
  Printf(">                                                                      <\r\n");
  Printf(">             PICos18 - Real-time kernel for PIC18 family              <\r\n");
  Printf(">                                                                      <\r\n");
  Printf(">                                                                      <\r\n");
  Printf("> www.picos18.com                                    www.pragmatec.net <\r\n");
  Printf(">______________________________________________________________________<\r\n");
  Printf(" \r\n");
  Printf(" \r\n");

  while(1)
  {
    WaitEvent(ALARM_EVENT);
    ClearEvent(ALARM_EVENT); 

    Printf("%02d : %02d : %02d\r", (int)hour, (int)min, (int)sec);
  }
}

/**********************************************************************
 * Function in charge of structure registration and buffer transmission.
 *
 * @param  string   IN const string send to the USART port
 * @return void
 **********************************************************************/
int Printf (const rom char *fmt, ...)
{
  va_list ap;
  int n;
  RS_enqMsg(&RS_msg, buffer, 0);
  va_start (ap, fmt); 
  n = vfprintf (_H_USER, fmt, ap);
  va_end (ap);
  SetEvent(RS_DRV_ID, RS_NEW_MSG); 
  WaitEvent(RS_QUEUE_EMPTY);
  ClearEvent(RS_QUEUE_EMPTY);
  return n;
}

The first part of the code is used to include the headers dedicated to the RS232 driver and the "printf" functions. Then you need to declare the ALARM_TASK4 identifier and also to declare as "extern" the external variables "hour", "min" and "sec" to tell the C18 compiler the definition is not in this C file.

In RAM we add 2 kinds of variables :

Then you discover the TASK4 task core with an alarm that occurs every 1000ms, so every secondes. The function "Printf" is called to send a const string, to convert data from one type to another,... Every format are managed by C18 except the FLOAT variables...!
The "Printf" function needs a "\r\n" at the end of the string if you whish a cariage return with a line feed : the "\r" is used to go back to the beginning of the line and the "\n" to load a new line.

To display the banner we need a "\r\n" and to display the time at the same location we just need "\r".

Finally you have to get the following message with HyperTerminal :


> HyperTeminal settings

Be carrefull with the COM settings : set 115200 bits per secconde and no hardware check (sorry for the screenshots in french only ;-) :