0% found this document useful (0 votes)
91 views30 pages

ESRTOS Manual-2019 - Final

The document describes an experiment to implement LED blinking using an LPC1768 microcontroller. It discusses using the microcontroller's output ports and special function registers to control LEDs. The theory section explains the features of the LPC1768 microcontroller. An embedded C program is provided to set the port direction and toggle an LED on and off to create a blinking effect.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views30 pages

ESRTOS Manual-2019 - Final

The document describes an experiment to implement LED blinking using an LPC1768 microcontroller. It discusses using the microcontroller's output ports and special function registers to control LEDs. The theory section explains the features of the LPC1768 microcontroller. An embedded C program is provided to set the port direction and toggle an LED on and off to create a blinking effect.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Lab Manual-Embedded Systems & RTOS T.E.

-VI, Electronics

Experiment-1

Arithmetic and Logic Unit

Aim:- Using mixed C/C++ and assembly language programming, implement Arithmetic and Logic Unit
to perform following operations: Addition ,Subtraction, Multiplication, Division, AND, OR ,XOR, NOR,
left shift, right shift.

Learning Objective:-

1. Advantages of Mixed c and Assembly Programming


2. Embedded C-programming concepts
3. Data types used in Embedded c

Theory:-

Advantage of Assembly Language Programming

● Assembly codes sensitive to the processor, memory, ports and devices hardware
● Gives a precise control of the processor internal devices
● Enables full use of processor specific features in its instruction set and its
● Machine codes are compact, processor and memory sensitive
● System needs a smaller memory.
● Memory needed does not depend on the programmer data type selection and rule-declarations
● Not the compiler specific and library-functions specific
● Device driver codes may need only a few assembly instructions.
● Bottom-up-design approach

Use of Data Type and Declarations

• Each data type provides an abstraction of the

(i) methods to use, manipulate and represent, and

(ii) set of permissible operations.

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 1


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Data types:

Use of Modifiers and Qualifiers:

● auto
● unsigned
● static
● const
● register
● interrupt
● extern
● volatile
● volatile static

Macro – It is a named collection of codes that is defined in a program as preprocessor directive.

• Differs from a function in the sense that once a macro is defined by a name, the compiler

puts the corresponding codes at the macro at every place where that macro-name appears.

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 2


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Algorithm:-

1. Input unsigned integer data and operation to be performed from user.

2. Use “asm” as preprocessor directive to execute assembly instructions given in C program.

3. Use switch case logic to implement different Arithmetic and Logical functions.

4. Display the result.

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 3


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

ALU program:-

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int a,b, choice, result;
printf(“\n1. ADD \n2. SUB \n3. MUL \n4. DIV \n5. AND \n6. XOR \n7. OR \n8.EXIT”);
printf(“\nEnter your choice”);
scanf(“%d”,&choice);
printf(“\nEnter two numbers”);
scanf(“%d %d”,&a,&b);

switch(choice)
{
Case 1: asm{
mov ax, a;
mov bx, b;
add ax, bx;
mov result, ax;
}
printf(“\n Addition=%d”, result);
break;
Case 2: asm{
mov ax, a;
mov bx, b;
sub ax, bx;
mov result, ax;
}
printf(“\n Subtraction=%d”, result);
break;
Case 3: asm{
mov ax, a;
mov bx, b;
mul bx;
mov result, ax;
}
printf(“\n Multiplication=%d”, result);
break;
Case 4: asm{
mov ax, a;
mov bx, b;
ALU program:-

div bx;
mov result, ax;

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 4


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

}
printf(“\n Division=%d”, result);
break;
Case 5: asm{
mov ax, a;
mov bx, b;
AND ax, bx;
mov result, ax;
}
printf(“\n AND=%d”, result);
break;
Case 6: asm{
mov ax, a;
mov bx, b;
xor ax, bx;
mov result, ax;
}
printf(“\n XOR=%d”, result);
break;
Case 7: asm{
mov ax, a;
mov bx, b;
or ax, bx;
mov result, ax;
}
printf(“\n OR=%d”, result);
break;
case 8:
break;
default: printf(“\nEnter proper choice”);
}
getch():
}

OUTPUT

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 5


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Experiment -2

Traffic light controller

