Sie sind auf Seite 1von 22

Keil Programming R-tist

I/O Programing
The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins All the ports upon RESET are configured as input, ready to be used as input ports. 1. When the first 0 is written to a port, it becomes an output 2. To reconfigure it as an input, a 1 must be sent to the port

An open collector is a common type of output found on many integrated circuits (IC). Instead of outputting a signal of a specific voltage or current, the output signal is applied to the base of an internal NPN transistor whose collector is externalized (open) on a pin of the IC. The emitter of the transistor is connected internally to the ground pin. If the output device is a MOSFET the output is called open drain and it functions in a similar way.

The output essentially acts as either an open circuit (no connection to anything) or a connection to ground. The output usually has an external pull-up resistor, which raises the output voltage when the transistor is turned off. When any transistor connected to this resistor is turned on, the output is forced to nearly 0 volts. Open-collector outputs can be useful for analog weighting, summing, limiting, etc., but such applications are not discussed here.

Port0
It can be used for input or output,each pin must be connected externally to a 10K ohm pull-up resistor This is due to the fact that P0 is an open drain, unlike P1, P2, and P3. In order to make port 0 an input, the port must be programmed by writing 1 to all the bits

Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data When connecting an 8051/31 to an external memory, port 0 provides both address and data.

PORT 1 AND 2
PORT1 AND PORT2 USED AS INPUT AS WELL AS OUTPUT PORT To make port 1 and port 2 an input port, it must be programmed as such by writing 1 to all its bits. By default they work as output port. Port 2 is also designated as A8 A15, indicating its dual function Port 0 provides the lower 8 bits via A0 A7

PORT3

1. Using the 8051 timer 2. Using a simple for loop Write an 8051 C program to toggle bits of P1 continuously forever with some delay. #include <reg51.h> void main(void) { unsigned int x; for (;;) //repeat forever { p1=0x55; for (x=0;x<40000;x++); //delay size //unknown p1=0xAA; for (x=0;x<40000;x++); } }

DELAY C There are two way s to create a time delay in 8051

USING FOR LOOP


Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms
#include <reg51.h> void MSDelay(unsigned int); void main(void) { while (1) //repeat forever { p1=0x55; MSDelay(250); p1=0xAA; MSDelay(250); } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }

A door sensor is connected to the P1.1 pin, and a buzzer is connected to P1.7. Write an 8051 C program to monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few hundred Hz. Solution: #include <reg51.h> void MSDelay(unsigned int); sbit Dsensor=P1^1; sbit Buzzer=P1^7; void main(void) { Dsensor=1; //make P1.1 an input while (1) { while (Dsensor==1)//while it opens { Buzzer=0; MSDelay(200); Buzzer=1; MSDelay(200); } } }

Problem
Write a program to take input from P0^0 and P0^1 from sensor and give output to two motor connected port P2 run them in such a way that if one sensor give low output one motor will stop .

FOR FURTHER QUERY CONTACT TO R-TIST TEAM ..

Thanks