You are on page 1of 17

Q1

-To understand Arithmetic and Logical instructions via Assembly as well as


Embedded C.
Code:
Conversion from packed BCD to unpacked BCD.
#include<lpc214x.h>
int main(void)
{
unsigned int b,i,j,c,d,e,f,a[]={0x7E,0x45,0x28,0xA9,0x5F}; // Defining an array of
elements to convert from Packed BCD to unpacked BCD.
for(i=0;i<=4;i++)
{ b=a[i]; // Take the number from the array indexed by
current number say first number andthe
store it temporarily in
b. c=b&0x0000000f // Mask the upper 4-bits of the
;IO0PIN=c number.
// Display the lower 4-bits at GPIO
;d=b; Port0.the original number from b into
// Load
d=d&0x000000f0 d. // Mask the lower 4-bits of the
;d=d>>4 number.
// Shift the upper bits right by 4
;IO1PIN=d positions.
// Display the upper 4-bits at GPIO
} ; Port1.

}
 Find 2’s compliment of a number. (1-Bytes user defined array)
Code:

#include<lpc214x.h>
int main(void)
{
unsigned int b,c,i,a[]={0x14,0x3E,0xDF,0xAB,0x77,0x56,0x28,0xE5,0x8C,0x99};
// Defining array containing numbers whose 2's compliment is to be found out.
for(i=0;i<10;i++)
{
b=a[i]; // Take the number indexed to by the loop and
store it into b.
c=~b; // Take compliment of the number (1's
compliment).
c=c+0x01; // Add 1 to it to find the 2's compliment.
IO0PIN=c; // Display the 2's compliment value at Port0.
}
}
Q.2
To understand looping constraints in C.
 Arrange 20 bytes of data in descending order. (Assume
user defined array)
Code:
#include<lpc214x.h>

int main(void)

unsigned int
b,c,i,j,a[]={0x55,0x78,0x34,0x12,0x67,0x23,0x44,0x89,0x90,0x09,0x70,0x55,0x39,0x11,0x15,0x21,0x22,0x9
9,0x33,0xAA};

unsigned int d[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Defining array containing


unsorted numbers in a and a blank array d.

for(i=0;i<20;i++) // Storing the values of array a into d to perform operation of


bubble sort to arrange the elements in descending order.

b=a[i];

d[i]=b;

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

for(j=i+1;j<20;++j) // Logic to arrange the numbers in descending


order.

if(d[i]<d[j])

c=d[i];

d[i]=d[j];

d[j]=c;

for(i=0;i<20;i++) // Storing the array d containing all the elements in


descending order back into array a.

b=d[i];

a[i]=b;
}

}
 Generate a square wave on P1.16 pin of 50-50 duty cycle of
20KHz. Explain the delay calculation in detail. (Assume crystal
of 30MHz is connected to ARM7).
Code:

#include<lpc214x.h>

void delay(void); int

main(void)
{

PINSEL1=0; // Selecting Port 1 for square wave generation.

IO1DIR=0x00010000;

while(1)

IO1SET=0x00010000; // using P1.16 for square wave generation logic 1.

delay();

IO1CLR=0x00010000; // logic 0 at P1.16.

delay();

void delay(void)

unsigned int i,j,k;

for(i=0;i<375;i++) // Loading the calculated Count value to generate the desired


delay of 0.025 msec in each half cycle.

}
Delay calculations:

XTAL connected to ARM7 is of 30MHz

frequency of oscillator = 30MHz

Time period of 1 cycle = 1/30MHz = 3.333*10^-8 sec

Required time delay =1/20KHz = 50 usec = 0.05 msec total

Since it is given that we have to consider 50% duty cycle for the wave.

i.e. Ton = 25 usec = 0.025 msec and Toff = 25usec = 0.05 msec.

Count value to be loaded in the loop = (25*10^-6)/ (3.333*10^-8)

Total count vale for full wave = 750

Count value for each half cycle = 750/2

= 375

-3:
Aim: - To develop algorithms pertaining to Simple GPIO interfacing.

 Connect eight Led’s in Common Anode from P0.10 onwards.


Demonstrate binary up counter of 8-bit in size.
Code:

#include<lpc214x.h>

void delay(int j);// Delay function declaration. int main(void)


{

unsigned int i,a=0x00000000;// Declaring variables to be used in the code.

PINSEL0=0x0000FC00; // We need to utilize pins P0.10 onwards for displaying


the binary up counter.

IO0DIR=0x0003FC00; // Declaring the respective port pins from P0.10 to


P0.17 as input pins for getting the count values.

while(a<0x0003FC00) // Until the final count is achieved loop through.

a=a+0x00000400; // Increment the counter by one.

IO0SET=a; // Send the count to the LED array.

delay(100); // finite delay between ON state and OFF state.

IO0CLR=a; // clear the count on the LED.

delay(100); // finite delay.

void delay (int j)

unsigned int l,k; for(l=0;l<j;l++)


{

for(k=0;k<5000;k++); // Using two loop logic to generate the delay.

}
 Connect two switches on P0.0 and P0.1 and a single seven
segment display in Common anode across P0.16 to P0.23 pins.
Demonstrate a Hex up and down counter where when P0.0 is
pressed count to be displayed on seven segment increases
and when P0.1 is pressed the count to be displayed decreases.
Minimum count in Hex code is 0 and maximum count in Hex
code is F.
Code:
#include<lpc214x.h>
#define switch_1
(IO0PIN&(1<<0)) #define
switch_2 (IO0PIN&(1<<1)) void
delay(int j);
int main(void)
{
unsigned int
i,a[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98,0x88,0x80,0xC6,0x
C0,0x86,0x8E};
PINSEL0=0x0007F000;
IO0DIR=0xFFF7FFFD;
while(1)
{
if(switch_1==1)
{
for(i=0;i<16;++i)
{
IO0SET=(a[i]<<16);
delay(3000);
IO0CLR=(a[i]<<16);
delay(3000);
}
}
else if(switch_2==0)
{
for(i=16;i>0;--i)
{
IO0SET=(a[i]<<16);
delay(3000);
IO0CLR=(a[i]<<16);
delay(3000);
}
}
else
{
for(i=16;i>0;++i)
{
IO0SET=(a[i]<<16);
delay(3000);
IO0CLR=(a[i]<<16);
delay(3000);
}
}
}
}

void delay(int j)
{
int b,c;
for(b=0;b<j;b++)
{
for(c=0;c<1000;c++);
}
}

You might also like