View Course Path

Set up Keil c51 for 8051 microcontroller simulations – A step by step guide

The 8051 is a powerful microcontroller. To transfer code from the PC to the flash memory (a process also known as “burning”), generating the hex file is essential. To create this file from assembly-level code or any other high-level language like C you need an IDE that has a compiler that will do this job for you.

In our 8051 course, we will be using the Keil Microvision C51. It’s a free (conditions apply – See the box below) IDE for 8051 related embedded development and is a very popular simulation platform as well. You can simulate the code written in the IDE to see the transfer of data in memory locations and registers making it a great tool for simulation. I thas advanced debug capabilities too that makes it extremely powerful for testing.

Programs that generate more than 2 Kbytes of object code will not compile, assemble, or link. However, you don’t need to worry about this for most of the programs we will run to learn.

To generate the hex file the IDE follows the following steps:

  • It generates a .asm file and sends it to the assembler.
  • The assembler generates two files from this. A .lst file and a .obj file.
  • In the next step, a process known as “linking” connects the .obj file to others.obj files
  • Conversion of the object files to a .hex file.

You can read more about the components of the IDE in our post on software development tools in embedded systems.

The hex file can then be burnt into the flash memory of the 8051 using an ISP hardware programmer.

Installing and setting up Keil C51 for 8051

Step 1: Head to the Keil C51 site to download the installation files for Keil C51 IDE. Once downloaded run the .exe file, and then launch the Keil C51 IDE.


Step 2: Click on the Project dropdown menu and then click on New μvision Project.

keil setup


Step 3: Create a new folder at any suitable location on your computer where you wish to keep all project files. In our case it is \thispc\desktop\8051. Create a new project file at this location and click on Save.

keil setup step 3


Step 4: Select the microcontroller of your choice in the new pop up window. In our case, it is the AT89C51. Now press Ok.

keil setup step 4


Step 5: Click on NO in the pop-up window that appears next. We will do this step manually.

keilsetupstep5


Step 6: Create a new file by clicking on the new file button in the top right corner.

keilsetupstep6


Step 7: Write your code in the newly created file.

keil setup step 7


Step 8: Save the file in your Project folder. The extension to be used is .c for c code and .asm in case of assembly-level code. As we are using assembly-level code we use .asm here.

keil setup step 8a

keil setup step 8b


Step 9: Click on the expand (plus) sign next to Target 1 and then right-click on Source group 1. In the options box, click on Add Existing files to Source group 1. This adds the new file that contains your code to your Project file.

keil setup step 9


Step 10: Select the file you created and add it here. Now you have a Project file with its constituent code files. All the other files that will be generated will be present in this Project folder.

keil setup step 10


Step 11: Right-click on Target 1 and then select Options for Target “Target 1”.

keil setup step 11


Step 12: In the new dialog box, click on the Output tab and then check the box in front of the Create hex file option.

keil setup step 12


Step 10: Build the project by pressing F7 on your keyboard and if there are no errors in your code the hex file will be in the objects folder of your project folder.


Simulating code using Keil Microvision

As mentioned earlier we can use Keil to simulate our code. Let’s do that by clicking on the magnifying scope symbol in the toolbar.

keil simulation 1

Once you click here after compiling your code you can see a step by step execution of your code. Let’s get insights into how the addition of numbers happens inside the microcontroller using the simulator.

  • The execution starts from memory location 0000H where the first instruction MOV A,#60 is placed. The first number in the rectangular box shows the address in the memory where the opcode is stored.

keil simulation 2

  • After this, the next instruction stored at 0002H; MOV R1,#46 runs

keil simulation 3

Notice how the values in registers A and R1 have changed

  • The ADD instruction gets executed in the next step. The value in the accumulator and PSW register gets updated due to an overflow condition. To understand why this happens, look at our post on Arithmetic operations in 8051.

keil simulation 4

Conclusion

Now you can learn assembly language programming/embedded C with 8051 without actually needing a kit. Just take up any sample programs, compile them, and run the simulation tool. This is a great exercise in learning the working of instructions from a close perspective.

Also, as we advise in our course FAQs for this 8051 course, save your money on getting an 8051 kit. Just learn the basics of assembly and embedded C in this course and move on to learn ARM in our free ARM Cortex M3/M4 course. That will be more beneficial for your career.

If you have any issues with installing the IDE, let us know in the comments.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.