0% found this document useful (0 votes)
28 views9 pages

Micro Controllers Codes

The document contains programs for various microcontroller applications including LED blinking, switch input, LCD display, 7 segment display, PWM output, DAC output, UART communication, timer interrupts and external interrupts. All programs utilize LPC2148 microcontroller peripherals.

Uploaded by

The Creator --
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

Micro Controllers Codes

The document contains programs for various microcontroller applications including LED blinking, switch input, LCD display, 7 segment display, PWM output, DAC output, UART communication, timer interrupts and external interrupts. All programs utilize LPC2148 microcontroller peripherals.

Uploaded by

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

Flash Program:

#include<lpc21xx.h>
#include<stdio.h>
void delay()
{
int i;
for(i=0; i<=0xefff; i++);
}
int main()
{
PINSEL1 = 0x00000000;
IO1DIR = 0x00ff0000;
while(1)
{
IO1SET=0x00ff0000;
delay();
IO1CLR=0x00ff0000;
delay();
}
}
----------------------------------x------------------------------------------
Swap Program:

#include<lpc21xx.h>
#include<stdio.h>
void delay()
{
int i;
for(i=0; i<=0xefff; i++);
}
int main()
{
PINSEL1 = 0x00000000;
IO1DIR = 0xffffffff;
while(1)
{
IO1SET=0x00f00000;
delay();
IO1CLR=0x00f00000;
delay();
IO1SET=0x000f0000;
delay();
IO1CLR=0x000f0000;
delay();
}
}
---------------------------------x------------------------------------------
Rotate Program:
#include<lpc21xx.h>
#include<stdio.h>
void delay()
{
int i;
for(i=0;i<0xefff;i++);
}
int main()
{
int i;
while(1)
{
for(i=0;i<=31;i++)
{
IO1SET=0x00000001<<i;
delay();
IO1CLR=0x00000001<<i;
delay();
}
}
}
--------------------------x---------------------------------
LED Program

