Set 3 Embedded Programs Write An Embedded C Program To Control The DC Motor Using 4 Switches Connected in H Bridge Format
Set 3 Embedded Programs Write An Embedded C Program To Control The DC Motor Using 4 Switches Connected in H Bridge Format
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.
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.
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
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;
}
5
{
P2=ch;
rs=1;//data register
rw=0;
en=1;
delay();
en=0;
}
void delay ()
{
int i,j;
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.
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
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;
}
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++);
}
}
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 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;
}
for(i=0;i<500;i++)
{
for(j=0;j<100;j++);
}
}
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 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
}
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]);
}
}
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++);
}
}
19
SET 3 EMBEDDED SYSTEM 20
20