You are on page 1of 12

Sub: Embedded Controller Lab Sub Code: 15ECL67

List of Laboratory Experiments


1. Write a C program for interfacing of “LED” – 8 no’s to a GPIO of Cortex M3
Controller to (i) blink LEDs (ii) Scroll LED blink.

2. Write a C program to interface of toggle switch to a GPIO of Cortex M3 Controller.


i. Switch Status to be displayed on Buzzer.
ii. Switch status to be displayed on Relay.
3. Write a C program for usage of an external interrupt to toggle an LED on/off.

4. Write a C program for usage of Internal UART - To display the message “HELLO
WORLD”

5. Write a C program for usage of internal DAC of cortex M3 controller to generate

[i] Triangular waveform. [ii] Square waveform.

6. Write a C Program to interface a Stepper motor to a GPIO to rotate it in


clockwise and Anti-clockwise directions.

7. Write a C program for the usage of Internal PWM to generate PWM and
vary its duty cycle

8. Write a C program to interface a DC Motor to GPIO to rotate Anti-clockwise and


clockwise directions.

9. Write a C program to interface a seven segment module to display the counter value
from 0 to F and 0 to 9

10. Write a C program to interface a LCD module to display the message “ ACHARYA”.

11. Write a C program to interface a 4x4 hex keypad and display the keycode on a LCD
screeen.
12. Write a C program for the usage of internal ADC of Cortex M3 controller to
determine digital output for a given Analog input and display the same on LCD.
1. Write a C program for interfacing of “LED” – 8 no’s to a GPIO of Cortex M3 Controller to (i)
blink LEDs (ii) Scroll LED blink.

(i) blink LEDs

#include <LPC17xx.h>
int main()
{
LPC_GPIO0- >FIODIR = 0x0FF0; // P0.4-P0.11 pins as o/p.
while(1) //infinite loop
{
int i;
LPC_GPIO0 - >FIOSET = 0x0FF0; // SET po.4 to p0.11 LEDS Turns ON
for (i=2000000; i >0; i- -); // delay
LPC_GPIO0 - >FIOCLR = 0x0FF0; //clear P0.4 to P.11 LEDS Turns OFF
for (i=2000000;i>0; i--); //delay
}
}

1.b to Scroll LEDs (Ring Counter)

#include <lpc17xx.h>
unsigned int x, y, z;
int main(void)
{
LPC_GPIO0->FIODIR = 0x0FF0; // Configure P0.4 to p0.11 as Output
while(1) //infinite loop
{
z = 0x0800; // initialize with 1000 0000 0000;
for (x=8; x > 0; x - - )
{
LPC_GPIO0 - > FIOPIN = z; // Initially P0.11 to turn LED ON
for (y=1000000; y > 0; y- -); // delay
z = z >> 1; // right shift Z content by 1 time
}
}
}
2. Write a C program to interface of toggle switch to a GPIO of Cortex M3 Controller.

i. Switch Status to be displayed on Buzzer.


ii. Switch status to be displayed on Relay.
Switch Status to be displayed on Buzzer.
#include <LPC17xx.h>

int main()
{
LPC_GPIO3->FIODIR = 1<< 25; // port 3.25 pin is configured as o/p
LPC_GPIO1->FIODIR & = ~ (1<< 21); // port p1.21 pin is configured as i/p

while(1)
{
if (! (LPC_GPIO1->FIOPIN & (1<< 21))) // if switch P.21 pressed
{
LPC_GPIO3->FIOSET = (1<< 25); // Port 3.25 pin is set [ High]

}
else

{
LPC_GPIO3->FIOCLR = (1<< 25); // Port 3.25 pin is Clear [Low]

}
}
}

Switch status to be displayed on Relay.


#include <LPC17xx.h>

