Introduction: Getting Started With a 8051 Micro-controller

Hi all,

I love micro-controllers projects. I use often both Arduino cards and "naked" AVR micro-controllers (ATTINY and ATMEGA devices) for my projects.

For my culture, I choose to study an other kind of micro-controllers, the ones based on the 8051 architecture : http://en.wikipedia.org/wiki/Intel_MCS-51


The 8051 architecture was created by Intel in the last 70's. Ok, it's an old architecture, but very simple to understand, and easy to use. Modern micro-controllers based on this architecture are of course filled with up to date characteristics.

The advantages of these micro-controllers is that they are cheap, easy to use, and very well documented. Also, the code language and the pinout is an industry standard : It means that you can change the chip to an other one taken from an other supplier, they should work the same. The only cons is that you need to set up a new development process : new assembly language, new compiler, new programmer, new development boards... The hardest part was for me to find a simple tutorial that covers everything... As this kind of help doesn't exist, I choose to write down everything here... So, in this Instructable, we will see how to begin from scratch : how to set up the development environment, how to create sample programs and how to wire and flash the final chip.

I'm not an expert with these chips, but I'm still learning...

I assume that you have some knowledge in electronics and in micro-controllers programming... The process is still the same : writing the program, compile it to an HEX file, then upload it on the chip.

If you have questions / doubts, just ask. I'll try to do my best to answer...!

Step 1: Hardware List

Ok, I choose to work with the AT89S52 from Atmel. I don't really remember why this one was in my stock... But this chip is very cheap : less than 2€ from a supplier. This chip has the following features:

  • 8K Bytes of In-System Programmable (ISP) Flash Memory
  • 32 Programmable I/O Line, divided in 4 ports (P0, P1, P2 and P3)
  • Three 16-bit Timer/Counters
  • 256 bytes on internal RAM
  • Eight Interrupt Sources
  • Full duplex UART
  • Watchdog...


The datasheet of the chip is here : http://www.atmel.com/Images/doc1919.pdf


This chip follows the industry-standard 80C51 instruction set and pin-out. It means that you can replace it by a compatible model from an other supplier, they should work the same way.

For this project, you will also need :

  • an arduino card (acts as a programmer and 5V power supply),
  • breadboard wires,
  • breadboard,
  • a 10k resistor,
  • 100 nF capacitor,
  • two 22 pF capacitors,
  • one 11.0592 MHz quartz,
  • 1k resistors,
  • leds,
  • switch buttons,
  • a 10k pot, and a 1602 LCD screen (optional)


In my case, I've got a simple development board, with all the parts needed to run the AT89S52. Don't worry if you don't have it, you can do the same on a breadboard. The wiring is not as hard as it looks...!

So, I'll use :

  • an arduino card,
  • breadboard wires,
  • an AT89S52 chip,
  • a development board,
  • and 1602 LCD screen.

Step 2: Software Needed

Ok, we will program this chip in assembly language. For this, you will need :

All the necessary and includes files are into the archive. You don't need anything else...

You will also have to use an arduino card, so you should have the arduino IDE installed on your computer. We will also have to use a small utility to download the HEX file into the chip. More on this later...

Step 3: Set Up the Dev Environnement

Ok, unzip the "c51asm_win_1-2.zip" file.

Open the BIN folder, and create a file called "COMPILATION.BAT" containing the following code :

@Echo off
@c51asm %1 --include ..\INC
@pause

Save it. Now, to compile a file, just drop it on the batch file. The compiler will be called and the HEX file written in the same folder.


That's all, you are ready to create you first program ! But before, some explanations on the assembly language. I tried to put a lot of comments in my program sources, but we need first to study some instructions...

Step 4: Notes on Assembly Langage