#include<lpc21xx.h>
#include<stdio.h>
void delay()
{
int i;
for(i=0; i<=0xeffff; i++);
}
int main()
{
PINSEL1 = 0x00000000;
IO1DIR = 0x000F0000;
while(1)
{
IO1SET=0x000F0000;
delay();
IO1CLR=0x000F0000;
delay();
}
}
-----------------------------------x-----------------------
Switch Program:
#include<lpc21xx.h>
#include<stdio.h>
int main() {
IO1DIR = 0x00010000;
IO0DIR = 0x00;
while(1) {
if((IO0PIN & (0x00010000))==0)
IO1CLR= 0x00010000;
else
IO1SET= 0x00010000;
}
}
-------------------------------x------------------------------
LCD-1 Programming:
#include<lpc214x.h>
#define RS 0x02000000
#define EN 0x01000000
unsigned char msg[]="WELCOME";
void lcd_delay()
{
int i;
for(i=0;i<100000;i++);
}
void lcd_cmd(unsigned char cmd)
{
IO1CLR = 0x03FF0000;
IO1SET = cmd<<16;
IO1CLR=RS;
IO1SET=EN;
lcd_delay();
IO1CLR=EN;
lcd_delay();
}
void lcd_init()
{
PINSEL2 = 0x00000000;
IO1DIR = 0x03FF0000;
lcd_cmd(0x01);
lcd_cmd(0x38);
lcd_cmd(0x06);
lcd_cmd(0x0E);
lcd_cmd(0x80);
}
void lcd_data(unsigned char data)
{
IO1CLR = 0x03FF0000;
IO1SET = data<<16;
IO1SET=RS;
IO1SET=EN;
lcd_delay();
IO1CLR=EN;
lcd_delay();
}
int main()
{
int i;
lcd_init();
for(i=0;msg[i]!='\0';i++)
lcd_data(msg[i]);
}
-----------------------------------xxx-----------------------------------
7 Segment Display:
#include<lpc21xx.h>
unsigned int i, j;
unsigned int
display[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x98};
void delay()
{
for(j=0;j<100000;j++);
}
int main()
{
PINSEL0=0X00000000;
IO0DIR =0X000000FF;
while(1)
{
for(i=0;i<10;i++)
{
IO0SET =display[i];
delay();
IO0CLR =display[i];
}
}
}
-----------------------------xxx-----------------------------------------
pLL Program:
#include<lpc214x.h>
#define PLOCK 0x00000400
void led_on()
{
IO1SET=0x00FF0000;
}
void led_off()
{
IO1CLR= 0x00FF0000;
}
void delay(int count)
{
int i;
for(i=0;i<count;i++);
}
int main(void)
{
PLL0CON=0X01;
PLL0CFG=0X24;
PLL0FEED=0XAA;
PLL0FEED=0X55;
while(!(PLL0STAT & PLOCK));
PLL0CON=0x03;
PLL0FEED=0XAA;
PLL0FEED=0X55;
PINSEL1=0x0000000;
IO1DIR=0x00FF0000;
while(1)
{
led_on();
delay(12000000);
led_off();
delay(12000000);
}
}
---------------------------------------xxx------------------------------------
Full Step – Counter-Clockwise Rotation:
#include <lpc214x.h>
void delay()
{
int i;
for(i=0; i<=100000; i++);
}
int main(void)
{
PINSEL1 = 0x00000000;
IODIR0 = 0xF0000000;
while(1)
{
IOCLR0 = 0x60000000;
IOSET0 = 0x50000000;
delay();
IOCLR0 = 0x50000000;
IOSET0 = 0x90000000;
delay();
IOCLR0 = 0x90000000;
IOSET0 = 0xA0000000;
delay();
IOCLR0 = 0xA0000000;
IOSET0 = 0x60000000;
delay();
}
}
--------------------------------xxx---------------------------
DAC Program
(i) Square Signal
#include <lpc214x.h>
#include <stdio.h>
void delay()
{
int i;
for(i=0;i<1000000;i++);
}
int main(void)
{
PINSEL1=0x80000;
IODIR0=0x80000;
while(1)
{
DACR=(0x03FF<<6); //DACR=0xFFC0;
delay();
DACR=(0x0000<<6); //DACR=0x0000;
delay();
}
}
------------------------------xxx-------------------------------------
DAC Program
(ii) Ramp Signal
#include <lpc214x.h>
#include <stdio.h>
void delay()
{
int i;
for(i=0;i<1000000;i++);
}
int main(void)
{
int i;
PINSEL1=0x80000;
IODIR0=0x80000;
while(1)
{
for(i=0;i<=0x3FF;i++)
{
DACR=(i<<6);
}
}
}
-----------------------------------xxx----------------------------------------
DAC Program
(iii) Triangular Signal
#include <lpc214x.h>
#include <stdio.h>
void delay()
{
int i;
for(i=0;i<1000000;i++);
}
int main(void)
{
int i;
PINSEL1=0x80000;
IODIR0=0x80000;
while(1)
{
for(i=0;i<=0x3FF;i++)
DACR=(i<<6);
for(i=0x3FF;i>=0;i--)
DACR=(i<<6);
}
}
--------------------------------xxx-------------------------------------------
UART0 Reception and UART1 Transmission
#include<lpc214x.h>
#include<stdio.h>
int main()
{
unsigned char ch;
PINSEL0 = 0x00050005;
U0LCR = 0x83;
U0DLM = 0x00;
U0DLL = 97;
U0LCR = 0x03;
U1LCR = 0x83;
U1DLM = 0x00;
U1DLL = 97;
U1LCR = 0x03;
while(1)
{
while(!(U0LSR & 0x01));
while(!(U1LSR & 0x20));
ch=U0RBR;
if (U0RBR == '\r')
{
U1THR = '\n';
U0THR = '\n';
}
U0THR=ch;
U1THR=ch;
}
}
--------------------------------xxx-------------------------------
UART0 Reception and UART0 Transmission:
#include<lpc214x.h>
#include<stdio.h>
unsigned char RxD()
{
while(!(U0LSR & 0x01));
return(U0RBR);
}
void TxD (unsigned char c)
{
while(!(U0LSR & 0x20));
U0THR=c;
}
int main()
{
unsigned char ch;
PINSEL0=5;
U0LCR=0x83;
U0DLM=0x00;
U0DLL=97;
U0LCR=0x03;
while(1)
{
ch=RxD();
TxD (ch);
}
}
----------------------------xxx--------------------------------------
Timer0 – 1 Second Delay using Match Register
#include<lpc214x.h>
#define PLOCK 0x00000400
void PLL(void)
{
PLL0CON=0x01;
PLL0CFG=0x24;
PLL0FEED=0xAA;
PLL0FEED=0x55;
while(!(PLL0STAT&PLOCK));
PLL0CON=0x03;
PLL0FEED=0xAA;
PLL0FEED=0x55;
VPBDIV=0x01;
}
void delay()
{
T0CTCR=0X00;
T0PR=59999;
T0MR0=1000;
T0MCR=0x00000004;
T0TCR=0X02;
T0TCR=0X01;
while(T0TC!=T0MR0);
T0TCR=0x00;
T0TC=0x00;
}
int main(void)
{
IO1DIR=0x000F0000;
PLL();
while(1)
{
IO1SET=0x000F0000;
delay();
IO1CLR=0x000F0000;
delay();
}
}
-----------------------xxx-----------------------------------------------
External Interrupt Programming :
#include <lpc21xx.h>
void delay()
{
int i;
for(i=0; i<=0xefffff; i++);
}
void INT0ISR()__irq
{
int int1;
int1 = EXTINT;
if ((int1&0x01)==0x01)
{
IO1CLR = 0x00400000;
delay();
IO1SET = 0x00400000;
delay();
}
EXTINT = 0x01;
VICVectAddr = 0x00;
}
void init_interrupt()
{
VICIntEnable = 0x01<<14;
VICVectCntl0 = 0x20|14;
VICVectAddr0 = (unsigned long)INT0ISR;
PINSEL1 = 0x00000001;
EXTMODE = 0x01;
EXTPOLAR = 0x01;
}
int main(void)
{
PINSEL2 = 0x00000000;
IO1DIR = 0x00C00000;
init_interrupt();
while (1)
{
IO1SET = 0x00800000;
delay();
IO1CLR = 0x00800000;
delay();
}
}
------------------------xxx----------------------------
PWM – 50% and 80% Duty Cycle
#include<LPC214x.h>
int main(void)
{
PINSEL0 = 0x8002;
PWMPCR = 0x600;
PWMMR0 = 150000;
PWMMR1 = 75000;
PWMMR2 = 120000;
while(1)
{
PWMMCR = 0x02;
PWMLER = 0x06;
PWMTCR = 0x0A;
PWMTCR = 0x09;
}
}
-----------------------xxx-----------------------------------

You might also like