KEMBAR78
Lab 7 | PDF | Computer Engineering | Computer Hardware
0% found this document useful (0 votes)
18 views14 pages

Lab 7

Uploaded by

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

Lab 7

Uploaded by

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

Amrita School of Engineering, Bengaluru Campus

Academic Year: 2023-2024 Semester: V ECE


Department: Electronics and Communication Engineering Programme: B. Tech
Course Code:19ECE304 Course Title: Microcontroller Lab
Student Name: Register Number:

Student Signature: Faculty Signature:

Lab: – 7 Date:
Hardware Interfacing using LPC2148
7 a. Write an embedded C program to blink the on board LED’s of MCB2140 board.
#include <lpc214x.h>
unsigned int delay;
int main(void)
{
//IO1DIR = (1<<16); // Configure P0.16 as Output
IO1DIR = 0xFF0000;
// IODIR1 = 0xFF0000;
While (1)
{
//IO1CLR = (1<<16); // CLEAR (0) P0.16
IO1CLR = 0xFF0000;
for (delay=0; delay<500000; delay++); // delay
IO1SET=0xFF0000;
// IO1SET = (1<<16); // SET (1) P0.16
for (delay=0; delay<500000; delay++); // delay
}
}

Hardware connection diagram:


7. b. Write an embedded C program to interface Keyboard with MCB2140 board and analyze
its working on seven segment display as a simple calculator.
#include <LPC214X.H>
void DispChar (unsigned char ch);
void ClrLED (void);
void delay (unsigned int i);
unsigned char getkey (void);
unsigned char getnum (void);
unsigned char getOp (void);
main()
{
unsigned char n1=0, n2, Op, Res, Neg;
unsigned char NumTab [10] = {0x0c0,0x0f9,0x0a4,0xb0,0x99,0x92,0x82,0x0f8,0x80,0x90};
unsigned char OpTab[7] = { 0x88,0x0c8,0x0af,0xff,0x0Bf,0xff,0x0a1};
PINSEL2 = PINSEL2 & 0xFFFFFFF7;
IODIR0 = 0x007F800C;
IODIR1 = 0x00000000;
ClrLED(); /* Clear the 7 segment display */
while(1)
{
Neg = 0x00; /* Negative flag */
n1=getnum(); /* Get the 1st number */
if(n1==0x13)
{
ClrLED(); /* If pressed key is "AC", then Clear LED */
continue;
}
DispChar(NumTab[n1]); /* Display the number */
Op = getOp() - 0x0C; /* Get Opcode. 0x0C is keycode of '+'(see keyboard Display
schematics)*/
if(Op==0x07)
{
ClrLED(); /* If pressed key is "AC",then Clear LED */
continue;
}
DispChar(OpTab[Op]);
n2=getnum(); /* Get the 2nd number */
if (n2==0x13)
{
ClrLED(); /* If pressed key is "AC", then Clear LED */
continue;
}
DispChar(NumTab[n2]);
while(getkey()!=0x0B); /* wait for '=' key */
ClrLED();
switch(Op) /* Perform corresponding operation */
{
case 0:
Res = n1 + n2; /* Perform addition */
break;
case 1:
Res = n1 * n2; /* Perform multiplication */
break;
case 2:
Res = n1 % n2; /* Display Reminder */
break;
case 4:
if(n2>n1) /* check for negative number */
{ /* Perform substaction */
Neg = 0x01;
Res = n2 - n1;
break;
}
Res = n1 - n2;
break;
case 6: /* Perform division */
Res = n1 / n2;
break;
}
DispChar(NumTab[Res%10]); /* Display the Result */
DispChar(NumTab[Res/10]);
if(Neg)
DispChar(0x0Bf); /* If negative result display '-' */
while(getkey()!= 0x13);
{ /* wait for "AC" key */
ClrLED();
}
}
}
void DispChar(unsigned char ch) /* Function to display the character on 7 segment */
{
unsigned char i,tmp;
IOCLR0 = 0x003F8000; /* For all bits */
for(i=0;i<8;i++)
{
tmp = ch & 0x80;
if(tmp) /* write data depending on MSB */
IOSET0 |= 0x00000008;
else
IOCLR0 |= 0x00000008;
IOCLR0 |= 0x00000004; /* Give Clk Pulse for synchronization */
IOSET0 |= 0x00000004;
ch = ch << 1; /* Get next bit */
}
}
void ClrLED()
{
unsigned char i;
for(i=0;i<6;i++)
DispChar(0x0ff); /* 0xff for clear segment */
}
unsigned char getkey()
{
unsigned char j,indx;
unsigned long i,s=00;
while(1)
{
indx = 0x00; /* Index for storing the first value of scanline */
for(i=0x00008000;i<=0x00080000;i<<=1) /* For 4 scanlines */
{
IOCLR0 = 0x00078000;
IOSET0 = i; /* write data to scanline */
delay(500);
s = 0x001F0000 & IOPIN1; /* Read readlines connected to P0*/
s >>= 16;
if(s > 0) /* If key press is true */
{
for(j=0; j<=4; j++)
{ /* Check for 5 lines */
s >>=1;
if(s==0) /* If get pressed key*/
{
return(indx+j); /* Return index of the key pressed */
}
}
}
indx += 5; /* If no key pressed increment index */
}
}
}
unsigned char getnum() /* Function to get the number */
{
unsigned char tmp;
while(1)
{
tmp = getkey();
if(tmp < 0x0a || tmp==0x13) /* If pressed key is number, return */
return(tmp);
}
}
unsigned char getOp() /* Method for getting Operator */
{
unsigned int tmp;
while(1)
{
tmp = getkey();
if(tmp == 0x0c|| tmp == 0x0d|| tmp == 0x10|| tmp == 0x12||tmp == 0x13|| tmp == 0x0e )
/* If pressed key is a Operator, return */
return(tmp);
}
}
void delay (unsigned int i)
{
unsigned int j;
for (j=0; j<=i; j++); /* Delay function */
}
Hardware connection diagram:
7.c. Write an embedded C program to generate different tones of music when different keys
are pressed interfaced on MCB 2140 board.
#include <LPC214X.H>
#define CR 0x0d
void init_serial (void)
{ /* Initialize Serial Interface */
PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL = 97; /* 9600 Baud Rate @ 12 MHz VPB Clock */
U1LCR = 0x03; /* DLAB = 0 */
}
int putchar(int ch) /* Send the character to UART */
{
if(ch == '\n')
{
while(!(U1LSR & 0x20));
U1THR = CR;
}
while(!(U1LSR & 0x20));
return (U1THR = ch);
}
void sendstr(unsigned char *p) /* Send the string of characters to UART */
{
while(*p)
{
putchar(*p++);
}
}
int getchar(void) /* Routine to read a character from UART */
{
while(!(U1LSR & 0x01));
return U1RBR;
}
int main ()
{
unsigned char i,j,k,l;
unsigned char freqtabl[]=
{0xf8,0xee,0xe0,0xd8,0xc0,0xb0,0xa8,0xa0,0x98,0x90,0x88,0x80,0x78,0x70,0x68,0
x60,0x56} ;
IODIR0=0x0000f000 ;
init_serial();
while(1)
{ /* checking for the key between A-P*/
i=0;
while((i=getchar())>=0x41 )
{ if (i<0x51)
{ i=(i&0x1f);
for(j=0;j<=0x7F;j++)
{
IOSET0=0xffff; /*sending low on portc*/
for (k=0;k<=freqtabl[i];k++)
{
for(l=0;l<=0x20;l++);
}
IOCLR0=0xffff; /* sending high on port c*/
for (k=0;k<=freqtabl[i];k++)
{
for(l=0;l<=0x20;l++) ;
}
}
}
}
}
}
Hardware connection diagram:
7. d. Write an embedded C program to demonstrate the working of DC motor.
#include <LPC214X.H>
#define CR 0x0d
unsigned char count;
unsigned long pedestal;
void delay() /* Delay Routine */
{
unsigned int i = 0x00;
for(i=0;i<=0x80;i++);
}
int getkey(void) /* Routine to read a character from UART */
{
while(!(U1LSR & 0x01));
return U1RBR;
}
int putkey(int ch) /* Function to send a character to Serial Port */
{
if(ch == '\n')
{
while(!(U1LSR & 0x20));
U1THR = CR;
}
while(!(U1LSR & 0x20));
return (U1THR = ch);
}
void sendstr(char *p) /* Function to send a string to Serial Port */
{
while(*p)
{
putkey(*p++);
}
}
void speed()
{
char i;
sendstr("\n SPEED OF THE DC MOTOR AS ROTATIONS/SEC = ");

while(0x00010000 & IOPIN0); /* Loop for P0.16 high */


while(!(0x00010000 & IOPIN0)); /* Loop for P0.16 Low */
count=0x00;
loop:
while(!(0x00008000 & IOPIN0)); /* Loop for P0.15 Low */
count += 1; /* Increament the count if P0.15 high*/
delay();
while(0x00008000 & IOPIN0); /* Loop for P0.15 High */
delay();
if(0x00010000 & IOPIN0) /* continue if Time puse is present */
goto loop;
else
{
for(i=0;count>= 0x0A;i++) /* Conver the pulse count */
{
count=(count-0x0A);
}
}
putkey((i+0x30)); /* Display the speed */
putkey((count+0x30));
}
main()
{
unsigned char s=0x7f,input = 0;
IODIR0 = 0x00000000;
IODIR1 = 0x00FF0000;
PINSEL0 = 0x00050000; /* Initialise serial interfaace */
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL = 97; /* 9600 Baud Rate @ 12 MHz VPB Clock */
U1LCR = 0x03;
pedestal = 0x007F0000;
IOSET1 = pedestal;
sendstr("\nPRESS 'i' TO INCREASE THE RPM OF THE DC MOTOR");
sendstr("\nPRESS 'd' TO DECREASE THE RPM OF THE DC MOTOR");
sendstr("\nPRESS 's' TO DISPLAY RGE SPEED IN ROTATIONS/SEC\n");
while(1)
{
loop: IOCLR1 = 0x00FF0000;
IOSET1 = pedestal;
key: input = getkey(); /* Read Key */
if(input == 0x69)
{
if(s != 0x1F)
{ /* If input = i */
s -= 1; /* Decrease the Pedestal to increase the motor speed */
pedestal = s;
pedestal <<= 16;
putkey((0x08));
goto loop;
}
else
goto key;
}
else if(input == 0x64)
{ /* If input = d */
if(s != 0xE0)
{
s += 1;
pedestal = s;
pedestal <<= 16;
putkey((0x08));
goto loop;
}
else
goto key;
}
else if(input == 0x73)
speed(); /* If input = s */
else
{
sendstr(" Enter i/d/s only");
goto key;
}
}
}
Hardware connection diagram:
7.e. Write an Embedded C program to interface LCD display and to demonstrate its working
on MCB 2140 board.
#include <LPC21xx.H>
#define CR 0xd
unsigned char mess[] = {'I','S',' ','T','Y','P','E','D',0x00};
unsigned char disp1[] = {'W','A','I','T','I','N','G',' ','F','O','R',' ','K','E','Y',0x00};
/*Function to dislay a character on the Console (Hyperterminal)*/
int putchar(int ch)
{
if(ch == '\n')
{
while(!(U1LSR & 0x20));
U1THR = CR;
}
while(!(U1LSR & 0x20));
return (U1THR = ch);
}
/*Function to send a string to Console (Hyperterminal) */
void sendstr(char *p)
{
while(*p)
{
putchar(*p++);
}
}
/* Function to read a character from Keyboard through Serial Port */
int getchar (void) {
while (!(U1LSR & 0x01));
return (U1RBR);
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i = 0; i < 10; i ++)
{
for(j = 0; j < x; j ++)
{}
}
}
void delay1()
{
unsigned int i,j;
for(i = 0; i < 0x50; i ++)
{
for(j = 0; j < 0x4fb; j ++)
{}
}
}

