0% found this document useful (0 votes)
102 views12 pages

LED Program

The document contains code for interfacing various peripherals like LEDs, LCD, ADC, buzzer, and DAC to generate waves with a microcontroller. It includes functions for initializing and controlling the peripherals through I/O ports and timers. Code segments generate blinking LED patterns, display text on an LCD screen, read analog sensor values, produce tones with a buzzer, and output sine, square, and triangular waves using a DAC.

Uploaded by

Akshata Unkal
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)
102 views12 pages

LED Program

The document contains code for interfacing various peripherals like LEDs, LCD, ADC, buzzer, and DAC to generate waves with a microcontroller. It includes functions for initializing and controlling the peripherals through I/O ports and timers. Code segments generate blinking LED patterns, display text on an LCD screen, read analog sensor values, produce tones with a buzzer, and output sine, square, and triangular waves using a DAC.

Uploaded by

Akshata Unkal
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
You are on page 1/ 12

LED Program.

// LED's INTERFACE
#include <LPC21xx.h>
unsigned int delay=0 , i = 0 , temp = 0x00 ;
int main ()
{
PINSEL1 = 0x00000000 ;
IO0DIR = 0x0000F0000;
IO0SET = 0x000F0000 ;
while(1)

{
temp =0x00008000 ;
for( i = 0 ; i < 4 ; i++ )
{
temp <<= 1 ;
IO0CLR = temp ;

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


}
for(delay = 0 ; delay < 100000 ; delay++ );
for(delay = 0 ; delay < 100000 ; delay++ );
temp =0x00100000 ;
for( i = 0 ; i < 4 ; i++ )
{
temp >>= 1 ;
IO0SET = temp ;
for(delay = 0 ; delay < 100000 ; delay++ );
}
for(delay = 0 ; delay < 100000 ; delay++ );
for(delay = 0 ; delay < 100000 ; delay++ );
}

}
LCD INTERFACING

#include<lpc214x.h>
#include<stdio.h>

//Function prototypes
void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);
void wr_dn(void);
void lcd_data(void);

unsigned char temp1;


unsigned long int temp,r=0;
unsigned char *ptr,disp[] = "ALS BENGALURU",disp1[] = "LCD INTERFACING";

int main()
{
IO0DIR = 0x000000FC;
lcd_init();
delay(3200);
clr_disp();
temp1 = 0x81;
lcd_com();
ptr = disp;
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}

temp1 = 0xC0;
lcd_com();
ptr = disp1;
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
while(1);
}
void lcd_init(void)
{
temp = 0x30;
wr_cn();
delay(3200);

temp = 0x30;
wr_cn();
delay(3200);

temp = 0x30;
wr_cn();
delay(3200);

temp = 0x20;
wr_cn();
delay(3200);

temp = 0x28;
lcd_com();
delay(3200);

temp1 = 0x0C;
lcd_com();
delay(800);

temp1 = 0x06;
lcd_com();
delay(800);

temp1 = 0x80;
lcd_com();
delay(800);
}
void lcd_com(void)
{
temp = temp1 & 0xf0;
wr_cn();
temp = temp1 & 0x0f;
temp = temp << 4;
wr_cn();
delay(500);
}
void wr_cn(void)
{
IO0CLR = 0x000000FC;
IO0SET = temp;
IO0CLR = 0x00000004;
IO0SET = 0x00000008;
delay(10);
IO0CLR = 0x00000008;
}

void wr_dn(void)
{
IO0CLR = 0x000000FC;
IO0SET = temp;
IO0SET = 0x00000004;
IO0SET = 0x00000008;
delay(10);
IO0CLR = 0x00000008;
}

void lcd_data(void)
{
temp = temp1 & 0xf0;
temp = temp ;//<< 6;
wr_dn();
temp= temp1 & 0x0f;
temp= temp << 4;
wr_dn();
delay(100);
}

void clr_disp(void)
{
temp1 = 0x01;
lcd_com();
delay(500);
}

void delay(unsigned int r1)


{
for(r=0;r<r1;r++);
}

Sine wave
#include <LPC21xx.h>
int count=0,sinevalue,value;
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)
{
PINSEL0 = 0x00000000 ;
IO0DIR = 0x00FF0000 ;
count = 0;
while(1)
{
for(count=0;count<48;count++)
{
sinevalue = sine_tab[count];
value= 0x00FF0000 & (sinevalue << 16);
IO0PIN = value;
}
}
}

Square Wave
// program to generate square wave with DAC interface

#include <lpc21xx.h>
void delay(void);
int main ()
{
PINSEL0 = 0x00000000 ;
PINSEL1 = 0x00000000 ;
IO0DIR = 0x00FF0000 ;

while(1)
{
IO0PIN = 0x00000000;
delay();
IO0PIN = 0x00FF0000;
delay();
}
}

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

