Microcontroller Intermediate
Kit - C Programmers Guide to Building Hardware - Part 2
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.