/* Function to display the character entered from the keyboard */


void Disp_Key(unsigned long c)
{
unsigned int x;
unsigned long l;
l = c << 16;
IOCLR1 = 0x00FF0000;
IOSET1 = l;
IOCLR0 = 0x003F8000; /* E R/W* RS */
IOSET0 = 0xfa << 15; /* 0 1 0 */
IOCLR0 = 0x003F8000;
IOSET0 = 0xf9 << 15; /* 0 0 1 */
IOCLR0 = 0x003F8000;
IOSET0 = 0xfd << 15; /* 1 0 1 */
IOCLR0 = 0x003F8000;
IOSET0 = 0xf9 << 15; /* 0 0 1 */
IOCLR0 = 0x003F8000;
x = 0x50;
delay(x);
}
/* Function to issue the series of commands to the LCD module */
void Command_Write(unsigned long com_word)
{
unsigned int x;
IOCLR1 = 0x00FF0000;
IOSET1 = com_word; /* Send commands to the LCD through GPIO1 */
IOCLR0 = 0x003F8000; /* E R/W* RS */
IOSET0 = 0xfb << 15; /* 0 1 1 */
IOCLR0 = 0x003F8000;
IOSET0 = 0xf8 << 15; /* 0 0 0 */
IOCLR0 = 0x003F8000;
IOSET0 = 0xfc << 15; /* 1 0 0 */
IOCLR0 = 0x003F8000;
IOSET0 = 0xf8 << 15; /* 0 0 0 */
IOCLR0 = 0x003F8000;
x = 0x50;
delay(x);
}
/* Function to read characters from a string and displaying the same by calling the
Disp_key fucntion */
void Disp_String1(void)
{
int i;
i = 0;
while(disp1[i] != 0x00)
{
Disp_Key(disp1[i]);
i ++;
}
}
/* Function to initialize the LCD module */
void LCD_init(void)
{
int j;
unsigned long command;

j = 0x9f; /* Delay given for the voltage rise from 0V to 5V */


delay(j);
command = 0x38 << 16; /* Issue software reset for the LCD module */
Command_Write(command);
j = 0x13F;
delay(j);
command = 0x38 << 16; /* Function set 8-bit, 1 line 5*7 font */
Command_Write(command);
command = 0x0c << 16; /* Display on off control. Diplay on, cursor off, */
Command_Write(command); /* blink off */
command = 0x01 << 16; /* Clear the LCD display */
Command_Write(command);
}

