controlling the real world with c .docx
TRANSCRIPT
-
8/14/2019 controlling the real world with c .docx
1/4
Controlling real world with c
WAP to toggle led connected to pin 1 of port 1
#include
void delay(unsigned int);
sbit led=P1;
void main(){
P1=0X00;
while(1)
{
led=0xff;
delay(50);
led=0x00;
delay(50);
}
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i
-
8/14/2019 controlling the real world with c .docx
2/4
Switch programming
A switch is interfaced to P1.0. If the switch is
pressed toggle the port 0, else toggle the
port 2.
#include
void delay(unsigned int);
sbit SWITCH=P1^0;
p0=0x00; p2=0x00;void main()
{
SWITCH=1;
while(1)
{
if (SWITCH ==1)
{P0=0xff; delay(500);
P0=0x00; delay(500);
}
else
{P2=0xff; delay(500);P2=0x00; delay(500);
}}}
void delay(unsigned int x)
{unsigned int i,j;
for(i=0;i
-
8/14/2019 controlling the real world with c .docx
3/4
sbit reset=P1^2;
void delay(unsigned int);
void main()
{reset=1;
while (1)
{ if(reset==1)
{Pin1=1; Pin2=0;
delay(1000);}
else
{Pin1=0; Pin2=1;
delay(1000);}
}}
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i Backward
Key 4 -> Left TurnKey 6 -> Right Turn
Key 5 -> Stop
Keypad is interfaced to port 1 and motor driver
is connected to port2
#include
unsigned char i;
void main()
{
P1=0xFF; // configure port 1 as input
P2=0x00; // configure port 2 as outputwhile(1)
{ i=P1;
}
switch(i)
{
case 2: P2=0x05; break;
case 4: P2=0x04; break;
case 5: P2=0x00; break;
case 6: P2=0x02; break;
case 8: P2=0x0A; break;
default: break;
}}
LEDs are connected to bits of P1 and P2. Write
an 8051 C program that shows
the count form 0 to FFH on the LEDs.
#include
#define LED P2
void main(){
P1=00;
LED=0;
for(;;)
{
P1++;
LED++;
}
}
Interfacing Stepper Motor
Fig. shows how to interface the Stepper Motor to
microcontroller. As you can see the stepper motor is
connected with Microcontroller output port pins
through a ULN2003 array. So when the microcontroller is
giving pulses with particular frequency to ULN2003, the
motor is rotated in clockwise or anticlockwise.
Fig. 1 Interfacing Stepper Motor to Microcontroller
Interfacing Stepper Motor with 8051
We now want to control a stepper motor in 8051
Primer Board. It works by turning ON & OFF a four
I/O port lines generating at a particular frequency.
The 8051 Primer board has four numbers of I/O portlines, connected with I/O Port lines (P0.0 P0.3) to
-
8/14/2019 controlling the real world with c .docx
4/4
rotate the stepper motor. ULN2003 is used as a
driver for port I/O lines, drivers output connected
to stepper motor, connector provided for external
power supply if needed
Source Code
#include
#include
void DelayMs(unsigned int);void Clockwise (void)
{ unsigned int i;
for (i=0;i