53
1 LABORATORY MANUAL EMBEDDED SYSTEMS M. Tech – I Year – II Sem – R13 DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGG. BALAJI INSTITUTE OF TECHNOLOGY & SCIENCE Laknepally, Narsampet, Warangal

MTECH -DSCE - II SEM EMBEDDED SYSTEMS LAB … Lab manuals/M. Tech – I Year – II Sem... · Transmission from Kit and reception from PC using ... Sending messages to mailbox by

Embed Size (px)

Citation preview

1

LABORATORY MANUAL

EMBEDDED SYSTEMS

M. Tech – I Year – II Sem – R13

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGG.

BALAJI INSTITUTE OF TECHNOLOGY & SCIENCE

Laknepally, Narsampet, Warangal

2

M. Tech – I Year – II Sem. (DIGITAL SYSTEMS & COMPU TER ELECTRONICS)

EMBEDDED SYSTEMS LABORATORY (R13) Note: A. The following programs are to be implemented on ARM based Processors/Equivalent. B. Minimum of 10 programs from Part –I and 6 programs from Part -II are to be conducted. PART- I: The following Programs are to be implemented on ARM Processor 1. Simple Assembly Program for

a. Addition | Subtraction | Multiplication | Division b. Operating Modes, System Calls and Interrupts c. Loops, Branches

2. Write an Assembly programs to configure and control General Purpose Input/Output (GPIO) port pins. 3. Write an Assembly programs to read digital values from external peripherals and execute them with the Target board. 4. Program for reading and writing of a file 5. Program to demonstrate Time delay program using built in Timer / Counter feature on IDE environment 6. Program to demonstrates a simple interrupt handler and setting up a timer 7. Program demonstrates setting up interrupt handlers. Press button to generate an interrupt and trace the program flow with debug terminal. 8. Program to Interface 8 Bit LED and Switch Interface 9. Program to implement Buzzer Interface on IDE environment 10. Program to Displaying a message in a 2 line x 16 Characters LCD display and verify the result in debug terminal. 11. Program to demonstrate I2C Interface on IDE environment 12. Program to demonstrate I2C Interface – Serial EEPROM 13. Demonstration of Serial communication. Transmission from Kit and reception from PC using Serial Port on IDE environment use debug terminal to trace the program. 14. Generation of PWM Signal 15. Program to demonstrate SD-MMC Card Interface. PART- II: Write the following programs to understand the use of RTOS with ARM Processor on IDE Environment using ARM Tool chain and Library: 1. Create an application that creates two tasks that wait on a timer whilst the main task loops. 2. Write an application that creates a task which is scheduled when a button is pressed, which illustrates the use of an event set between an ISR and a task 3. Write an application that Demonstrates the interruptible ISRs(Requires timer to have higher priority than external interrupt button) 4. a).Write an application to Test message queues and memory blocks. b).Write an application to Test byte queues 5. Write an application that creates two tasks of the same priority and sets the time slice period to illustrate time slicing. Interfacing Programs: 6. Write an application that creates a two task to Blinking two different LEDs at different timings 7. Write an application that creates a two task displaying two different messages in LCD display in two lines. 8. Sending messages to mailbox by one task and reading the message from mailbox by another task. 9. Sending message to PC through serial port by three different tasks on priority Basis. 10. Basic Audio Processing on IDE environment.

3

1. Simple Assembly Program for a. Addition | Subtraction | Multiplication | Division b. Operating Modes, System Calls and Interrupts c. Loops, Branches

Addition of 4 numbers : Total = A+B+C+D Program: start MOV r0, r1; Make the first number the subtotal ADD r0, r0, r2; Add the second number to the subtotal ADD r0, r0, r3; Add the third number to the subtotal ADD r0, r0, r4; Add the fourth number to the subtotal stop B ; stop

SUBTRACTION of 4 numbers : Total = A-B-C-D

Program: start MOV r0, r1; Make the first number the subtotal SUB r0, r0, r2; Add the second number to the subtotal SUB r0, r0, r3; Add the third number to the subtotal SUB r0, r0, r4; Add the fourth number to the subtotal stop B ; stop

MULTIPLIATION of 4 numbers : Total = A X B X C X D Program: start MOV r0, r1; Make the first number the subtotal MUL r0, r0, r2; Add the second number to the subtotal MUL r0, r0, r3; Add the third number to the subtotal MUL r0, r0, r4; Add the fourth number to the subtotal stop B ; stop

DIVISION of 4 numbers : Total = A/B/C/D Program: start MOV r0, r1; Make the first number the subtotal DIV r0, r0, r2; Add the second number to the subtotal DIV r0, r0, r3; Add the third number to the subtotal DIV r0, r0, r4; Add the fourth number to the subtotal stop B ; stop

4

Write an assembly language program to compute ... 4x2 + 3x, . if xis stored in r1. Store the result in r0

start MUL r0, r1, r1; result <--x * x LDR r2, =4; tmp <--4 MUL r0, r2, r0; result <--4 * x * x LDR r2, =3; tmp <--3 MUL r2, r1, r2; tmp <--x * tmp ADD r0, r0, r2; result <--result + tmp stop B stop

II) Computing the greatest common divisor of two numbers using Euclid's GCD algorithm using branch instructions.

MOV R0, #40 ; R0 is a MOV R1, #25 ; R1 is b again CMP R0, R1 SUBGT R0, R0, R1 SUBLT R1, R1, R0 BNE again halt B halt

b)write a program to add numbers in an array using loop instruction

addInts MOV R4, #0 addLoop LDR R2, [R0] ADD R4, R4, R2 ADD R0, R0, #4 SUBS R1, R1, #1 BNE addLoop

5

2. Write an Assembly programs to configure and control General Purpose Input/Output (GPIO) port pins. /* Examples Program For "CP-JR ARM7 USB-LPC2148" */

/* Target MCU : Philips ARM7-LPC2148 */

/* : X-TAL : 12.00 MHz */

/* : Run Speed 60.00 MHz (With PLL) */

/* : PLL Setup = M(5),P(2) */

/* : VPB Clock = CPU Clock = 60.00 MHz */

/* Keil Editor : uVision3 V3.03a */

/* Compiler : Keil CARM V2.50a */

/* Function : Example Used Fast GPIO Function */

/************************************************** **/

// Connect P1.24 to LED For Test ON / OFF (Blink)

#include "LPC214x.H" // LPC2148 MPU Register

/* pototype section */

void delay(unsigned long int); // Delay Time Function

int main(void)

{

// Enable GPIO Function

//SCS |= 0x00000001; // Enable GPIO0 = Fast GPIO Mode

SCS |= 0x00000002; // Enable GPIO1 = Fast GPIO Mode

// xxxx xxx1 xxxx xxxx xxxx xxxx xxxx xxxx

6

FIO1MASK = 0xFEFFFFFF; // Enable GPIO1[24] = Fast GPIO Mode

FIO1DIR = 0x01000000; // Set GPIO-1[24] = Output

FIO1SET = 0x01000000; // Set GPIO-1[24] Output Pin(OFF LED)

// Loop Test Output GPIO1.24

while(1) // Loop Continue

{

FIO1CLR = 0x01000000; // Clear Output GPIO1[24] Pin (ON LED)

delay(1000000); // Display Delay

FIO1SET = 0x01000000; // Set Output GPIO1[24] Pin (OFF LED)

delay(1000000); // Display Delay

}

}

