0% found this document useful (0 votes)
18 views5 pages

Ioprogramin C

The document provides several examples of 8051 C programs that demonstrate how to interact with I/O ports and bit-addressable memory. It explains the use of the sbit data type for accessing individual bits of ports and introduces methods for accessing special function registers (SFR) in the 8051 microcontroller. Additionally, it includes examples of monitoring bits and sending data based on conditions.

Uploaded by

ppat2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Ioprogramin C

The document provides several examples of 8051 C programs that demonstrate how to interact with I/O ports and bit-addressable memory. It explains the use of the sbit data type for accessing individual bits of ports and introduces methods for accessing special function registers (SFR) in the 8051 microcontroller. Additionally, it includes examples of monitoring bits and sending data based on conditions.

Uploaded by

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

Example 1

LEDs are connected to bits PI and P2. Write an 8051 C program that shows the
count from 0 to FFH (0000 0000 to 1111 1111 in binary) on the LEDs.
Solution:

Example 2

Write an 8051 C program to get a byte of data from PI, wait 1/2 second, and then
send it to P2.
Solution:

Example 3

Write an 8051 C program to get a byte of data from PO. If it is less than 100, send it
to
PI; otherwise, send it to P2.
Solution:
Bit-addressable I/O programming
The I/O ports of PO – P3 are 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 PO
– P3′. One way to do that is to use the Px Ay format where x is the port 0, 1, 2, or 3,
and y is the bit 0 – 7 of that port. For example, P1 A7 indicates PI.7. When using this
method, you need to include the reg51 .h file. Study the next few examples to
become familiar with the syntax.

Example 3

Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the
rest of the bits of P2.
Solution:

Example 4

Write an 8051 C program to monitor bit PI.5. If it is high, send 55H to PO; otherwise,
sendAAHtoP2.
Solution:
Write an 8051 C program to monitor bit PI.5. If it is high, send 55H to PO; otherwise,
sendAAHtoP2.
Solution:

Accesssing SFR addresses 80 – FFH


Another way to access the SFR RAM space 80 – FFH is to use the sfr data type.
This is shown in Example 7-16. We can also access a single bit of any SFR if we
specify the bit address as shown in Example 7-17. Both the bit and byte addresses
for the PO – P3 ports are given in Table 7-2. Notice in Examples 7-16 and 7-17, that
there is no ^include <reg51.h> statement. This allows us to access any byte of the
SFR RAM space 80 – FFH. This is a method widely used for the new generation of
8051 microcontrollers, and we will use it in future chapters.
Table 7-2: Single Bit Addresses of Ports
Example 7-17

Using bit data type for bit-addressable RAM

The sbit data type is used for bit-addressable SFR registers only. Sometimes we
need to store some data in a bit-addressable section of the data RAM space 20 –
2FH. To do that, we use the bit data type, as shown in Example 7-18.
Example 7-18
Write an 8051 C program to get the status of bit Pl.O, save it, and send it to P2.7
continuously.
Solution:

You might also like