Aim:- Design system based on 8051 microcontroller and PROTEUS for demonstration of traffic light.

Learning Objective:-
i)To study embedded c programming for 8bit microcontroller (8051).
ii)To study delay logic and switching of LED’s at different ports.
Theory: Traffic lights are interfaced to port1 and port 2 of 8051 microcontroller. According to sequence
of traffic lights GREEN, RED, ORANGE lights are made ON and OFF for specified time duration. At a
time only one road traffic will be on, remaining road will have RED signal.

Algorithm:-

1. Set port direction in output mode.


2. Send data to port according to sequence 1
3. Give delay
4. Send data to port according to sequence 2
5. Give delay
6. Send data to port according to sequence 3
7. Give delay
8. Send data to port according to sequence 4
9. Give delay
10. To repeat sequence go to step 2.

Procedure :-

1. Open Keil µvision4 software.


2. Start new project and save it.
3. Select target device as AT89C51.
4. Select new file and write program in ‘c’. save it with filename.c
5. Add file to source group in target.
6. Build the program.
7. Click on DEBUG.
8. Run program. Observe results in peripherals and in port1 and port2.

Conclusion:-

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 6


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Program for Traffic light controller.

#include< reg51.h>

sbit red_n = P1^0;

sbit yellow_n = P1^1;

sbit green_n = P1^2;

sbit red_s = P1^3;

sbit yellow_s = P1^4;

sbit green_s = P1^5;

sbit red_e = P2^0;

sbit yellow_e = P2^1;

sbit green_e = P2^2;

sbit red_w = P2^3;

sbit yellow_w = P2^4;

sbit green_w = P2^5;

void delay()

int i,j;

for (i=0;i<1000;i++)

for (j=0;j<1000;j++);

void main()

while(1)

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 7


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

red_n = 1;

yellow_n = 0;

green_n = 0;

red_s = 0;

yellow_s = 0;

green_s = 1;

red_e = 0;

yellow_e = 1;

green_e = 0;

red_w = 1;

yellow_w = 0;

green_w = 0;

delay();

red_n = 0;

yellow_n = 1;

green_n = 0;

red_s = 1;

yellow_s = 0;

green_s = 0;

red_e = 0;

yellow_e = 0;

green_e = 1;

red_w = 1;

yellow_w = 0;

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 8


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

green_w = 0;

delay();

red_n = 0;

yellow_n = 0;

green_n = 1;

red_s = 1;

yellow_s = 0;

green_s = 0;

red_e = 1;

yellow_e = 0;

green_e = 0;

red_w = 0;

yellow_w = 1;

green_w = 0;

delay();

red_n = 1;

yellow_n = 0;

green_n = 0;

red_s = 0;

yellow_s = 1;

green_s = 0;

red_e = 1;

yellow_e = 0;

green_e = 0;

red_w = 0;

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 9


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

yellow_w = 0;

green_w = 1;

delay();

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 10


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Experiment 3

LED blinking

Aim:- Write a program in embedded c for LED blinking using LPC1768.

Learning Objective:- i) To study output port functionality of LPC1768.


ii) To study special functional registers (SFR’s) for port.
Theory:-

The NXP’s (founded by Philips) LPC 1768 is ARM 32-bit Cortex-M3 Microcontroller with MPU, CPU
clock up to 100MHz, 512kB on-chip Flash ROM with enhanced Flash Memory Accelerator,

In-System Programming (ISP) and In-Application Programming (IAP),64kB RAM, Nested Vectored
Interrupt Controller, Eight channel General purpose DMA controller, AHB Matrix, APB.

Ethernet 10/100 MAC with RMII interface and dedicated DMA, USB 2.0 full-speed Device controller
and Host/OTG controller with DMA.

CAN 2.0B with two channels, Four UARTs, one with full Modem interface, Three I2C serial interfaces,
Three SPI/SSP serial interfaces, I2S interface, General purpose I/O pins, 12-bit ADC with 8 channels, 10-
bit DAC, Four 32-bit Timers with capture/compare, Standard PWM Timer block,Motor control PWM for
three-phase Motor control, Quadrature Encoder,Watchdog Timer, Real Time Clock with optional Battery
backup, System Tick Timer, Repetitive Interrupt Timer, Brown-out detect circuit,