Triangular Wave
#include <LPC21xx.h>
int main ()
{
unsigned long int temp=0x00000000;
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;
}
for(i=0xFF; i!=0;i--)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}
}
}

// BUZZER INTERFACE
#include <LPC21xx.h>
void timer0_isr(void) __irq;
void timer0_init(void);
unsigned char tmr_ovflg=0x00;
int main(void)
{
PINSEL0 &= 0xCFFFFFFF ;
IO0DIR |= 0x00004000 ;
timer0_init();

while(1)
{
IO0SET = 0x00004000 ;
while(tmr_ovflg == 0x00);
tmr_ovflg = 0x00;
IO0CLR = 0x00004000 ;
while(tmr_ovflg == 0x00);
tmr_ovflg = 0x00;
}
}
void timer0_init(void)
{
T0MR0 = 3000*1000;
T0MCR = 0x00000003;
VICVectAddr0 = (unsigned long)timer0_isr;
VICVectCntl0 = 0x20|4;
VICIntEnable |= 0x00000010;
T0TCR = 0x00000001;
}
void timer0_isr(void) __irq {
T0IR |=0x01;
tmr_ovflg = 0xFF;
VICVectAddr=0;
}

// INTERNAL ADC INTERFACING(Polling method).


#include <lpc214x.h>
#include <Stdio.h>
#define vol 3.3
#define fullscale 0x3ff

void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);
void wr_dn(void);
void lcd_data(void);

unsigned int data_lcd=0;


unsigned int adc_value=0,temp_adc=0,temp1,temp2;
float temp;
char var[15],var1[15];
char *ptr,arr[]= "ADC O/P= ";
char *ptr1,dis[]="A I/P = ";

int main()
{
PINSEL1 = 0X00040000;
IO0DIR = 0x000000FC;
delay(3200);
lcd_init();
delay(3200);
clr_disp();
delay(3200);
ptr = dis;
temp1 = 0x80;
lcd_com();
delay(800);
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
ptr1 = arr;
temp1 = 0xC0;
lcd_com();
delay(800);
while(*ptr1!='\0')
{
temp1 = *ptr1;
lcd_data();
ptr1 ++;
}
while(1)
{
AD0CR = 0x01200010;
while(((temp_adc = AD0GDR) &0x80000000) == 0x00000000);
adc_value = AD0GDR;
adc_value >>=6;
adc_value &= 0x000003ff;
temp = ((float)adc_value * (float)vol)/(float)fullscale;
sprintf(var1,"%4.2fV",temp);
sprintf(var,"%3x",adc_value);

temp1 = 0x89;
lcd_com();
delay(1200);
ptr = var1;

while(*ptr!='\0')
{
temp1=*ptr;
lcd_data();
ptr++;
}
temp1 = 0xc9;
lcd_com();
delay(1200);
ptr1 = var;
while(*ptr1!='\0')
{
temp1=*ptr1;
lcd_data();
ptr1++;
}

void lcd_init()
{
temp2=0x30;
wr_cn();
delay(800);
temp2=0x30;
wr_cn();
delay(800);
temp2=0x30;
wr_cn();
delay(800);
temp2=0x20;
wr_cn();
delay(800);
temp1 = 0x28;
lcd_com();
delay(800);
temp1 = 0x0c;
lcd_com();
delay(800);
temp1 = 0x06;
lcd_com();
delay(800);
temp1 = 0x80;
lcd_com();
delay(800);
}
void lcd_com(void)
{
temp2= temp1 & 0xf0;
wr_cn();
temp2 = temp1 & 0x0f;
temp2 = temp2 << 4;
wr_cn();
delay(500);
}
void wr_cn(void)
{
IO0CLR = 0x000000FC;
IO0SET = temp2;
IO0CLR = 0x00000004;
IO0SET = 0x00000008;
delay(10);
IO0CLR = 0x00000008;
}
void wr_dn(void)
{
IO0CLR = 0x000000FC;
IO0SET = temp2;
IO0SET = 0x00000004;
IO0SET = 0x00000008;
delay(10);
IO0CLR = 0x00000008;
}

void lcd_data(void)
{
temp2 = temp1 & 0xf0;
wr_dn();
temp2= temp1 & 0x0f;
temp2= temp2 << 4;
wr_dn();
delay(100);
}

void delay(unsigned int r1)


{
unsigned int r;
for(r=0;r<r1;r++);
}
void clr_disp(void)
{
temp1 = 0x01;
lcd_com();
delay(500);
}

You might also like