Sie sind auf Seite 1von 4

Controlling real world with c

WAP to toggle led connected to pin 1 of port 1 #include<reg51.h> 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<x;i++) for(j=0;j<1275;j++); } Wap to toggle all leds connected to port 1 #include<reg51.h> void delay(unsigned int); void main() { P1=0X00; while(1) { P1=0xff; delay(50); P1=0x00; delay(50); } } void delay(unsigned int x) { unsigned int i,j; for(i=0;i<x;i++) for(j=0;j<1275;j++); } WAP to toggle led connected to pin 3 of port1 (SOURCE MODE) #include<reg51.h> void delay(unsigned int); sbit LED=P1^3; void main() { while(1) {LED=0x08; delay(50); LED=0x00; delay(50); } } void delay(unsigned int x) { unsigned int i,j; for(i=0;i<x;i++) for(j=0;j<1275;j++); } WAP to toggle led connected to pin 3 of port1 (SINK MODE) #include<reg51.h> void delay(unsigned int); sbit LED=P1^3; void main() { while(1) {LED=0x08; delay(50); LED=0x00; delay(50); } } void delay(unsigned int x) { unsigned int i,j; for(i=0;i<x;i++) for(j=0;j<1275;j++);}

Switch programming A switch is interfaced to P1.0. If the switch is pressed toggle the port 0, else toggle the port 2. #include<reg51.h> 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<x;i++) for(j=0;j<1275;j++); } A switch is connected to the P1.1 pin, and a buzzer is connected to P1.7.Write an 8051 C program to monitor the switch, and when it goes high, sounds a buzzer. #include<reg51.h> sbit SWITCH=P1^1; sbit buzzer=P1^7; void delay(unsigned int); void main() {SWITCH=1; buzzer=0; while (SWITCH==1) { buzzer=1; delay(500); buzzer=0; delay(500); }} void delay(unsigned int time) { unsigned int i,j; for(i=0;i<time;i++) for(j=0;j<1275;j++); } Bi directional DC motor interfacing using 8051.

This project describes a bidirectional DC motor that changes its direction automatically after a preset amount of time (around 1S). AT89S51 is the microcontroller used here and L293 forms the motor driver. Circuit diagram is shown below.

Bi directional DC motor using 8051 In the circuit components R1, S1 and C3 forms a debouncing reset circuitry. C1, C2 and X1 are related to the oscillator. Port pins P1.0 and P1.1 are connected to the corresponding input pins of the L293 motor driver. The motor is connected across output pins 3 and 6 of the L293. The software is so written that the logic combinations of P1.0 and P1.1 controls the direction of the motor. Initially when power is switched ON, P1.0 will be high and P1.1 will be low. This condition is maintained for a preset amount of time (around 1S) and for this time the motor will be running in the clockwise direction Then the logic of P1.0 and P1.1 are swapped and this condition is also maintained for the same duration . This makes the motor to run in the anti clockwise direction for the same duration and the entire cycle is repeated. Source program #include<reg51.h> sbit Pin1=P1^0; sbit Pin2=P1^1;

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<time;i++) for(j=0;j<1275;j++); } Consider a land rover robot, which is controlled using a numeric keypad which generates digital values and performed operation according to the conditions given below: Key 2 -> Forward Key 8 -> Backward Key 4 -> Left Turn Key 6 -> Right Turn Key 5 -> Stop Keypad is interfaced to port 1 and motor driver is connected to port2 #include<reg51.h> unsigned char i; void main() { P1=0xFF; // configure port 1 as input P2=0x00; // configure port 2 as output while(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<reg51.h> #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 port lines, connected with I/O Port lines (P0.0 P0.3) to

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

void DelayMs(unsigned int n) { unsigned int i,j; for(j=0;j<n;j++) { for(i=0;i<1275;i++); } }

Source Code #include <reg51.h> #include<stdio.h> void DelayMs(unsigned int); void Clockwise (void) { unsigned int i; for (i=0;i<30;i++) { P0 = 0x01; DelayMs(5); P0 = 0x02; DelayMs(5); P0 = 0x04; DelayMs(5); P0 = 0x08; DelayMs(5); }} void AntiClockwise (void) {unsigned int i; for (i=0;i<30;i++) { P0 = 0x08;DelayMs(5); P0 = 0x04;DelayMs(5); P0 = 0x02;DelayMs(5); P0= 0x01;DelayMs(5); } } void main (void) { P0 = 0; while(1) { Clockwise (); DelayMs (100); P0 = 0; AntiClockwise (); DelayMs (100); P0 =0; } }

Das könnte Ihnen auch gefallen