Power-On Reset, Power Management Unit, Wakeup Interrupt Controller,Crystal oscillator, 4MHz
internal RC oscillator, PLL,JTAG and Serial Wire Debug/Trace Port with ETM

All ports are controlled via different registers as follows:

FIODIR :- Port Direction control register. This register individually controls the direction of each port
pin.

FIOSET:- Port Output Set register. This register controls the state of output pins in conjunction with the
IOCLR register. Writing ones produces highs at the corresponding port pins. Writing zeroes has no effect.

FIOCLR:-Port Output Clear register. This register controls the state of output pins. Writing ones
produces lows at the corresponding port pins and clears the corresponding bits in the IOSET register.
Writing zeroes has no effect.

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 11


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Algorithm:-

To toggle LED’s connected at PORT2

1. Set P2 in output direction by setting respective bit in FIODIR register.


2. Send logic high to P2 by setting respective bits in FIOSET register.
3. Call delay of few milli second.
4. Send logic low to P2 by setting respective bits in FIOCLR register.
5. Repeat from step 3 to toggle LED continues.

Procedure :-

1. Open Keil µvision4 software.


2. Start new project and save it.
3. Select target device as LPC1768 in NxP.
4. Select new file and write program in ‘c’. save it with filename.c
5. Add file to source group in target
6. Click on DEBUG.
7. Run program. Observe results in peripherals and in fast I/O port2

Conclusion:-

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 12


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

#include <lpc17xx.h>

void delay_ms(unsigned int ms)

unsigned int i,j;

for(i=0;i<ms;i++)

for(j=0;j<20000;j++);

/* start the main program */

int main()

char n;

// SystemInit(); //Clock and PLL configuration

LPC_PINCON->PINSEL4 = 0x000000; //Configure the PORT2 Pins as GPIO;

LPC_GPIO2->FIODIR = 0xffffffff; //Configure the PORT2 pins as OUTPUT;

while(1)

LPC_GPIO2->FIOSET = 0x0000000C; // Make all the Port pins as high

delay_ms(100);

LPC_GPIO2->FIOCLR = 0x0000000C; // Make all the Port pins as low

delay_ms(100);

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 13


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Output

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 14


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Experiment 4

ADC

Aim:-Write a program in embedded c for implementing ADC functionality using LPC1768.


Learning Objective :- i) To study inbuilt LPC1768 ADC functionality
ii) Register associated with ADC 
Theory:

LPC1768 has an inbuilt 12 bit Successive Approximation ADC which is multiplexed among 8 input pins.
The ADC reference voltage is measured across VREFN to VREFP, meaning it can do the conversion
within this range. Usually the VREFP is connected to VDD and VREFN is connected to GND.
As LPC1768 works on 3.3 volts, this will be the ADC reference voltage.
The below block diagram shows the ADC input pins multiplexed with other GPIO pins.
The ADC pin can be enabled by configuring the corresponding PINSEL register to select ADC function.
When the ADC function is selected for that pin in the Pin Select register, other Digital signals are
disconnected from the ADC input pins.

ADC Channel Port Pin Pin Functions Associated PINSEL


Register

AD0 P0.23 0-GPIO, 1-AD0[0], 2- 14,15 bits of PINSEL1


I2SRX_CLK, 3-
CAP3[0]

AD1 P0.24 0-GPIO, 1-AD0[1], 2- 16,17 bits of PINSEL1


I2SRX_WS, 3-
CAP3[1]

AD2 P0.25 0-GPIO, 1-AD0[2], 2- 18,19 bits of PINSEL1


I2SRX_SDA, 3-TXD3

AD3 P0.26 0-GPIO, 1-AD0[3], 2- 20,21 bits of PINSEL1


AOUT, 3-RXD3

AD4 P1.30 0-GPIO, 1-VBUS, 2- , 28,29 bits of PINSEL3


3-AD0[4]

AD5 P1.31 0-GPIO, 1-SCK1, 2- , 30,31 bits of PINSEL3


3-AD0[5]

AD6 P0.3 0-GPIO, 1-RXD0, 2- 6,7 bits of PINSEL0


AD0[6], 3-