/***********************/

/* Delay Time Function */

/* 1-4294967296 */

/***********************/

void delay(unsigned long int count1)

{

while(count1 > 0) {count1--;} // Loop Decrease Counter

}

7

3. Write an Assembly programs to read digital values from external peripherals and execute them with the Target board. ( write a program to read digital values from eternal peripherals and execute them with the target board.)

/* Examples Program For "CP-JR ARM7 USB-LPC2148" */

/* Target MCU : Philips ARM7-LPC2148 */

/* : X-TAL : 12.00 MHz */

/* : Run Speed 60.00 MHz (With PLL) */

/* : PLL Setup = M(5),P(2) */

/* : VPB Clock = CPU Clock = 60.00 MHz */

/* Keil Editor : uVision3 V3.03a */

/* Compiler : Keil CARM V2.50a */

/* Function : Example Used DAC Generate Sinewave */

/************************************************** **/

// P0.25 = DAC Output = Sinewave Signal

#include "LPC214x.H" // LPC2148 MPU Register

#include <stdio.h> // For Used Function printf

int main(void)

{

const static unsigned short table_sine[64] = // Sine Function Table(12Bit)

{

0x07FF, 0x08C8, 0x098E, 0x0A51,

0x0B0F, 0x0BC4, 0x0C71, 0x0D12,

0x0DA7, 0x0E2E, 0x0EA5, 0x0F0D,

0x0F63, 0x0FA6, 0x0FD7, 0x0FF5,

0x0FFF, 0x0FF5, 0x0FD7, 0x0FA6,

0x0F63, 0x0F0D, 0x0EA5, 0x0E2E,

0x0DA7, 0x0D12, 0x0C71, 0x0BC4,

8

0x0B0F, 0x0A51, 0x098E, 0x08C8,

0x07FF, 0x0736, 0x0670, 0x05AD,

0x04EF, 0x043A, 0x038D, 0x02EC,

0x0257, 0x01D0, 0x0159, 0x00F1,

0x009B, 0x0058, 0x0027, 0x0009,

0x0000, 0x0009, 0x0027, 0x0058,

0x009B, 0x00F1, 0x0159, 0x01D0,

0x0257, 0x02EC, 0x038D, 0x043A,

0x04EF, 0x05AD, 0x0670, 0x0736

};

int i = 0; // Pointer

// Initial DAC (GPIO-0.25) By Set PINSEL1[19:18=10]

// xxxx xxxx xxxx 10xx xxxx xxxx xxxx xxxx

PINSEL1 &= 0xFFF3FFFF; // Select DAC Pin Connect P0.25

PINSEL1 |= 0x00080000;

while(1) // Loop Continue

{

// 10-Bit Data = xxxx xxxx xxxx xxxx DDDD DDDD DDxx xxxx

DACR = ((table_sine[i]/4) << 6); // Update DAC Sine Output

//DACR = ((i * 16) << 6); // Update DAC SAW Output

i++; // Next Pointer

i &= 0x3F; // 0..63

}

}

9

5. Program to demonstrate Time delay program using built in Timer / Counter feature on IDE environment. (demonstrate Time delay program using built in timer/Counter feature on IDE environment) Timer functionality:

#include <LPC214X.H>

main()

{

// T0PR = 0x00000003;

PINSEL0=0x00000020;

T0CCR=0x00000004;

T0TCR = 0x00000001;

//T0MCR = 0x00000020;

//T0MR1 = 0X0000000f;

while(1);

}

Counter functionality:

#include <LPC214X.H>

void main()

{

PINSEL0=0X00000020;

T0TCR=0X01;

T0CTCR=0X03;

}

10

7. Program to demonstrate setting up interrupt handlers. Press button to generate an interrupt and trace the program flow with debug terminal.

/************************************************** ************************************** * File Name : main.c * * * * Purpose : - This program describes the usage of GPIO pins * * to activate external interrupt pins * * * * Author : NTIL Engineers * * * * Date : Monday, November 01, 2004 * * * * Copyright or License : Monday, November 01, 2004 NTIL * * * * Algorithms : NONE * * * * Hardware notes : arm7TDMI controller * * * * * * Microcontroller : PHILIPS ARM 2104 * * * * Operating Speed : 14.7456 MHz * * * ----------------------------------------------------------------------------------------- * * * Revision History : Version 1.1.1 * * * ----------------------------------------------------------------------------------------- * * * Table Of Contents : >>>main * * >>>int0ISR * * >>>int1ISR * * >>>int2ISR * * >>>ledOn * * >>>ledOff * * >>>ledInit * * >>>delay * * * * ....HEADER FILES.... * * * * >>>LPC210x.h *

11

* >>>irq.h * * * * Description : Building and Running * * -------------------- * * To run from FLASH: * * make flash * * connect to Philips UTILITY * * Download the program * * * * To run from RAM: * * make ram * * connect to GDB * * Download the program * * * *************************************************** **************************************/ #include "LPC210x.h" #include "irq.h" /* Interrupt Servce Routine for Ecternal Interrupt0 */ static void int0ISR(void) __attribute__ ((interrupt ("IRQ"))); /* Interrupt Servce Routine for Ecternal Interrupt1 */ static void int1ISR(void) __attribute__ ((interrupt ("IRQ"))); /* Interrupt Servce Routine for Ecternal Interrupt2 */ static void int2ISR(void) __attribute__ ((interrupt ("IRQ"))); /* To make LED's On */ static void ledOn(unsigned long int led); /* To make LED's Off */ static void ledOff(unsigned long int led); /* Interrupt Servce Routine for Ecternal Interrupt0 */ static void int0ISR(void); /* Interrupt Servce Routine for Ecternal Interrupt1 */ static void int1ISR(void); /* Interrupt Servce Routine for Ecternal Interrupt2 */ static void int2ISR(void);

12

