Your computer needs the SDCC compiler to do this project. The Small Device C Compiler is a free software for Linux or Windows. You need to download and install or you will see messages like this:
During installation make sure to tick the box for Add to the PATH a list of file locations. Restarts are required.
Command terminal needs to show that it recognizes the suite of programs contained in SDCC on a list of folders called PATH.
Open program. Menu Project->New. We will make a fresh project we can edit.
Choose a good location and names for the project and its files.
We can select a different MCU. These are all similar 8051 type chips with different clock speeds, flash size and advanced features.
Untitled fileDefault is for an empty text file for our code. The program thinks untitled is an assembly file. Paste in the code from the next section.
The variable P1 is assigned a number value by the compiler. P1 is given the value of IO port 1 on the chip and the simulator will use all 8 pins to go on and off. P1 is stored in an electronic circuit we will see in the simulator.
This binary count lets us see the processor work through a single module of source code instructions. Match the register values, our variables, the chip pins as the turn on and off.
#include <8051.h>
void DELAY(char count);
int main() {
while(1)
{ P1++;
DELAY(3);
}}
void DELAY(char count)
{ char i;
for(i=0;
i<count;
i++)
{ ; }}
We use this DELAY function so that you can look at the stack as the processor jumps around. We could use delay() from a library but it would complicate the example.
Paste the code into the untitled file. It does not look good with the error indications. The program is spellchecking for assembly and does not know this will be C code.
Save AsRight click on the file tab and select Save as. CountP1.c is a descriptive name with an extension used for C programs. Error indications clear.
Will save file to our project folder and see it displayed in the Project Files area. The error indications will clear once it's checked for GCC C language grammar.
The big moment. Will the compiler generate an executable binary hex file that we can upload to a processor chip and run? Is our code grammatically correct?
The bottom Messages panel shows details of compiler operation. Green text says success. Our project folder now contains several new files including a hex executable binary and an ihx file.
Look at the messages the program is actually telling you. mcu8051ide automatically ticked the x-debug setting.
Switch from editor mode to simulator mode by clicking on the rocketship icon. Try the Animate program command and watch the PC program counter. The simulator is running slower than a real circuit and there is a delay during initialization.
You may need to generate simulator data file. Hover over the menu bar and see icons that step forwards or back through the program. Run program goes through more quickly but does not show internal details.
Does your computer keyboard have function keys? The menus say F2 and F6 will turn the simulator on and off, try them. Maybe your keyboard needs you to hold down Fn for the keys to work.
Animate programThere will be a delay while the simulated MCU initializes. The bouncing green line shows us the instruction being processed. Look for the PC program counter register.
Top selection LED Panel will open a simulated LED bar with 8 lamps. We turn it ON and configure colours with the wrench icon.
The simulator needs us to tell it which leads are connected. Why are we putting pin 7 on the left? It makes reading the binary value for P1 line up with the LEDs.
Animate program with more features open. We get to see right inside of the chip and the code going through it. Run program goes much faster but does not show you what is going on inside.
Look very closely for P1 counting up. Watch for the loops, the stack values. PC is the program counter. R0 to R7 are registers.
Are the Lamps Wired Right?This panel simulates a connection to +5v bus. Look at the little circuit diagrams. When our IO pin goes to ground it pulls current through the LED to make light. 0v means light on, 5v light off.
Click on bottom right corner where you see AT89S51. Type in 8051 see the map of the chip with pins? These are all the same family of MCUs. They have different features and speeds. Click okay to change. Simple code does not need to modify.
When your code requires a feature not on your selected chip you may see errors. Changing to the proper MCU is one option, masking messages is another. Read configuration options and use tick boxes. Sometimes the program sets automatically.
Computers can be complicated. It helps to read what the messages say and follow some instructions, yourself.
Sometimes your code fails to compile. Read messages to find out why. It could be your code or the compiler setup.
The demo code in this project uses an old standard for C. Your code may not run because of new syntax. Syntax errors happen because // comment is not permitted. Function declarations before main() function are also a requirement of C89.
Configure CompilerChange your Compiler settings for C language standard version to C99. mcu8051ide defaults to C89.
The demo code in this project uses the old standard. Your code may not compile because of this syntax. mcu8051ide automatically ticked the x-debug setting.
Running the simulator does take resources from your computer. You may see a significant slow down. Compare simulator performance between Animateprogram and Run program.
Unable to open file is because of READ ONLY protection.
Recompile? is not an error message. It says you've changed the source code and need to recompile in order to use fresh firmware in the simulation.
Look at what is saying. These are the same SDCC commands you would type into a terminal manually. It tells you a lot of details about what the compiler is doing and how. Look for options to use C89 Standard and the -i include instruction.
C programs are modular. Groups of functions and number values are written in different source code files. #include <8051.h> tells the C compiler to add all of the code from a file located elsewhere in the computer. #include "8051.h" means use a file in the same folder.
8051.h is a text source code file provided by SDCC. It gives numbers to use for pin names. We can edit it with notepad. All of the MCU files have the same type of information.
Linux has the same includes folder at /usr/share/sdcc with data for 8051 variants, STM8, PIC chips and others.
Comments
Please log in or sign up to comment.