void Disp_Message(void)
{
int i;
i = 0;
while(mess[i] != 0x00)
{
Disp_Key(mess[i]);
i ++;
}
}
int main()
{
unsigned long com_word;
unsigned char x;
unsigned char c;
PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000004;
IODIR0 = 0x007FB0FC; /* Configure the GPIO0 and GPIO1 as O/P ports */
IODIR1 = 0x00FF0000;
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */
U1LCR = 0x03; /* Initialize LCD module */
LCD_init();
com_word = 0x80 << 16;
Command_Write(com_word);
delay1();
delay1();
Disp_String1(); /* Displays the "WAITING FOR KEY" message on LCD */
sendstr("\nEnter any key to clear the display : ");
c = getchar();
com_word = 0x01 << 16; /* Clears the LCD Display */
Command_Write(com_word);
com_word = 0x80 << 16; /* Bring the cursor to the home location */
Command_Write(com_word);
while(1)
{
sendstr("\nEnter Key : "); /* Enter the key to be displayed on the display */
c = getchar();
Disp_Key(c); /* Displays the key entered */
delay1();
com_word = 0xc0 << 16;
Command_Write(com_word);
Disp_Message(); /* Displays the "IS TYPED" message on LCD */
com_word = 0x02 << 16;
Command_Write(com_word);
x = 10;
delay(x);
}
}
Hardware connection diagram:

You might also like