/* Configuring P0.0-P0.7 for LED's */ static void ledInit(void); /* To create Delay */ static void delay(void); /************************************************** ************************************** * * * Function name : Main * * * * Arguments : void * * * * Return Values : integer type * * * * Description : Program starts here * * * *************************************************** *************************************/ int main(void) { /* Configuring PINSEL0 register for Ext. Interrupts */ PINSEL0=0XA0000000; PINSEL1=0X00000001; /* Int0 is an IRQ interrupt */ VICIntSelect &= ~0x4000; /* Enable Int0 interrupt */ VICIntEnable = 0x4000; /* Use slot 0 for Int0 interrupt */ VICVectCntl0 = 0x2E; /* Set the address of ISR for slot 0 */ VICVectAddr0 = (unsigned int)int0ISR; /* Int1 interrupt is an IRQ interrupt */ VICIntSelect &= ~0x8000; /* Enable Int1 interrupt */ VICIntEnable = 0x8000; /* Use slot 1 for Int1 interrupt */

13

VICVectCntl1 = 0x2F; /* Set the address of ISR for slot 1 */ VICVectAddr1 = (unsigned int)int1ISR; /* Int2 interrupt is an IRQ interrupt */ VICIntSelect &= ~0x10000; /* Enable Int2 interrupt */ VICIntEnable = 0x10000; /* Use slot 2 for Int2 interrupt */ VICVectCntl2 = 0x30; /* Set the address of ISR for slot 2 */ VICVectAddr2 = (unsigned int)int2ISR; /* Function to Initialize LEDs */ ledInit(); /* Macro for Handling IRQ */ enable_irq(); while (1) { } } /* End of MAIN Function */ /************************************************** ************************************** * * * Function name : int0ISR * * * * Arguments : void * * * * Return Values : void * * * * Description : Service Routine to Handle Int0 IRQ * * * *************************************************** *************************************/ static void int0ISR(void) { ledOn(0x00000001); delay();

14

ledOff(0x00000001); /* Clear the Int0 interrupt */ EXTINT=0X01; /* Update VIC priorities */ VICVectAddr = 0; } /* End of 'int0ISR' function */ /************************************************** ************************************** * * * Function name : int1ISR * * * * Arguments : void * * * * Return Values : void * * * * Description : Service Routine to Handle Int1 IRQ * * * *************************************************** *************************************/ static void int1ISR(void) { ledOn(0x00000002); delay(); ledOff(0x00000002); /* Clear the Int1 interrupt */ EXTINT=0X02; /* Update VIC priorities */ VICVectAddr = 1; } /* End of 'int1ISR' function */ /************************************************** ************************************** * * * Function name : int2ISR * * * * Arguments : void * * *

15

* Return Values : void * * * * Description : Service Routine to Handle Int2 IRQ * * * *************************************************** *************************************/ static void int2ISR(void) { ledOn(0x00000004); delay(); ledOff(0x00000004); /* Clear the Int2 interrupt */ EXTINT=0X04; /* Update VIC priorities */ VICVectAddr = 2; } /* End of 'int2ISR' function */ /************************************************** ************************************** * * * Function name : ledOn * * * * Arguments : unsigned long int * * * * Return Values : void * * * * Description : To make LEDs On * * * *************************************************** *************************************/ static void ledOn(unsigned long int led) { /* Makes LED ON */ IOCLR = led; } /* End of 'ledOn' function */ /************************************************** ************************************** * * * Function name : ledOff * * *

16

* Arguments : unsigned long int * * * * Return Values : void * * * * Description : To Make LEDs Off * * * *************************************************** *************************************/ static void ledOff(unsigned long int led) { /* Makes LED Off */ IOSET = led; } /* End of 'ledOff' function */ /************************************************** ************************************** * * * Function name : ledInit * * * * Arguments : void * * * * Return Values : void * * * * Description : To Configure I/O lines for LEDs * * * *************************************************** *************************************/ static void ledInit() { /* Making P0.0-P0.7 as Output Lines */ IODIR |= 0x000000FF; IOSET = 0x000000FF; } /* End of 'ledInit' function */ /************************************************** ************************************** * * * Function name : delay * * * * Arguments : void * * * * Return Values : void * * *

17

* Description : To create Delay * * * *************************************************** *************************************/ static void delay(void) { int i,j; for(i=0;i<500;i++) for(j=0;j<100;j++); } /* End of 'delay' function */ /***********************End of File**************** *********************/

18

8. Write a Program to Interface 8 Bit LED and Switch Interface.

#include<lpc214x.h>

///////////////////////////// Delay Function //////////////////////////

void main_delay( unsigned int value )

{

unsigned int ui_temp1,ui_temp2; // Delay Variables

for(ui_temp1=0;ui_temp1<value;ui_temp1++) //Delay loop

for(ui_temp2=0;ui_temp2<5000;ui_temp2++); //Delay loop

}

int main()

{

IODIR0 = 0xffffffff;

IODIR1 = 0xffffffff;

while(1)

{

IOSET0= 0xffffffff;

IOSET1= 0xffffffff;

main_delay(5000);

IOCLR0= 0xffffffff;

IOCLR1= 0xffffffff;

main_delay(5000);

}

}

19

9. Program to implement Buzzer Interface on IDE environment

#include<lpc214x.h>

#define buzz 0x00000001

#define buzz_on IOCLR0=buzz

#define buzz_off IOSET0=buzz

void delay( unsigned int value )

{

unsigned int ui_temp1,ui_temp2;

for(ui_temp1=0;ui_temp1<value;ui_temp1++)

for(ui_temp2=0;ui_temp2<5000;ui_temp2++);

}

int main()

{

IODIR0 |=buzz;

while(1)

{

buzz_on;

delay(2000);

buzz_off;

delay(2000);

}

}

20

10. Program to Displaying a message in a 2 line x 16 Characters LCD display and verify the result in debug terminal. #include<lpc214x.h> // LPC2148 MPU Registers // Define LCD PinIO Mask #define LCD_RS 0x00010000 // P1.16(0000 0000 0000 000x 0000 0000 0000 0000) #define LCD_EN 0x00020000 // P1.17(0000 0000 0000 00x0 0000 0000 0000 0000) #define LCD_D4 0x00040000 // P1.18(0000 0000 0000 0x00 0000 0000 0000 0000) #define LCD_D5 0x00080000 // P1.19(0000 0000 0000 x000 0000 0000 0000 0000) #define LCD_D6 0x00100000 // P1.20(0000 0000 000x 0000 0000 0000 0000 0000) #define LCD_D7 0x00200000 // P1.21(0000 0000 00x0 0000 0000 0000 0000 0000) ///////////////////////////// Delay Function ////////////////////////// void main_delay( unsigned int value ) { unsigned int ui_temp1,ui_temp2; // Delay Variables for(ui_temp1=0;ui_temp1<value;ui_temp1++) //Delay loop for(ui_temp2=0;ui_temp2<5000;ui_temp2++); //Delay loop } /////////////////////// LCD Command Sending Function///////////////////// void lcd_cmd(unsigned char val) { unsigned int lcd_ch; // LCD Initial Data unsigned int lcd_i; // LCD Initial Delay Count IOCLR1 = LCD_RS ; // RS = 0 lcd_ch=((val>>4)&0x0F); // Strobe 4-Bit High-Nibble to LCD IOCLR1 = (LCD_D7|LCD_D6|LCD_D5|LCD_D4); // Reset 4-Bit Pin Data IOSET1 = (lcd_ch<<18); // Data Send to Respective Pins IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_i=0;lcd_i<10000;lcd_i++); //delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) lcd_ch=(val&0x0F); // Strobe 4-Bit Low-Nibble to LCD IOCLR1 = (LCD_D7|LCD_D6|LCD_D5|LCD_D4); // Reset 4-Bit Pin Data IOSET1 = (lcd_ch<<18); // EN,0,RW,RS:DDDD:0000:0000:0000:0000:0000:0000 IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_i=0;lcd_i<10000;lcd_i++); //delay

21