int main()
{
LPC_GPIO0->FIODIR = 1<< 25; // port 0.25 pin is configured as o/p
LPC_GPIO1->FIODIR & = ~ (1<< 21); // port p1.21 pin is configured as i/p

while(1)
{
if (! (LPC_GPIO1->FIOPIN & (1<< 21))) // if switch P.21 pressed
{
LPC_GPIO0->FIOSET = (1<< 25); // Port 0.25 pin is set [ High]

}
else

{
LPC_GPIO0->FIOCLR = (1<< 25); // Port 0.25 pin is Clear [Low]

}
}
}
3. Write a C program for usage of an external interrupt to toggle an LED on/off.
#include <lpc17xx.h>
void EINT0_IRQHandler(void)
{
LPC_SC->EXTINT = 1; /* Clear Interrupt Flag ext int 0 */
LPC_GPIO0->FIOPIN ^ = (1<<4); /* Toggle the LED1 everytime INTR0 is generated */
}
int main( )
{
LPC_SC->EXTINT = 1; /* Clear Pending interrupts */
LPC_PINCON->PINSEL4 = (1<< 20); /* Configure P2_10 as EINT0 */
LPC_GPIO0->FIODIR = (1<<4); /*configure LED pins as OUTPUT */
NVIC_EnableIRQ(EINT0_IRQn); /* Enable the EINT0 interrupts */
while(1)
{ // any main program running
}
}
4. Write a C program for usage of Internal UART - To display the message “HELLO
WORLD”

#include "LPC17xx.h"

void UART0_Init (void)


{

LPC_PINCON->PINSEL0 |= (1 << 4)| (1 << 6); //Pin P0.2 used as TXD0 & Pin P0.3 used as RXD0
LPC_UART0->LCR = 0x83; /* 8 bits, no polarity, 1stop bit */
LPC_UART0->DLM = 0;
LPC_UART0->DLL = 9; // 115200 baus rate @25MHz clk
LPC_UART0->LCR = 0x03; /* Lock the baud rate */
LPC_UART0->FDR = 0x21; //MULVAL=2,DIVADDval=1
}

int UART0_SendChar (int Data)


{
while (!(LPC_UART0->LSR & 0x20)); //if LSR.5th bit =0 wait for valid data.
return (LPC_UART0->THR = Data); // LSR.5th bit =1 ,THR has valid data
}

void UART0_SendString (unsigned char *s)


{
while (*s != 0) // if content of string is not 0
UART0_SendChar(*s++); //send char and auto increment i.e s=s+1
}

int main(void)
{
char ch;
UART0_Init( ); //initialize UART0
while(1)
{
UART0_SendString("hello world! "); //send to uart terminal
}
}
5. Write a C program for usage of internal DAC of Cortex M3 controller to generate
[i]Triangular wave [ii]Square waveform.
[i]Triangular wave
#include <LPC17xx.H
int main (void)
{
int i, m;

LPC_PINCON->PINSEL1 = (1<< 21); //Select AOUT function for P0.26

while(1)
{
for (i = 0; i <= 1023; i++) //send value start from 0 to 1023 insteps of 1
{
LPC_DAC->DACR = (i << 6); // DACR register value should placed in 15:6
for(m = 10; m > 1; m--); //delay to vary freq
}
for (i = 1023 ; i > 0; i--) //once reaches max of 3.3v starts decreasing
{
LPC_DAC->DACR = (i << 6);
for(m = 10; m > 1; m--);
}
}

[ii]Square waveform.
#include <LPC17xx.H>

int main (void)


{
int i;

LPC_PINCON->PINSEL1 = (1<<21) ; //Select AOUT function for P0.26

while (1)
{
LPC_DAC -> DACR = (1024 << 6); // 10bit = 1024 Vmax = 3.3 V
for (i = 120000; i > 1; i--); // maintain this value for some delay

LPC_DAC -> DACR = (512 << 6); // for Vmax/2 = 1.74V


for (i = 120000; i > 1; i--); // delay
}
}
6. Write a C Program to interface a Stepper motor to a GPIO to rotate it in
[i]clockwise direction and [ii]clockwise direction.

[i]clockwise direction
#include <LPC17xx.H>

int main (void)


{
unsigned int i,k, z;
LPC_GPIO1->FIODIR = (1<< 22)|(1<< 23)|(1<< 24)|(1<< 25);

while (1)
{
k = 0x02000000; //initially P.25 =1 [ 1000 ]
for(z=0; z<4; z++) // four coils to be made high one by one
{
LPC_GPIO1->FIOPIN =k; //Port1 pins assigned with Ka value
for ( i=120000; i >0 ; i-- ) ; // Delay
k = k>>1; // k content rotate right
}
}
}

[ii]Anti clockwise direction

#include <LPC17xx.H>

int main (void)


{
unsigned int i k, z;
LPC_GPIO1->FIODIR = 0x03C00000;
while (1)
{
k = 0x00400000; // initialize p1.22 as high [0001]
for(z=0; z<4; z++) // executes 4 times
{
LPC_GPIO1->FIOPIN =k; //port 1 pins assigned with k value
for (i=120000 ; i >0 ; i--) ; //delay
k = k<<1; // rotate left shift by 1
}
}
}
7. Write a C program for the usage of Internal PWM to generate PWM and
vary its duty cycle

#include <lpc17xx.h>
int main(void)
{
int j,DutyCycle;
LPC_PINCON->PINSEL7 = 3 << 20; /* Cofigure pin P3.26 for PWM mode. */
LPC_PWM1->TCR = (1<< 0) | (1<< 2); //enable counter and PWM
LPC_PWM1->MCR = (1<< 1); /*Reset on PWMMR0, reset TC if it matches MR0 */
LPC_PWM1->MR0 = 100; /* set PWM cycle(Ton+Toff)=100) */
LPC_PWM1->PCR = 1 << 11; /* Enable the PWM output pin for PWM_3 */

while(1)
{
for(DutyCycle=0; DutyCycle < 100; DutyCycle++)
{
LPC_PWM1->MR3 = DutyCycle; /* Increase the DutyCycle from 0-100 */
for(j=0;j<1000000;j++);
}
}
}
8 .Write a C program to interface a DC Motor to GPIO to rotate [i] Anti clockwise and [ii] Clockwise
directions.

[i] Anti Clockwise rotation

#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR = (1<<8); //Configure the PORT2 pins as OUTPUT
LPC_GPIO4->FIODIR = (1<<28)|(1<<29); //configure port4 pins p4.28,p4.29 as output

while(1)
{
LPC_GPIO2->FIOSET=(1<<8); //enable DC motor by making P2.8=1
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOCLR = (1<<29); // P4.29 = 0 Anti Clockwise rotation
}
}

[ii] Clockwise rotation

#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR = (1<<8); //Configure the PORT2 pins as OUTPUT
LPC_GPIO4->FIODIR = (1<<28)|(1<<29); //configure port4 pins p4.28,p4.29 as Input

while(1)
{
LPC_GPIO2->FIOSET=(1<<8); //enable DC motor by making P2.8=1
LPC_GPIO4->FIOCLR = (1<<28); // P4.28 = 0
LPC_GPIO4->FIOSET = (1<<29); // P4.29 =1 clockwise rotation
}
}
9.Write a C program to interface a seven segment module to display the counter value
from 0 to F and 0 to 9
#include <LPC17xx.H>
unsigned char dat7seg[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0xa,0x38,0x9C,0x68,
0x1c,0x1e};
int main (void)
{
unsigned int i, j;
unsigned int count=0; // initially count=0;
LPC_GPIO2->FIODIR = 0x000000FF; // PORT2 p2.0 to p2.7 are output
LPC_GPIO1->FIODIR = 0x3C000000; //Dig control on PORT1 p1.26-.29 are output

while(1)
{
if (count > 0xF) count = 0; // if count Greater than F, re initialize to 0
for (i=0; i < 20000; i++) //change to inc/dec speed of count
{
LPC_GPIO2->FIOPIN = dat7seg[count]; // & 0x000F];
LPC_GPIO1->FIOSET = (1<<26); // 26 for first Sev segment , 27 for second
for (j=0; j<1000; j++); //change to inc/dec brightness of display
LPC_GPIO1->FIOCLR = (1<<26);
}
Count++; //count = count+1
}
}

