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.
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.
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.
Step 4: Select the microcontroller of your choice in the new pop up window. In our case, it is the AT89C51. Now press Ok.
Step 5: Click on NO in the pop-up window that appears next. We will do this step manually.
Step 6: Create a new file by clicking on the new file button in the top right corner.
Step 7: Write your code in the newly created file.
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.
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.
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.
Step 11: Right-click on Target 1 and then select Options for Target “Target 1”.
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.
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.
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.
- After this, the next instruction stored at 0002H; MOV R1,#46 runs
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.
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.