Ertos Codes
Ertos Codes
1
Code:
#include<lpc214x.h>
//LED Mapping for the board
#define LED_MASK 0xFF000000
#define LED0 24
void delay(unsigned int time)
{
unsigned int i,j;
for(i = 0; i < time ;i++ )
{
for(j = 0; j < 5000 ; j++);
}
}
int main(void)
{
//PINSEL2 &= 0xFFFFFFF3; // set PORT1 as GPIO
PINSEL2 = 0;
IODIR1 = LED_MASK; // set the direction of the pins as output
: 1
while(1)
{
IOSET1 = LED_MASK; // set the port pins to 1
delay(150);
IOCLR1 = LED_MASK; //clear the port pins to 0
delay(150);
}
}
Experiment No. 2
Code:
/*
Pin Configuration
A - P1.16
B - P1.17
C - P1.18
D - P1.19
E - P1.20
F - P1.21
G - P1.22
DOT - P1.23
SEG1 - P0.10
SEG2 - P0.11
*/
#include"lpc214x.h"
#define SEG_A 1<<16 // SEGMENT A
#define SEG_B 1<<17 // SEGMENT B
#define SEG_C 1<<18 // SEGMENT C
#define SEG_D 1<<19 // SEGMENT D
#define SEG_E 1<<20 // SEGMENT E
#define SEG_F 1<<21 // SEGMENT F
#define SEG_G 1<<22 // SEGMENT G
#define SEG_DP 1<<23 // SEGMENT DECIMAL POINT
#define SEG1 1<<10
#define SEG2 1<<11
#define ONE (SEG_B | SEG_C)
//DATA FOR SHOWING '1'
#define TWO (SEG_A | SEG_B | SEG_D | SEG_E | SEG_G)
//DATA FOR SHOWING '2'
#define THREE (SEG_A | SEG_B | SEG_C | SEG_D | SEG_G)
//DATA FOR SHOWING '3'
#define FOUR (SEG_B | SEG_C | SEG_F | SEG_G)
//DATA FOR SHOWING '4'
#define FIVE (SEG_A | SEG_C | SEG_D | SEG_F | SEG_G)
//DATA FOR SHOWING '5'
#define SIX (SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G)
//DATA FOR SHOWING '6'
#define SEVEN (SEG_A | SEG_B | SEG_C)
//DATA FOR SHOWING '7'
#define EIGHT (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F |
SEG_G)
//DATA FOR SHOWING '8'
#define NINE (SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G)
//DATA FOR SHOWING '9'
#define ZERO (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F)
//DATA FOR SHOWING '0'
#define AA (SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G)
//DATA FOR SHOWING 'A'
#define BB (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F |
SEG_G)
//DATA FOR SHOWING 'B'
#define CC (SEG_A | SEG_D | SEG_E | SEG_F)
//DATA FOR SHOWING 'C'
#define DD (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F )
//DATA FOR SHOWING 'D'
#define EE (SEG_A | SEG_D | SEG_E | SEG_F | SEG_G)
//DATA FOR SHOWING 'E'
#define FF (SEG_A | SEG_E | SEG_F | SEG_G)
//DATA FOR SHOWING 'F'
#define DP (SEG_DP)
//DATA FOR SHOWING '.'
#define SSEG_MASK
(SEG_A|SEG_B|SEG_C|SEG_D|SEG_E|SEG_F|SEG_G|SEG_DP)
unsigned int seg_data[]={ZERO , ONE,TWO, THREE, FOUR, FIVE,
SIX,
SEVEN,EIGHT,\
NINE, AA, BB, CC, DD, EE, FF, DP };
void delay(unsigned int time)
{
unsigned int i,j;
for(i = 0; i < time ;i++ )
{
for(j = 0; j < 5000 ; j++);
}
}
int main(void)
{
int i,j;
PINSEL0 &= 0xFF0FFFFF; //P0.10 and P.11 as GPIO
PINSEL2 = 0xFFFFFFF3; //PORT1 as GPIO
IO0DIR = SEG1 | SEG2;
IO1DIR = SSEG_MASK; // set the direction of the pins
IO1SET= SSEG_MASK; // set the direction of the pins
while(1)
{
for(i =0 ; i < 99 ; i++ )
{
for(j=0; j<30; j++)
{
IO0SET = SEG1;
IO0CLR = SEG2;
IO1CLR = seg_data[i/10]; // since the 7 seg display is
common anode we have to clear the port pins to make SEGMENT
delay(2);
IO1SET = seg_data[i/10];
IO0CLR = SEG1;
IO0SET = SEG2;
IO1CLR = seg_data[i%10]; // since the 7 seg display is
common anode we have to clear the port pins to make SEGMENT
delay(2);
IO1SET = seg_data[i%10];
}
}
}
}
Experiment No. 3
Code:
C Program 1. Main:
#include "adc.h"
#include "LCD.h"
#include <stdio.h>
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<5000;j++);
}
int main()
{
unsigned int temp;
char buf[16];
LCD_init();
ADCInit();
while(1)
{
temp = ADC_Read(2)/2; //read AN0.2
sprintf(buf,"Temp: %2d'C",(temp*330)/1024); //convert to
string
and store in a buffer
LCD_display(1,1,buf); //display buffer
delay(20);
}
}
C Program 2. ADC:
/*************************************************************
**/
/* This Program is to demonstrate the On Chip ADC
Functionality */
/*************************************************************
***
Processor : LPC2148
ADC 0 Mapping : AD0.1 -> P0.28
AD0.2 -> P0.29
Jumper Settings : No jumper selection
**************************************************************
***/
#include "lpc214x.h"
void ADCInit(void)
{
PINSEL1 &= 0xF0FFFFFF; //Configure pins P0.28 and P0.29 as
ADC
pins
PINSEL1 |= 0x05000000;
}
unsigned int ADC_Read(unsigned char channel)
{
static unsigned int ad1_data;
AD0CR = 0x00200300 | (1<<channel); //Set Burst mode to 0,
CLK_DIV,
Channels, 10bit ADC/11 cycles
AD0CR |= 1<<24; //Start ADC
while(!(AD0GDR & 0x80000000)); //Check DONE bit
ad1_data = (AD0GDR & 0x0000FFC0)>>6; //Assign ADC result to
ad1_data n display in mV
return ad1_data;
}
C Program 3. LCD
/*
==============================================================
=============
=
Name : LCD.c
Interface :
D0 - P1.16
D1 - P1.17
D2 - P1.18
D3 - P1.19
D4 - P1.20
D5 - P1.21
D6 - P1.22
D7 - P1.23
EN - P0.10
RS - P0.11
RW - P0.20
==============================================================
=============
=
*/
#include<lpc214x.h>
#define LCD_PORT 0x00FF0000
#define EN 1<<10 //define RS pin
#define RS 1<<11 //define EN pin
#define RW 1<<20 //define RW pin
#define LCD_SHIFT 16 //shift data by LCD_SHIFT bits
void lcd_delay(unsigned int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<200;j++);
}
void LCD_strobe() //Enable pulse
{
lcd_delay(100);
IOSET0 = EN;
lcd_delay(100);
IOCLR0 = EN;
lcd_delay(100);
}
void LCD_data(unsigned char ch) //function to send data
{
IOCLR1 = LCD_PORT; //clear LCD pins
IOSET1 = ch<<LCD_SHIFT; //shift data and set only the
data bits
IOSET0 = RS; //RS =1
IOCLR0 = RW; //RW = 0
LCD_strobe(); //EN pulse
}
void LCD_cmd(unsigned char ch) //function to send command
{
IOCLR1 = LCD_PORT;
IOSET1 = ch<<LCD_SHIFT;
IOCLR0 = RS; //RS = 0
IOCLR0 = RW; //RW = 0
LCD_strobe(); //EN pulse
}
void LCD_init(void)
{
PINSEL0 &= 0xFF0FFFFF; //Pins P0.10 and P0.11 as GPIO
PINSEL1 &= 0xFFFFFCFF; //Pin P0.20 as GPIO
PINSEL2 &= 0xFFFFFFF3; //PORT1 as GPIO
IODIR0 = RS | EN | RW; //set the pins as output
IODIR1 = LCD_PORT;
LCD_cmd(0x38); //8bit use both lines
LCD_cmd(0x06); //Entry mode
LCD_cmd(0x0C); //display ON cursor OFF
LCD_cmd(0x01); //Clear display
LCD_cmd(0x80); //cursor at 1st line 1st position
}
void LCD_display(int row, int pos, char *ch)
{
unsigned char temp;
if(row==1)
{
temp = 0x80 | (pos-1); //set cursor at 1st line pos
position
}
else
{
temp = 0xC0 | (pos-1); //set cursor at 2nd line pos
position
}
LCD_cmd(temp);
while(*ch) //while data is valid, display the
string
LCD_data(*ch++);
}
Experiment No. 4
Code:
#include<LPC214x.h>
void delay()
{
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<2000;j++);
}
void main()
{
PINSEL0=0;
IODIR0=1<<12;
while(1)
{
IOSET0=1<<12;
delay();
IOCLR0=1<<12;
delay();
}
}
Experiment No. 5
Code:
#include "LPC214x.h"
#include "stdio.h"
#include "LCD.h"
#include "UART.h"
#define ADC0 1 << 24
#define ADC1 1 << 26
#define ADC_ON 1 << 21
#define ADC_Start 1 << 24
#define ADC_Channel 0x03 // selects the channel 0 and 1 for
ADC0
#define ADC_Divider 0x03 << 8 // selects the divider value
#define ADC_Burst 1 << 16 // burst mode selection
#define ADC_CLKS 0x00 << 17 // number of bits
void adcdelay(unsigned int time)
{
unsigned int i,j;
for(i = 0; i < time ;i++ )
{
for(j = 0; j < 10000 ; j++);
}
}
void ADCInit(void)
{
int i;
i = PINSEL1 ;
i = ( i & 0xF0FFFFFF );
PINSEL1 = (i | (ADC0 | ADC1));
AD0CR = (ADC_Channel | ADC_Divider | ADC_Burst | ADC_CLKS);
// ADC
control register settings
i = AD0CR;
AD0CR = (i | ADC_ON); // start ADC Conversion
}
unsigned int get_adc_voltage(unsigned int volt)
{
unsigned int volt_mv;
volt_mv = ((volt * 3300) / 1024);
return volt_mv ;
}
display_on_lcd(unsigned int value)
{
unsigned int ch, number,i;
value = value << 16;
for(i=0;i<4;i++)
{
ch = number =0;
ch = value & 0xF0000000;
ch = ch >> 28;
if((0 <= ch && ch <= 9))
{
number = ch + 0x30;
}
if((0xA <= ch && ch <= 0xF))
{
number = ch + 0x37;
}
SendData(number);
value = value << 4;
}
}
int main(void)
{
unsigned int ad0_data,ad1_data,voltage;
unsigned char *String="ADC Value (hex)";
UartInit(9600);
ADCInit();
InitLCD();
while(*String)
{
SendData(*String);
String++;
}
while(1)
{
if(AD0STAT & 0x03)
{
// SendInstruction(0xC0);
ad1_data = (AD0DR1 & 0x0000FFC0)>>6;
adcdelay(500);
voltage = get_adc_voltage(ad1_data);
// display_on_lcd(ad1_data);
printf("\r\nADC Voltage (mV) = %4d ",voltage);
}
}
return 0;
}
UART
#include "lpc214x.h"
#include "stdio.h"
void UartInit(unsigned int baudrate) //setting the baud rate
for
115200 baud
{
int i,FDiv;
i = PINSEL0; // read the value of the pins function
i = i & 0xFFFFFFF0; // modify the value
PINSEL0 = (i | 0x05); // set the functionality of the TxD and
Rxd Pin
:01
//set the baud rate
U0LCR = 0x83; // Line control register :DLAB=1 ;
8 bits ; 1 stop bit ; no parity
FDiv = (15000000 / 16 ) / baudrate ; //
U0DLM = FDiv /256; //0x00;
U0DLL = FDiv %256; //0x97;
U0LCR = 0x03; // Line control register :DLAB=0 ;
8 bits ; 1 stop bit ; no parity
U0TER = 0x80;
}
int UART_GetChar(void)
{
while(!(U0LSR & 0x1));
return(U0RBR);
}
int UART_PutChar(unsigned char Ch)
{
if (Ch == '\n') {
while (!(U0LSR & 0x20));
U0THR = 0x0D; /* output CR */
}
while(!(U0LSR & 0x20));
return( U0THR = Ch);
}
int fputc(int ch, FILE *f) {
return (UART_PutChar(ch));
}
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
Experiment No. 6
Code:
#include <includes.h>
#include "func.h"
/*--------------- AAPLICATION STACKS ---------*/
static OS_STK AppTaskStartStk[APP_TASK_STK_SIZE];
static OS_STK AppTaskLED0stk[APP_TASK_STK_SIZE];
static OS_STK AppTaskLED1stk[APP_TASK_STK_SIZE];
static OS_STK AppTaskLED2stk[APP_TASK_STK_SIZE];
/*-------------LOCAL FUNCTION PROTOTYPES--------------*/
/*--------------- A PARENT TASK (MAIN TASK) ---------*/
static void AppTaskStart (void *p_arg); /* Main(Parent)
Task Function */
static void AppTaskCreate(void); /* Separate
Function To Create Child Task(s) */
/*--------------- FOUR CHILDERN TRASKS --------------*/
static void AppTask1 (void *p_arg);
static void AppTask2 (void *p_arg);
static void AppTask0 (void *p_arg);
/*
**************************************************************
***************
****************************
* main()
*
* Description : This is the standard entry point for C code.
It is assumed
that your code will call
* main() once you have performed all necessary initialization.
*
* Argument(s) : none
*
* Return(s) : none
**************************************************************
***************
*****************************/
int main (void)
{
BSP_IntDisAll(); /* Disable all interrupts until
we are ready to accept them */
OSInit(); /* Initialize "uC/OS-II, The
Real-Time Kernel" */
OSTaskCreate(AppTaskStart, /* Create the
starting task i.e. Main Task */
(void *)0,
(OS_STK *)&AppTaskStartStk[APP_TASK_STK_SIZE - 1],
APP_TASK_START_PRIO);
OSStart(); /* Start
multitasking (i.e. give control to uC/OS-II) */
}
/*************************************************************
***************
***********
* AppTaskStart()
*
* Description : The startup task. The uC/OS-II ticker should
only be
initialize once multitasking starts.
*
* Argument(s) : p_arg Argument passed to 'AppTaskStart()' by
'OSTaskCreate()'.
*
* Return(s) : none.
*
* Note(s) : (1) The first line of code is used to prevent a
compiler
warning because 'p_arg' is not
* used. The compiler should not generate any code for this
* statement.
*
* (2) Interrupts are enabled by uCoss-II once the task starts
because
* main() has disbled it.
**************************************************************
***************
***********/
static void AppTaskStart (void *p_arg)
{
p_arg = p_arg; /*Just to avoid compiler Warning
*/
BSP_Init(); /* Initialize BSP functions
*/
UartInit(9600); /*initialise the UART*/
LEDInit(); /*Initialize LED */
AppTaskCreate(); /* Create application tasks
(child tasks) */
while(DEF_TRUE)
{
printf(" \r\nMAIN TASK: Created 3 Tasks. Now going to deep
sleep...");
printf("
\r\n======================================================");
OSTimeDlyHMSM(1, 0, 0, 0);
}
}
/*
**************************************************************
***************
****************************
* AppTaskCreate()
*
* Description : Create the application tasks.
*
* Argument(s) : none.
*
* Return(s) : none.
**************************************************************
***************
****************************
*/
static void AppTaskCreate (void)
{
/* Create Task0 */
OSTaskCreate(AppTask0,
(void *)0,
(OS_STK *)&AppTaskLED0stk[APP_TASK_STK_SIZE - 1],
APP_TASK3_PRIO);
/* Create Task1 */
OSTaskCreate(AppTask1,
(void *)0,
(OS_STK *)&AppTaskLED1stk[APP_TASK_STK_SIZE - 1],
APP_TASK1_PRIO);
/* Create Task2 */
OSTaskCreate(AppTask2,
(void *)0,
(OS_STK *)&AppTaskLED2stk[APP_TASK_STK_SIZE - 1],
APP_TASK2_PRIO);
}
/*************************************************************
***************
***************
* TASK-0 : AppTask0()
*
* Description : This task will toggle LED0 i.e. pin P0.12
*
* Argument(s) : p_arg Argument passed to 'AppTask0()' by
'OSTaskCreate()'.
*
* Return(s) : none.
*
* Note(s) : (1) The first line of code is used to prevent a
compiler
warning
* because 'p_arg' is not used. The compiler should not
generate
* any code for this statement.
**************************************************************
***************
***********/
static void AppTask0 (void *p_arg)
{
p_arg = p_arg; /*Just to avoid
compiler Warning */
while(DEF_TRUE)
{
LEDclr(0);
OSTimeDlyHMSM(0, 0, 0, 100); /* Sleep for a while (500
miliseconds )*/
LEDset(0);
OSTimeDlyHMSM(0, 0, 0, 100); /* Sleep for a while (500
miliseconds )*/
}
}
/*************************************************************
***************
***************
* TASK-1 : AppTask1()
*
* Description : This task will toggle LED1 i.e. pin P0.13
*
* Argument(s) : p_arg Argument passed to 'AppTask1()' by
'OSTaskCreate()'.
*
* Return(s) : none.
*
* Note(s) : (1) The first line of code is used to prevent a
compiler
warning
* because 'p_arg' is not used. The compiler should not
generate
* any code for this statement.
**************************************************************
***************
***********/
static void AppTask1 (void *p_arg)
{
p_arg = p_arg; /* Just to avoid
compiler Warning */
while(DEF_TRUE)
{
LEDclr(1);
OSTimeDlyHMSM(0, 0, 1, 300); /* Sleep for a while (500
miliseconds )*/
LEDset(1);
OSTimeDlyHMSM(0, 0, 1, 300); /* Sleep for a while (500
miliseconds )*/
}
}
/*************************************************************
***************
***************
* TASK-2 : AppTask2()
*
* Description : This task will toggle LED2 i.e. pin P0.14
*
* Argument(s) : p_arg Argument passed to 'AppTask2()' by
'OSTaskCreate()'.
*
* Return(s) : none.
*
* Note(s) : (1) The first line of code is used to prevent a
compiler
warning
* because 'p_arg' is not used. The compiler should not
generate
* any code for this statement.
**************************************************************
***************
***********/
static void AppTask2 (void *p_arg)
{
p_arg = p_arg; /* Just to avoid
compiler Warning */
while(DEF_TRUE)
{
LEDclr(2);
OSTimeDlyHMSM(0, 0, 3, 700); /* Sleep for a while (500
miliseconds )*/
LEDset(2);
OSTimeDlyHMSM(0, 0, 3, 700); /* Sleep for a while (500
miliseconds )*/
}
}
/*************************************************************
*************
**************
* uCos-II Multitasking Application Code
**************************************************************
*************
**************
*
* Program Name : Implementation of Message Queue in uCOS-II
for message
passing
* Description : This program creates total three uCos-II tasks;
out of
which
* main task acts as a parent task and create two child tasks
*
*
* Processor : NXP LPC2148
* Board : Micro-A748
*
**************************************************************
*************
*************/
#include <includes.h>
#include "func.h"
/********** Define Task Priorities ***********/
#define APP_TASK_START_PRIO 4
#define APP_TASK0_PRIO 5
#define APP_TASK1_PRIO 6
#define APP_TASK2_PRIO 7
/*--------------- AAPLICATION STACKS ---------*/
static OS_STK AppTaskStartStk[APP_TASK_STK_SIZE];
static OS_STK AppTask0stk[APP_TASK_STK_SIZE]; /* Create the
required number of stacks need for every child task*/
static OS_STK AppTask1stk[APP_TASK_STK_SIZE];
static OS_STK AppTask2stk[APP_TASK_STK_SIZE];
/*-------------LOCAL FUNCTION PROTOTYPES--------------*/
/*--------------- A PARENT TASK (MAIN TASK) ---------*/
static void AppTaskStart (void *p_arg); /*
Main(Parent) Task Function */
static void AppTaskCreate(void); /* Separate
Function To Create Child Task(s) */
/*--------------- CHILDERN TRASKS --------------*/
static void AppTask0 (void *p_arg);
static void AppTask1 (void *p_arg);
static void AppTask2 (void *p_arg);
OS_EVENT *MsgQ; /* Message Queue */
char *msg[5] = { "welcome", /* Create an array of 5 messages
*/
"JSPM",
"RSCOE",
"E&TC",
"Embedded world" };
/*************************************************************
*************
********************************
* main()
*
* Description : This is the standard entry point for C code.
It is assumed
that your code will call
* main() once you have performed all necessary
initialization.
*
* Argument(s) : none
*
* Return(s) : none
**************************************************************
*************
*******************************/
int main (void)
{
BSP_IntDisAll(); /* Disable all interrupts
until we are ready to accept them */
OSInit(); /* Initialize "uC/OS-II, The
Real-Time Kernel" */
/*************************************************************
*************
**************
* uCos-II Multitasking Application Code
**************************************************************
*************
**************
*
* Program Name : Mailbox implementation in uCos-II for message
passing
* Description : This program creates total three uCos-II tasks;
out of
which
* main task acts as a parent task and create two child
tasks.
* Task0 accepts a message from the PC keyboard through UART
and
* sends the message to TASK1 through Mailbox. Concurrently,
if TASK1
* receives any message through the Mailbox, it displays that
message
* on LCD.
*
*
* Processor : NXP LPC2148
* Board : Micro-A748
*
**************************************************************
*************
*************/
#include <includes.h>
#include "func.h"
/********** Define Task Priorities ***********/
#define APP_TASK_START_PRIO 4
#define APP_TASK0_PRIO 5
#define APP_TASK1_PRIO 6
#define APP_TASK2_PRIO 7
/*--------------- AAPLICATION STACKS ---------*/
static OS_STK AppTaskStartStk[APP_TASK_STK_SIZE];
static OS_STK AppTask0stk[APP_TASK_STK_SIZE]; /* Create the
required number of stacks need for every child task*/
static OS_STK AppTask1stk[APP_TASK_STK_SIZE];
static OS_STK AppTask2stk[APP_TASK_STK_SIZE];
/*-------------LOCAL FUNCTION PROTOTYPES--------------*/
/*--------------- A PARENT TASK (MAIN TASK) ---------*/
static void AppTaskStart (void *p_arg); /*
Main(Parent) Task Function */
static void AppTaskCreate(void); /* Separate
Function To Create Child Task(s) */
/*--------------- CHILDERN TRASKS --------------*/
static void AppTask0 (void *p_arg);
static void AppTask1 (void *p_arg);
static void AppTask2 (void *p_arg);
OS_EVENT *TxMbox;
/*************************************************************
*************
********************************
* main()
*
* Description : This is the standard entry point for C code.
It is assumed
that your code will call
* main() once you have performed all necessary
initialization.
*
* Argument(s) : none
*
* Return(s) : none
**************************************************************
*************
*******************************/
int main (void)
{
BSP_IntDisAll(); /* Disable all interrupts
until we are ready to accept them */
OSInit(); /* Initialize "uC/OS-II, The
Real-Time Kernel" */