I/O PROGRAMMING IN 8051 C

SECTION 7.2: 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

As we stated in Chapter 4, ports PO – P3 are byte-accessible. We use the PO – P3 labels as defined in the 8051/52 C header file. See Example 7-9. Examine the next few examples to get a better understanding of how ports are accessed in 8051 C.


Example 7-9
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 7-10
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 7-11
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 PxAy format where x is the port 0, 1, 2, or 3, and y is the bit 0 – 7 of that port. For example, P1A7 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 7-12
Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the rest of the bits of P2.
Solution:

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

Example 7-14
A door sensor is connected to the P 1.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:


Example 7-15
The data pins of an LCD are connected to PI. The information is latched into the LCD whenever its Enable pin goes from high to low. Write an 8051 C program to send “The Earth is but One Country” to this LCD.
Solution:




Run the above program on your simulator to see how PI displays each character of the message. Meanwhile, monitor bit P2.0 after each character is issued.
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-16
Write an 8051 C program to toggle all the bits of PO, PI, and P2 continuously with a
250 ms delay.’ Use the sfr keyword to declare the port addresses.
Example 7-17


Solution:




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:

Next post:

Previous post: