INPUT OUTPUT PROGRAMMING OF 8051



As we know that 8051 has 4 ports . These ports can be used as simple input output ports.
When we want to use any pin of any port as input we have to write 1 to its corresponding latch
And for output we have to write 0 .
When any pin is written 1 it serves 5v to the external hardware and when 0 serves 0V.

                         Input output programing in C


This program will toggle all the ports after 1 sec and this task will be repeated infinite times

#include<reg51.h>                    \\header file for 8051
void delay(unsigned int);         \\function prototype
void main()                                 \\intializing the main function
{
While(1)                                      \\super loop
{
P0=0x00;                                       \\write  0 to all pins of port0
P1=0x00;                                        \\write  0 to all pins of port1
P2=0x00;                                         \\write  0 to all pins of port2
P3=0x00;                                          \\write  0 to all pins of port3
delay(1000);                                   \\1 sec delay
P0=0xff;                                             \\write 1 to all pins of port0
P1=0xff;                                             \\write 1 to all pins of port1
P2=0xff;                                              \\write 1 to all pins of port2
P3=0xff;                                              \\write 1 to all pins of port3
}}



\\delay subroutine
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++}{
for(j=0;j<1275;j++);
}}

Port 3 is also used for special functions. These are written below

3.0                 RXD                used to receive data serially
3.1                 TXD                 used to transmit data serially
3.2                 INT0               external interrupt 0
3.3                 INT1                external interrupt1
3.4                 T0                    input pin for timer0
3.5                 T1                     input pin for timer1
3.6                 WR                  write signal for external memory
3.7                 RD                    read signal for external memory



Comments

Popular Posts