Ioprogramin C
Ioprogramin C
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:
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: