0% found this document useful (0 votes)
3 views10 pages

MC Lab Programs

The document contains a list of embedded-C programs for various interfacing projects, including LED and buzzer control, motor rotation, and waveform generation. Each program is designed to demonstrate specific functionalities such as blinking LEDs, controlling motors, and generating PWM signals. The document serves as a practical guide for implementing these projects using LPC214x and LPC21xx microcontrollers.

Uploaded by

santosh.v.t
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)
3 views10 pages

MC Lab Programs

The document contains a list of embedded-C programs for various interfacing projects, including LED and buzzer control, motor rotation, and waveform generation. Each program is designed to demonstrate specific functionalities such as blinking LEDs, controlling motors, and generating PWM signals. The document serves as a practical guide for implementing these projects using LPC214x and LPC21xx microcontrollers.

Uploaded by

santosh.v.t
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

+INDEX

Marks
Expt. Page Staff
Date Title (out of
No. No. Sign
10)
1 Interfacing of LED and BUZZER

2 Interfacing of 7-segment display

Interfacing of DC and Stepper


3 motor

4 Interfacing of on-chip Timer

5 Interfacing of on-chip PWM

Blinking of LEDs using External


6 Interrupts

7 Interfacing of on-chip UART

8 Interfacing of on-chip DAC

9 Interfacing of on-chip ADC

10 Interfacing of on-board LCD

AVERAGE MARKS

1a. Embedded-C program to blink LEDs


#include<lpc214x.h>
int main()
{
int i;
PINSEL0=0X00;
IO0DIR|=(0XFF<<16);// PORT PINS P0.16 TO P0.23 (1111 1111)=FF
while(1)
{
IO0CLR |=(0XFF<<16);// LED ON
for(i=0;i<=50000;i++);
IO0SET |=(0XFF<<16); // LED OFF
for(i=0;i<=50000;i++);
}
}

1b. Embedded-C program to turn ON and OFF the buzzer


#include <LPC21xx.h>
int main ()
{
unsigned int delay=0;
IO0DIR = 0x00000200 ; // Configure P0.9 as output
while(1)
{
IO0SET = 0x00000200 ; //Buzzer ON
for(delay=0;delay<100000;delay++);
IO0CLR = 0x00000200 ; //Buzzer OFF
for(delay=0;delay<100000;delay++);
}
}

Embedded-C program to display all the digits of hexadecimal number


system on a 7-segment display when a switch is pressed

#include <LPC21XX.h>
unsigned int delay;
unsigned int 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

int main (void)


{
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
IO0DIR = 0xF0FF0000;
IO1DIR = 0x00000000;
while(1)
{
IO0SET |= ALLDISP;
IO0CLR = 0x00FF0000;
IO0SET = Disp[Switchcount];
if(!(IO1PIN & 0x00800000))
{
for(delay=0;delay<1000000;delay++)
{}
Switchcount++;
if(Switchcount == 0xA)
{
Switchcount = 0;
IO0CLR = 0xF0FF0000;
}
}
}
}

a. Embedded-C program to rotate the DC motor in clockwise and anti-


clockwise direction
#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
}
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 Anti clockwise
}

b. Embedded-C program to rotate the Stepper motor in clockwise and anti-


clockwise direction

#include <LPC21xx.h>
#define PHASEA 0x00009000
#define PHASEB 0x0000C000
#define PHASEC 0x00006000
#define PHASED 0x00003000

void Reverse1(void);
void Forward1(void);

unsigned int delay ;


unsigned int temp_var;

int main(void)
{
IO0DIR = 0x0000F000 ;
IO0SET = 0x0000F000 ;
while(1)
{
for(temp_var=0; temp_var<50;temp_var++)
Reverse1();
for(temp_var=0; temp_var<50;temp_var++)
Forward1();
}
}

void Forward1(void)
{
IO0SET = PHASEA ;
for(delay=0; delay<100000; delay++) ;

IO0SET = PHASEB ;
for(delay=0; delay<100000; delay++) ;

IO0SET = PHASEC ;
for(delay=0; delay<100000; delay++) ;
IO0SET = PHASED ;
for(delay=0; delay<100000; delay++) ;
}

void Reverse1(void)
{
IO0SET = PHASED ;
for(delay=0; delay<100000; delay++) ;

IO0SET = PHASEC ;
for(delay=0; delay<100000; delay++) ;

IO0SET = PHASEB ;
for(delay=0; delay<100000; delay++) ;

IO0SET = PHASEA ;
for(delay=0; delay<100000; delay++) ;
}

Embedded-C program to blink the LEDs using on-chip Timer


#include<lpc214x.h>
/* Declaring the delay function */
void delay (unsigned int);
/* Function definition of delay function */
void delay (unsigned int d)
{
T0TCR=0x02; // Reset the Timer
T0TCR=0x01; // Enable the Timer
while (T0TC < d); //Wait till the TC reaches the desired delay
T0TCR=0x00; // Disable the Timer
}
int main()
{

PINSEL0=0x00;
/* Configure all pins of PORT 0 as outputs */
IO0DIR=0x00FF0000;
/* Initialize the PLL0 and configure it to produce 60 MHz System Clock (CCLK) and Peripheral Clock (PCLK)
*/
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xaa;
PLL0FEED = 0x55;
while (!(PLL0STAT & 0x00000400)); //while((PLL0STAT&(1<<10))==0);
PLL0CON = 0x03;
PLL0FEED = 0xaa;
PLL0FEED = 0x55;
VPBDIV = 0x01;
/* Initialize the Timer */
T0CTCR=0x00;
T0PR=59999;
T0MR0=1000;
T0TCR=0x02;
while (1)
{ IO0SET=0x00FF0000; // IO0SET |= (0xFF<<16)
delay(1000);
IO0CLR=0x00FF0000; // IO0CLR |= (0xFF<<16)
delay(1000);
}
}