IOCLR1 = LCD_EN ; // EN = 0 (Disable) for (lcd_i=0;lcd_i<10000;lcd_i++); //delay } ///////////////////////LCD Single Charecter Display Function///////////////////////////////////////// void lcd_data(unsigned char val) { unsigned int lcd_ch; // LCD Initial Data unsigned int lcd_i; // LCD Initial Delay Count IOSET1 = LCD_RS ; // RS = 1 lcd_ch=((val>>4)&0x0F); // Strobe 4-Bit High-Nibble to LCD IOCLR1 = (LCD_D7|LCD_D6|LCD_D5|LCD_D4); // Reset 4-Bit Pin Data IOSET1 = (lcd_ch<<18); // Data Send to Respective Pins IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_i=0;lcd_i<10000;lcd_i++); // delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) lcd_ch=(val&0x0F); // Strobe 4-Bit Low-Nibble to LCD IOCLR1 = (LCD_D7|LCD_D6|LCD_D5|LCD_D4); // Reset 4-Bit Pin Data IOSET1 = (lcd_ch<<18); // Data Send to Respective Pins IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_i=0;lcd_i<10000;lcd_i++); //delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) for (lcd_i=0;lcd_i<10000;lcd_i++); //delay } ///////////////////// LCD String Display Function /////////////////// void lcd_puts(unsigned char* str) { while(*str) // Cheack Data is there or not lcd_data(*str++); //LCD Single Charecter Display Function } ///////////////// LCD Initialization Function //////////////////// void lcd_init() { unsigned int lcd_main_i; // LCD Initial Delay Count PINSEL2 |= 0x00000000; // GPIO1[31..16] = I/O Function IODIR1 |= 0x003F0000 ; // GPIO1[21..16] = OUT Direction for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Power-On Delay (15 mS) IOCLR1 = ((LCD_D7|LCD_D6|LCD_D5|LCD_D4|LCD_RS|LCD_EN)); // Reset (RS,EN,4-Bit Data) Pin IOSET1 = (LCD_D5|LCD_D4); // Set D4,D5

22

IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay IOCLR1 = ((LCD_D7|LCD_D6|LCD_D5|LCD_D4|LCD_RS|LCD_EN)); // Reset (RS,EN,4-Bit Data) Pin IOSET1 = (LCD_D5|LCD_D4); // Set D4,D5 IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // delay IOCLR1 = ((LCD_D7|LCD_D6|LCD_D5|LCD_D4|LCD_RS|LCD_EN)); // Reset (RS,RW,EN,4-Bit Data) Pin IOSET1 = (LCD_D5|LCD_D4); // Set D4,D5 IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay IOCLR1 = ((LCD_D7|LCD_D6|LCD_D5|LCD_D4|LCD_RS|LCD_EN)); // Reset (RS,RW,EN,4-Bit Data) Pin IOSET1 = (LCD_D5); // Set D4,D5 IOSET1 = LCD_EN ; // EN = 1 (Enable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay IOCLR1 = LCD_EN ; // EN = 0 (Disable) for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Delay lcd_cmd(0x28); // LCD 4-Bit Mode lcd_cmd(0x0c); // LCD Courser Blinking Stop lcd_cmd(0x06); // LCD Shift Display Right lcd_cmd(0x01); // LCD Clear Display for (lcd_main_i=0;lcd_main_i<10000;lcd_main_i++); // Wait Command Ready } ///////////////////////// MAIN Function //////////////////// int main() { lcd_init(); // Initialization lcd_puts("LPC2148 LCD DEMO"); //Display Text lcd_cmd(0xc0); // Courser goto Secong Line lcd_puts(" Program "); //Display Text main_delay(5000); //delay lcd_cmd(0x01); // LCD Clear Display lcd_puts(" ELEGANT"); //Display Text lcd_cmd(0xc0); // Courser goto Secong Line lcd_puts(" Embedded Solu"); //Display Text while(1); // Continuous Loop }

23

12. Program to demonstrate I2C Interface – Serial EEPROM #include <LPC214x.H> /* LPC214x definitions */ #include "LCD.h" #include "i2c.h" unsigned char buf_size; unsigned char buf[70],bu[70],COUNT; int main (void) { unsigned char val,val1,val2,val3,val4,val5,val6,val7,val8,val9; lcd_init(); lcd_cmd(0x01); lcd_puts("EEPROM WITH "); lcd_cmd(0xc0); lcd_puts(" LPC2148"); i2c_lpc_init(1); // Initialize I2C i2c_delay(10000); i2c_delay(10000); lcd_cmd(0x01); lcd_puts("DATA WRITING..."); eeprom_write(0,'9'); //WRITING THE DAT TO EEPROM eeprom_write(1,'3'); eeprom_write(2,'9'); eeprom_write(3,'6'); eeprom_write(4,'6'); eeprom_write(5,'7'); eeprom_write(6,'1'); eeprom_write(7,'5'); eeprom_write(8,'4'); eeprom_write(9,'1'); i2c_delay(10000); i2c_delay(10000); lcd_cmd(0x01); lcd_puts("DATA READING..."); i2c_delay(10000); i2c_delay(10000); val=eeprom_read(0); //READING DATA FROM EEPROM val1=eeprom_read(1); val2=eeprom_read(2); val3=eeprom_read(3);

24

val4=eeprom_read(4); val5=eeprom_read(5); val6=eeprom_read(6); val7=eeprom_read(7); val8=eeprom_read(8); val9=eeprom_read(9); lcd_cmd(0x01); lcd_data(val); //DISPLAY THE DATA ON LCD lcd_data(val1); lcd_data(val2); lcd_data(val3); lcd_data(val4); lcd_data(val5); lcd_data(val6); lcd_data(val7); lcd_data(val8); lcd_data(val9); }

25

13. Demonstration of Serial communication. Transmission from Kit and reception from PC using Serial Port on IDE environment use debug terminal to trace the program. #include<lpc214x.h> // LPC2148 MPU Registers void uart0_init() // Uart0 Initialization Function { PINSEL0 =0x00000005; // GPIO1[1,0] = Uart Function // Select P0.0 = TxD(UART0) // Select P0.1 = RxD(UART0) U0LCR =0X80; // Enable Programming of Divisor Latches U0DLL =97; // Program Divisor Latch(391) for 9600 Baud U0LCR =0X03; // Data Bit = 8 Bit } void uart0_putch(unsigned char val) //Write character to UART0 { while(!(U0LSR & 0x20)); // Wait TXD Buffer Empty U0THR =val; // Write Character } unsigned char uart0_getch() //Read character from UART0 { while(!(U0LSR & 0x01)); // Wait RXD Receive Data Ready return(U0RBR); // Get Receice Data & Return } void uart0_puts( char *stringptr) //String Display Function { while (*stringptr) // Cheack Data is there or not uart0_putch(*stringptr++); //Write character to UART0 } ///////////////////////// MAIN Function //////////////////// int main() { unsigned char i; //variable for recive data from uart buffer uart0_init(); // Uart0 Initialization Function uart0_putch(13); //next line command 13,10 uart0_putch(10); uart0_puts("ELEGANT EMBEDDED SOLUTIONS"); //String Display on Hyperterminal uart0_putch(13); //next line command 13,10 uart0_putch(10); uart0_puts("***** UART0 DEMO PROGRAM *****"); // String Display on Hyperterminal uart0_putch(13); //next line command 13,10 uart0_putch(10); uart0_puts("***** Enter Some Test *****"); //String Display on Hyperterminal uart0_putch(13); //next line command 13,10 uart0_putch(10); while(1) // Continuous Loop { i=uart0_getch(); //get data from uart uart0_putch(i); //print data on Hyperterminal } }

26

