On the market since 1981 this Intel creation is a single chip computer to power consumer and industrial devices. A few external components and you have a computerized product.
You can still get 8051 chips. They are still running millions of coffee machines and clothes dryers. Thermostats do not need a complicated program. Handheld remote controls and USB controller chips.
C Programming LanguageThis project uses a high-level language called C. SDCC small system device compiler works on 8051 family processors.
#include <reg51.h>
void DELAY_ms(unsigned int ms_Count)
{ int i,j;
for(i=0;i<ms_Count;i++) {for(j=0;j<100;j++);}}
int main() {
while(1)
{
P0=255; P1 = 255; P2=255; P3=255; DELAY_ms(1000);
P0=0; P1 = 0; P2=0; P3=0; DELAY_ms(1000);
}}
This demo code makes variables named P0, P1, P2 and P3 equal to 255 decimal and then makes them all zero. The #include file provides definitions for these variables. A loop counts machine clock cycles for delay. A stable frequency source and you can program a time display.
C works like mathematics and accepts decimal, hexadecimal and binary numbers, we have to be careful using them.
This 8051 simulator is running a machine code version of our program turned into ones and zeros. It looks like this.
These are hexadecimal numbers in what's called an Intel Hex format. The binary version can be written on cassette tapes or punched in paper cards. The extensions .ihx and .hex are both used.
Today's 8051 use flash memory. Flash came in the 1990s because it was like a camera photo flash in speed. We will call it flash in this project, the simulator calls it flash and you can find information about flash memory on the interweb.
The original 1980s version had EEPROM that was "burnt". It was often called ROM for read only memory or firmware because it needed costly reprogramming.
Assembly language programming is an option for generating a hex file. We could execute our program by moving bytes from register to register. We call this a low level language because it is almost pressing buttons to make things go.
Hexadecimal and BinaryThe 8051 is a computer. It computes numbers. Digital electronics work with binary digits, circuits can be on(1) or off(0).
The numbers at address 0x80, 0x90, 0xA0 and 0xB0 toggle between zero 0b00000000 and 0b11111111 = 0xFF. 255 in decimal is 0b11111111 in binary and 0xFF in hexadecimal. Zero is 0b00000000 and 0x00.
Our commands in C are performed in the processor as mathematical functions. They look like mathematics and are executed that way.
Random Access MemoryThe 8051 has 256 addresses for RAM bytes but keeps half these addresses as SFR special function registers. A program we write has the first 128 bytes of RAM for short-term memory.
The RAM memory address of P0 is 0x80, P1 is 0x80, P2 is 0xA0 and P3 is located at 0xB0. They are SFR special function registers controlled by the 8051. These are memory circuits that interface with the outside world.
Watch our program toggle them back and forth. These are 8bit values with 8bit addresses located in the logic circuits of the 8051.
The ones are voltages we push out with a tiny current. For a zero the output voltage goes to ground. 15 addresses of 8bit bytes are in between each of our IO ports.
Flash MemoryOur blinky program fits into this much flash. The simulator goes up to 0x0FF0 in memory addresses so it thinks it has 4kB of memory to store any program.
These are 8bit values with 16bit addresses. There are 65kB of these addresses but only 4kB can be expected on-chip. We see the PC program counter point at flash memory locations to find commands and data.
Physical DeviceThis is a computer chip. Inside the black box is a piece of silicon a few mm square. Chemicals printed on stone were baked into electronic circuits and wires connect to the metal pins on the outside of the package.
Our P0, P1, P2 and P3 are all here. Plus power, control and XTAL clock connections. They are also locations in RAM and variables in our programs.
P0 and P2 are normal input/output ports that we can use to interface with signals and devices. They can also expand your flash storage to 64kB.
P1 has all 8 pins available to use as IO input or output. Our demo circuit connects to P1.0 and is activated by bit0 of our variable P1. The demo code in this project blinks the LED in the circuit.
P3 is also available. P3 has interrupt leads you can connect a pump failure indicator or other external device. Our demo has connector headers for P3.1 and P3.3 to transmit or receive as a port.
All it takes is software and you have a serial uart port to the 8051. P3.3 is chosen so that a connected keyboard can generate an interrupt. Interrupts mean our device can be reset or woken by a signal.
External Memory FlashInside of the 8051 there is only 4kB of storage for our program. Ports P0 and P2 line up on one side of the chip. You can add an external flash memory chip. A 16bit memory space goes up to 64kB the maximum of the 8051.
XTAL ClockMoving our numbers into the IO input output circuits is complex. Interlocks like a mechanical system use a clock system to synchronize machine cycles.
The original 8051 needed an external component made of crystal. It resonates at 12MHz and this clock frequency timed the CPU. Several clock cycles make up a single machine cycle.
The LED circuitWhen our variable P0 has a one in the first bit then the voltage on pin P1.0 becomes high. A tiny amount of electricity comes out and a buffer amplifies this to drive an LED and resistor. The resistor value affects the brightness of the light and amount of current.
Let's focus on P1.0 where the LED is connected. First physical pin for port P1 and first bit for our variable P1. Let's change our code to make variable P1=1 then P1=0.
#include <reg51.h>
void DELAY_ms(unsigned int ms_Count)
{ int i,j;
for(i=0;i<ms_Count;i++) {for(j=0;j<100;j++);}}
int main() {
while(1)
{
P1 = 1; DELAY_ms(1000);
P1 = 0; DELAY_ms(1000);
}}
Look inside and we see our 1 and zero make the pin come on and off.
Because 1 in decimal is 0b00000001 in binary and 0x01 in hexadecimal. Zero is 0b00000000 and 0x00.
How to program a real 8051?It depends on who sells you the chip or development board. Intel no longer owns the unique intellectual property. You can make your own version of the 8051 and many manufacturers do.
There are USB boards and there are boards that need an external programmer device. Avrdude program can upload to 8051s from MicroChip. Silicon Laboratories have a sophisticated IDE and USB loading.
The screengrabs for this project are from SimulIDE simulation software, MCU 8051 IDE and Proteus are other simulators with 8051 emulation.
SDCC is available for Windows and Linux. The includes file reg51.h is provided in the download. You can download a copy from the web. This is the file that tells the compiler what keywords to use programming for the mcs51 family 8051 device.
Comments
Please log in or sign up to comment.