RTOS Task switching based on priority using LCD - CodesExplorer

Prerequisites:-

The prerequisite for this program you should have knowledge of Simple LCD interfacing on ARM LPC2148. You can visit this post to get the program, algorithm about LCD interfacing.

Introduction:-

In this post, We will be dealing with RTOS(Real-time Operating System) Program to switch the tasks based on the priorities.At first the OS is initialised with Task1 as highest priority and later task2 is created. For simplicity only two taskes are used in this program.Before completion of Task1 the priority of task2 is increased.Similarily in task2 before completion the priority of task1 is set to high.It continues like this  Task1 ---->  Task2  ----> Task1 ---> Task2 and so on.Task1 will display the message on first line of LCD and Task2 will display message om second line of LCD.

Algorithm:-

  1. Start.
  2. Declare Task1,Task2.
  3. Call the task1.
  4. Assign highest priority to task1,next priority to task2.
  5. Wait till the completion and jump to task2.
  6. Loop continues.
  7. End.

Code:-


#include<rtl.h>
#include<lpc21xx.h>
#include<stdio.h>
/*----------------------------------------------------------------------------
 *   Function Prototypes
 *---------------------------------------------------------------------------*/
void comm(void);
void writeData(void);
void data(void);
void lcd_initialize(void);
void writeCommand(void);
void clr_display(void);
void delay(unsigned int);
void first(void);
void second(void);
/*----------------------------------------------------------------------------
 *    Global Variable declaration
 *---------------------------------------------------------------------------*/

unsigned char temp1;
unsigned long int temp,r=0;
unsigned char *ptr;
unsigned char data1[] = "RTOS",data2[] = "ABHIJEET ABHAY";
OS_TID t1,t2;      
__task void task1(void);
__task void task2(void);

 int main()
{
   os_sys_init(task1); //starts with task1
   while(1);
}
/*----------------------------------------------------------------------------
 *    Task 1 - High Priority -
 *---------------------------------------------------------------------------*/

 __task void task1(void)
{
 t2=os_tsk_create(task2,0);
 t1=os_tsk_self();
 while(1)
 {
    IO0DIR = 0x000000FC;      //configure o/p lines for lcd
    lcd_initialize();              
    delay(3200);
    clr_display();       //lear lcd
    delay(3200); 
    temp1 = 0x80;      //Display starting address of first line
    comm();
    ptr = data1;
    while(*ptr!='\0')
    {
    temp1 = *ptr;
     data();
     ptr ++;
    }
    delay(1000000);
    clr_display();  
    delay(3200);
    os_tsk_prio(t2,5);
    os_tsk_prio(t1,2);
  }
 
 }
 __task void task2(void)
{
   
 while(1)
 {
    IO0DIR = 0x000000FC;  
    lcd_initialize();              
    delay(3200);
    clr_display();  
    delay(3200); 
    temp1 = 0xC0;        //Display starting address of second line
    comm();
    ptr = data2;
    while(*ptr!='\0')
    {
     temp1 = *ptr;
     data();
     ptr ++;
    }
    delay(1000000);
    clr_display();  
    delay(3200); 
    os_tsk_prio(t1,5);
    os_tsk_prio(t2,2);
  }
}
void delay(unsigned int r1)
{
 for(r=0;r<r1;r++);
}
// lcd initialisation routine.
void lcd_initialize(void)
{
 temp = 0x30;
 writeCommand();
 delay(3200);

 temp = 0x30;
 writeCommand();
 delay(3200);

 temp = 0x30;
 writeCommand();
 delay(3200);

 temp = 0x20; // change to 4 bit mode from default 8 bit mode
 writeCommand();
 delay(3200);

 temp = 0x28; // load command for lcd function setting with lcd in 4 bit mode,
 comm();    // 2 line and 5x7 matrix display
 delay(3200);

 comm();
 temp1 = 0x0C; // load a command for display on, cursor on and blinking off
 delay(800);

 temp1 = 0x06;  // command for cursor increment after data dump
 comm();
 delay(800);

 temp1 = 0x80;  // set the cursor to beginning of line 1
 comm();
 delay(800);
}

void comm(void)
{
    temp = temp1 & 0xf0;
    writeCommand();
    temp = temp1 & 0x0f;
    temp = temp << 4;
    writeCommand();
    delay(500);
}

// command nibble o/p routine
void writeCommand(void)         //write the values to command reg
{
 IO0CLR  = 0x000000FC;  // clear the port lines.
 IO0SET  = temp;    // Assign the value to the PORT lines
 IO0CLR  = 0x00000004;  // clear bit  RS = 0
 IO0SET  = 0x00000008;    // E=1
 delay(10);
 IO0CLR  = 0x00000008;
}
// data nibble
void writeData(void)   ////write the values to  data reg
{
 IO0CLR = 0x000000FC; // clear the port lines.
 IO0SET = temp;   // Assign the value to the PORT lines
 IO0SET = 0x00000004;    // set bit  RS = 1
 IO0SET = 0x00000008;    // E=1
 delay(10);
 IO0CLR = 0x00000008;
}
// data o/p routine which also outputs high nibble first
// and lower nibble next
void data(void)
{
    temp = temp1 & 0xf0;
    temp = temp ;//<< 6;
    writeData();
    temp= temp1 & 0x0f;
    temp= temp << 4;
    writeData();
    delay(100);
}
void clr_display(void)
{
    temp1 = 0x01;
    comm();
    delay(500);
}

Download the Code:
 To Download the Complete Project click on the link below:
Tasking switching LCD Program Download

1 comment:

  1. Nice article.Love you admin for posting this.Life saver...!

    ReplyDelete

Powered by Blogger.