14. Generation of PWM Signal #include <lpc214x.h> //#define PLOCK 0x00000400 #define PWMPRESCALE 60 //60 PCLK cycles to increment TC by 1 i.e 1 Micro-second void initPWM(void); void main_delay( unsigned int value ) { unsigned int ui_temp1,ui_temp2; for(ui_temp1=0;ui_temp1<value;ui_temp1++) for(ui_temp2=0;ui_temp2<5000;ui_temp2++); } int main(void) { initPWM(); //Initialize PWM //IO0DIR = 0x1; This is not needed! //Also by default all pins are configured as Inputs after MCU Reset. while(1) { PWMMR1 = 1250; //T-ON=25% , Hence 25% Bright PWMLER = (1<<1); //Update Latch Enable bit for PWMMR1 main_delay(2000); PWMMR1 = 2500; //50% Bright PWMLER = (1<<1); main_delay(2000); PWMMR1 = 3750; //75% Bright PWMLER = (1<<1); main_delay(2000); PWMMR1 = 5000; //100% Bright PWMLER = (1<<1); main_delay(2000); } } void initPWM(void) { /*Assuming that PLL0 has been setup with CCLK = 60Mhz and PCLK also = 60Mhz.*/ /*This is a per the Setup & Init Sequence given in the tutorial*/ PINSEL0 = (1<<1); // Select PWM1 output for Pin0.0

27

PWMPCR = 0x0; //Select Single Edge PWM - by default its single Edged so this line can be removed PWMPR = PWMPRESCALE-1; // 1 micro-second resolution PWMMR0 = 5000; // 10ms period duration PWMMR1 = 2500; // 2.5ms - pulse duration i.e width (Brigtness level) PWMMCR = (1<<1); // Reset PWMTC on PWMMR0 match PWMLER = (1<<1) | (1<<0); // update MR0 and MR1 PWMPCR = (1<<9); // enable PWM output PWMTCR = (1<<1) ; //Reset PWM TC & PR //Now , the final moment - enable everything PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode //PWM Generation goes active now - LED must be 25% Bright after Reset!! //Now you can get the PWM output at Pin P0.0! } /* #include<lpc214x.h> #define PLOCK 0x00000400 #define PWMPRESCALE 60 int main() { PINSEL1 =0x00000400; //PWM channel 5 is selected }*/

28

15. Program to demonstrate SD-MMC Card Interface. /*------------------------------------------------- --------------------------- * RL-ARM - FlashFS *---------------------------------------------------------------------------- * Name: SD_FILE.C * Purpose: File manipulation example program *---------------------------------------------------------------------------- * This code is part of the RealView Run-Time Library. * Copyright (c) 2004-2011 KEIL - An ARM Company. All rights reserved. *---------------------------------------------------------------------------*/ #include <RTL.h> /* RTL kernel functions & defines */ #include <stdio.h> /* standard I/O .h-file */ #include <ctype.h> /* character functions */ #include <string.h> /* string and memory functions */ #include "File_Config.h" #include "SD_File.h" /* Command Functions */ static void cmd_capture (char *par); static void cmd_type (char *par); static void cmd_rename (char *par); static void cmd_copy (char *par); static void cmd_delete (char *par); static void cmd_dir (char *par); static void cmd_format (char *par); static void cmd_help (char *par); static void cmd_fill (char *par); /* Local constants */ static const char intro[] = "\n\n\n\n\n\n\n\n" "+-----------------------------------------------------------------------+\n" "| SD/MMC Card File Manipulation example |\n"; static const char help[] = "+ command ------------------+ function ---------------------------------+\n" "| CAP \"fname\" [/A] | captures serial data to a file |\n" "| | [/A option appends data to a file] |\n" "| FILL \"fname\" [nnnn] | create a file filled with text |\n" "| | [nnnn - number of lines, default=1000] |\n" "| TYPE \"fname\" | displays the content of a text file |\n" "| REN \"fname1\" \"fname2\" | renames a file 'fname1' to 'fname2' |\n" "| COPY \"fin\" [\"fin2\"] \"fout\"| copies a file 'fin' to 'fout' file |\n" "| | ['fin2' option merges 'fin' and 'fin2'] |\n" "| DEL \"fname\" | deletes a file |\n" "| DIR \"[mask]\" | displays a list of files in the directory |\n" "| FORMAT [label [/FAT32]] | formats Flash Memory Card |\n"

29

"| | [/FAT32 option selects FAT32 file system] |\n" "| HELP or ? | displays this help |\n" "+---------------------------+-------------------------------------------+\n"; static const SCMD cmd[] = { "CAP", cmd_capture, "TYPE", cmd_type, "REN", cmd_rename, "COPY", cmd_copy, "DEL", cmd_delete, "DIR", cmd_dir, "FORMAT", cmd_format, "HELP", cmd_help, "FILL", cmd_fill, "?", cmd_help }; #define CMD_COUNT (sizeof (cmd) / sizeof (cmd[0])) /* Local variables */ static char in_line[160]; /* Local Function Prototypes */ static void dot_format (U64 val, char *sp); static char *get_entry (char *cp, char **pNext); static void init_card (void); /*------------------------------------------------- --------------------------- * Process input string for long or short name entry *---------------------------------------------------------------------------*/ static char *get_entry (char *cp, char **pNext) { char *sp, lfn = 0, sep_ch = ' '; if (cp == NULL) { /* skip NULL pointers */ *pNext = cp; return (cp); } for ( ; *cp == ' ' || *cp == '\"'; cp++) { /* skip blanks and starting " */ if (*cp == '\"') { sep_ch = '\"'; lfn = 1; } *cp = 0; } for (sp = cp; *sp != CR && *sp != LF; sp++) { if ( lfn && *sp == '\"') break; if (!lfn && *sp == ' ' ) break; } for ( ; *sp == sep_ch || *sp == CR || *sp == LF; sp++) { *sp = 0; if ( lfn && *sp == sep_ch) { sp ++; break; } }

30