Embedded-C program to generate PWM wave of required duty cycle


#include <lpc214x.h>
void pll(void)
{
PLL0CON=0x01;
PLL0CFG=0X24;
PLL0FEED=0XAA;
PLL0FEED=0X55;
while(!(PLL0STAT&0X0400));
PLL0CON=0X03;
PLL0FEED=0XAA;
PLL0FEED=0X55;
VPBDIV=0X01;
}

int main(void)
{
pll();

PINSEL0 = (PINSEL0 & ~(1 << 16)) | (1 << 17); // Select PWM4 output for Pin0.8
PWMPCR = 0x0; //Select Single Edge PWM
PWMPR = 60-1; // 1 micro-second resolution
PWMMR0 = 1000; // 10ms period duration
PWMMR4 = 500; // 0.5ms - pulse duration i.e width (Brigtness level)
PWMMCR = (1<<1); // Reset PWMTC on PWMMR0 match
PWMLER = (1<<0)|(1<<4); // update MR0 and MR4
PWMPCR = (1<<12); // enable PWM output
PWMTCR = (1<<1) ; //Reset PWM TC & PR
PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode
}
Embedded-C program to blink the LEDs using external interrupt
#include<lpc214x.h>

unsigned int delay1=0;


void Extint0_Isr(void) __irq; //declaration of ISR

int main(void)
{
IO0DIR =0X00FE0000;
IO0SET =0X00FF0000;
IO1DIR =0X02000000;

PINSEL1 =0X00000001; //Setup P0.16 to alternate function EINT0


EXTMODE =0x01; //edge i.e falling egge trigger and active low
EXTPOLAR= 0X00;
VICVectAddr0 = (unsigned long) Extint0_Isr; //Assign the EINT0 ISR function
VICVectCntl0 = 0x20 | 14; //Assign the VIC channel EINT0 to interrupt priority 0
VICIntEnable |= 0x00004000; //Enable the EINT0 interrupt

while(1) //waiting for interrupt to occur


{
for(delay1=0;delay1<800;delay1++); //some delay to sense interrupt.
}
}

void Extint0_Isr(void)__irq //whenever there is a low level on EINT0


{
unsigned int delay;
IO1PIN =~ IO1PIN;
IO0PIN =~ IO0PIN; //0X00FE0000; //set high on port lines(P0.17-P0.23 i.e. Led3-Led10)
for(delay = 0; delay<1650; delay++); //delay
EXTINT |=0X00000001; //Clear interrupt
VICVectAddr=0; //Acknowledge Interrupt
}
#define THRE (1<<5) // Transmit Holding Register Empty
#define RDR (1<<0) // Receiver Data Ready
#include <LPC21XX.h>
int main()
{
VPBDIV=0x0;
char msg[10];
int i;
PINSEL0= 0x00000005; // Make P0.0 as TXD and P0.1 RXD of UART0
U0LCR = 0x83; // Line control Register DLAB enabled for Baud Rate calculation
U0DLM = 0x00; U0DLL = 0x97;
U0LCR = 0x03;
for(i=0;i<10;i++)
{
while( !(U0LSR & RDR ) ); // Check if U0RBR is empty or not when its not wait
msg[i]=U0RBR; }
for(i=0;i<10;i++)
{
while ( !(U0LSR & THRE ) ); // Now wait till THRE is empty.
U0THR=msg[i];
}
}

Square wave
// Program to generate square wave
#include<LPC21xx.h>
void delay()
{
int i;
for(i=0;i<5000;i++);
}
int main()
{
int i;
PINSEL1=(0X02<<18);
while(1)
{
DACR=(1<<16) | (0xff<<6);
delay();
DACR=(1<<16) | (0x00<<6);
delay();
}
}
b: Triangular waveform
#include<LPC21xx.h>
int main()
{
int i;
PINSEL1=(0X02<<18);
while(1)
{
for(i=0;i<=0x3FF;i++)
{
DACR=(1<<16) | (i<<6);
}
for(i=0x3FF;i>=0;i--)
{
DACR=(1<<16) | (i<<6);
}
}}
c: Ramp waveform
#include<LPC21xx.h>
int main()
{
int i;
PINSEL1=(0X02<<18);
while(1)
{
for(i=0;i<=0x3FF;i++)
{
DACR=(1<<16) | (i<<6);
}
}
}
d: Sine waveform
#include <LPC21xx.h>
// Sine Look Up table consider Vref as 5 volts Vout=5+5sin(angle)
// angle in degrees sin(angle) Vout digital value Vout*25.6 Hex Value
// 0 0 5 128 0x80
int count=0;
unsigned char sine_tab[49]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,
0xFF,0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,
0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,
0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80};
int main(void)
{
PINSEL1|=1<<19;
count = 0;
while(1)
{
for(count=0;count<48;count++)
{
DACR = (1<<16) |(sine_tab[count]<<6) ;
}
}
}

You might also like