Note: For 0 – 9 change 0xF by 9 in if statement.


10. Write a C program to interface a LCD module to display the message “ACHARYA”.

#include <LPC17xx.h>
#define LCD_RS (1<<28)
#define LCD_EN (1<<27)

void LCD_Command(unsigned char cmd)


{
LPC_GPIO2->FIOPIN = cmd ;
LPC_GPIO0->FIOCLR |= LCD_RS; // RS=0
LPC_GPIO0->FIOSET |= LCD_EN; // EN=1
for(int k=0;k<100000;k++); // delay
LPC_GPIO0->FIOCLR |= LCD_EN; //EN=0
}

void LCD_Data(unsigned char data)


{
LPC_GPIO2->FIOPIN = data;
LPC_GPIO0->FIOSET |= LCD_RS; // RS=1
LPC_GPIO0->FIOSET |= LCD_EN; //EN=1
for(int j=0;j<100000;j++); // delay
LPC_GPIO0->FIOCLR |= LCD_EN; // EN=0
}

void LCD_Init(void)
{
LPC_GPIO0->FIODIR |= (LCD_RS | LCD_EN);
LPC_GPIO2->FIODIR0 = 0x00FF; // Config P2.0 to P2.7 as o/p pins
LCD_Command(0x38); // 8 bits, 2 lines, 5x7 Dots
LCD_Command(0x01); // Clear Display screen
LCD_Command(0x0e); // Display on & cursor On
}

int main()
{
unsigned char str[]={"Acharya "};
unsigned int x=0; // initialize x=0
LCD_Init();
LCD_Command(0x80); // Force cursor to beginging of I row, [ 0xc0 for II row]
while(str[x] != '\0') // if content of str in not a null char
{
LCD_Data(str[x]); // send to LCD as Data

x++; //increment x
}
}
11.Write a C program to interface a 4x4 hex keypad and display the keycode on a LCD
screeen.

#include <LPC17xx.H>
#include "lcd.h"

void col_write( unsigned char data )


{
LPC_GPIO1->FIOSET |= (data << 14) & (1<<14 | 1<<15 | 1<<16 | 1<<17);
}

int main (void)


{
unsigned char key, i;
unsigned char rval[] = {0x7,0xB,0xD,0xE};
unsigned char keyPadMatrix[ ] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

init_lcd();

LPC_GPIO1->FIODIR = (1<<14 | 1<<15 | 1<<16 | 1<<17); //Set COLs as Outputs

lcd_putstring16(0,"Press HEX_KeyBoard..");
lcd_putstring16(1,"Key Pressed = ");

while (1)
{
key = 0;
for( i = 0; i < 4; i++ )
{
LPC_GPIO1->FIOCLR |= (1<<14 | 1<<15 | 1<<16 | 1<<17);
col_write(rval[i]); // turn on COL output one by one
if (!(LPC_GPIO1->FIOPIN & 1<<21))
break;
key++;
if (!(LPC_GPIO1->FIOPIN & 1<<20))
break;
key++;
if (!(LPC_GPIO1->FIOPIN & 1<<19))
break;
key++;
if (!(LPC_GPIO1->FIOPIN & 1<<18))
break;
key++;
}
if (key == 0x10) // if no key pressed key++ ncrements upto 16 i.e 0x10
lcd_putstring16(1,"Key Pressed = ");
else
{
lcd_gotoxy(1,14);
lcd_putchar(keyPadMatrix[key]);
}
}
}

You might also like