AD7 P0.2 0-GPIO, 1-TXD0, 2- 4,5 bits of PINSEL0


SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 15
Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

AD0[7], 3-

ADC Registers
The below table shows the registers associated with LPC1768 ADC.

Register Description

ADCR A/D Control Register: Used for Configuring the ADC

ADGDR A/D Global Data Register: This register contains the ADC’s DONE bit and the
result of the most recent A/D conversion

ADINTEN A/D Interrupt Enable Register

ADDR0 - A/D Channel Data Register: Contains the recent ADC value for respective channel
ADDR7

ADSTAT A/D Status Register: Contains DONE & OVERRUN flag for all the ADC channels

Algorithm:-

1. Configure the GPIO pin for ADC function using PINSEL register.
2. Enable the Clock to ADC module.
3. Select the Particular channel for A/D conversion by setting the corresponding bits in ADCR.SEL
4. Set the ADC START bit for starting the A/D conversion for selected channel.
5. Wait for the conversion to complete, ADC_DONE bit will be set once conversion is over.
6. Read the 12-bit A/D value from ADC.RESULT.

Procedure :-

1. Open Keil µvision4 software.


2. Start new project and save it.
3. Select target device as LPC1768 in NxP.
4. Select new file and write program in ‘c’. save it with filename.c
5. Add file to source group in target ,Click on DEBUG.
6. Run program and observe the results in peripherals.
Conclusion:-

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 16


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

#include <lpc17xx.h>
#include <stdio.h>
//#include "ocf_lpc176x_lib.h" //contains uart, timer & fputc to retarget printf

#define VREF 3.3 //Reference Voltage at VREFP pin, given VREFN = 0V(GND)
#define ADC_CLK_EN (1<<12)
#define SEL_AD0_0 (1<<0) //Select Channel AD0.0
#define CLKDIV 1 //ADC clock-divider (ADC_CLOCK=PCLK/CLKDIV+1) = 12.5Mhz @ 25Mhz
PCLK
#define PWRUP (1<<21) //setting it to 0 will power it down
#define START_CNV (1<<24) //001 for starting the conversion immediately
#define ADC_DONE (1U<<31) //define it as unsigned value or compiler will throw #61-D warning
#define ADCR_SETUP_SCM ((CLKDIV<<8) | PWRUP)

void delayMS(unsigned int ms)


{
int i,j;
for (i=0; i<ms; i++)
for (j=0;j<100;j++);
}

int main(void)
{
int result = 0;
float volts = 0;

LPC_SC->PCONP |= ADC_CLK_EN; //Enable ADC clock


LPC_ADC->ADCR = ADCR_SETUP_SCM | SEL_AD0_0;
LPC_PINCON->PINSEL1 |= (1<<14) ; //select AD0.0 for P0.23

while(1)
{
LPC_ADC->ADCR |= START_CNV; //Start new Conversion

while((LPC_ADC->ADDR0 & ADC_DONE) == 0); //Wait untill conversion is finished

result = (LPC_ADC->ADDR0>>4) & 0xFFF; //12 bit Mask to extract result

volts = (result*VREF)/4096.0; //Convert result to Voltage


delayMS(500); //Slowing down Updates to 2 Updates per second
}

//return 0; //This won't execute


}

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 17


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Output:

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 18


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Experiment 5

PWM

Aim:- Write a program to implement PWM using LPC1768.


Learning Objective:-i) To study inbuilt LPC1768
ii) PWM module
iii) PWM Registers, Register Configuration
iv) Working.

Theory: LPC1768 has 6 PWM output pins which can be used as 6-Single edged or 3-Double edged.
There as seven match registers to support these 6 PWM output signals.

Below block diagram shows the PWM pins and the associated match (Duty Cycle) registers.

PWM Channel Port Pin Pin Functions Associated Corresponding


PINSEL Register Match Register

PWM_1 P2.0 0-GPIO, 1- 0,1 bits of MR1


PWM1[1], 2- PINSEL4
TXD1, 3-

PWM_2 P2.1 0-GPIO, 1- 2,3 bits of MR2


PWM1[2], 2- PINSEL4
RXD1, 3-

PWM_3 P2.2 0-GPIO, 1- 4,5 bits of MR3