Ok, the assembly language can looks strange (and I don't pretend to know everything) but here are the main instructions used for it. More information on the instruction set is available here :http://en.wikipedia.org/wiki/Intel_MCS-51#Instruc...

Everything after a ";" is a comment.

A word that finishes with a ":" is a line label. Used for jumps. Spaces are not allowed.

.equ A, B : Placed at the beginning of a file, allow the programmer to define a symbol A equals to B. Ex : .equ InputButton , P3.5 means that InputButton is now attached to the P3.5 pin.

MOV A,B : Put the value B into A (B can be a constant, a register, a memory address...)

DJNZ register, line : Decrement and Jump if Not Zero. Decrements the register, then jump to the given line if the register contains 0. Useful to create simple loops.

CALL line : Calls an external function.

RET : move back to the master program. Used into a sub function called by CALL.

JB bit, line : Jump if Bit set. if the specified bit is SET (so true),the program jumps to the given line. If not, moves to the next instruction.

SETB bit : set the given bit to TRUE

CLR bit : set the given bit to FALSE

R0, R1 , R2 : general purpose byte registers.

A register is simply a byte that is used to store data. The chip contains also special functions registers (SFR), used to drive the I/O ports and general configuration.

The Accumulator ("A" in the code) is also a general-purpose register, like R0, R1 or R2.

The general I/O ports of the chip are named here with numbers (unlike letters used with ATMEGA and ATTINY devices). P2 is a register byte, so the total eight individual ports. P2.0 is a bit that indicates the first individual port. The ports are form P2.0 to P2.7

You can also read the "doc3710_C51ASM" PDF file available in the DOC folder from the compiler zip file. Everything is explained in details : full instructions set, how to use the compiler...

Step 5: Setting Up the Hardware

Ok, connect the arduino card to the computer. Load the attached sketch in the editor, compile and upload it on the card. You should not get errors.


(Note : the flashing process was taken from this other Instructable :https://www.instructables.com/id/ARDUINO-AS-A-8051-... Thanks ! I didn't take credits...)

Follow the attached schematic to wire the AT89S52 (see the picture for more details). It's quite similar than others ATMEL micro-controllers (ATTINYs and ATMEGAs...), the main difference is the RESET pin, now pulled down with a 10k resistor. Also, put the pin 31 (/EA - VPP) to 5V, it means that the chip will use it's internal memory to run the program.

Connect the arduino board to the AT89S52 chip :

Arduino / AT89S52

  • digital pin 2 / physical pin 9 (reset)
  • digital pin 3 / pin 8 (P1.7, MOSI)
  • digital pin 4 / pin 7 (P1.6, MISO)
  • digital pin 5 / pin 6 (P1.5)

We use the 5v pin of the arduino to feed power into the chip :

  • arduino 5V : pin 40 of the AT89S52 (Vcc)
  • arduino GND : pin 20 of the AT89S52 (GND)

Step 6: About My Development Board

Ok, for this project I'll use a special development board. This one is quite simple, you can find it easily on the web. It have the following features:

  • USB powered
  • direct ICSP download port (Unfortunately I don't use it !)
  • zero-insertion-force support for the DIL40 pin
  • leds (pulled-down) on the P2 port
  • integrated LCD ports, with contrast pot
  • four switches buttons (pulled down) from P3.2 to P3.5
  • RS232 interface, with MAX232 chip
  • reset button

I also have a second one, with less features, but still useful. It contains only the required parts used to run the chip (quartz, oscillators caps, pull-down reset resistor)...

Again, you can find it on the web.

Now, it's time to create our first program.

Step 7: First Program

Ok, go back in the "BIN" folder. With the notepad, create a file called "BLINK_LED.ASM" and enter the following code :

;Blinking LED test<br>;written by yoruk for Instructables.com
$INCLUDE (at89s52.inc) ; include the header file
org 000h ; starts program
start:               ; this is a line label name
        setb p2.0 ; setting pin p2.0 to high (so turn the led off)
        setb p2.1 ; setting pin p2.1 to high
        setb p2.2 ; setting pin p2.2 to high
                
        clr p2.5  ; setting pin 2.5 to LOW,so turn on the led
        
        lcall delay  ;wait a little bit by calling the Delay function
        
        clr p2.0 ; setting p2.0 to low
        clr p2.1 ; so turn on the leds
        clr p2.2
        
        lcall delay ;wait again
        
        jmp start ; go to the first line... in a loop
delay :
        mov R2,  #40
outer : 
        mov R1, #200
inner : 
        mov R0, #240
take :  
        djnz R0, Take    
        djnz R1, inner
        djnz R2, outer
ret
    
end  ;end of this file, no more assembly language after

As you can see, we will just blink a led to begin. How it works ? We turn off the led wired on the P2.0, P2.1 and P2.2 pins. Then we call a delay function, we turn on the leds, we wait again, and we do it again. The delay function just do a couple of loops to have dummy computing time. By putting loops together, we can wait a lot.

Save the file, then drop it on the batch file. You should not have any errors or warnings. The result of the compilation is an HEX file (also attached in this step), ready to upload.

Step 8: Upload It !

Ok, get the following utility, attached in this step. Again, I didn't take credits about it.

  • Launch it, then select the port COM of your arduino (in my case, the COM14). Use the arduino IDE to check it.
  • Select Connect
  • Click on Identify. The programmer then reads the signature of the chip, then display it. In my case, the AT89S52. Everything is working correctly.
  • Select OPEN HEX, then select the LED_BLINK hex file.
  • Push UPLOAD. Wait until the flashing is done (this programmer software is a bit slow).
  • Once done, reset the chip. Reset it after every uploads. (Sometimes, the chip doesn't restart correctly by himself...)

You should see the LED blinking on the P3_0 port at about 1 Hz !

Congratulations, you make it work !

Step 9: Use an LCD Screen

Ok, a little bit more complicated : we will put a LCD screen.
We will use a standard 1602 LCD screen. Mine came with a nice blue back-light.

Follow this wiring, the rest of the circuit is the same (you can keep the leds, if you want...)

Lcd module pinout, from left to right, connector on the top : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  • lcd pin 1 : GND
  • pin 2 : VCC 5v
  • pin 3 : contrast, middle pin of the 10k potentiometer
  • pin 4 : p1.2 ( RS)
  • pin 5 : p1.1 ( RW, connected to GND here)
  • pin 6 : p1.0 ( E )
  • pin 7 : p2.0 ( D0)
  • pin 8 : p2.1 ( D1)
  • pin 9 : p2.2 ( D2)
  • pin 10 : p2.3 ( d3)
  • pin 11 : p2.4 ( d4)
  • pin 12 : p2.5 ( d5)
  • pin 13 : p2.6 ( d6 )
  • pin 14 : p2.7 ( d7)
  • pin 15 : Back-light power supply (if your module got one)
  • pin 16 : back-light ground

Fortunately, my development board contains directly a port to connect the screen. I had to manually check the connections, as I don't have the schematics of this board.

I learned almost everything on this web page : http://www.dnatechindia.com/Tutorial/8051-Tutoria...

Watch out, the code on their page contains some errors.

Again, compile this new file and upload it on the chip. Check the contrast pot to get a nice rendering of the letters.

How the program works ? The screen is driven by sending commands. First, we initialize the screen, by setting its size, the behavior of the cursor, the font size... Then, we clear the screen, then we sent each character one by one.

Sending a character or a commands works the same way : the data is loaded into the Accumulator register, then a function is called (lcd_datadisplay for the characters, lcd_command for system commands). The last part of the program contains the functions used to drive the screen, and a delay function.

Step 10: Bigger Project : a Simple Dice

Ok, in this last step we will use the screen as a dice. A button connected to an input will help us to randomize the numbers.


Random numbers are something strange for the chip... In this case, the program will count numbers from 1 to 6 in a (very fast) loop while a button is pressed. As soon as the button is released, the last number stays on the screen. Due to the fast execution speed, it's almost impossible to get the number we want. Push the button again to browse the loop again... Push reset if you want to see the "welcome" screen again.

The wiring of the screen is similar than the previous step. We will add a pull-down button on the port P3.5.

The code and HEX file are attached in this step.

How it works ? First, we display a welcome message. The program waits for a low level on P3.5. Once this button is pressed, the program displays one, two, three... in a loop, until the button is released. Then the last number stays on the screen, and the program waits again for a new push on the P3.5 button. So it's not a real random generator, we just assume that the user can't push the button during the same amount of time...

Step 11: What About C ?

Ok, you can think that the assembly language is a bit hard to understand. If you are familiar with arduino boards, you have a knowledge in C programming. But can we use the C language to program our boards ?


Maybe yes. I heard about an open-source program called SDCC (http://sdcc.sourceforge.net) (Small Device C compiler). The MCS 51 chips are supported. I'll try this out, and maybe write an Instructable on this subject. Again, all the hardware will be the same, even the flashing process will be the same, the only real difference is that we will use an other compiler to get the HEX file...

Step 12: Last Words

Ok, here we are for this Instructable. Again, I'm not an expert with this kind of chips, and I still have a small knowledge in this assembly language. But now, I am able to create something from scratch, as the entire development process is now OK.


If you look on the web, you can find some development boards and other stuff. It's a great way to learn more things ! I'll try to use this chip for my projects... But the main problem will be that it's hard to find libraries and ready-made code for external modules. It's easier to use them with a modern chip, like arduino cards.

Have you ideas for some great projects to make with this chip ?

Thanks for reading !

Comments

author
Granzeier (author)2015-03-21

NICE!!!! It was nice to see this great MCU being used here.

Just a FYI, the old BASIC-52 will fit into this microcontroller; Jan Axelson has made her classic book, The Microcontroller Idea Book, available for sale, and also free download. You can find them both at: http://janaxelson.com/microc.htm. She also has several other books available.

author
Yoruk (author)Granzeier2015-03-22

Oh, thanks for the tip ! I already have some books about this subject, but it's great to know more.

I'll search, but any ideas on how to put the BASIC inside ? I thought it was only possible to do this only with the 8052...

author
Granzeier (author)Yoruk2015-03-22

Ok, it has been quite a while since I have looked into BASIC-52, but I found a link to it in this article about BASIC-52 (http://www.nomad.ee/micros/8052bas.html.

I just got a book back from my editor tonight, and so will be busy with the book for the next week or so. But after that, I think that I will take a short break - a nice retro project like BASIC-52 should be just what the doctor ordered. I'm pretty sure that I have everything needed to put BASIC into one of my old 89C52s and breadboard a system. If so, then I will post the information here.

Thanks again for a very nice 'ible and a great walk down memory lane.

author
Yoruk (author)Granzeier2015-03-23

Thanks, I'll study this link too, and keep you informed... Thanks...!

About This Instructable

5,318views

47favorites

License:

Bio: French engineer in robotics. I love Arduino projects / coding / guitar / various electronics. Oh, I like cooking also ! PS : English is not my main language, please ... More »
More by Yoruk:How to create phenakistoscope discsDiscover the Phenakistoscope : a vintage animation systemHow to make a tasty gingerbread
Add instructable to: