Keil
Inputs
1. Introduction
In this exercise, we will look at supplying an external input to our system. We will do this both
in the simulator in Keil and also using the push button we used in the logic project before
Christmas.
In this example, we will use port 3 pin 2 as our input, and use this to change the value of port 1
pin 0.
2. Setting Up Keil
Create a new folder for the first program today and give it a suitable name such as ‘Input
Example 1’. Start a new project and give it a suitable name too. When the ‘Select Device for
Target’ window appears, click the + symbol to the left of ‘NXP’ and then select ‘P89C668’.
This is the version of the basic 8051 we use in the lab.
Complete the rest of the set-up process as we did last week.
You can now add an assembly language file to ‘Source Group 1’. (Right click on source
group 1, add item, and then select .asm file. Give it a suitable name)
3. The Code
You should add the following code to the source file:
ORG 0H
SJMP START; Jump to start of code
ORG 40H
START:
SETB P1.0; Initialise Port 1 bit 0 to 1
REPEAT:
JB P3.2, START; Using a bit-conditional jump. If port
; 3 bit 2 is set, then jump to START to set port 1
; bit 0
CLR P1.0; clear port 1 bit 0
SJMP REPEAT; unconditional short jump to repeat
END
Note that the comment on the line which starts ‘JB’ is too long for one line here, but it will fit
on one line in Keil.
3.1. Code Explanation
You will notice that the code is quite simple. We begin with the normal jump over the reserved
memory area to 40H. Remember the ‘H’ tells us this is a hex value, rather than decimal.
We define the destination of the jump using the ‘START:’ label, and at this location we set
port 1 bit 0 to 1 using the SETB P1.0 command.
Before we go any further, we need to think about what we want the code to do:
1. If the input is high, set port 1 bit 0
2. It the input is low, clear port 1 bit 0.
This is all we need to happen.
We test for whether port 3 bit 2 (the input) is high by using the JB (jump if bit is set) command,
which we will learn about next week. If the bit is set, we jump to the label if not we carry on.
Condition 1 above, means that if the bit (input) is set, we need the output to be set, and we
already do this at the ‘START:’ label, so we use this label again here to save repeating this line
of code. In this instance it wouldn’t matter that much, but it can save a lot of space working
like this in larger programs. If the input is not set, then the jump is ignored and the next line
of code is processed. Condition 2 above says that if the input is 0, the output should be zero,
so the next line of code is ‘CLR P1.0’ which is clear port 1 bit 0. At this point, we have done
all we need to do and we can go round again, so we jump back to ‘REPEAT’. We jump back
to repeat rather than to start because we want to repeat the test of the input, we don’t want to
set the output at this stage.
4. Testing on Keil
1. Save the file and build the project
2. Start the debugger.
3. Go to ‘Peripherals’, select ‘I/O Ports’ and open Port 1 and Port 3.
4. Single step through the program- it is only short so will step through quite quickly.
5. Change the input to zero. To do this click on the lower row in the port 3 window, as
highlighted in red in figure 1 below.
Figure 1: Changing the input
6. Keep single-stepping, and note what happens to the value of Port 1 bit 0.
7. Take a screen image of this and upload to Moodle
8. Click the box again to set the input back to 1, and step through to see what happens
now. Again, grab a screen image of this and upload to Moodle.
5. Testing in Reality
In order to actually test the code on the microcontroller board in the lab, we need to download
the executable code to the program memory on the microcontroller.
This can be done in a number of ways but here we will use a dedicated program called Flip.
This will download the ‘hex’ file, which is a binary file containing the numerical values to be
stored in memory on the microcontroller to the microcontroller memory. It does this via a
USB-RS232 link which communicates with a small program on the microcontroller already,
known as a Bootloader.
Three things to note when downloading to the microcontroller:
1. RS232 communicates using numbered ‘COM’ ports. In USB, these are automatically
numbered, and sometimes the number of the link to the microcontroller board will
change. If COM1 doesn’t work, try COM2 or COM3 until you connect correctly.
2. The 89C668 has ‘run’ and ‘load’ modes which are selected by a switch. This switch is
only read when the microcontroller is reset so you need to select the desired mode either
before you turn the power on, or you switch modes and then press reset and hold for a
couple of counts.
3. Before you build the code, make sure that the ‘Create Hex File’ check box is checked
on the output tab of the ‘options for target 1’ window shown in figure 2.
Figure 2: The Options for Target Window Showing Output Tab.
5.1. Connecting the Circuit
Before we can download to the microcontroller board, we need to connect the board and
connect the programming interface.
The various parts of the board and the connections required at this stage are shown in figure 3.
1. Connect the black and red 2mm terminals on the microcontroller board to the ground
and 5V digital outputs on the lab power supply.
2. Connect the 9-pin D, RS232 connector to the terminal shown.
3. Switch the ‘Run/Load’ switch to ‘load’ mode.
4. Switch the power supply on. The system should only draw about 10mA at this stage.
As ever, if the overload light comes on, switch off immediately and try to find the source
of the issue.
Figure 3: The Programming Connections on the 89C668 Microcontroller Board
In order to check that pressing the button causes the LED to come on, we also need to connect
the microcontroller board to the breadboard. This is achieve using the screw terminals on the
microcontroller board and connecting it to the switch and LED on our breadboard from last
term.
1. Make sure that the power is turned off on the microcontroller board.
2. Make sure that the ground and 5V leads are connected to the power supply. It is
important, particularly that the ground connection is made to protect the IC. Officially,
you should also wear a grounding bracelet to ensure you are grounded too.
You may now need to remove the logic circuit you built before Christmas. You need
to leave the switch circuit and the LED drive circuit connected.
3. Connect the power connections on the breadboard to the power supply.
4. Using the screw terminals on the microcontroller board to make connections, connect
one wire from port 3 pin 2, to the switch output (which unless you’ve moved it, should
be the yellow 4mm terminal).
5. Connect a second wire from port 1 pin 0 to pin 1 on the ULN2308A transistor drive IC
(again, assuming you did not reconnect things from the original connection in lab 1)
6. Switch the power supply back on, again, check that there isn’t excessive current flow.
A close-up of the connections on the microcontroller board is shown in figure 4, and the
connections to the breadboard are shown in figure 5. Note that the switch and LED circuitry
on the breadboard needs to be kept, but the logic circuitry you used before Christmas should
be removed.
Figure 4: Wiring Connections on the Microcontroller Board
Figure 5: Wiring Connections to the Breadboard
5.2. Programming the Microcontroller
To open ‘Flip’, click on the flip shortcut as shown in figure 6.
Figure 6: The Flip Boot Loader Icon
A small program window will appear, as shown in figure 7.
Figure 7: The Flip Window
Firstly, select your target device by clicking on the ‘Device’ menu and ‘Select’. The window
shown in figure 8 will appear. You should scroll down the list to select AT89C51ED2.
Figure 8: Device Selection
You should now load the hex file to be downloaded to the microcontroller. This file is
automatically produced when you build your project if you have the ‘create hex file’ box
checked, and should be located in the ‘objects’ subfolder in your project folder.
Once you have loaded the file, you should now ensure that the microcontroller board is in load
mode and press the reset button on the board, holding it for about 2 seconds.
You should now select the ‘Settings’ and select ‘RS232 Setup’. Work through the ports in turn
and click ‘Connect’ in each case. Eventually the program should connect with the
microcontroller board.
If it does not connect:
1. Check that the board is in ‘load’ mode, and press reset to ensure that the mode is
detected.
2. Check that the power supply is set to 5V and power is supplied to the board.
3. Check that the signal lead is connected both to the board and the PC. It should already
be connected at the PC end.
4. Check that the baud rate is 115200. The baud rate is the speed at which data is sent to
the microcontroller, and the communication only works if the speed at both ends is the
same.
5. Try all the com ports once more and if it still doesn’t work, ask.
Figure 9: Connection Setup
Figure 9 shows the RS232 set up window. Hopefully, the correct COM port will be selected
by default as here. Once the connection is successfully made, the icons along the top of the
main window become active and coloured as shown in figure 10.
Figure 10: Connection Successful
Click on the run button to the bottom left of the window. This will then go through the stages
of erasing, downloading and checking the microcontroller. If each of these stages complete
successfully, the indicators to the side of each stage will become green, and successful
completion is indicated in the message area at the bottom of the window, as shown in figure 11.
Figure 11: Programming Successful
Once this is complete, switch the microcontroller back to run mode and again press the reset
button for about 2 seconds.
If you now press the button, you should find that when the button is pressed the LED comes
on, and when the button is released, the LED should go off.
6. Using the Input to Toggle the Output
As an extension task, think how you would rewrite this code so that each time the input is
pressed (each time the input goes high) the output toggles. You should ensure that the output
only toggles once for each press rather than repeatedly toggling on each press. To do this, you
will need to use the following commands:
JB
We can use this as before to test that the input bit is set. This can then be used to trigger the
change.
JNB
This is the opposite of the above and tests for the bit being zero. You can use this to test that
the input has been released.
CPL
CPL is the command to complement. You will need to be sure that you initialise port 1 bit 0
to start as we did earlier, but then you can use this command to toggle the value of this bit on
each press.
Again, you can test this both in simulation and on the board itself.
7. Further Tasks
1. Develop the basic tasks to switch all the LEDs
2. Think about how to get the LEDs to switch in the right combinations for the dice on
each press- this will help with the dice project itself