100% found this document useful (1 vote)
667 views22 pages

I/O Programming in 8051 C

This document discusses I/O programming in 8051 C. It covers both byte and bit I/O programming using ports P0-P3. It also provides an example of a program that monitors a door sensor connected to P1.1 and sounds a buzzer connected to P1.7 when the door opens. Finally, it discusses logical operations in 8051 C including logical operators, bitwise logical operators, and examples using these operators.

Uploaded by

Ammar Ehab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
667 views22 pages

I/O Programming in 8051 C

This document discusses I/O programming in 8051 C. It covers both byte and bit I/O programming using ports P0-P3. It also provides an example of a program that monitors a door sensor connected to P1.1 and sounds a buzzer connected to P1.7 when the door opens. Finally, it discusses logical operations in 8051 C including logical operators, bitwise logical operators, and examples using these operators.

Uploaded by

Ammar Ehab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

I/O PROGRAMMING IN 8051 C

In this section we look at C programming of the I/O ports for the 8051.
We look at both byte and bit I/O programming
Byte size I/O
• Ports P0 - P3 are bit/byte-accessible.
• We use the P0 - P3 labels as defined in the 8051/52 C header file.
Bit-addressable I/O programming
• The I/O ports of P0 - P3 are also bit-addressable.
• We can access a single bit without disturbing the rest of the port.
• We use the sbit data type to access a single bit of P0 - P3.
• One way to do that is to use the Px^y format where x is the port 0,
1, 2, or 3, and y is the bit 0 - 7 of that port.
Example A door sensor is connected to the PI.1 pin, and a buzzer is connected to PI.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; //notice the way single bit is defined
sbit Buzzer = P1^7;
void main(void)
{
Dsensor=1; //make P1.1 an input
while(Dsensor==1) //door is opened
{
buzzer=0;
MSDelay(200); //?
buzzer=1;
MSDelay(200); //?
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i <itime;i++)
for(j =0;j <1275;j++);
}
Accessing SFR addresses 80 - FFH
• Another way to access the SFR RAM space 80 - FFH is to use the sfr
data type
• We can also access a single bit of any SFR if we specify the bit address
• Both bit and byte addresses for the P0 - P3 ports are given in Table
below (byte address = bit 0 address)
Using bit data type for bit-addressable RAM
• sbit data type is used for bit-addressable SFR registers only
• bit data type is used for bit-addressable section of the data RAM
space 20 - 2FH
LOGICAl OPERATIONS IN
8051 C
Logical and bit-wise logical operators
• Logical operators: AND (&&), OR (||), and NOT (!)
• Bitwise logical operators: AND (&), OR (|), EX-OR (^), Inverter (~), Shift
Right (»), and Shift Left («)
Examples using the C bit-wise logical
operators
• 0x35 & 0x0F = 0x05 /* ANDing */
• 0x04 | 0x68- 0x6C /* ORing: */
• 0x54 A 0x78 = 0x2C /* XORing */
• ~0x55 = 0xAA /* Inverting 55H */
• 0x9A» 3= 0x13 /* shifting right 3 times */
• 0x77» 4 = 0x07 /* shifting right 4 times */
• 0x6« 4 = 0x60 /* shifting left 4 times */

You might also like