SET 3 EMBEDDED PROGRAMS
1: Write an Embedded C program to control the DC motor using 4
switches connected in H bridge format.
CLOCKWISE
SET 3 EMBEDDED SYSTEM 1
ANTICLOCKWISE
1
2: Write an Embedded C program to control two DC motors using
the motor driver IC L293D. Also connect two switches at the input
for controlling the DC motors according to the following format.
Switch 1 Switch 2 Operation
0FF OFF Both the motors should stop
OFF ON Motor1- clockwise
Motor2- Anticlockwise
ON OFF Motor2- clockwise
Motor1- Anticlockwise
ON ON Both the motors should stop
//Program to rotate the dc motor
#include<reg51.h>
//Assigning the switches to the ports
sbit sw1=P1^0;
sbit sw2=P1^1;
//Assigning the output ports to the input to the IC
sbit in1=P2^0;
SET 3 EMBEDDED SYSTEM 2
sbit in2=P2^1;
sbit in3=P2^2;
sbit in4=P2^3;
void main()
{
sw1=sw2=1;//Assigning the switch 1 and switch 2 as inputs
in1=in2=in3=in4=0;//Assigning the output ports to the port
while (1)
{
if(sw1==0&&sw2==1)
{
//clockwise
in1=1;
in2=0;
//anti clockwise
in3=0;
2
in4=1;
}
else if(sw1==1&&sw2==0)
{
//clockwise
in1=0;
in2=1;
//anti clockwise
in3=1;
in4=0;
}
else
{
in1=in2=in3=in4=0;//motor will not rotate
}
}
SET 3 EMBEDDED SYSTEM 3
}
3
SET 3 EMBEDDED SYSTEM 4
4
3. Write an Embedded C program to display your name on the first line of
2*16 LCD.
//Programs to display the characters on the LCD
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
void lcdCommand(unsigned int);//Function for the sending the Commands to lcd
void lcddataa(unsigned char);//Function for the sending the data to lcd
void delay ();//Delay function
void main ()
{
P2=0x00;
//P1=1;
lcdCommand(0x38) ;//5*7 crystal
lcdCommand(0x06) ;//increment cursor
while (1)
{
lcdCommand(0x01);
SET 3 EMBEDDED SYSTEM 5
delay ();
lcdCommand(0x80) ;//cursor to first line first position
lcddataa('V’) ;//Sending the data to the lcddataa function
//delay ();
lcddataa('I’) ;//Sending the data to the lcddataa function
//delay ();
lcddataa('N’) ;//Sending the data to the lcddataa function
//delay ();
lcddataa('A’) ;//Sending the data to the lcddataa function
//delay ();
lcddataa('Y’) ;//Sending the data to the lcddataa function
delay ();
}
}
void lcdCommand(unsigned int value)
{
P2=value;
rs=0;//Command register
rw=0;//write operation
en=1;
delay ();
en=0;
}
void lcddataa (unsigned char ch)
5
{
P2=ch;
rs=1;//data register
rw=0;
en=1;
delay();
en=0;
}
void delay ()
{
int i,j;
for (i=0; i<1500; i++);
{
for (j=0; j<10; j++);
}
}
SET 3 EMBEDDED SYSTEM 6
6
SET 3 EMBEDDED SYSTEM 7
7
4. Write an Embedded C program to scroll the word “EMBEDDED” from left to
right on the first line.
//Program to display "Embedded" on: LCD
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
void lcdCommand(unsigned int);//Function for the sending the command to LCD
void lcddataa(unsigned char);//Function for the sending the data to LCD
void lcdStr(unsigned char *str);//Function for the sending the string to LCD
void delay();
void main()
{
P2=0x00;
//P1=1;
lcdCommand(0x38);//5*7 crystal
lcdCommand(0x0C);//display on cursor off
lcdCommand(0x06);//increment cursor
SET 3 EMBEDDED SYSTEM 8
while(1)
{
lcdCommand(0x01);//clear screen
delay();
lcdCommand(0x80);//cursor to first line first position
lcdStr("EMBEDDED");//Calling the string function
}
}
void lcdCommand(unsigned int value)
{
P2=value;
rs=0;//Command register
rw=0;//write operation
en=1;
delay();
en=0;
}
void lcddataa(unsigned char ch)
{
P2=ch;
rs=1;//data register
rw=0;
8
en=1;
delay();
en=0;
}
void lcdStr(unsigned char *str)
{
int index;
for(index=0;str[index]!='\0';index++)
{
lcddataa(str[index]);
delay();
}
}
void delay()
{
int i,j;
for(i=0;i<150;i++);
{
for(j=0;j<10000;j++);
}
}
SET 3 EMBEDDED SYSTEM 9
9
SET 3 EMBEDDED SYSTEM 10
10
5. Write an Embedded C program to blink the character “E” five times with
sufficient delay.
//Program to blink the letter "E" five times with sufficient delay
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
void lcdCommand(unsigned int);
void lcddataa(unsigned char );
void delay();
void main()
{
int index;
P2=0x00;
//P1=1;
lcdCommand(0x38);//5*7 crystal
lcdCommand(0x0C);//display on cursor on
SET 3 EMBEDDED SYSTEM
for(index=0;index<5;index++) 11
{
lcdCommand(0x80);//cursor to first line first position
lcddataa('E');
lcdCommand(0x01);//clear the screen
delay();
}
while(1)
{
lcddataa('E');
lcdCommand(0x80);
}
}
void lcdCommand(unsigned int value)
{
P2=value;
rs=0;//Command register
rw=0;//write operation
en=1;
delay();
en=0;
}
void lcddataa(unsigned char ch)
{
P2=ch;
11
rs=1;//data register
rw=0;
en=1;
delay();
en=0;
}
void delay ()
{
int i,j;
for(i=0;i<500;i++)
{
for(j=0;j<100;j++);
}
}
SET 3 EMBEDDED SYSTEM 12
12
SET 3 EMBEDDED SYSTEM 13
13
6. Write an Embedded C program to display hex counter from 0 to F in the
middle of the first line with one second delay (used timer 1 mode 1)
//Program to display the hex counter 0-F with one second dela
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
void lcdCommand(unsigned int);//function for sending the command to lcd
void lcddataa(unsigned char);// //function for sending the data to lcd
void delay();
void main()
{
SET 3 EMBEDDED
unsigned SYSTEM
char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};//initilize the values in 14
array
int index;
P2=0x00;
//P1=1;
lcdCommand(0x38);//5*7 crystal
lcdCommand(0x0C);//display on cursor
for(index=0;index<=15;index++)
{
lcdCommand(0x87);//first line seventh position
lcddataa(a[index]);
delay();
}
while(1)
{
lcdCommand(0x08);
14
}
}
void lcdCommand(unsigned int value)
{
P2=value;
rs=0;//Command register
rw=0;//write operation
en=1;
delay();
en=0;
}
void lcddataa(unsigned char ch)
P2=ch;
rs=1;//data register
rw=0;
SET 3 EMBEDDED SYSTEM 15
en=1;
delay();
en=0;
void delay()
int i;
for(i=0;i<15;i++);
TMOD=0x10;
TL1=0X00;
TH1=0X00;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
15
}
SET 3 EMBEDDED SYSTEM 16
16
8. Write an Embedded C program to display CLOCKWISE on LCD and 0 on SSD
if the DC motor is running in clockwise else display ANTICLOCKWISE on LCD
and 1 on SSD.
#include<reg51.h>
#define input P2
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit sw=P1^3;
sbit sw1=P0^0;
sbit sw2=P0^1;
void display(unsigned char* ptr);
void lcdCommand(unsigned char cmd);
void lcddata(unsigned char data);
void
SET delay(int
3 EMBEDDEDnum);
SYSTEM 17
void main()
{
unsigned char ch1[]="CLOCKWISE";
unsigned char ch2[]="ANTI CLOCKWISE";
P3=0X00;
lcdCommand(0x01);//clear the display
lcdCommand(0x06);//increment the cursor
lcdCommand(0x0c);//display on cursor off
lcdCommand(0x38);//2 lines matrix
lcdCommand(0x80);//beginning of 2nd line
if(sw==1)
{
sw1=1;
sw2=0;
P3=0X3F;
display(ch1);
delay(5000);
17
}
else
{
sw1=0;
sw2=1;
P3=0X06;
display(ch2);
delay (5000);
}
}
void lcdCommand(unsigned char cmd)
{
input=cmd;
rs=0;
rw=0;
en=1;
SET 3 EMBEDDED SYSTEM 18
delay (50);
en=0;
}
void display (unsigned char* ptr)
{
int i;
for(i=0;ptr[i]!='\0';i++)
{
lcddata(ptr[i]);
}
}
void lcddata(unsigned char dat)
{
input=dat;
rs=1;
rw=0;
18
en=1;
delay(50);
en=0;
}
void delay(int num)
{
int i,j;
for(i=1;i<=num;i++)
{
for(j=0;j<200;j++);
}
}
SET 3 EMBEDDED SYSTEM 19
19
SET 3 EMBEDDED SYSTEM 20
20