PWM1[3], 2- PINSEL4
CTS1, 3-
TRACEDATA[3]

PWM_4 P2.3 0-GPIO, 1- 6,7 bits of MR4


PWM1[4], 2- PINSEL4
DCD1, 3-
TRACEDATA[2]

PWM_5 P2.4 0-GPIO, 1- 8,9 bits of MR5


PWM1[5], 2- PINSEL4
DSR1, 3-
TRACEDATA[1]

PWM_6 P2.5 0-GPIO, 1- 10,11 bits of MR6


PWM1[6], 2- PINSEL4
DTR1, 3-
TRACEDATA[0]

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 19


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Register Configuration
The below table shows the registers associated with LPC1768 PWM.

TCR

31:4 3 2 1 0

Reserved PWM Enable Reserved Counter Reset Counter Enable

Bit 0 – Counter Enable


This bit is used to Enable or Disable the PWM Timer and PWM Prescalar Counters
0- Disable the Counters
1- Enable the Counter incrementing.

Bit 1 – Counter reset


This bit is used to clear the PWM Timer and PWM Prescalar Counter values.
0- Do not clear.
1- The PWM Timer Counter and the PWM Prescale Counter are synchronously reset on the next positive
edge of PCLK.

MCR

31:21 20 19 18 -

Reserved PWMMR6S PWMMR6R PWMMR6I -

Bit 3 – PWM Enable


Used to Enable or Disable the PWM Block.
0- PWM Disabled
1- PWM Enabled 

PWMMRxI
This bit is used to Enable or Disable the PWM interrupts when the PWMTC matches PWMMRx (x:0-6)
0- Disable the PWM Match interrupt
1- Enable the PWM Match interrupt.

PWMMRxR
This bit is used to Reset PWMTC whenever it Matches PWMRx(x:0-6)
0- Do not Clear.
1- Reset the PWMTC counter value whenever it matches PWMRx.

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 20


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

PWMMRxS
This bit is used to Stop the PWMTC,PWMPC whenever the PWMTC matches PWMMRx(x:0-6).
0- Disable the PWM stop o match feature
1- Enable the PWM Stop feature. This will stop the PWM whenever the PWMTC reaches the Match
register value. 

PCR

31:15 14-9 8-7 6-2 1-0 31:15

Unused PWMENA6- Unused PWMSEL6- Unused Unused


PWMENA1 PWMSEL2

PWMSELx
This bit is used to select the single edged and double edge mode form PWMx (x:2-6)
0- Single Edge mode for PWMx
1- Double Edge Mode for PWMx.

PWMENAx
This bit is used to enable/disable the PWM output for PWMx(x:1-6)
0- PWMx Disable.
1- PWMx Enabled. 

LER

31-7 6 5 4 3

Unused LEN6 LEN5 LEN4 LEN3

LENx
This bit is used Enable/Disable the loading of new Match value whenever the PWMTC is reset(x:0-6)
PWMTC will be continuously incrementing whenever it reaches the PWMMRO, timer will be reset
depending on PWMTCR configuration. Once the Timer is reset the New Match values will be loaded
from MR0-MR6 depending on bits set in this register.
0- Disable the loading of new Match Values
1- Load the new Match values from MRx when the timer is reset. 

PWM Working:
The TC is continuously incremented and once it matches the MR1(Duty Cycle) the PWM pin is pulled
Low. TC still continues to increment and once it reaches the Cycle time (Ton+Toff) the PWM module
does the following things:

 Reset the TC value.


SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 21
Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

 Pull the PWM pin High.


 Loads the new Match register values.
Algorithm:-

1. Configure the GPIO pins for PWM operation in respective PINSEL register.
2. Configure TCR to enable the Counter for incrementing the TC, and Enable the PWM block.
3. Set the required pre-scalar value in PR. In our case it will be zero.
4. Configure MCR to reset the TC whenever it matches MR0.
5. Update the Cycle time in MR0. In our case it will be 100.
6. Load the Duty cycles for required PWMx channels in respective match registers MRx(x: 1-
6).
7. Enable the bits in LER register to load and latch the new match values.
8. Enable the required pwm channels in PCR register.
Procedure :-

1. Open Keil µvision4 software.