*pNext = (*sp) ? sp : NULL; /* next entry */ return (cp); } /*------------------------------------------------- --------------------------- * Print size in dotted fomat *---------------------------------------------------------------------------*/ static void dot_format (U64 val, char *sp) { if (val >= (U64)1e9) { sp += sprintf (sp,"%d.",(U32)(val/(U64)1e9)); val %= (U64)1e9; sp += sprintf (sp,"%03d.",(U32)(val/(U64)1e6)); val %= (U64)1e6; sprintf (sp,"%03d.%03d",(U32)(val/1000),(U32)(val%1000)); return; } if (val >= (U64)1e6) { sp += sprintf (sp,"%d.",(U32)(val/(U64)1e6)); val %= (U64)1e6; sprintf (sp,"%03d.%03d",(U32)(val/1000),(U32)(val%1000)); return; } if (val >= 1000) { sprintf (sp,"%d.%03d",(U32)(val/1000),(U32)(val%1000)); return; } sprintf (sp,"%d",(U32)(val)); } /*------------------------------------------------- --------------------------- * Capture serial data to file *---------------------------------------------------------------------------*/ static void cmd_capture (char *par) { char *fname,*next; BOOL append,retv; FILE *f; fname = get_entry (par, &next); if (fname == NULL) { printf ("\nFilename missing.\n"); return; } append = __FALSE; if (next) { par = get_entry (next, &next); if ((strcmp (par, "/A") == 0) ||(strcmp (par, "/a") == 0)) { append = __TRUE; } else {

31

printf ("\nCommand error.\n"); return; } } printf ((append) ? "\nAppend data to file %s" : "\nCapture data to file %s", fname); printf("\nPress ESC to stop.\n"); f = fopen (fname,append ? "a" : "w"); /* open a file for writing */ if (f == NULL) { printf ("\nCan not open file!\n"); /* error when trying to open file */ return; } do { retv = getline (in_line, sizeof (in_line)); fputs (in_line, f); } while (retv == __TRUE); fclose (f); /* close the output file */ printf ("\nFile closed.\n"); } /*------------------------------------------------- --------------------------- * Create a file and fill it with some text *---------------------------------------------------------------------------*/ static void cmd_fill (char *par) { char *fname, *next; FILE *f; int i,cnt = 1000; fname = get_entry (par, &next); if (fname == NULL) { printf ("\nFilename missing.\n"); return; } if (next) { par = get_entry (next, &next); if (sscanf (par,"%d", &cnt) == 0) { printf ("\nCommand error.\n"); return; } } f = fopen (fname, "w"); /* open a file for writing */ if (f == NULL) { printf ("\nCan not open file!\n"); /* error when trying to open file */ return; } for (i = 0; i < cnt; i++) { fprintf (f, "This is line # %d in file %s\n", i, fname); } fclose (f); /* close the output file */ printf ("\nFile closed.\n");

32

} /*------------------------------------------------- --------------------------- * Read file and dump it to serial window *---------------------------------------------------------------------------*/ static void cmd_type (char *par) { char *fname,*next; FILE *f; int ch; fname = get_entry (par, &next); if (fname == NULL) { printf ("\nFilename missing.\n"); return; } printf("\nRead data from file %s\n",fname); f = fopen (fname,"r"); /* open the file for reading */ if (f == NULL) { printf ("\nFile not found!\n"); return; } while ((ch = fgetc (f)) != EOF) { /* read the characters from the file */ putchar (ch); /* and write them on the screen */ } fclose (f); /* close the input file when done */ printf ("\nFile closed.\n"); } /*------------------------------------------------- --------------------------- * Rename a File *---------------------------------------------------------------------------*/ static void cmd_rename (char *par) { char *fname,*fnew,*next,dir; fname = get_entry (par, &next); if (fname == NULL) { printf ("\nFilename missing.\n"); return; } fnew = get_entry (next, &next); if (fnew == NULL) { printf ("\nNew Filename missing.\n"); return; } if (strcmp (fname,fnew) == 0) { printf ("\nNew name is the same.\n"); return; }

33

dir = 0; if (*(fname + strlen(fname) - 1) == '\\') { dir = 1; } if (frename (fname, fnew) == 0) { if (dir) { printf ("\nDirectory %s renamed to %s\n",fname,fnew); } else { printf ("\nFile %s renamed to %s\n",fname,fnew); } } else { if (dir) { printf ("\nDirectory rename error.\n"); } else { printf ("\nFile rename error.\n"); } } } /*------------------------------------------------- --------------------------- * Copy a File *---------------------------------------------------------------------------*/ static void cmd_copy (char *par) { char *fname,*fnew,*fmer,*next; FILE *fin,*fout; U32 cnt,total; char buf[512]; BOOL merge; fname = get_entry (par, &next); if (fname == NULL) { printf ("\nFilename missing.\n"); return; } fmer = get_entry (next, &next); if (fmer == NULL) { printf ("\nNew Filename missing.\n"); return; } fnew = get_entry (next, &next); if (fnew != NULL) { merge = __TRUE; } else { merge = __FALSE; fnew = fmer; }

34

if ((strcmp (fname,fnew) == 0) || (merge && strcmp (fmer,fnew) == 0)) { printf ("\nNew name is the same.\n"); return; } fin = fopen (fname,"r"); /* open the file for reading */ if (fin == NULL) { printf ("\nFile %s not found!\n",fname); return; } if (merge == __FALSE) { printf ("\nCopy file %s to %s\n",fname,fnew); } else { printf ("\nCopy file %s, %s to %s\n",fname,fmer,fnew); } fout = fopen (fnew,"w"); /* open the file for writing */ if (fout == NULL) { printf ("\nFailed to open %s for writing!\n",fnew); fclose (fin); return; } total = 0; while ((cnt = fread (&buf, 1, 512, fin)) != 0) { fwrite (&buf, 1, cnt, fout); total += cnt; } fclose (fin); /* close input file when done */ if (merge == __TRUE) { fin = fopen (fmer,"r"); /* open the file for reading */ if (fin == NULL) { printf ("\nFile %s not found!\n",fmer); } else { while ((cnt = fread (&buf, 1, 512, fin)) != 0) { fwrite (&buf, 1, cnt, fout); total += cnt; } fclose (fin); } } fclose (fout); dot_format (total, &buf[0]); printf ("\n%s bytes copied.\n", &buf[0]); } /*------------------------------------------------- ---------------------------

35

* Delete a File *---------------------------------------------------------------------------*/ static void cmd_delete (char *par) { char *fname,*next,dir; fname = get_entry (par, &next); if (fname == NULL) { printf ("\nFilename missing.\n"); return; } dir = 0; if (*(fname + strlen(fname) - 1) == '\\') { dir = 1; } if (fdelete (fname) == 0) { if (dir) { printf ("\nDirectory %s deleted.\n",fname); } else { printf ("\nFile %s deleted.\n",fname); } } else { if (dir) { printf ("\nDirectory %s not found or not empty.\n",fname); } else { printf ("\nFile %s not found.\n",fname); } } } /*------------------------------------------------- --------------------------- * Print a Flash Memory Card Directory *---------------------------------------------------------------------------*/ static void cmd_dir (char *par) { U64 fsize; U32 files,dirs,i; char temp[32],*mask,*next,ch; FINFO info; mask = get_entry (par, &next); if (mask == NULL) { mask = "*.*"; } printf ("\nFile System Directory..."); files = 0; dirs = 0;

36

fsize = 0; info.fileID = 0; while (ffind (mask,&info) == 0) { if (info.attrib & ATTR_DIRECTORY) { i = 0; while (strlen((const char *)info.name+i) > 41) { ch = info.name[i+41]; info.name[i+41] = 0; printf ("\n%-41s", &info.name[i]); info.name[i+41] = ch; i += 41; } printf ("\n%-41s <DIR> ", &info.name[i]); printf (" %02d.%02d.%04d %02d:%02d", info.time.day, info.time.mon, info.time.year, info.time.hr, info.time.min); dirs++; } else { dot_format (info.size, &temp[0]); i = 0; while (strlen((const char *)info.name+i) > 41) { ch = info.name[i+41]; info.name[i+41] = 0; printf ("\n%-41s", &info.name[i]); info.name[i+41] = ch; i += 41; } printf ("\n%-41s %14s ", &info.name[i], temp); printf (" %02d.%02d.%04d %02d:%02d", info.time.day, info.time.mon, info.time.year, info.time.hr, info.time.min); fsize += info.size; files++; } } if (info.fileID == 0) { printf ("\nNo files..."); } else { dot_format (fsize, &temp[0]); printf ("\n %9d File(s) %21s bytes", files, temp); } dot_format (ffree(""), &temp[0]); if (dirs) { printf ("\n %9d Dir(s) %21s bytes free.\n", dirs, temp); } else { printf ("\n%56s bytes free.\n",temp); } }

37

