Hardware Lab Programs
Hardware Lab Programs
Aim: Write a program to Display “Hello World” message using Internal UART.
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;
#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
}
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)
//************************************************************************
void delay(void);
void stepper_clock(void);
void stepper_Aclock(void);
int main()
{
PINSEL2 = 0x00000000; // P1.16 to P1.31 configured as GPIO
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)
}
}
void delay(void)
{
unsigned int i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
}
//*************** END of Delay Program **************//
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();
}
}
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();
IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
}
}
Aim: Write a Program to Interface a DAC and generate Triangular and Square waveforms.
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()
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;
var= 0x00000000;
delay();
IO0PIN = 0x00FF0000;
var= 0x00FF0000;
delay();
}
}
void delay(void)
{
unsigned int i=0;
for(i=0;i<=95000;i++);
}
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
int main()
{
PINSEL2 = 0x00000000; //P1.16 to P1.31 configured as GPIO using PINSEL1 reg.
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)
}
}
for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
}
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
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;
}
}}