2. Start new project and save it.
3. Select target device as LPC1768 in NxP.
4. Select new file and write program in ‘c’. save it with filename.c
5. Add file to source group in target, Click on DEBUG.
6. Run program and observe the results in peripherals.

Conclusion:-

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 22


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

#include <LPC17xx.h>

#define PWMPRESCALE (25-1) //25 PCLK cycles to increment TC by 1 i.e. 1 Micro-second

void initPWM(void);
void updatePulseWidth(unsigned int pulseWidth);
void delayMS(unsigned int milliseconds);
void initTimer0(void);

int main(void)
{
int pulseWidths[] = {1000,1250,1500,1750,2000};
const int num PulseWidths = 5;
int count=1;
int dir=0; //direction, 0 = Increasing, 1 = Decreasing
//SystemInit(); //gets called by Startup code before main()
initTimer0(); //Initialize Timer
initPWM(); //Initialize PWM

while(1)
{
updatePulseWidth(pulseWidths[count]); //Update Servo Pulse Width
delayMS(1000); //Wait for servo to reach the new position

if(count == (numPulseWidths-1) || count == 0)


{
dir = !dir; //Toggle direction if we have reached count limit
}

if(dir) count--;
else count++;
}
//return 0; //normally this won't execute ever
}

void initPWM(void)
{
/*Assuming that PLL0 has been setup with CCLK = 100Mhz and PCLK = 25Mhz.*/

LPC_PINCON->PINSEL3 |= (1<<5); //Select PWM1.1 output for Pin1.18


LPC_PWM1->PCR = 0x0; //Select Single Edge PWM - by default its single Edged so this line
can be removed
LPC_PWM1->PR = PWMPRESCALE; //1 micro-second resolution
LPC_PWM1->MR0 = 20000; //20000us = 20ms period duration
SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 23
Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

LPC_PWM1->MR1 = 1250; //1ms - default pulse duration i.e. width


LPC_PWM1->MCR = (1<<1); //Reset PWM TC on PWM1MR0 match
LPC_PWM1->LER = (1<<1) | (1<<0); // update values in MR0 and MR1
LPC_PWM1->PCR = (1<<9); //enable PWM output
LPC_PWM1->TCR = (1<<1); //Reset PWM TC & PR

LPC_PWM1->TCR = (1<<0) | (1<<3); //enable counters and PWM Mode

//PWM Generation goes active now!


//Now you can get the PWM output on Pin P1.18
}

void updatePulseWidth(unsigned int pulseWidth)


{
LPC_PWM1->MR1 = pulseWidth; //Update MR1 with new value
LPC_PWM1->LER = (1<<1); //Load the MR1 new value at start of next cycle
}

void delayMS(unsigned int milliseconds) //Using Timer0


{
LPC_TIM0->TCR = 0x02; //Reset Timer
LPC_TIM0->TCR = 0x01; //Enable timer

while(LPC_TIM0->TC < milliseconds); //wait until timer counter reaches the desired delay

LPC_TIM0->TCR = 0x00; //Disable timer


}

void initTimer0(void) //To setup Timer0 used delayMS() function


{
/*Assuming that PLL0 has been setup with CCLK = 100Mhz and PCLK = 25Mhz.*/

LPC_TIM0->CTCR = 0x0;
LPC_TIM0->PR = 25000-1; //Increment TC at every 24999+1 clock cycles
//25000 clock cycles @25Mhz = 1 mS

LPC_TIM0->TCR = 0x02; //Reset Timer


}

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 24


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Output:

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 25


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Experiment 6

Aim: - Write a program in C/C++ to determine task schedulability of uniprocessor system using
necessary and sufficient conditions.

Learning Objective: - i) To learn the application of schedulability theorem from CPU utilization.
ii) Total utilization theorem (Sufficient condition) 
n

∑ U i≤ 1
i=1

iii) Utilization bound theorem (Necessary and sufficient condition) -

n 1

∑ U i ≤ n(2 n – 1)
i=1

Theory:-

If Ci is the execution time of task  and Ti is its request period, then the processor utilization is defined as
the fraction of time during which the processor is busy running one of the tasks and is given as  .

