0% found this document useful (0 votes)
88 views

Hardware Lab Programs

The program initializes the port pins of the microcontroller that are connected to the 7-segment display as output. It then uses a for loop to display the hexadecimal digits from 0 to F on the display with a delay between each digit. For each digit, the corresponding port pins are set to display that digit on the 7-segment display. After displaying all digits, it repeats the process continuously.

Uploaded by

H S Parangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Hardware Lab Programs

The program initializes the port pins of the microcontroller that are connected to the 7-segment display as output. It then uses a for loop to display the hexadecimal digits from 0 to F on the display with a delay between each digit. For each digit, the corresponding port pins are set to display that digit on the 7-segment display. After displaying all digits, it repeats the process continuously.

Uploaded by

H S Parangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Microcontroller and Embedded Systems Lab 18CSL48

9. Display “Hello World” message using Internal UART.

Aim: Write a program to Display “Hello World” message using Internal UART.

Program: [/* Use ALS Kit only*/]


#include<lpc214x.h>

void uart_init(void);
unsigned int delay;
unsigned char *ptr;
unsigned char arr[]="HELLO WORLD\r";

int main()
{
while(1)
{
uart_init();
ptr = arr;
while(*ptr!='\0')
{
U0THR=*ptr++;
while(!(U0LSR & 0x40)== 0x40);
for(delay=0;delay<=600;delay++);
}
for(delay=0;delay<=60000;delay++);
}
}
void uart_init(void)
{
PINSEL0=0X0000005; //select TXD0 and RXD0 lines
U0LCR = 0X00000083; //enable baud rate divisor loading and
U0DLM = 0X00; //select the data format
U0DLL = 0x13; //select baud rate 9600 bps
U0LCR = 0X00000003;

Dept. of Computer Science & Engineering Page 2


Microcontroller and Embedded Systems Lab 18CSL48

10. Interface and Control a DC Motor.

Aim: Write a program to Interface and Control a DC Motor.

Program: [/* Use ALS Kit only*/]

#include<lpc214x.h>

void clock_wise(void);
void anti_clock_wise(void);
unsigned int j=0;

int main()
{
IO0DIR= 0X00000900;
IO0SET= 0X00000100; //P0.8 should always high.

while(1)
{
clock_wise();
for(j=0;j<400000;j++); //delay
anti_clock_wise();
for(j=0;j<400000;j++); //delay
} //End of while(1)
} //End of Main

void clock_wise(void)
{
IO0CLR = 0x00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000900; //Selecting the P0.11 line for clockwise
//and turn on motor
}

void anti_clock_wise(void)
{
IO0CLR = 0X00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000100; //not selecting the P0.11 line for Anticlockwise
}

Dept. of Computer Science & Engineering Page 3


Microcontroller and Embedded Systems Lab 18CSL48

11. Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.

Aim: Write a Program to Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.

Program:
// Description: Stepper Motor Rotating Clockwise direction after certain
delay rotating Anticlockwise direction.
// Pin Connection: P1.28 to P1.31 connected to Stepper motor driver
(ULN2003/2803)
//************************************************************************

#include<lpc214x.h> // Header file for LPC2148

//**** Function Declaration ********//

void delay(void);
void stepper_clock(void);
void stepper_Aclock(void);

//*************** END of Function Declaration **************//

//*************** MAIN Program **************//

int main()
{
PINSEL2 = 0x00000000; // P1.16 to P1.31 configured as GPIO

IODIR1 = 0xFFFFFFFF; // P1.16 to P1.31 configured as ouput port

while(1)
{
stepper_clock(); // Function calling (P1.28 to P1.31 port pins
//connected to stepper motor)
delay();

stepper_Aclock();
delay(); // Function calling (P1.28 to P1.31 port pins
//connected to stepper motor)
}
}

//*************** END of MAIN Program **************//

//*************** Delay Program **************//

void delay(void)
{
unsigned int i,j;

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

Dept. of Computer Science & Engineering Page 4


Microcontroller and Embedded Systems Lab 18CSL48

}
//*************** END of Delay Program **************//

//********* Stepper MOtor Clockwise and Anti clockwise Program**********//

void stepper_Aclock(void) //Stepper Motor Anti-Clockwise subprogram


{
unsigned int i;

for(i=0;i<5;i++)
{
IOSET1 = 0x90000000;
delay();
IOCLR1 = 0x90000000;
delay();
IOSET1 = 0xC0000000;
delay();
IOCLR1 = 0xC0000000;
delay();
IOSET1 = 0x60000000;
delay();
IOCLR1 = 0x60000000;
delay();
IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
}
}

void stepper_clock(void) /Stepper Motor Clockwise subprogram


{
unsigned int i;

for(i=0;i<5;i++)
{
IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
IOSET1 = 0x60000000;
delay();
IOCLR1 = 0x60000000;
delay();
IOSET1 = 0xC0000000;
delay();
IOCLR1 = 0xC0000000;
delay();
IOSET1 = 0x90000000;
delay();
IOCLR1 = 0x90000000;
delay();

Dept. of Computer Science & Engineering Page 5


Microcontroller and Embedded Systems Lab 18CSL48

IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
}
}

//**** END of Stepper Motor Clockwise and Anti clockwise Program***********//

Dept. of Computer Science & Engineering Page 6


Microcontroller and Embedded Systems Lab 18CSL48

13. Interface a DAC and generate Triangular and Square waveforms.

Aim: Write a Program to Interface a DAC and generate Triangular and Square waveforms.

Program: [/* Use ALS Kit only*/]


(i) TRIANGULAR WAVEFORM
#include <LPC21xx.h>
unsigned long int temp=0x00000000;

int main()
{
unsigned int i=0;

IO0DIR=0x00FF0000;

while(1)
{
// output 0 to FE
for(i=0;i!=0xFF;i++)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}
// output FF to 1
for(i=0xFF; i!=0;i--)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}
}//End of while(1)
}//End of main()

