|
Programming 8051s in C - Part
2
Click
here to go to Part 1.
Next we will take a closer look at the way the
8051 hardware is defined in C programs.
Special Function Registers
The 8051 has many memory locations that are given
special names and are called Special Function Registers (SFRs). Each is
just a space in memory with an address like any other location in memory.
To simply writing software, these spaces are given names and we use the
names in our programs rather than the addresses. In C the SFR names are
assigned to the correct memory locations using
sfr P0 = 0x80;
where P0 is the SFR name (Port 0 in this case)
and 0x80 is the memory address in hex. (80 Hex is the address of Port 0.)
We can collect all of the SFR definitions and
put them in a seperate file and just include that file at the beginning
of each program we write. We have collected these as 8052.h.
Accessing Individual Port Pins
In the first two examples we moved data to the
ports a whole byte at a time. We can also work with the individual pins
of each port. The individual bits of each Port are defined in the 8052.h
file and can be accessed using an underscore. For example P0_0 is bit 0
of Port 0.
Here is a simple example that reads the input
from pin 39 (Port 0, Bit 0) and writes it to pin 1 (Port 1, Bit 0). transfer.c.
The line P1_0 = P0_0; does the trick. The LED comes on when you connect
pin 39 to ground and goes off when you connect it to 5 volts.
To get all the parts required for this project,
get the Microcontroller Beginner
Kit, the ADT87,
and the 8951Kit. Click
here to go to the main products page with links for these parts.
More C examples coming soon!
Tutorials
Menu
Click
here to send us comments or questions.
This page last updated on July 27, 2004. |