Corresponding to the priority assignment of a set of tasks, it is said that these tasks fully utilize the
processor if the priority assignment is feasible i.e if U <1 and an increase in the run time of any of the
tasks in the set will make the priority assignment infeasible i.e if U>1.

For a given fixed priority scheduling algorithm, the least upper bound of the utilization factor is the
minimum of the utilization factors over all sets of tasks that fully utilize the processor. This means that for
all task sets whose utilization factor is below this bound, there exists a priority assignment that is feasible.

The Scheduler schedules different processes to be assigned to the CPU based on particular scheduling
algorithm. There are six popular process scheduling algorithms which we are going to discuss in the
following section: First-Come, First-Served (FCFS) Scheduling Shortest-Job-Next (SJN) Scheduling
Priority Scheduling Shortest Remaining Time Round Robin(RR) Scheduling Multiple-Level Queues
Scheduling These algorithms are either non preemptive or preemptive. Non-preemptive algorithms are
designed so that once a process enters the running state, it cannot be preempted until it completes its
allotted time where as the preemptive scheduling is based on priority where a scheduler may preempt a
low priority running process anytime when a high priority process enters into a ready state. First Come

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 26


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

First Serve (FCFS) Jobs are executed on first come, first serve basis. It is a non preemptive scheduling
algorithm. Easy to understand and implement. Its implementation is based on FIFO queue. Poor in
performance as average wait time is high. The various types of scheduling task is

Shortest job first scheduling


Key concept of this algorithm is: “CPU is allocated to the process with least CPU-burst time.” Amongst
the processes in the ready queue. CPU is always assigned to the process with least CPU burst
requirement. If the two processes having the same length, next CPU burst, fcfs scheduling is used i.e. one
which arrives first, will be taken up first by the CPU.This algorithm can be preemptive or non-
preemptive.

Round Robin Scheduling Round Robin is the preemptive process scheduling algorithm. Each process is
provided a fix time to execute called quantum. Once a process is executed for given time period. Process
is preempted and other process executes for given time period. Context switching is used to save states of
preempted processes.

Rate monotonic scheduling Algorithm works on the principle of preemption. Preemption occurs on a
given processor when higher priority task blocked lower priority task from execution. This blocking
occurs due to priority level of different tasks in a given task set.  rate monotonic is a preemptive algorithm
which means if a task with shorter period comes during execution it will gain a higher priority and can
block or preemptive currently running tasks. In RM priorities are assigned according to time period.
Priority of a task is inversely proportional to its timer period. Task with lowest time period has highest
priority and the task with highest period will have lowest priority.

Algorithm:-

1. Input task id for 4/5 different periodic tasks.


2. Input time period and execution time for each task.
3. Apply total utilization theorem as per sufficient condition.
4. Apply utilization bound theorem as per necessary and sufficient condition.
5. Write a comment on the schedulability.

Conclusion:

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 27


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

Task scheduling program:-

#include<iostream.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,temp,i,j,x=0;
float ut=0;
float u[10];
float t[10],c[10],p[10],r[10];
cout<<"Enter the no of task:=";
cin>> a;
for(i=0;i<a;i++)
{
cout<<"Enter period of task "<<i+1;
cout<<":=";
cin>>t[i];
p[i]=t[i];
cout<<"Enter CPU time of task "<<i+1;
cout<<":=";
cin>>c[i];
}
for(i=0;i<a-1;i++)
{
for(j=0;j<a;j++)
{
if(p[j]>p[j+1])
{
temp=p[j];
p[j]+p[j+1];
p[j+1]=temp;
}
}
}
for(i=0;i<a;i++)
{
for(j=0;j<a;j++)
{
if(t[i]==p[j])
cout<<"priority of Task "<<i+1<<"is: "<<j+1<<endl;
}
Task scheduling program:-
}
for(i=0;i<a;i++)
{

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 28


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

u[i]=c[i]/t[i];
ut=ut+u[i];
}
cout<<"Total Utilization is: "<<ut<<endl;
if(ut<1)
{
cout<<"Tasks are Schedulable";

Output:

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 29


Lab Manual-Embedded Systems & RTOS T.E.-VI, Electronics

SHAH AND ANCHOR KUTCHHI ENGINEERING COLLEGE Page 30

You might also like