/*------------------------------------------------- --------------------------- * Format a Flash Memory Card *---------------------------------------------------------------------------*/ static void cmd_format (char *par) { char *label,*next,*opt; char arg[20]; U32 retv; label = get_entry (par, &next); if (label == NULL) { label = "KEIL"; } strcpy (arg, label); opt = get_entry (next, &next); if (opt != NULL) { if ((strcmp (opt, "/FAT32") == 0) ||(strcmp (opt, "/fat32") == 0)) { strcat (arg, "/FAT32"); } } printf ("\nFormat Flash Memory Card? [Y/N]\n"); retv = getkey(); if (retv == 'y' || retv == 'Y') { /* Format the Card with Label "KEIL". "*/ if (fformat (arg) == 0) { printf ("Memory Card Formatted.\n"); printf ("Card Label is %s\n",label); } else { printf ("Formatting failed.\n"); } } } /*------------------------------------------------- --------------------------- * Display Command Syntax help *---------------------------------------------------------------------------*/ static void cmd_help (char *par) { printf (help); } /*------------------------------------------------- --------------------------- * Initialize a Flash Memory Card *---------------------------------------------------------------------------*/ static void init_card (void) { U32 retv; while ((retv = finit (NULL)) != 0) { /* Wait until the Card is ready*/ if (retv == 1) { printf ("\nSD/MMC Init Failed"); printf ("\nInsert Memory card and press key...\n"); getkey ();

38

} else { printf ("\nSD/MMC Card is Unformatted"); strcpy (&in_line[0], "KEIL\r\n"); cmd_format (&in_line[0]); } } } /*------------------------------------------------- --------------------------- * Main: *---------------------------------------------------------------------------*/ int main (void) { char *sp,*cp,*next; U32 i; init_comm (); /* init communication interface*/ printf (intro); /* display example info */ printf (help); init_card (); while (1) { printf ("\nCmd> "); /* display prompt */ fflush (stdout); /* get command line input */ if (getline (in_line, sizeof (in_line)) == __FALSE) { continue; } sp = get_entry (&in_line[0], &next); if (*sp == 0) { continue; } for (cp = sp; *cp && *cp != ' '; cp++) { *cp = toupper (*cp); /* command to upper-case */ } for (i = 0; i < CMD_COUNT; i++) { if (strcmp (sp, (const char *)&cmd[i].val)) { continue; } init_card(); /* check if card is removed */ cmd[i].func (next); /* execute command function */ break; } if (i == CMD_COUNT) { printf ("\nCommand error\n"); } } } /*------------------------------------------------- --------------------------- * end of file *---------------------------------------------------------------------------*/

39

PART- II: Write the following programs to understand the use of RTOS with ARM Processor on IDE Environment using ARM Tool chain and Library:

1. Create an application that creates two tasks that wait on a timer whilst the main task loops.

// Hedar File #include <RTL.h> int count=10000; int count1=10000; // Task ID's OS_TID id1, id2; // Task Declarations __task void ORL_task1 (void); __task void ORL_task2 (void); // Task1 Definition __task void ORL_task1 (void) { // Step - 1: Generate Task ID for Task1 id1 = os_tsk_self (); // Step - 2: Assign Task ID for Task2 id2 = os_tsk_create (ORL_task2, 1); while(1) { while(count) count--; // Indicate to task2 completion of do-this os_evt_set (0x0004, id2); // Wait for completion of do-that (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); // Wait for 50ms os_dly_wait (5); } } // Task2 Definition __task void ORL_task2 (void) { while(1) {

40

while(count1) count1--; // Wait for completion of do-this (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); //Wait for 20ms os_dly_wait (2); // Task1 Completion os_evt_set (0x0004, id1); } } // Main Function int main (void) { os_sys_init (ORL_task1); }

41

2. Write an application that creates a task which is scheduled when a button is pressed, which illustrates the use of an event set between an ISR and a task.

// RT LINUX HEDAR FILE #include <RTL.H> #include <LPC21XX.H> int count=10000; #define led 0x00000001 // Task ID's OS_TID id1; // Task Declaration __task void ORL_task1(void); // Task Definition __task void ORL_task1(void) { // Step - 1: Generate Task ID id1=os_tsk_self(); while(1) { while(count) count--; IOCLR0=led; // Wait for completion of do-that (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); // Wait for 50ms os_dly_wait (5); count=10000; } } // ISR void INT2(void) __irq { IOSET0=led; EXTINT |= 1; VICVectAddr= 0x00000000; } // Interrupt Declaration void inter(void) { EXTMODE |= 1; //Edge sensitive mode on EINT0 EXTPOLAR = 0; //Falling Edge Sensitive

42

PINSEL0 |= 0x0000000C; //Enable EINT2 on P0.1 VICVectCntl0 = 0x20 | 14; //15 is index of EINT0 VICVectAddr0 = (unsigned long) INT2; VICIntEnable |= 1<<14; //Enable EINT0 } // Main Function int main() { IODIR0=led; // Output inter(); // Interrupt Enable os_sys_init(ORL_task1); }

43

3. Write an application that Demonstrates the interruptible ISRs(Requires timer to have higher priority than external interrupt button) // RT LINUX HEDAR FILE #include <RTL.H> #include <LPC21XX.H> int count=10000; #define led 0x00000001 // Task ID's OS_TID id1; // Task Declaration __task void ORL_task1(void); // Task Definition __task void ORL_task1(void) { // Step - 1: Generate Task ID id1=os_tsk_self(); while(1) { while(count) count--; IOCLR0=led; // Wait for completion of do-that (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); // Wait for 50ms os_dly_wait (5); count=10000; } } // ISR void INT2(void) __irq { IOSET0=led; EXTINT |= 1; VICVectAddr= 0x00000000; } void T0isr (void) __irq {

44

T0EMR |= 0x00000002; //Set MAT1 high for begining of the cycle T0MR1++; //Increment PWM Duty cycle T0MR1 = T0MR1&0x000000FF; //Limit duty cycle T0IR |= 0x00000001; //Clear match 0 interrupt VICVectAddr = 0x00000000; //Dummy write to signal end of interrupt } // Interrupt Declaration void inter(void) { EXTMODE |= 1; //Edge sensitive mode on EINT2 EXTPOLAR = 0; //Falling Edge Sensitive PINSEL0 |= 0x0000000C; //Enable EINT2 on P0.14 VICVectCntl0 = 0x20 | 14; //15 is index of EINT2 VICVectAddr0 = (unsigned long) INT2; VICIntEnable |= 1<<14; //Enable EINT2 } // Main Function int main() { VPBDIV = 0x00000002; // Configure the VPB divi PINSEL0 |= 0x00000800; //Match1 as output T0PR = 0x0000001E; //Load prescaler T0TCR = 0x00000002; //Reset counter and prescaler T0MCR = 0x00000003; //On match reset the counter and generate an interrupt T0MR0 = 0x00000101; //Set the cycle time T0MR1 = 0x00000000; // Set duty cycle to zero T0EMR = 0x00000042; //On match clear MAT1 T0TCR = 0x00000001; //enable timer VICVectAddr4 = (unsigned)T0isr; //Set the timer ISR vector address VICVectCntl4 = 0x00000024; //Set channel VICIntEnable |= 0x00000010; //Enable the interrupt IODIR0=led; // Output inter(); // Interrupt Enable os_sys_init(ORL_task1); }

45

5. Write an application that creates two tasks of the same priority and sets the time slice period to illustrate time slicing. // Hedar Files #include <RTL.H> #include "MAD/ORL.H" #include "MAD/DELAY.H" // Variables U32 count1,count2; // Task ID's OS_TID id1,id2; // Task Declarations __task void ORL_task1(void); __task void ORL_task2(void); //Task1 Definition __task void ORL_task1(void) { id1=os_tsk_self(); os_tsk_prio_self(2); id2=os_tsk_create(ORL_task2,2); while(1) { count1++; os_dly_wait(2); os_evt_set(0x0004,id2); } } // Task2 Definition __task void ORL_task2(void) { while(1) { count2++; os_dly_wait(2); os_evt_set(0x0004,id1); } } // Main Function int main() { os_sys_init(ORL_task1); }

46

Interfacing Programs: 6. Write an application that creates a two task to Blinking two different LEDs at different Timings. /* Target: Blinking LED's at different Timings Programmer: Madhu P @ ORANGE RESEARCH LABS */ // Hedar Files #include <RTL.H> #include "MAD/ORL.H" // LED's #define led1 0x00000001 #define led2 0x00000002 // Task ID's OS_TID id1,id2; void delay () { unsigned int y,h; for (y=0;y<40000;y++) for (h=0;h<200;h++); } // Task Declarations __task void ORL_task1(void); __task void ORL_task2(void); // Task1 Definition __task void ORL_task1 (void) { // Step - 1: Generate Task ID for Task1 id1 = os_tsk_self (); // Step - 2: Assign Task ID for Task2 id2 = os_tsk_create (ORL_task2, 1); while(1) { IOSET0=led1; os_dly_wait(10); IOCLR0=led1; // os_dly_wait(1);

47

// Indicate to task2 completion of do-this os_evt_set (0x0004, id2); // Wait for completion of do-that (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); os_dly_wait(5); } } // Task2 Definition __task void ORL_task2 (void) { while(1) { IOSET0=led2; os_dly_wait(100); IOCLR0=led2; // os_dly_wait(1); // Wait for completion of do-this (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); //Wait for 20ms os_dly_wait (2); // Task1 Completion os_evt_set (0x0004, id1); } } // Main Function int main() { IODIR0|=led1|led2; os_sys_init(ORL_task1); }

48

7. Write an application that creates a two task displaying two different messages in LCD display in two lines. /* Target: Displaying Message through LCD Programmer: Madhu P @ ORANGE RESEARCH LABS */ // Hedar Files #include <RTL.H> #include "MAD/ORL.H" #include "MAD/DELAY.H" #include "MAD/LCD.H" // TASK IDS OS_TID id1,id2; // TASK DECLARATIONS __task void ORL_task1(void); __task void ORL_task2(void); // Task1 Definition __task void ORL_task1 (void) { // Step - 1: Generate Task ID for Task1 id1 = os_tsk_self (); // Step - 2: Assign Task ID for Task2 id2 = os_tsk_create (ORL_task2, 1); while(1) { lcd_cmd(0x80); lcd_string("EES"); // Indicate to task2 completion of do-this /// os_evt_set (0x0004, id2); // Wait for completion of do-that (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); os_dly_wait(20); os_evt_set (0x0004, id1); } } // Task2 Definition __task void ORL_task2 (void) {

49

while(1) { lcd_cmd(0xC0); lcd_string("EMBEDDED "); // Wait for completion of do-this (0xffff means no time-out) os_evt_wait_or (0x0004, 0xffff); //Wait for 20ms os_dly_wait (20); // Task1 Completion os_evt_set (0x0004, id1); } } // Main Function int main() { lcd_init(); os_sys_init(ORL_task1); }

50

8. Sending messages to mailbox by one task and reading the message from mailbox by another task. /* Target: Sending and Receiving Tasks for Mail Box */ #include <RTL.h> #include "MAD/ORL.H" #include "MAD/DELAY.H" #include "MAD/UART.H" #include <stdio.h> // Task ID's OS_TID id1; OS_TID id2; typedef struct { float voltage; float current; U32 counter; } T_MEAS; os_mbx_declare (MsgBox,16); /* Declare an RTX mailbox */ _declare_box (mpool,sizeof(T_MEAS),16);/* Dynamic memory pool */ // Task Declarations __task void send_task (void); __task void rec_task (void); // Task1 Definition __task void send_task (void) { T_MEAS *mptr; id1 = os_tsk_self (); id2 = os_tsk_create (rec_task, 0); os_mbx_init (MsgBox, sizeof(MsgBox));/* initialize the mailbox */ os_dly_wait (5); mptr = _alloc_box (mpool); /* Allocate a memory for the message */ mptr->voltage = 223.72; /* Set the message content */ mptr->current = 17.54; mptr->counter = 120786; os_mbx_send (MsgBox, mptr, 0xffff); /* Send the message to the mailbox */ IOSET1 = 0x10000; os_dly_wait (100);

51

mptr = _alloc_box (mpool); mptr->voltage = 227.23; /* Prepare a 2nd message */ mptr->current = 12.41; mptr->counter = 170823; os_mbx_send (MsgBox, mptr, 0xffff); /* And send it. */ os_tsk_pass (); /* Cooperative multitasking */ IOSET1 = 0x20000; os_dly_wait (100); mptr = _alloc_box (mpool); mptr->voltage = 229.44; /* Prepare a 3rd message */ mptr->current = 11.89; mptr->counter = 237178; os_mbx_send (MsgBox, mptr, 0xffff); /* And send it. */ IOSET1 = 0x40000; os_dly_wait (100); os_tsk_delete_self (); /* We are done here, delete this task */ } // Task2 Definition __task void rec_task (void) { T_MEAS *rptr; while(1) { os_mbx_wait (MsgBox, (void **)&rptr, 0xffff); /* wait for the message */ printf ("\nVoltage: %.2f V\n",rptr->voltage); printf ("Current: %.2f A\n",rptr->current); printf ("Number of cycles: %d\n",rptr->counter); _free_box (mpool, rptr); /* free memory allocated for message */ } } // Main Function int main (void) { IODIR1 = 0xFF0000; uart0_init(); _init_box (mpool, sizeof(mpool), /* initialize the 'mpool' memory for */ sizeof(T_MEAS)); /* the membox dynamic allocation */ os_sys_init (send_task); /* initialize and start task 1 */ }

52

9. Sending message to PC through serial port by three different tasks on priority Basis. /* Target: Sending Messages to PC on Priority Basis */ // Hedar Files #include <RTL.H> #include "MAD/ORL.H" #include "MAD/DELAY.H" #include "MAD/UART.H" // Task ID's OS_TID id1,id2,id3; // Task Declarations __task void ORL_task1(void); __task void ORL_task2(void); __task void ORL_task3(void); // Task1 Definition __task void ORL_task1(void) { os_tsk_prio_self(2); id1=os_tsk_self(); id2=os_tsk_create(ORL_task2,1); id3=os_tsk_create(ORL_task3,2); while(1) { uart0_string("WELCOME"); os_dly_wait(5); } } // Task2 Definition __task void ORL_task2(void) { while(1) { uart0_string("EMBEDDED SYS LAB"); os_dly_wait(5); } } // Task3 Definition __task void ORL_task3(void) { while(1)

53

{ uart0_string("B V R I T \n"); os_dly_wait(5); } } // Main Function int main() { uart0_init(); os_sys_init(ORL_task1); }