(ii) SQUARE WAVEFORM


#include <lpc21xx.h>
unsigned int var;
void delay(void);

int main()
{
PINSEL0 = 0x00000000 ; // Configure P0.0 to P0.15 as GPIO
PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO
IO0DIR = 0x00FF0000 ;

while(1)
{
IO0PIN = 0x00000000;

Dept. of Computer Science & Engineering Page 7


Microcontroller and Embedded Systems Lab 18CSL48

var= 0x00000000;
delay();
IO0PIN = 0x00FF0000;
var= 0x00FF0000;
delay();
}
}

void delay(void)
{
unsigned int i=0;
for(i=0;i<=95000;i++);
}

Dept. of Computer Science & Engineering Page 8


Microcontroller and Embedded Systems Lab 18CSL48

15. Demonstrate the use of an external interrupt to toggle an LED On/Off.


Aim: Write a program to demonstrate the use of an external interrupt to toggle an LED On/Off.

Program:
// Description: Using PORT-1 all port pins of are making HIGH with certain
// delay and all port pin made LOW.
#include<lpc214x.h> // Header File for LPC2148

void delay(void); // function declaration (delay function declaring)(no


//return type and no argument passing)

int main()
{
PINSEL2 = 0x00000000; //P1.16 to P1.31 configured as GPIO using PINSEL1 reg.

IODIR1 = 0xFFFF0000; // P1.16 to P1.31 as OUTPUT Port

while(1)
{
IOSET1 = 0xFFFF0000; // P1.16 to P1.31 set to high
delay(); // function call (delay function calling)
IOCLR1 = 0xFFFF0000; // P1.16 to P1.31 set to low
delay(); // function call (delay function calling)
}
}

//************* END of MAIN PROGRAM *****************************//

//****************** Delay Function Starts **********//

void delay(void) // Function Definition


{
unsigned int i,j; // Variable declaration

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

//****************** END of Delay Function **********//

Dept. of Computer Science & Engineering Page 9


Microcontroller and Embedded Systems Lab 18CSL48

16. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay
intermediately

Aim: Write a program to Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate
delay intermediately. a = P0.10
a
b = P0.11
----
c = P0.12
f| g |b
d = P0.13
|----|
e = P0.18
e| |c
f = P0.19
---- . dot
g = P0.20
d
dot = P0.21
Program (/*Use ALS Kit only*/)
#include <LPC21xx.h>
unsigned int delay, count=0, Switchcount=0;
unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,0x006D0000, 0x007D0000, 0x00070000, 0x007F0000, 0x006F0000,
0x00770000,0x007C0000, 0x00390000, 0x005E0000, 0x00790000, 0x00710000 };
#define SELDISP1 0x10000000 //P0.28
#define SELDISP2 0x20000000 //P0.29
#define SELDISP3 0x40000000 //P0.30
#define SELDISP4 0x80000000 //P0.31
#define ALLDISP 0xF0000000 //Select all display
#define DATAPORT 0x00FF0000 //P0.16 to P0.23: Data lines to drive 7-Segments

int main (void)


{
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
IO0DIR = 0xF0FF0000;
IO1DIR = 0x01000000;

while(1)
{ //Display values on Seven Segment
IO0SET |= ALLDISP;
IO0CLR = 0x00FF0000;
for(delay=0;delay<100;delay++)
IO0SET = Disp[Switchcount]; // displays values 0 to F one after other
for(delay=0;delay<1000000;delay++)
{
}
Switchcount++;
if(Switchcount == 16) // after F go back to 0
{
Switchcount = 0;
}
}}

Dept. of Computer Science & Engineering Page 10

You might also like