Getting started with 8051 at89 c51 using keil uvision 4 and proteus
2016)/EE/Ali... · Web viewThis tutorial will assist you in writing your first 8051 Assembly...
Transcript of 2016)/EE/Ali... · Web viewThis tutorial will assist you in writing your first 8051 Assembly...
Microprocessor Interfacing and Programming
Lab 1: Basic Tutorial for Keil Software
Objective
To learn how to use keil software
Required Tools
Keil software
Introduction
This tutorial will assist you in writing your first 8051 Assembly language program using the popular Keil compiler. Keil offers an evaluation package that will allow the assembly and debugging of files 2K or less. This package is freely available at their web site. Keil’s website address is www.keil.com. The sample program included in the tutorial toggles Ports 1 and 2 on the 8051.
1. Open Keil from the Start menu2. The Figure below shows the basic names of the windows referred in this document
Page | 1Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
3. Select New µVision Project from the Project Menu.
4. Name the project ‘Toggle and Click on the Save Button.
Page | 2Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
5. The device window will be displayed. Select the part you will be using to testwith. Select “Atmel” part from the menu .
6. Double click on Atmel. A scroll down window will expand. Select “AT89C51”
Page | 3Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
7. Select NO
8. Click File Menu and select New
9. A new window will open as follows
Page | 4Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
10. Copy the following example into the new window. This file will toggle Ports 1 and 2 with a delay.
ORG 0H MOV A, #55
AGAIN: MOV P1, A MOV P2, A ACALL DELAY CPL A SJMP AGAINDELAY: MOV R3, #200OUTER: MOV R2, #0255INNER: DJNZ R2, INNER DJNZ R3, OUTER RETEND
Code to toggle port 1 and 2
11. Click the file menu and save as
Page | 5Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
12. Name the file as toggle.asm and save.
Page | 6Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
13. Click on the “Source Group” and a click “Add File to Group ‘Source Group 1’
14. Select from “toggle.asm” file from desktop and add it to the “Source Group 1”
Page | 7Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
15. Click on “Project” tab and select “Options for Target ‘Target 1’ as follows
16. Enter crystal value 11MHz
Page | 8Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
17. Click on “output” tab. Check “Create HEX File” check-box. And click “OK”
18. Click on “Rebuild all target files” button as shown below. In the Build Window it should report ‘0 Errors (s), 0 Warnings’. A HEX file has been generated in the project folder having name “toggle.hex”
Page | 9Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
Testing Program in Debugger
1. Comment out line ACALL DELAY by placing a Semicolon at the beginning. This will allow you to see the port change immediately. Click on the File Menu and select Save. Rebuilt program again
2. Click on “Debug” tab and select “Start/Stop Debug Session”
Page | 10Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
3. The Keil Debugger should be now Running. Click on Peripherals. Select I/O Ports, Select Port 1.
4. A new window should port will pop up. This represent the Port and Pins
Page | 11Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
5. Step through the code by pressing F11 on the Keyboard. The Parallel Port 1 Box should change as you completely step through the code.
Page | 12Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
LAB 2: Arithmetic Operations using Assembly Language for 8051
Objectives
Introduction to Arithmetic Operators in assembly language To understand the use of debugger of Keil software to observe memory and operations. To understand MOV instruction in assembly language
Tools/Components
Keil Software
MOV Instruction
In assembly language, the MOV instruction is concerned with moving data internally in a processor/microcontroller, i.e. between Internal RAM, special function registers (SFR), program status word (PSW) register, general purpose registers (R0 – R7) etc. MOVX and MOVC commands are used in accessing external memory data. The MOV instruction has the following format:
MOV destination, source
The instruction copies (copy is a more accurate word than move) data from a defined source location to a destination location. Example MOV instructions are given below
MOV R2, #80h ; Move data value 80h to register R2
MOV R4, A ; Copy data from accumulator to register R4
MOV R2, 80h ; Copy data from RAM location 80h to R2
MOV 50h, #52h ; Copy data value 52h to RAM location 50h
MOV 52h, 53h ; Copy data from RAM location 53h to RAM location 52h
MOV A, @R0 ; Copy contents of location addressed in R0 to Accumulator (A)
; It is also called indirect addressing
Page | 13Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
Program Status Word (PSW) Register
Every microcontroller contains flags that may be used for testing the outcome of an instruction's execution. For example, the carry flag may be used to test the outcome of an 8-bit addition to see if the result is greater than 255. 8051 microcontroller has a flag register; also called as program status word (PSW) register. It has 8 bits. Detail of all these 8 bits is given below.
Program status word (PSW) register
Bit Symbol Description ExplanationPSW.7 CY Carry flag This flag bit value becomes 1 when there is a
carry out from the D7 bit. This flag bit is affected after an 8-bit addition or subtraction.
PSW.6 AC Auxiliary carry flag If there is a carry from D3 to D4 during an addition or subtraction operation, this bit gets value 1 otherwise it is 0.
PSW.5 F0 Flag 0 Available to the user for general purposePSW.4 RS1 Register bank select 1 This bit is used for allocation of RAM memory
space in 8051PSW.3 RS0 Register bank select 0 This bit is used for allocation of RAM memory
space in 8051PSW.2 OV Overflow flag If addition or subtraction of two signed
numbers generates a carry, this bit becomes 1. Otherwise it is 0
PSW.1 -- User defined bit User defined bitPSW.0 P Parity flag This parity flag reflects the number of 1,s in
the Accumulator register (A). If A register contains odd number of 1,s, then P has value 1, otherwise it has 0 value.
Addition
Register A (the accumulator) is used to hold the result of any addition operation. Perform these simple additions and observe the memory in Keil software.
MOV A, #0 ; Move 0 into Accumulator register
ADD A, #25h ; Adds the number 25h to A and putting sum in A
ADD A, R3 ; Adds the register R3 value to A and putting sum in A
Page | 14Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
Subtraction
Computer subtraction can be achieved using 2’s complement arithmetic. Most computers also provide instructions to directly subtract signed or unsigned numbers. The accumulator; register A, will contain the result (difference) of the subtraction operation. The C (carry) flag is treated as a borrow flag, which is always subtracted from the minuend during a subtraction operation. Perform these subtraction operations and observe the memory in Keil software.
SUBB A, #55d ; Subtract the number 55 (decimal) and the C flag from A and put the result in A.
SUBB A, R6 ; Subtract value in R6 and C flag from A and put the result in A.
SUBB A, 58h ; Subtract the number in RAM location 58h and the C flag From A and put the
; result in A.
Increment/Decrement
The increment (INC) instruction has the effect of simply adding a binary 1 to a number while a decrement (DEC) instruction has the effect of subtracting a binary 1 from a number. The flags C, AC, and OV are not affected by the increment or decrement instructions. If a value of FFh is incremented it overflows to 00h. If a value of 00h is decrement it underflows to FFh. The DPTR can overflow from FFFFh to 0000h. The DPTR register cannot be decremented using a DEC instruction (unfortunately!). Some example INC and DEC instructions are as follows:
INC R7 ; Increment register R7
INC A ; Increment A
INC @R1 ; Increment the number which is the content of the address in R1
DEC A ; Decrement register A
DEC 43h ; Decrement the number in RAM address 43h
INC DPTR ; Increment the DPTR register
Multiplication and Division
The 8051 supports 8-bit multiplication and division. This is low precision (8 bit) arithmetic but is useful for many simple control applications. The arithmetic is relatively fast since multiplication and division are implemented as single instructions. If floating point arithmetic is required then special software routines need to be written. For the MUL or DIV instructions the A and B registers must be used and only unsigned numbers are supported.
Page | 15Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
Multiplication
The MUL instruction is used as follows (note absence of a comma between the A and B operands):
MUL AB
; Multiply A by B. The resulting product resides in registers A and B, the low-order byte is in A and the high order byte is in B.
Note the register values in Keil software
Division
The DIV instruction is used as follows:
DIV AB
; A is divided by B. The remainder is put in register B and the integer part of the quotient is put in register A
LAB 3: To simulate blinking of an LED with some delay and push button
Page | 16Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
Objective
To blink an LED with some delay To blink an LED using a push button
Required Components
Proteus Software to simulate experiment Keil software to make HEX file of the required program
Theory
8051 is a 40 pin microcontroller. These pins are dedicated to perform various functions such as input/output, read/write operations, to set frequency of operation, as timers and as external interrupts. Figure 1 shows the pin description of 8051 microcontroller.
Figure 1: Pin description of 8051 microcontroller
Detailed Pin Description
Page | 17Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
A detailed pin description of 8051 microcontroller is shown in table 1. Pin 40 is supplied by +5 volt power supply and pin 20 is connected to negative pin of the power supply.
Pin Number Pin Name 8051 Pin Details1 P 1.0 Pin 0 of port 1 used as input/output2 P 1.1 Pin 1 of port 1 used as input/output3 P 1.2 Pin 2 of port 1 used as input/output4 P 1.3 Pin 3 of port 1 used as input/output5 P 1.4 Pin 4 of port 1 used as input/output6 P 1.5 Pin 5 of port 1 used as input/output7 P 1.6 Pin 6 of port 1 used as input/output8 P 1.7 Pin 7 of port 1 used as input/output9 RST It is reset pin which is kept low through a resistor. When it is high
it resets all registers of microcontroller 10 P 3.0 (RXD) Pin 0 of port 3 used as input/output
This pin (RXD) is also used to receive serial communication signal11 P 3.1 (TXD) Pin 1 of port 3 used as input/output
This pin (TXD) is also used to transmit serial communication signal12 P 3.2 (INT0) Pin 2 of port 3 used as input/output
It is also used for external interrupt 013 P 3.3 (INT1) Pin 3 of port 3 used as input/output
It is also used for external interrupt 114 P 3.4 (T0) Pin 4 of port 3 used as input/output
It is also used to set timer 0 value15 P 3.5 (T1) Pin 5 of port 3 used as input/output
It is also used to set timer 1 value16 P 3.6 (WR) Pin 6 of port 3 used as input/output
If external memory is attached with microcontroller, this pin is used to write data on it
17 P 3.7 (RD) Pin 7 of port 3 used as input/output
If external memory is attached with microcontroller, this pin is used to read data from it
18 XTAL2 A crystal of appropriate value is attached between pin 18 and 19 19 XTAL1 A crystal of appropriate value is attached between pin 18 and 1920 GND This pin is connected to ground terminal of the battery
Page | 18Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
21 P 2.0 Pin 0 of port 2 used as input/output22 P 2.1 Pin 1 of port 2 used as input/output23 P 2.2 Pin 2 of port 2 used as input/output24 P 2.3 Pin 3 of port 2 used as input/output25 P 2.4 Pin 4 of port 2 used as input/output26 P 2.5 Pin 5 of port 2 used as input/output27 P 2.6 Pin 6 of port 2 used as input/output28 P 2.7 Pin 7 of port 2 used as input/output29 PSEN This pin is used if an external ROM is attached with the
microcontroller.30 ALE/PROG This pin is used to de-multiplex port 0 to use as address and data
lines if an external ROM is attached with the microcontroller.31 EA/ VPP EA pin is connected to VCC because 8051 has an internal ROM to
store our program. In 8031 it is connected to ground because an external ROM is attached with 8031.
32 P 0.7 Pin 7 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
33 P 0.6 Pin 6 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
34 P 0.5 Pin 5 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
35 P 0.4 Pin 4 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
36 P 0.3 Pin 3 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
37 P 0.2 Pin 2 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
38 P 0.1 Pin 1 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
39 P 0.0 Pin 0 of port 0 used as input/outputThis pin is connected to VCC (+5v) through is a pull-up resistor
40 VCC This pin is connected to +5V battery
Table 1: Pin details of 8051 microcontroller
To blink an LED with some delay
Page | 19Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
First we learn how to connect and write a code for blinking an LED with some delay. Our task is to build the circuit as shown in figure 2.
Figure 2: To blink an LED with some delay
Step-By-Step introduction to Proteus according to our requirements
Now we will go through following steps to build our required circuit in Proteus software1. Open Proteus and click on the “isis” blue symbol. A window will appear as shown in figure 3.
Figure 3: Schematic Capture window of Proteus
Page | 20Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
2. Click on “component mode” button on the left most icon-bar. Then click on the "P" button (on the left side panel) to open up a search window as shown in figure 3. Write AT89C51 in the search window and find AT89C51 microcontroller. Double click on the controller and it will be added to the component list. Then search for “LED-Red”. Double click on the red led and it will also be added to the component list. Now search for a resistor and double click on it to select it. Now select AT89C51, led and resistor and place them on the circuit schematic area. Construct the circuit as shown in the figure 4. Now click on the “terminal mode” icon on the left side-bar and select “Ground” terminal. Place this ground terminal on the schematic window and construct the circuit.
Figure 4: How to search for the required IC
Figure 5 show s the final circuit to blink an LED.
3. Now create a project in “uVision Keil” software. Write the required code for blinking an LED as shown below. Create its hex file.
Figure 5: To blink an LED
Page | 21Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
Code for blinking an LED
Write following code in Keil software for blinking an LED. Create its hex file.
ORG 0HERE: SETB P1.0 ;SET P1.0 AS 1 ACALL DELAY ACALL DELAY CLR P1.0 ;CLEAR P1.0 ACALL DELAY ACALL DELAY SJMP HERE
DELAY: MOV R2, #255 OUTER: MOV R3,#255INNER: DJNZ R3, INNER DJNZ R2, OUTER RET
ENDCode for blinking an LED
4. Double click on the microcontroller in the Proteus software. A pop-up window will open. Set crystal frequency to 11MHz. Load the HEX file of the program in the microcontroller by clicking on the button which is infront of “Program File” text box as shown in figure 6. Note that we create HEX file of the program using Keil software.
Figure 6: Upload HEX file in the microcontroller.
Page | 22Department of Electrical EngineeringGift University, Gujranwala
Microprocessor Interfacing and Programming
5. Now run the program using “run” button on the lowest bar of the proteus software.
To blink an LED using a push button
Aim of this part is to blink an LED using a push button. A push button is attached to the pin number P1.5 and an LED is connected to P1.0. Status of push button is read in the microcontroller continuously. If this push-button is pressed then blink an LED connected to pin P1.0.
Connect the circuit as shown in figure 7. Write the required code for the task
Figure 7: To blink an LED using a push button
Page | 23Department of Electrical EngineeringGift University, Gujranwala