Beruflich Dokumente
Kultur Dokumente
ET2640
Microprocessors and Microcontrollers
Onsite Course
GRADED ASSIGNMENTS
GRADED ASSIGNMENTS
Graded Assignments
Unit 1 Assignment 1: Basic Concepts and Numerical Methods
Course Objectives and Learning Outcomes
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Draw and label block diagrams of microcontrollers showing the arithmetic logic unit (ALU),
internal bus structure, and registers and peripheral function blocks.
Describe the essential processor control, data, and address bus elements and timing cycles.
Describe the function of the major elements in the 8051 CPU architecture.
Compare address bus, data bus, and control bus in terms of their function, direction, and bus
width.
Contrast Harvard vs. Von Neumann processor architectures of 8051 and Atmel AVR devices.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R.
Chapter 0: Problems 1-8, 21, 24-34, 37-40, 43, 44, 46, 51-55, 64-66
Search the ITT Tech Virtual Library and find five applications for microprocessors and five
applications for microcontrollers. Record your findings in a Microsoft Word document.
-1-
04/19/2012
GRADED ASSIGNMENTS
Using the textbook and online resources as references, summarize the history of the 8051 family
of microcontrollers. The summary should be at least one page in length and written as a Microsoft
Word document. In your summary, be sure to list all your references.
Using Figure 0-31 in the text, describe the primary function of the following resources: ALU,
program counter, instruction decoder, and registers. Identify the type of bus that is interfaced to
each resource. Record your answers in a Microsoft Word document.
Required Resources
Textbook: Mazidi, M. A., Mazidi, J., & McKinlay, R. (2009). Microprocessors. New York: Pearson
Custom Publishing.
Submission Requirements
Submit written answers to all textbook problems making sure to include any formulas and
calculations when applicable.
-2-
04/19/2012
GRADED ASSIGNMENTS
Draw and label block diagrams of microcontrollers showing the arithmetic logic unit (ALU),
internal bus structure, and registers and peripheral function blocks.
Describe the function of the program counter, data registers, and accumulators.
Define data blocks, variable names, and constants used in the 8051.
Program the 8051 using the stack pointer to access a stack data storage area.
Describe how individual instructions can affect the CY, AC, and P flags on the PSW register.
Compare the functions of Port 0, Port 1, Port 2, and Port 3 on the 8051.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors.
Write and document a program to add 3BH and 9CH together using register R2 and accumulator
A. Determine the values of the CY, P, and OV flags in the PSW register after the program is
executed.
Write and document a program to toggle all 8 bits of I/O Port P2 every 300 ms. Assume the
crystal frequency is 11.0592 MHz and that there are 12 clock periods per machine cycle.
-3-
04/19/2012
GRADED ASSIGNMENTS
Required Resources
Submission Requirements
Submit written answers to all textbook problems making sure to include any formulas and
calculations when applicable.
Preferably submit all programs in a Microsoft Word document, although a neatly handwritten
program is acceptable.
-4-
04/19/2012
GRADED ASSIGNMENTS
Program the 8051 to use indirect addressing to access look-up table values.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors.
Write 8051 Assembly programs as they are described in the PROBLEMS section of the text,
Mazidi, M. A., Mazidi, J., & McKinlay, R. Microprocessors.
o
Required Resources
Submission Requirements
Submit written answers to all non-programming textbook problems making sure to include any
formulas and calculations when applicable.
-5-
04/19/2012
GRADED ASSIGNMENTS
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Write an assembly program to perform arithmetic operations in assembly (ADD, ADDC, DA,
SUBB, MUL, DIV).
Write an assembly program to perform bit-wise logical operations (AND, OR, XOR, CPL).
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors.
o
Write 8051 Assembly programs as they are described in the PROBLEMS section of the text,
Mazidi, M. A., Mazidi, J., & McKinlay, R. Microprocessors.
o
Required Resources
Submission Requirements
Submit written answers to all non-programming textbook problems making sure to include any
formulas and calculations when applicable.
Preferably submit all programming problems as a Microsoft Word document, although a neatly
handwritten program is acceptable.
-6-
04/19/2012
GRADED ASSIGNMENTS
-7-
04/19/2012
GRADED ASSIGNMENTS
Explain how functions and subroutines are utilized for embedded system programming using the
C programming language.
Program the 8051 to generate a time delay using the C programming language.
Program the 8051 to perform I/O operations using the C programming language.
Program the 8051 to perform BCD data conversions using the C programming language.
Program the 8051 to perform logical and arithmetic operations using the C programming
language.
Program the 8051 to serially shift data using the C programming language.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors:
Write 8051 C programs as they are described in the PROBLEMS section of the text, Mazidi, M.
A., Mazidi, J., & McKinlay, R. Microprocessors:
o
Required Resources
Submission Requirements
-8-
04/19/2012
GRADED ASSIGNMENTS
Submit written answers to all non-programming textbook problems making sure to include any
formulas and calculations when applicable.
Preferably submit all programming problems as a Microsoft Word document, although a neatly
handwritten program is acceptable.
-9-
04/19/2012
GRADED ASSIGNMENTS
Explain the purpose of each operating mode used by the TMOD register.
Program the 8051 to generate time delays using the C programming language.
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Explain how function block features and interrupt usage are used in designing products.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors:
o
Chapter 9: Problems 1-15, 20, 21, 25, 26, 43, 44, 45, 46, 50, 51
Write 8051 C programs as they are described in the PROBLEMS section of the text, Mazidi, M.
A., Mazidi, J., & McKinlay, R. Microprocessors:
o
Required Resources
Submission Requirements
Submit written answers to all non-programming textbook problems making sure to include any
formulas and calculations when applicable.
Preferably submit all programming problems as a Microsoft Word document, although a neatly
handwritten program is acceptable.
-10-
04/19/2012
GRADED ASSIGNMENTS
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Explain how function block features and interrupt usage are used in designing products.
Explain the function of the 8051 SBUF, SCON, and PCON registers for serial communications.
Calculate the value stored in the THx register for a given baud rate.
Program the 8051 to transmit data from the serial port using the C programming language.
Program the 8051 to receive data from the serial port using the C programming language.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors:
o
Chapter 10: Problems 1-16, 22, 25, 27, 29-36, 40-43, 45, 46, 52, 53
Write 8051 C programs as they are described in the PROBLEMS section of the text, Mazidi, M.
A., Mazidi, J., & McKinlay, R. Microprocessors:
o
Required Resources
Submission Requirements
Submit written answers to all non-programming textbook problems making sure to include any
formulas and calculations when applicable.
-11-
04/19/2012
GRADED ASSIGNMENTS
Preferably submit all programming problems as a Microsoft Word document, although a neatly
handwritten program is acceptable.
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Explain how function block features and interrupt usage are used in designing products.
Program the 8051 to perform Timer operations with interrupts using the C programming
language.
Program the 8051 to perform external hardware interrupts using the C programming language.
Program the 8051 to perform serial communications with interrupts using the C programming
language.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors:
o
Chapter 11: Problems 1, 2, 4, 17, 18, 21, 22, 29, 30, 33-38, 45, 46, 49, 50, 57, 60, 66, 67
Write 8051 C programs as they are described in the PROBLEMS section of the text, Mazidi, M.
A., Mazidi, J., & McKinlay, R. Microprocessors:
o
-12-
04/19/2012
GRADED ASSIGNMENTS
Required Resources
Submission Requirements
Submit written answers to all nonprogramming textbook problems, making sure to include any
formulas and calculations when applicable.
Preferably submit all programming problems as a Microsoft Word document, although a neatly
handwritten program is acceptable.
-13-
04/19/2012
GRADED ASSIGNMENTS
Explain how commands and data are passed between the 8051 and LCD.
Describe the functions of the RS, R/W, and E pins on the LCD.
Program the 8051 to perform I/O operations using the C programming language.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors:
o
Write an 8051 C program to send the string Hello to the LCD by checking the busy flag.
Required Resources
Submission Requirements
Submit written answers to all nonprogramming textbook problems, making sure to include any
formulas and calculations when applicable.
Preferably submit all programming problems as a Microsoft Word document, although a neatly
handwritten program is acceptable.
All parts of this assignment are due at the beginning of Unit 10.
-14-
04/19/2012
GRADED ASSIGNMENTS
Describe the functions of the CS, RD, WR, CLK, INTR, Vin, Vcc, and Vref/2 pins of an ADC chip.
Assignment Requirements
Complete the following questions from the PROBLEMS section of the text, Mazidi, M. A., Mazidi,
J., & McKinlay, R. Microprocessors:
o
Chapter 13: Problems 1, 2, 6, 11, 13, 14, 18, 28, 37, 39, 47, 48, 52, 53, 54, 58, 59
Required Resources
Submission Requirements
Submit written answers to all problems, making sure to include any formulas and calculations
when applicable.
All parts of this assignment are due at the beginning of Unit 11.
-15-
04/19/2012
GRADED ASSIGNMENTS
Project
Unit 3 Project Part 1: Introduction and Research Assignment
Purpose
This is the first assignment for the Project where you will compile a list of potential project ideas. You must
be prepared to discuss the project ideas at the next class meeting.
Course Objectives and Learning Outcomes
Plan, design, implement, and debug at least one functional microcontroller student project.
Demonstrate how to break down embedded projects into separable software tasks and hardware
functions.
Required Resources
Project Logistics
The Project will continue until the end of the course and will consist of the following requirements:
Write a proposal of your design concept that describes the design features and specifies the
project deliverables.
The first deliverable is the Research Assignment where you must research embedded system
applications and come up with a list of at least ten potential project ideas. Keep in mind that you only have
until Unit 10 to complete your project. The embedded system design must be implemented with the 8051
microcontroller.
Suggested references for this assignment include:
http://www.8051projects.info/projects.asp
http://www.hobbyprojects.com/microcontroller_tutorials.html
16
04/19/2012
GRADED ASSIGNMENTS
Deliverables
The Research Assignment must be submitted as a Microsoft Word document at the beginning of Unit 4.
The project ideas will need to be approved by the instructor before you can begin work on the Project
Proposal.
Project deliverables for future units are as follows:
17
04/19/2012
GRADED ASSIGNMENTS
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Project Logistics
The Project Proposal must include the following:
The names of each group member and their assigned roles within the groupone member of the
group is appointed as group leader
A brief description of your proposed design methodology, including items such as:
A timeline showing the major deliverables and any intermediate goals for the next 6 units.
You and your group must be prepared to discuss your proposal with the class at the next class meeting.
Deliverables
The Project Proposal must be at least two pages in length and submitted as a Microsoft Word document
at the beginning of Unit 5. Only one proposal per group is required. The Project Proposal will need to be
approved by the instructor before you can begin work on the Design Plan.
18
04/19/2012
GRADED ASSIGNMENTS
Demonstrate how to break down embedded projects into separable software tasks and hardware
functions.
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Project Logistics
Your Design Plan must include the following:
The title of your project and the names of each group member
A block diagram/sketch displaying the major elements of the proposed embedded systemthis is
an extended version of the block diagram submitted in your proposal
A basic list of components you will need to implement the proposed embedded systemthis list
need not be detailed at this point (i.e., part numbers and vendor names are not required)
The programming language used for programming the microcontrollerexplain the reason for
your decision
A design methodology describing your initial thoughts on how you will proceed with the design, for
example:
A test plan describing how you will test your system along with a description of the expected
results
As you work on this assignment, keep in mind that this is an initial design of your proposed embedded
system. Expect to make ongoing modifications to your design over the next several units.
19
04/19/2012
GRADED ASSIGNMENTS
You and your group must be prepared to discuss your design plan with the class at the next class
meeting.
Deliverables
The Design Plan must be submitted as a Microsoft Word document at the end of Unit 6. Only one design
plan per group is required.
20
04/19/2012
GRADED ASSIGNMENTS
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Project Logistics
Each group will present an informal progress report of their embedded design to the class. The progress
report must include:
An updated block diagram/sketch displaying a more detailed layout of the embedded system
(make copies for the entire class)
Deliverables
The Progress Report is an informal presentation given during Unit 7. Each group member must
participate in the presentation in order to receive credit for this assignment. The Progress Report
assignment is graded as either satisfactory (100) or unsatisfactory (0).
21
04/19/2012
GRADED ASSIGNMENTS
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Project Logistics
Each group will present an informal progress report of their embedded design to the class. The progress
report must include:
An updated block diagram/sketch displaying a more detailed layout of the embedded system
(make copies for the entire class)
Deliverables
The Progress Report is an informal presentation given during Unit 8. Each group member must
participate in the presentation in order to receive credit for this assignment. The Progress Report
assignment is graded as either satisfactory (100) or unsatisfactory (0).
22
04/19/2012
GRADED ASSIGNMENTS
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Project Logistics
Each group will present an informal progress report of their embedded design to the class. The progress
report must include the following:
An updated block diagram/sketch displaying a more detailed layout of the embedded system
(make copies for the entire class)
Deliverables
The Progress Report is an informal presentation you will give during Unit 9. Each group member must
participate in the presentation to receive credit for this assignment. The Progress Report assignment will
be graded as either satisfactory (100) or unsatisfactory (0).
23
04/19/2012
GRADED ASSIGNMENTS
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Project Logistics
Each group will submit one final report that contains the following:
Design plan
Component list
Software program
Test plan
Results
Conclusion
Deliverables
You should write your Final Report in the form of a Word document that includes a title page, table of
contents, and list of references. The Final Report assignment is worth 40% of the total project grade. The
assignment is due at the end of Unit 10.
24
04/19/2012
GRADED ASSIGNMENTS
Demonstrate how to break down embedded projects into separable software tasks and hardware
functions.
Plan, design, implement, and debug at least one functional microcontroller student project.
Required Resources
Lab computers with Keil software for 8051 and either Brays or Tera Terminal
8051 Trainer
USB adapter
Project Logistics
Your group will give a formal PowerPoint presentation of your embedded system design that includes the
following:
Design purpose
Test plan
Results
Deliverables
Groups will give their presentations and demonstrations at the conclusion of the final exam during Unit 11.
Each group member must participate to receive credit for this assignment.
25
04/19/2012
GRADED ASSIGNMENTS
Laboratory Assignments
Unit 1 Lab 1: Assembling Programs and Using the Simulator (Part 1)
Course Objectives and Learning Outcomes
Examine a listing file to determine the mapping of memory locations to code and data segments.
Introduction
To program any microcontroller, you need to master software tools such as an assembler and a compiler.
The assembler/compiler provides a way to test a given program for syntax errors. The syntax errors are
listed in a file with an .lst extension. Once you examine the .lst file and locate the error, you can go back
to the source file, correct the errors, and reassemble it. This process continues until all the errors in the
.lst file are eliminated. Note that even if you receive zero errors in the .lst file, it does not mean that your
program will work properly.
To see if the program does what you intended, you will need to use a simulator. When simulating the
program, you execute it line by line and examine the registers, ports, and memory contents to verify the
operation of code. This lab introduces the process of assembling and simulating a program.
You will learn to use an 8051 assembler/simulator and complete activities to write, make, debug, and
execute programs. This assembler tool will be used throughout the course and in all lab activities.
Therefore, it is essential for you to master all aspects of this assembler tool for future lab activities.
Note: If you are not able to complete this lab by the end of class, you will need to complete the lab on
your own time prior to the start of Unit 2.
26
04/19/2012
GRADED ASSIGNMENTS
Recommended Procedures
Activity 1: Use the Keil uVision 4.22 Integrated Development Environment.
C:\Keil\UV4\Uv4.exe.
1. Follow the directions in the Basic Tutorial for Keil Software handout (ET2640.U1.HO1) to
accomplish the following activities that you will use to complete all of the labs for this course.
a. Locate and open the Keil uVision IDE.
b. Create and save a new source code file.
c.
Configure the IDE to use with the 8051 trainer using the DS89C420 processor.
d. Write code in a source file that will toggle data between Port 1 & Port 2 (as explained in
Keil tutorial).
e. Import the source file into the project.
f.
Compile the source file turning it into a HEX file that can be executed by the simulator
and/or downloaded to the microprocessor and executed.
0000H
MOV
AL,#99H
MOV
R0,A
MOV
R1,A
MOV
R2,A
MOV
R3,A
MOV
R4,A
MOV
R5,A
MOV
R6,A
MOV
R7,A
MOV
P0,A
MOV
P1,A
27
04/19/2012
MOV
GRADED ASSIGNMENTS
P2,A
END
3. Import the source file, compile it to HEX and execute the debugger.
4. Locate the Build Output window to see if it indicates an error. See Figure 1-1.
Figure 1-1
5. Correct the error in line 2. (AL is not a valid register. It should be A)
a.
b.
0000H
MOV
A,#0
MOV
MOV
R1,#1
MOV
P0,R0
MOV
P1,R1
MOV
R2,#1 ; 15th
MOV
R3,#5
MOV
P1,R2
MOV
P2,R3
28
04/19/2012
GRADED ASSIGNMENTS
MOV
R4,#1 ; 1929
MOV
R5,#9
MOV
R6,#2
MOV
MOV
P0,R4
MOV
P1,R5
MOV
P2,R6
MOV
P3,R7
ADD
A,R0
ADD
A,R1
ADD
A,R2
ADD
A,R3
ADD
A,R4
ADD
A,R5
ADD
A,R6
ADD
A,R7
MOV
END
2. Start the simulator and open the Ports 0, 1, 2 & 3 windows.
3. Use the simulator to single-step through the program and examine the registers. You should see the
dates entered into the registers and then the sum of the numbers in the accumulator.
Note: If the sum is more than FFH, as limited by 8 bits, the number will roll over from 00H.
4. Terminate the program.
5. Start Debug again.
6. Select View and open the Register window.
7. View registers as you single-step through the program and examine how the registers update with
relationship to the program.
29
04/19/2012
GRADED ASSIGNMENTS
Figure 1-2
30
04/19/2012
GRADED ASSIGNMENTS
Figure 1-3
4. Notice the headings LOC, OBJ, LINE, and SOURCE. Note that LOC and OBJ values are presented in
hexadecimal (base 16).
5. Carefully examine the OBJ column and relate it to the SOURCE column.
6. 7400H is the op-code to move a value to the accumulator. Use Appendix A in the textbook to identify
some of the other op-codes.
Activity 5: Create, Make, and Execute a Program
1. Create, make, and execute one or both of the following programs:
a. Make the ACC register read 0FH, R0 read FFH, R1 read ABH, and P1 read DAH.
b. Write a program that stores 10H in R0 and another value in R1. The program should add
those two numbers together and store the result in Port 3. Port 3 must read AAH. Calculate a
value for R1 that, when added to the contents of R0, will result in P3 reading AAH.
31
04/19/2012
GRADED ASSIGNMENTS
Deliverables
32
04/19/2012
GRADED ASSIGNMENTS
Examine a listing file to determine the mapping of memory locations to code and data segments.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
33
04/19/2012
GRADED ASSIGNMENTS
Unit 2 Lab 1: 8051 Registers, Stack, Ports, Trainer, and Program Download (Part
1)
Course Objectives and Learning Outcomes
Examine a listing file to determine the mapping of memory locations to code and data segments.
Describe the function of the program counter, data registers, and accumulators.
Program the 8051 using the stack pointer to access a stack data storage area.
Describe how individual instructions can affect the CY, AC, and P flags on the PSW register.
Introduction
While assemblers/compilers allow you to assemble a given program, you must run the program on an
actual microcontroller to test its operation. The assembler creates an executable file with the .hex
extension. This hex file must be downloaded into a microcontroller. In older systems, the microcontroller
had to first be taken out of the system board and placed into an eraser to erase its contents. After the
contents were erased, a PROM burner was used to burn the hex file into the chip.
With the advent of Flash memory in recent years, microcontrollers are equipped with a loader that
enables a program to be downloaded into the chip without removing it from the system board. The
DS89C450 chip used in the 8051 Trainer has a built-in serial boot-loader that allows the hex file to be
downloaded into the chip with the use of an x86 PC serial port. Windows HyperTerminal software is used
to access the chips contents via the serial port of the x86 PC. The HyperTerminal enables you to both
delete and program the chips contents.
34
04/19/2012
GRADED ASSIGNMENTS
This lab shows the process of accessing the Flash contents of the 8051 Trainer chip, deleting the old
contents, and downloading a new hex file into it. You will use a simple program such as toggling bits to
verify the operation of the 8051 Trainer. This will ensure the port is operating properly before you connect
the port to devices such as an LCD or a stepper motor.
Note: Before connecting a microcontroller to any device, first run a toggle program to make sure the CPU
and ports are working properly.
Recommended Procedures
Activity 1: Examine the Program Counter, Accumulator and the Program Status Word (PSW)
1. Open the Keil IDE and create a new project. Write and assemble the following program. The program
will add three pairs of numbers. At each operation, the PC will increment and the PSW will update
based on the contents of accumulator A and the changes to it:
o
PSW.AC bit is high when there is a carry from ACC bit 3 to ABB bit 4.
ORG
MOV
ADD
MOV
ADD
MOV
ADD
NOP
0000H
A, #38H
A, #2FH
;A=67H, CY=0, AC=1,
A, #9CH
A, #64H
;A=00H, CY=1, AC=1,
A, #88H
A, #93H
;A=1bH, CY=1, AC=0,
;No operation- helps with
P=1
P=0
P=0
debugging
2. Use the simulator to single-step and examine the registers, paying close attention to the PSW
registers.
a. Start a Debug session and make sure the Registers window is open.
i. View > Registers Window.
35
04/19/2012
GRADED ASSIGNMENTS
Note: Register A is the accumulator and is identified as the Sys.a register in the Keil IDE. From this point
forward the accumulator labelled as Sys.a will be identified by ACC. Also, the program counter is
identified as PC $ and will be identified as the PC.
3. The PSW should update as the value in the ACC register) change. Use Figure 2-4 on page 52 of the
text for reference.
4. Also the PC should increment as every program step is executed.
5. Step past the first MOV instruction: MOV A, #38H so the step pointer is pointing to the next
instruction: ADD A, #2FH
6. The previous instruction moved 38h into the ACC. In binary, 38h is: 0011 1000.
7. The PSW register should display 0x01 because the parity bit or PSW.1 is set high due to the odd
number of 1s in the ACC.
8. Step through the program while carefully examining and comparing the ACC and the PSW registers.
Also, notice the PC increments at each step.
9. Repeat this activity until you understand the relationship between the contents of the ACC and PSW.
Activity 2: Examine memory, the program counter (PC), the stack pointer (SP) and stack operations
1. Create a working project by following these steps.
36
04/19/2012
GRADED ASSIGNMENTS
a.
b.
c.
d.
e.
f.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
g.
h.
Start Debug
ORG 0000H
MOV R0, #18H
MOV R1, #12H
MOV R2, #99H
MOV R3, #10H
MOV R4, #0FFH
MOV R5, #0FFH
MOV R6, #0FFH
MOV R7, #0FFH
PUSH 3
PUSH 1
PUSH 0
PUSH 2
POP 4 ; top of
POP 5 ; top of
POP 6 ; top of
POP 7 ; top of
NOP
;SP=07
; R3 to
; R1 to
; R0 to
; R2 to
stack to R4stack to R5stack to R6stack to R7-
Stack- SP=08
Stack- SP=09
Stack- SP=0A
Stack- SP=0B
SP=0A
SP=09
SP=08
SP=07
3. Build and start the debugger and open the Register window.
37
04/19/2012
GRADED ASSIGNMENTS
38
04/19/2012
GRADED ASSIGNMENTS
7. Enter C: 00h in the Address box to display the CPU program memory space. Expand the Memory 1
window to display a few lines of program memory.
R0, #0x17.
9. Notice the Disassembly Window displays the op-code 7818 located a memory location 0000h. Op-code
78 means move data to register R0, 18 is the value of the data.
10. Notice that the first line in Memory 1 Window begins with the following:
11. The program source code starts with instruction ORG 0000H which starts the program at memory location
0000H, hence the first instruction, 7818, is stored in that memory location. The instructions are stored
serially starting at address 0000H,
o
78 18 is the first instruction and moves 18h into R0, MOV R0, #18h
79 12 is the next instruction and moves 12h into R1, MOV R1, #12h
39
04/19/2012
GRADED ASSIGNMENTS
12. The next instruction that follows the instruction that moves data into register 7 is pushing the contents of
register 3 onto the stack.
o
13. The next instruction that follows the instruction that pushes register 2 onto the stack is popping the top of
the stack into register 4.
o
14. Start Debug and notice that the PC is initially C: 0x0000h which is the address of the first instruction to be
executed. Locate memory location 0000h, it is op-code 78, location 0001h is the data 18.
15. Press F11 to single step and execute the first instruction. Notice that 18h is moved into R0 and the PC
updates to 0x0002h which is the memory location of the next instruction.
16. Single step through the program, notice that the PC will always point to the memory location that holds
the next op-code.
17. Select Memory Window 2 and enter D: 00h in the Address box to display the CPU data memory space.
18. Notice that the first line in Memory 2 Window begins with the following:
40
04/19/2012
GRADED ASSIGNMENTS
19. Sixteen memory locations are displayed. The first location is data memory address 0000h and is register
0. Registers 1 through 7 follow at memory addresses 0001h through 0007h. The 9th location is data
memory address 0008h and is the top of the stack. Data memory 0008-001Fh is the stack.
20. Step through the program while carefully examining the values of R0 through R7, SP, 00, and 08.
21. This program initially loads four values into registers. The values are placed on the stack in a random
order.
22. Then the stack is emptied and the values are placed in a different set of registers.
23. SP increments when data is pushed onto the stack and decrements when data is pulled from the stack.
24. Notice the order of the values is different.
25. Repeat the program until you understand how the memory is used.
Activity 3: Examine the trainer and its features
1. Examine the Trainer Figure carefully. You should observe the following features:
POWER LED5on the left and PRG LED6 program mode on the right
RESET button
5VDC jack
41
04/19/2012
GRADED ASSIGNMENTS
Two 20-pin male/female combination of connectors labeled from left to right: J1, J3, J4,
J2
Two 8-pin male/female combinations of connectors, one set connected to the DIP
switches and the other to the LEDs
Two 2-pin jumpers labeled S1-RXD and S2-TXD (These are for serial communication and
will be covered later.)
Maxim DS89C450 microprocessor, 74HC244 tri-state buffer (to drive the LEDs), and
MAX232 serial interface along with an 11-MHz crystal and various components
2. Examine the left-side 20-pin male/female combination of connectors J1 and J3. They are labeled P1.0
through P1.7 and P3.0 through P3.7. These are connected to the microprocessor ports 1 and 3,
respectively. Notice that Port 3 has additional labels such as RXD and TXD. These are for serial
communication signals and will be covered later.
3. Examine the right-side 20-pin male/female combination of connectors J4 and J2. They are labeled P0.0
through P0.7 and P2.0 through P2.7. These are connected to the microprocessor ports 0 and 2,
respectively. Notice that these ports also have additional labels such as AD0 and A15. Port 0 acts as
both an 8-bit address bus and an 8-bit data bus. Port 2 can also function as the MSB of a 2-byte
external address bus. These will be covered in more detail later.
4. Examine the 8-pin male/female combination labeled J6 LED CONNECTORS. Pin 1 is connected to LED
1, pin 2 to LED 2, and so on. Applying VCC on any pin of J6 will illuminate the related LED. These
pins will be connected to the ports to view the output of the microprocessor and to verify the program
operation.
5. Examine the 8-pin male/female combination labeled J5 DIP SWITCH CON. Pin 1 is connected to switch
1, pin 2 to switch 2, and so on. Each pin has either GND or VCC available, depending on the switch
setting. These pins will be connected to the ports to provide input to the microprocessor to verify your
program operation.
6. The PRG / RUN switch transfers the Trainer from program to run mode. Leave the Trainer in RUN mode
by default.
7. The reset button resets the Trainer.
8. The 9-pin din connector labeled SERIAL 0 will be connected to the PC. The other serial port will be
discussed later.
9. At this point, you should be ready to establish a connection between the PC and the Trainer.
Activity 4: You will learn to interface the Trainer to the x86 PC and establish a terminal connection.
1. Attach or verify that the interface (RS232 cable or USB-to-RS232 cable) is connected at J10- SERIAL
0 and the correct port on the PC. Your instructor will verify the connection.
2. NOTE: Some PC systems will not have a COM port and will use a USB-to- RS232 cable.
42
04/19/2012
GRADED ASSIGNMENTS
J8
P0.0
to
-1
P0.1
to
-2
through
P0.7
to
-8
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
Start Debug and verify that the program. Locate the port memory locations.
ORG
0000H
MAIN: MOV P0, #55H
MOV
P1, #66H
MOV
P2, #33H
ACALL DELAY
MOV
P0, #0AAH
MOV
P1, #099H
MOV
P2, #0CCH
ACALL DELAY
SJMP
MAIN
;-----------------------------DELAY:
43
04/19/2012
MOV
LOOP3:
LOOP2:
LOOP1:
NOP
NOP
DJNZ
DJNZ
DJNZ
RET
END
R5, #250
MOV R4, #29
MOV R3, #100
NOP
GRADED ASSIGNMENTS
R3, LOOP1
R4, LOOP2
R5, LOOP3
MAIN
5. Establish a connection to the Trainer and download the hex file to the Trainer.
6. Move the slide switch to the RUN position and press the RESET button to run the test program.
7. The program is alternately updating Port 0s even and odd bits. LED bits 0, 2, 4, 6 should be
alternately flashing with bits 1, 3, 5, 7.
8. Examine the program closely. You can see that Ports 0, 1, and 2 are alternately loaded with 55H and
AAH. Reviewing the binary values of each should help you understand what is happening on the
Trainer.
9. The Trainer LEDs are demonstrating what is happening at Port 0. Port 1 and 2 have different
patterns. You can rewire the LED bank to those ports. Power the Trainer off, move the wires, re-apply
power, and press reset.
10. Examine the program closely. You can see three nested loops at LOOP3, LOOP2, and LOOP1.
LOOP1 will repeat the six instructions from LOOP1: To the RET until R3 is decremented
down to 0, which are 100 repetitions.
LOOP2 will repeat LOOP1 until R4 is decremented down to 0, which is 29 times, for a total of
29 times 100, or 2900 total repetitions.
LOOP3 will repeat LOOP2 until R5 is decremented down to 0, which is 250 times, for a total
of 250 times 29 times 100, or 735000 total repetitions.
You can figure out the time it takes for the ports to update by understanding the length of a
clock cycle and the number of cycles within the loop. Multiply that value by the total
repetitions to obtain the frequency of the port update.
11. Change the value of R3 on the line of code at LOOP3. Save and remake the changes. Download to
the Trainer. You may need to power cycle the Trainer first. Run the new program. What changed?
12. Can you determine what value of R3 would yield a frequency of 1 Hz? Try it and see if you can obtain
1 Hz flashing at the LEDs.
Deliverables
44
04/19/2012
GRADED ASSIGNMENTS
Program the 8051 using the stack pointer to access a stack data storage area.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
45
04/19/2012
GRADED ASSIGNMENTS
Unit 3 Lab 1: Performing I/O Operations on 8051 Ports and Transferring Data in
Memory (Part 1)
Course Objectives and Learning Outcomes
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Plan, design, implement, and debug at least one functional microcontroller student project.
Program the 8051 to use indirect addressing to access look-up table values.
Oscilloscope
Logic probe
Introduction
The simplest test of operation for a given microcontroller is performed by accessing its I/O (input/output)
ports. In Lab 2, you tested the operation of the Output port by toggling the bits. In this lab, you will test the
operation of the Input port by connecting the port to DIP switches and writing a program to bring the
status of the switched into the CPU. This will ensure the Input port is operating properly before you
connect the port to devices such as a keypad or ADC. More importantly, you will test the software
algorithm for BCD-to-ASCII and binary-to-ASCII programs, which are widely used in data acquisition
engineering, using a DIP switch as an input.
Recommended Procedures
Activity 1: Simulate I/O port operations and trace subroutine CALL program operations
1. Create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
46
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
ORG 0000H
MAIN:
MOV
P0, #55H
MOV P1, #55H
MOV P2, #55H
ACALL DELAY
MOV P0, #0AAH
MOV P1, #0AAH
MOV P2, #0AAH
ACALL DELAY
SJMP MAIN
DELAY:
MOV
R3, #3
LOOP1:
NOP
DJNZ R3, LOOP1
RET
END MAIN
;
;
;
;
;
;
;
;
;
toggles 55 to Port
toggles 55 to Port
toggles 55 to Port
jumps to DELAY
toggles AA to Port
toggles AA to Port
toggles AA to Port
jumps to DELAY
jumps to MAIN
0
1
2
0
1
2
3. Notice this program has three program blocks: MAIN, DELAY, and LOOP1.
4. The MAIN block toggles the ports from 55H to AAH (01010101b to 10101010b).
5. After each toggle, the DELAY block is called. The DELAY block consists of a single loop that repeats
three times. Three was chosen because it is small enough to demonstrate loops, but not so large as
to waste your time. This value could be up to 255. In a previous lab, three loops were nested to create
a longer time delay that allowed the LED update to be visible.
6. After the repetition, the program returns to the step immediately after the ACALL statement that called
the loop.
7. The program does not have a termination point and will loop until you manually terminate it.
8. Start Debug and make sure the following windows are open:
o
Ports 0, 1, 2, & 3
Registers
9. The program counter should be at Step 3. Press F11 to step through the instruction.
10. Examine the Registers window. The ports should reflect the following values:
47
04/19/2012
GRADED ASSIGNMENTS
Remember to return the window to D: 0000h after you check a specific memory location.
17. Change the value stored to R3 from 3 to 10 or some other value. Reassemble and run the program.
What changed?
Activity 2: Perform I/O Port Operations
1. Create a working project by following these steps.
a.
b.
c.
d.
e.
f.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
g.
h.
Start Debug
ORG
MOV
MOV
HERE:
MOV
0000H
A,#0FFH
P1,A
;A=FFH
;make P1 an input port
A,P1
48
04/19/2012
MOV P2,A
SJMP HERE
END
GRADED ASSIGNMENTS
;send it to P2
;keep doing this
3. Connect each pin of P1 of the 8051 (P1.0 through P1.7 on J3) to the DIP switches of the 8051
Trainer, P1.0 to J7-1, P1.1 to J7-2, etc.
4. Connect each pin of P2 to an LED on the LED bar of the 8051 Trainer.
5. The program will read the data applied to P1 from the DIP switches and transfer that data to P2. The
data at P2 will illuminate the applicable LED.
6. Build and save the executable hex code.
7. Establish a connection, download the program, and run the program.
8. Any change of status of the switches connected to P1 will be instantly reflected (within a fraction of a
microsecond)on LEDs connected to P2. Change the DIP switches to a variety of settings and verify
the LED reflects the change correctly.
9. Use the Oscilloscope and or logic probe to verify the voltage levels applied to the input port and
delivered by the output port.
Activity 3: Perform I/O Port Operations on Specific Port Bits
1. Without changing any existing wire connections, create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
ORG
MOV
SETB
HERE:
MOV
MOV
SJMP
END
0000H
P2, P1
P1.7
C, P1.7
P2.7,C
HERE
49
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
ORG 0000H
;burn program into ROM at addr 0000h
MOV DPTR, #200H
;load 200h into DPTR
CLR A
;clear ACC
MOVC A, @A+DPTR ;move contents of ACC + DPTR to ACC
MOV R0, A
;move ACC to R0, first letter
INC DPTR
;increment to addr of next letter
CLR A
;clear ACC
MOVC A, @A+DPTR ;repeat twice more
MOV R1, A
INC DPTR
CLR A
MOVC A, @A+DPTR ;@ is indirect addressing
MOV R2, A
HERE: SJMP HERE
;you have transferred the first 3
;letters of your last name
ORG 200H
MYDATA: DB "LASTNAME"
END
3. The program stores your last name in ASCII in the code space starting at address 200H.
4. Notice that direct and indirect addressing modes are used.
5. It then transfers the first three letters of your last name to R0, R1, and R2.
6. Open the Memory 2 Window at address D: 0000h.
7. Look up your last name in the ASCII table.
8. Modify the program to move the last three letters of your name to R0, R1, and R2.
9. Modify the program to move the first three letters of your name to addresses 40H, 41H, and 42H.
50
04/19/2012
GRADED ASSIGNMENTS
10. Modify the program to reverse the order of the contents of 40H, 41H, and 42H and store them starting
at 60H.
11. Watch the Register and Memory 2 windows as you step through the program, notice that the registers
and memory locations update.
12. Make sure to identify each memory location and verify that the first three letters of your name are
stored in the correct locations.
Activity 5: Design, Simulate, and Test the Lookup Table, Memory, and Addressing Operations
1. Write a program to accept an input y from Port 1 (your DIP switches), calculate (i.e., look up) x2 for
any number between 0 and 9, and output the result to Port 2.
2. The lookup table for x2 is located at the address (code space) of 200H.
3. Register R0 should hold the value of x, and, at the end of the program, R2 should hold the value of y.
4. Use the simulator to change the x value and single-step through the program, examining the registers
as you go.
5. Once the program works correctly, download and run on the Trainer. No wiring changes should be
necessary. The LEDs should show the squared value of the DIP switch settings (0-9).
6. The lookup table code should look like this:
ORG 200H
XSQR_TABLE:
DB 0, 1, 2, 9, 16, 25, 36, 49, 64, 81
END
Deliverables
51
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Plan, design, implement, and debug at least one functional microcontroller student project.
Program the 8051 to use indirect addressing to access look-up table values.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
52
04/19/2012
GRADED ASSIGNMENTS
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Plan, design, implement, and debug at least one functional microcontroller student project.
Write an assembly program to perform arithmetic operations in assembly (ADD, ADDC, DA,
SUBB, MUL, DIV).
Write an assembly program to perform bit-wise logical operations (AND, OR, XOR, CPL).
Oscilloscope
Logic probe
Introduction
Microcontrollers are commonly used as counters. Some examples of products that use microcontrollers
as counters are copiers, microwave ovens, and answering machines. Many of these counters use an
input device, such as a keypad, to set the final counter value. In the absence of a keypad, DIP switches
can be used to input the value that sets the maximum count. This lab explores counters by using realworld examples.
Recommended Procedures
Activity 1: Create a Manually Programmable Up-Counter Using the 8051
53
04/19/2012
GRADED ASSIGNMENTS
In many systems, an up-counter is used to keep track of an action. For example, a copier must count the
number of copies it makes. In this activity, you will set up a manually programmable up-counter using the
8051 microcontroller. This activity reviews I/O operations and loops and uses an ADD statement instead
of an increment statement (INC).
1. Create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
ORG 0000H
MAIN: MOV P2, #0H
;make the P2 an output port
MOV P1, #0FFH
;make P1 an input port
BACK: MOV A, P1
;get the maximum count from P1
MOV B, A
CLR A
AGAIN: MOV P2, A
ACALL DELAY
CJNE A, B, OVER
;if a <> B goto over
SJMP BACK
OVER: ADD A, #1
;same as INC A
SJMP AGAIN
;-----------------------------DELAY:
MOV R5, #30
HERE3: MOV R4, #100
HERE2: MOV R3, #200
HERE1: NOP
NOP
NOP
DJNZ R3, HERE1
DJNZ R4, HERE2
DJNZ R5, HERE3
RET
END MAIN
3. Connect Port 1 to the DIP switches. Connect Port 2 to the LED bank. Note: Do not remove the wiring,
you will use it again later in activity 4.
4. Set the dip switches to 0FH, binary 00001111. This setting will cause the counter to count from 0 to
255. The LEDs should cycle through and display binary 0 through 255.
54
04/19/2012
GRADED ASSIGNMENTS
100 by fives?
Activity 2: Manipulate Multi-Byte Numbers and Learn to Use the Carry Flag
Until now, you have worked exclusively with single-byte data. Most actual operations require numbers
larger than 0 to 255.
1. Create a working project by following these steps.
a.
b.
c.
d.
e.
f.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
g.
h.
Start Debug
2. The program that demonstrates how to set the carry flag. The correct code follows:
ORG
MOV
ADD
NOP
END
0000H
A, #0FFH
A, #1H
;ACC equals FF
;add 1 to it
3. Start Debug and make sure the following windows are open:
Ports 0, 1, 2, 3, & 4
Registers
55
04/19/2012
GRADED ASSIGNMENTS
4. Use the Simulator to step through while carefully examining the Register window, specifically the
Accumulator (Sys.a) and the Program Status Word Carry bit (Sys.psw.cy). Notice that address E0h is
the memory address where the accumulator is stored and D0h is the memory address where the
PSW is stored.
5. Repeat this until you understand when 0FFH and 01H are added.
6. Once you understand what happens when a carry is created at the accumulator, modify the program
to take two numbers, add them, and store the result. The correct code follows:
ORG
MOV
MOV
MOV
ADD
MOV
CLR
ADDC
MOV
NOP
END
0000H
R0, #250
R1, #10
A, R0
A, R1
R3, A
A
A, #0
R2, A
ORG
MOV
MOV
MOV
MOV
MOV
ADD
MOV
CLR
MOV
ADDC
MOV
NOP
END
0000H
R0, #01FH
R1, #0FAH
R2, #08H
R3, #63H
A, R1
A, R3
R7, A
A
A, R0
A, R2
R6, A
;number 1 MSB
;number 1 LSB
;number 2 MSB
;number 2 LSB
;load ACC with number 1 LSB
;add both LSBs
;store LSB result in R7
;load ACC with number 1 MSB
;add both MSBs and carry
;store MSB result in R6
56
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
0110 0000
+70
166
ORG
MOV
MOV
ADD
ADD
NOP
END
0000H
R0, #+96
R1, #+70
A, R0
A, R1
4. Start Debug and make sure the following windows are open:
Ports 0, 1, 2, 3, & 4
Registers
57
04/19/2012
GRADED ASSIGNMENTS
5. Step through while carefully examining the Register window, the Data Memory view, and the psw.ov.
A review of PSW indicates that the OV bit is bit 3 of address D0. You should see that the ov bit is high
when an overflow condition occurs.
6. The following program that demonstrates how to set the overflow flag using negative numbers:
-2,
1111 1110
-128,
1000 0000
(equals)
-130,
0111 1100
ORG
MOV
MOV
ADD
ADD
NOP
END
0000H
R0, #-2
R1, #-128
A, R0
A, R1
8. Step through while carefully examining the Register window, the Data Memory view, and the
PSW.OV. A review of PSW indicates that the OV bit is bit 3 of address D0. You should see that the ov
bit is high when an overflow condition occurs. You should observe the OV flag set.
Activity 4: Examine Shift Operations
Shift operations are typically used to transfer data digitally. In this activity, you will enter a bit pattern and
watch it rotate at the output using the 8051 Trainer.
1. Create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
ORG
MAIN: MOV
0000H
P2, #0H
58
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
59
04/19/2012
GRADED ASSIGNMENTS
h. Start Debug
2. This activity will use the same wire configuration from Activity 1.
3. Set the DIP switches to 55h or 0101 0101b.
60
04/19/2012
GRADED ASSIGNMENTS
ORG 0000H
MOV R0, #0A5H
MOV P2, #0H
MOV P1, #0FFH
BACK: MOV A, P1
ANL A, R0
SJMP BACK
END
;
;
;
;
;
P1 R 0 = 01010101b
10100101b
00000101b
8. Observe that the LED output should be 05h or 0000 0101b.
9. Experiment by changing the dip switch settings and discover the new output value.
10. Change the ANL instruction to OR.
11. This program does a logic OR comparison of P1 and R0.
P1+ R 0 = 01010101b
10100101b
11110101b
12. Observe that the LED output should be F5h or 1111 0101b.
13. Experiment by changing the dip switch settings and discover the new output value.
14. Change the OR instruction to XOR.
15. This program does a logic OR comparison of P1 and R0.
P1 R 0 =
01010101b
10100101b
11110000b
16. Observe that the LED output should be F0h or 1111 0101b.
17. Experiment by changing the dip switch settings and discover the new output value.
Activity 6: Design, Simulate, and Test Logic Operations
Use Section 6.3 of the textbook, The 8051 Microcontroller and Embedded Systems. Design and simulate
one or more programs that will perform logic functions comparing the values stored in P1 and R0. Output
the result to P0.
61
04/19/2012
GRADED ASSIGNMENTS
1. Test the operation using the 8051 Trainer. The Trainer should already be wired correctly from the
previous activities.
Deliverables
62
04/19/2012
GRADED ASSIGNMENTS
Use numerical methods for processor-based systems to perform calculations and conversions
related to programming.
Plan, design, implement, and debug at least one functional microcontroller student project.
Write an assembly program to perform arithmetic operations in assembly (ADD, ADDC, DA,
SUBB, MUL, DIV).
Write an assembly program to perform bit-wise logical operations (AND, OR, XOR, CPL).
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
63
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Program the 8051 to generate a time delay using the C programming language.
Program the 8051 to perform I/O operations using the C programming language.
Oscilloscope
Logic probe
Power supply
7447 Decoder
Speaker
Introduction
Microcontrollers are designed to accept inputs and control external components. In this lab you will use
ports to control LED and sound devices. Also, you will use switches to obtain input from an external
device causing a change in program operation.
Recommended Procedures
Activity 1: Create a C Program that Sends an Output to 8051 Ports
This activity repeats some of the activities in earlier labs except now C is the language used to program.
In this activity you will use the C compiler to test the operation of the ports of the MDE8051 trainer and get
ready for C programming of the 8051 for peripherals. After verifying that the program toggles an output to
64
04/19/2012
GRADED ASSIGNMENTS
ports 1 & 2, you will interface the trainer board LEDS to Port 2. Through experimentation, you will create a
toggle pattern that lasts exactly 1 second.
1. Create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
void MSDelay(unsigned int);
void main()
{
P1=0x0;
P2=0x0;
for(;;)
{
P1= 0x55;
P2= 0x55;
MSDelay(0); // During simulation use a delay of 0
P1= 0xAA;
P2= 0xAA;
MSDelay(0); // During simulation use a delay of 0
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
3. Notice that the MSDelay value is set to 0. During the simulation, the MSDelay function will be
skipped! This value will be increased when the ports are connected to the LEDs.
65
04/19/2012
GRADED ASSIGNMENTS
4. Notice the header file reg51.h included in the C program code. This header file defines the address
location for the various registers and ports used by 8051 microprocessors. If you locate the file
(Project Window: Target 1 Source Group 1 C5.c reg51.h) and examine it, you will see
something similar to the following:
/*------------------------------REG51.H
Header file for generic 8051
microcontroller. */
#ifndef __REG51_H__
#define __REG51_H__
/*
BYTE Register
sfr P0
= 0x80;
sfr P1
= 0x90;
sfr P2
= 0xA0;
sfr P3
= 0xB0;
sfr PSW
= 0xD0;
sfr ACC
= 0xE0;
sfr B
= 0xF0;
sfr SP
= 0x81;
sfr DPL
= 0x82;
sfr DPH
= 0x83;
*/
/*
BIT Register
/*
PSW
*/
*/
sbit CY
= 0xD7;
sbit AC
= 0xD6;
sbit F0
= 0xD5;
sbit RS1
= 0xD4;
sbit RS0
= 0xD3;
sbit OV
= 0xD2;
sbit P
= 0xD0;
#endif
= 0x8A;
sfr TL1
= 0x8B;
sfr TH0
= 0x8C;
sfr TH1
= 0x8D;
sfr IE
= 0xA8;
sfr IP
= 0xB8;
Ports 1 & 2
Registers
66
04/19/2012
GRADED ASSIGNMENTS
7. As you step through the program, observe that Port 1 & Port 2 each toggle between:
0x55h 0101 0101
0xAAh 1010 1010
8. Also observe how the registers and ports update in memory.
9. Connect the trainer LEDs to Port 2.
10. Change the MSDelay argument from 0 to 250.
11. Build and save the executable hex code.
12. Establish a connection, download the program, and run the program.
13. Observe the LEDs toggling between 0x55h and 0xAAh.
14. Experiment with the MSDelay argument to see the affect of chaning the delay value. Increasing the
value should slow the time between the toggle; likewise, decreasing the value will speed up the
frequency of toggles.
15. Use the Oscilloscope to determine the actual delay between toggles. Determine the exact value of
MSDelay that will result in a 1-second delay between toggles. Write this value down; you will use it
again.
b.
c.
d.
e.
f.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
g.
h.
Start Debug
67
04/19/2012
GRADED ASSIGNMENTS
2. The correct code follows: (you may want to copy and paste the previous code and modify it)
#include <reg51.h>
void MSDelay(unsigned int);
void main()
{
unsigned char mybyte;
P1=0x0FF;
//P1 as input
P2=0x0;
//P2 as output
for(;;)
{
P2= 0x55;
MSDelay(0);
mybyte = P1;
P2= mybyte;
MSDelay(0);
}
3. Start Debug and make sure the following windows are open:
o
Ports 0, 1, 2, 3, & 4
Registers
4. Single step through the program until Port 1 configured as an input (after the port is set to 0x0FF).
5. Manually change the Port 1 window toggle switches to 0X33h.
6. Use the Simulator to step through while carefully examining Port 2; it should toggle between 0x55h &
0x33h.
7. Change the value of Port 1 to experiment with different values. Notice that the switch update doesnt
affect the values. Why? For now, restart the simulation as you check different values at Port 1.
8. After you understand how the program works, connect the trainer switches to Port 1. Set the switches
to 0x33h.
9. Change the MSDelay argument to the value that will cause a 1-second toggle time.
68
04/19/2012
GRADED ASSIGNMENTS
69
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
sbit mybit = P3^7;
void MSDelay(unsigned int);
void main(void)
{
while(1)
{
mybit=1;
MSDelay(250);
mybit=0;
MSDelay(250);
70
04/19/2012
GRADED ASSIGNMENTS
3. Remove power from the trainer and connect the speaker as follows: (note: do not remove the wiring
from activities 1 and 2; you will use them again)
71
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Program the 8051 to generate a time delay using the C programming language.
Program the 8051 to perform I/O operations using the C programming language.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
72
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Explain the purpose of each operating mode used by the TMOD register.
Program the 8051 to generate time delays using the C programming language.
Oscilloscope
Logic probe
Introduction
Timers provide one of the most important microcontroller functions. One timer application is to produce
square waves of different frequencies. In this lab, you will use timer modes 1 and 2 to generate square
waves at different frequencies.
Recommended Procedures
Activity 1: Use Timer Functions
In this activity you will use Timer 0 to create precise delays. During the last lab, the MSDelay function was
used to create time delays. The following will create and simulate a timer delay.
1. Create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
73
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
void T0Delay(void);
void main()
{
P2=0x0;
while(1)
{
P2=0x55;
T0Delay();
P2=0xAA;
T0Delay();
}
}
void T0Delay()
{
TMOD=0x01;
TL0=0x00;
TH0=0xFF;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
//P2 as output
3. If you examine the code, you should observe that Port 2 will toggle between 0x55h and 0xAAh using
T0Delay(). Examine the T0Delay() code.
5. In the Project Window, open the reg51.h file and locate the following variables.
TMOD
TCON
TL0
TH0
TR0
TF0
6. TMOD, TL0 & TH0 are part of the special function registers. TR0 & TF0 are part of the TCON register.
7. Build and save the executable hex code.
74
04/19/2012
GRADED ASSIGNMENTS
8. Start Debug and make sure the following windows are open:
Port 2
Registers
9. Initially the timer flags will all be cleared at 0x00 and the status will be stop.
10. Single step through the program until TMOD updates to 0x01; this indicates the timer is in 16 bit
mode. The Mode box should update to 1: 16 Bit Timer/Counter.
11. The next line of code clears the timer low byte TL0 to 0x00. Single step again.
12. The next line of code sets the timer high byte TH0 to 0xFF. Single step again.
13. The next line of code sets TR0, the timer timing flag to a 1. Single step again .
14. Status should change to Run, indicating that the timer is timing.
a. The timer timing flag TR0 is checked which indicates that it is a 1.
b. TCON will also update to 0x10, which indicates the TCON bit 4 is set to 1; this indicates that
timer 0 is turned on.
15. Single step repeatedly; you should notice the program pointer remains at while(TF0==0) and
that the TL0 register is increasing upon every step.
a. This means that the TL0 will count up from 0 to FF until it carries over to TH0 and causes an
overflow condition, which should stop the timer.
16. Quickly and repeatedly press the F11 key and rapidly increase TL0 until you notice the value of TL0
reach D# or E#.
17. Slowly press F11 and watch TL0 approach FF.
18. Continue pressing F11 until TL0 increases above 0xFF.
a. TH0 should toggle to 0x00 and the timer finished flag TF0 should set.
75
04/19/2012
GRADED ASSIGNMENTS
TCON should update to 0x30, which indicates that TCON bits 4 & 5 are both set to 1; this
indicates that timer 0 is on and has reached an overflow condition.
19. Single step until the program pointer leaves the while(TF0==0) line of code and is pointing at
TR0=0;.
20. The next line of code clears the timer timing TR0 flag to a 0.
a. Single step again.
b. Status should change to Stop, indicating that the timer is no longer timing.
c.
d. TCON updates to 0x20, which indicates that TCON bit 4 is now cleared (timer stopped) and
bit 5 is still set (overflow condition).
21. The next line of code clears the timer timing TF0 flag to a 0.
a. Single step again.
b. TR0 is unchecked, which indicates that it is a 0.
c.
TMOD
TCON
TL0
TH0
TR0
TF0
76
04/19/2012
c.
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
void T0Delay(void);
sbit mybit = P2^7;
void main()
{
P2=0x0;
while(1)
{
mybit=1;
T0Delay();
mybit=0;
T0Delay();
}
}
void T0Delay()
{
TMOD=0x01;
TL0=0x00;
TH0=0xFF;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
//P2 as output
3. Start Debug and make sure the following windows are open:
Port 2
Registers
Timer 0
4. Single step through the program and verify that the code works correctly and toggles port 2 pin 7.
5. Build and save the executable hex code.
6. Establish a connection, download the program, and run the program.
7. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output. Write down this information:
o
77
04/19/2012
GRADED ASSIGNMENTS
8. Modify the values of TL0 and TH0 that will provide the highest output frequency.
9. Build and save the executable hex code.
10. Establish a connection, download the program, and run the program.
11. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output. Write down this information.
o
12. Modify the values of TL0 and TH0 that will provide the lowest frequency.
13. Build and save the executable hex code.
14. Establish a connection, download the program, and run the program.
15. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output. Write down this information.
o
#include <reg51.h>
void T0BigDelay(unsigned int);
sbit mybit = P2^7;
void main()
{
P2=0x0;
//P2 as
while(1)
{
mybit=1;
T0BigDelay(10); //modify the
mybit=0;
// or larger
T0BigDelay(10); //modify the
}
// or larger
output
value
delay
value
delay
to see smaller
size
to see smaller
size
}
void T0BigDelay(unsigned int itime)
{
unsigned int x;
for (x=0;x<itime; x++)
{
TMOD=0x01;
//Timer0 16-bit mode
TL0=0x00;
//change this to increase
78
04/19/2012
TH0=0xFF
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
GRADED ASSIGNMENTS
}
2. Start Debug and make sure the following windows are open:
o
Port 2
Registers
Timer 0
3. Single step through the program and verify that the code works correctly and toggles port 2 pin 7.
4. Determine the values you need to create a 1kHz pulse. Hint: 1 millisecond per cycle. Or 500 microseconds per delay. Refer to the textbook starting at page 278 for more information.
5. Modify the program.
6. Build and save the executable hex code.
7. Establish a connection, download the program, and run the program.
8. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output.
9. Experiment until your output is correct.
10. Record this information once the output is correct.
o
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
79
04/19/2012
GRADED ASSIGNMENTS
#include <reg51.h>
void T0Delay(void);
sbit mybit = P2^7;
void main()
{
P2=0x0;
while(1) {
mybit=1;
T0Delay();
mybit=0;
T0Delay(); }
}
void T0Delay()
{
TMOD=0x02;
TH0=0xAA;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
//P2 as output
3. Start Debug and make sure the following windows are open:
o
Port 2
Registers
Timer 0
4. Single step through the program and watch the Timer 0 window. Notice that TL0 resets to the value of
TH0 whenever the timer resets.
5. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output. Write down this information.
o
TH0
6. Modify the values of TL0 and TH0 that will provide the highest output frequency.
7. Build and save the executable hex code.
8. Establish a connection, download the program, and run the program.
9. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output. Write down this information.
o
TH0
10. Modify the values of TL0 and TH0 that will provide the lowest frequency.
11. Build and save the executable hex code.
80
04/19/2012
GRADED ASSIGNMENTS
12. Establish a connection, download the program, and run the program.
13. Connect an oscilloscope to Port 2 pin 7 and measure the pulse width and determine the frequency of
the output. Write down this information.
o
TH0
14. The waves created so far should have a duty cycle of 50%. Can you find an easy way to create a
25% duty cycle waveform? How about 80% duty cycle (4 high cycles followed by 1 low)? Use the
oscilloscope to verify your results and show your instructor.
Activity 5: Design, Simulate, and Test Input/Output Operations
Use the information in activities 1 through 4 to use the trainer to create a perfect 1 (one) Hz output
frequency.
1. Determine the values of TL0 and TH0 that will provide a 1 Hz frequency.
2. Test the operation using the 8051 Trainer.
3. Verify that the pulse width and frequency of the output are correct.
4. Record this information.
o
Deliverables
81
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Explain the purpose of each operating mode used by the TMOD register.
Program the 8051 to generate time delays using the C programming language.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
82
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Explain the function of the 8051 SBUF, SCON, and PCON registers for serial communications.
Calculate the value stored in the THx register for a given baud rate.
Program the 8051 to transmit data from the serial port using the C programming language.
Program the 8051 to receive data from the serial port using the C programming language.
Oscilloscope
Power supply
7447 Decoder
Introduction
The serial port is a widely used method of transferring data. Although USB has diminished the use of the
serial COM port, its use has not been eliminated. Many devices still provide output via the serial port. One
reason is due to the simplicity of the hardware and ease of programming in both assembly and C
languages. In this lab, you will program the serial port of the microcontroller.
Recommended Procedures
Activity 1: Simulate Serial Port I/O communications
In this activity you will use the Serial I/O to simulate communications to the terminal.
1. Create a working project by following these steps.
83
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
void SerTx(unsigned char);
void main(void)
{
TMOD=0x20;
// Timer 1 mode 2
TH1=0xFD;
// 9600 baud
SCON=0x50;
// 8 bit, 1 stop, REN enabled
TR1=1;
// start timer
while(1)
{
SerTx('H');
SerTx('E');
SerTx('L');
SerTx('L');
SerTx('O');
SerTx(' ');
}
}
void SerTx(unsigned char x)
{
SBUF=x;
// transfer character
while(TI==0);
// do until transfer interrupt = 1
TI=0;
// clear TI for next character
}
3. If you examine the code, you should observe the message HELLO will repeatedly send to the serial
port. If you examine the code you will see that timer 1 is set to mode 2 (auto reload), the baud rate is
9600, and SCON is set to 01010000 or 0x50. This means 8 bits, 1 stop with REN (receive enable)
enabled. Notice that the SerTx) function will send the transferred character to the buffer continuously
until it successfully transfers to the terminal. The successful transfer to the terminal is acknowledged
when the TI (transfer interrupt) flag is set.
4. In the Project Window, open the reg51.h file and locate the following variables.
o
TMOD
SCON
TH1
84
04/19/2012
TR1
SBUF
TI
GRADED ASSIGNMENTS
5. TMOD, SCON, SBUF and TH1 are part of the special function register. TR1 is part of the TCON
register. TI is part of the SCON register.
6. Build and save the executable hex code.
7. Start Debug and make sure the following windows are open:
o
Registers
Timer 1
8. Initially the serial flags will all be cleared at 0x00 and the Baudrate will be 921600.
9. Single step through the program until SCON updates to 0x50, this indicates serial channel 0 is 8 bit
and 1 stop bit. The Mode box should update to 8 bit variable baudrate. REN_0 will be set (indicated
by the box being checked).
10. The next line of code starts the timer. Single step again.
11. The program pointer should now be inside the loop and pointing at the SerTx(H) line of code.
a. This will call the SerTx() function and transfer the ASCII character H to the function.
b. Single step again.
12. The program pointer should now be inside the SerTx() function and pointing at SBUF=x line of code.
a. This will transfer the value of the variable x to the serial buffer.
b. Single step again.
85
04/19/2012
GRADED ASSIGNMENTS
13. The program pointer should be pointed at the while loop. This loop will repeat until the TI (transfer
interrup) flag is set. The TI occurs when the receiving buffer acknowledges receipt of the transferred
character. Press F11 several times, the program pointer will not move.
14. Set the TI flag by checking the TI_0 box.
15. Press F11, the program pointer should move to the TI=0 line of code.
a. This clears the TI flag, preparing for the next character.
b. Single step again. This will take you to the end of the loop.
c.
16. The program pointer should have transferred out of the function to the next instruction, which should
be SerTx(E).
17. Single step through the program reviewing the the various windows until you understand how the
program works. Remember that SCON is a memory location. Find that location in the header file and
then use Memory window 2 to watch that location update.
Activity 2: Use Serial Port I/O Communications
In this activity you will use the previous program to send an output to the serial port and to the terminal.
1. The correct code follows: (you may want to copy and paste the previous code and modify it)
#include <reg51.h>
void SerTx(unsigned char);
void main(void)
{
TMOD=0x20;
// Timer mode 2
TH1=0xFD;
// 9600 baud
SCON=0x50;
// 8 bit, 1 stop, REN enabled
TR1=1;
// start timer
while(1)
{
SerTx('H');
SerTx('E');
SerTx('L');
SerTx('L');
SerTx('O');
SerTx(32);
SerTx('W');
SerTx('O');
SerTx('R');
SerTx('L');
SerTx('D');
SerTx('!');
SerTx(' '); // SerTx(10); or SerTx(13);
}
}
86
04/19/2012
GRADED ASSIGNMENTS
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
sbit MYSW=P2^0;
//input switch
void SerTx(unsigned char);
void main(void)
{
unsigned char z;
unsigned char fname[]="ALI";
unsigned char lname[]="SMITH";
TMOD=0x20;
//use Timer 1,8-BIT auto-reload
TH1=0xFD;
//9600 baud rate
SCON=0x50;
TR1=1;
//start timer
87
04/19/2012
GRADED ASSIGNMENTS
if(MYSW==0)
//check switch
{
for(z=0;z<3;z++) SerTx(fname[z]);
SerTx(' ');
}
else
{
for(z=0;z<5;z++) SerTx(lname[z]);
SerTx(' ');
}
while (1==1);
//write name
//write name
}
void SerTx(unsigned char x)
{
SBUF=x;
// transfer character
while(TI==0); // do until transfer interrupt = 1
TI=0;
// clear TI for next character
}
3. Start Debug and make sure the following windows are open:
o
Registers
Timer 1
Serial 0
4. Single step through the program and verify that the code works correctly. Remember, you must
manually change port 2 pin 0 and TI_0.
5. When you understand the program, build and save the executable hex code.
6. Establish a connection, download the program, and run the program.
7. Toggle (change) switch 1 to change the input at port 2 pin 0. Remember to press the reset button to
send a new message.
Activity 4: Use Serial Port I/O Communications to Obtain Input from the Terminal
In this activity you will use the terminal to send serial communications to the microcontroller.
1. Create a working project by following these steps.
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
88
04/19/2012
GRADED ASSIGNMENTS
89
04/19/2012
GRADED ASSIGNMENTS
#include <reg51.h>
void main (void)
{
unsigned char mybyte;
TMOD=0x20;
//use Timer 1,8-BIT auto-reload
TH1=0xFD;
//9600 baud rate
SCON=0x50;
TR1=1;
//start timer
while(1)
//repeat forever
{
while(RI==0);
//wait to receive
mybyte=SBUF;
//save value
P2=mybyte;
//write value to port
RI=0;
}
}
3. Remove power from the trainer and connect the 7447 decoder and the 7-segment display as follows:
Port 2
Registers
Timer 0
Serial 0
6. Single step through the program and verify that it works correctly. Remember, you must manually
update the RI_0 flag.
90
04/19/2012
GRADED ASSIGNMENTS
7. Notice that the output will be sent to Port 2 and the 7-segment display. What will be displayed, the
ASCII character or the lower 4 bits of the ASCII code? (Note: the 7447 decoder will decode numbers
0 through 9.) What must be done to display the character?
8. Apply power, establish a connection, download the program, and run the program.
Activity 5: Design, Simulate, and Test Input/Output Operations
Use the information in activities 1 through 4 to use the trainer to select one of four outputs to the terminal.
1. Use two DIP switches on P1 to send one of the following messages to the terminal:
P1.1
P1.0
first name
last name
2. For an added challenge, create a program that accepts input from the keyboard and uses that
information to determine which message is sent to the terminal.
Deliverables
91
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Explain the function of the 8051 SBUF, SCON, and PCON registers for serial communications.
Calculate the value stored in the THx register for a given baud rate.
Program the 8051 to transmit data from the serial port using the C programming language.
Program the 8051 to receive data from the serial port using the C programming language.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of experimental procedure Provide a detailed summary of how you performed the
lab.
Deliverables
92
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Plan, design, implement, and debug at least one functional microcontroller student project.
Program the 8051 to perform Timer operations with interrupts using the C programming
language.
Program the 8051 to perform external hardware interrupts using the C programming language.
Program the 8051 to perform serial communications with interrupts using the C programming
language.
Oscilloscope
Power supply
100 nF capacitor
Introduction
Microcontrollers must react to sensor and user inputs while still processing their programs. When an
external device needs attention, it will send a signal to the microcontroller. An example would be when a
car triggers a traffic light. Another example would be counting events, such as the number of bottles
moving past a sensor on a conveyor. When a sensor detects something, it sends a pulse to the
microcontroller, which generates an interrupt. In the case of the stop light, the interrupt routine will change
93
04/19/2012
GRADED ASSIGNMENTS
the light. In the case of the counter, the interrupt routine will update the count. In this lab, you will simulate
both types of events.
Recommended Procedures
Activity 1: Use the 8051 to Count Events
In this activity, you will use Timer/Counter 0 to count pulses. Square wave pulses will be applied from an
external source to the microcontroller via Port 3 pin 4. The microcontroller will detect the pulses, and the
counter will be updated.
1. Create a working project by following these steps:
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
void main()
{
TMOD=0x06; //Timer/counter 0, mode 2, counter
TH0=0;
//Clear the counter
TL0=0;
while(1)
{
TR0=1;
//Start Timer/Counter 0
while(TF0==0)
//Count until rollover
{
ACC=TL0;
P2=ACC;
}
TR0=0;
//Stop Timer/Counter
TF0=0;
//Clear Timer/Counter 0 flag
}
}
94
04/19/2012
GRADED ASSIGNMENTS
3. If you examine the code, you should observe the TL0 register will be continuously transferred to Port
2 until the counter counts pass FFh or 255.
4. The program does not reference P3.4 because once the counter is configured, any high to low
transition on P3.4 will automatically update the counter TL0 register.
5. During every loop, TL0 is transferred to the accumulator, which is then transferred to P2.
6. In the Project Window, open the reg51.h file, and locate the following variables:
ACC
TMOD
TL0
TH0
TR0
TF0
7. ACC is the accumlator; TMOD, TL0, and TH0 are part of the special function registers; and TR0 and
TF0 are part of the TCON register covered in previous units.
8. Build and save the executable hex code.
9. Start Debug, and make sure the following windows are open:
Port 2
Port 3
Timer 0
Registers
TCON also will update to 0x10, which indicates the TCON bit 4 is set to 1, and that indicates
counter 0 is turned on.
16. The next line of code starts a loop that continuously transfers the contents of the counter, TL0, to the
accumulator, ACC, and then from the accumulator to Port 2, P2.
a. This loop will repeat until the counter, TL0, rolls over from 0xFF to 0x00.
95
04/19/2012
GRADED ASSIGNMENTS
17. Single step repeatedly. You should notice the program pointer remains in the loop, but nothing else
changes. Neither TL0 nor ACC nor P2 indicates any change.
18. Click P3.4 and uncheck the box. TL0 immediately will increment from 0x00 to 0x01.
19. Single step repeatedly. You should notice that both the Accumulator and Port 2 update to the value of
the counter, TL0.
20. Toggle P3.4 by checking and unchecking the box. The counter will update every time P3.4 transitions
from a high to a low, regardless of when the transition occurs.
21. Continue to single step through the program while toggling P3.4.
22. You will notice that TL0 updates and then updates ACC and P2.
23. Once you are satisfied that you understand how the counter is working, manually change the value of
TL0 to 0xFD. Then toggle P3.4 until TL0 equals 0xFF.
24. Toggle P3.4. Notice that the counter finished flag, TF0, sets and TL0 has rolled over to 0x00.
25. Notice that the TF0 flag is equal to 0; hence, the condition of the while statement is now false. The
loop will terminate, and the program pointer should pass to stop the counter by clearing the TR0 flag.
Also, the counter will be prepared again by clearing the TF0 flag.
26. Single step a few more times, and notice that the counter will be enabled again by setting the TR0
flag, and the process will start over again.
27. Repeat this process until you understand how the counter works.
Activity 2: Use the Counter Function to Count and Display Input Pulses
In this activity, you will use a counter to track the number of input pulses provided by an external source.
1. You will use the program you created in Activity 1.
2. Connect the function generator to pin P3.4 using a 330 ohm resistor in series. Make sure you are
connected to the TTL output signal. Set the function generator to provide a square wave output at 1
or 2 Hz.
3. Connect Port 2 to the LED bank.
4. Connect an oscilloscope to Port 3 pin 4, and set the oscilloscope to measure the 1 or 2 Hz pulse
train.
5. Build and save the executable hex code.
6. Establish a connection, download the program, and run the program.
7. You should see the LED bank updating about once or twice per second. The LED bank should count
from 0 to 255 repeatedly.
Activity 3: Use Timer and External Hardware Interrupts
96
04/19/2012
GRADED ASSIGNMENTS
Internal timer interrupts are used to cause periodic actions, such as creating pulses or time-driven events.
External hardware inputs are used to signal events to a microprocessor. In this activity, you will use a
timer to create a continuous 5 kHz signal at Port 1 pin 0 while an external user input signal can be applied
to INT1 at Port 3 pin 2, causing a counter to update. The program will react to the input while continuously
providing the 5 kHz signal.
1. Create a working project by following these steps:
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
sbit WAVE5kHZ = P1^0;
sbit EDGE = TCON^0;
short int count = 0;
void timer0setup(int mode, int offset)
{
TMOD = mode;
TH0 = offset; //TH0 = -92
TL0 = offset; //
}
void timer0(void) interrupt 1
{
WAVE5kHZ = ~WAVE5kHZ; //toggle P1.0
}
void external0(void) interrupt 0
{
count++; //count times button pushed
}
void main()
{
timer0setup(0x02, 0xA4);
//0x02 timer 0 mode 2
97
04/19/2012
GRADED ASSIGNMENTS
3. Examine the program. A bit variable called WAVE5kHZ represents the value of the 5 kHz signal. A bit
variable called EDGE will allow the TCON register bit 0 to be set to enable an edge-triggered
hardware interrupt. A short integer called count will store the number of times that the external
interrupt is triggered.
4. Function timer0setup will configure the timer to create a 5 kHz waveform. The timer will toggle every
100 microseconds. This represents the half cycle of the waveform. The function timer0(void) interrupt
1 will trigger whenever timer 0 reloads. The function external0(void) interrupt 0 will trigger whenever
an external interrupt INT0 is detected at Port 3 pin 2. (See Table 11-4 on page 340 to review the
interrupt numbers used by C.)
5. Examine the main function. It begins by calling the timer0setup function. This is followed by setting
the TCON.0 bit to 1 (enables an edge-triggered interrupt) and enabling the interrupt enable register IE
(allows both INT0 and TF0 to trigger interrupts). Timer 0 is started, and Port 2 is initialized. Finally, a
continuous loop constantly updates Port 2 with the value of the count.
6. Start Debug and make sure the following windows are open:
Port 1
Port 2
Port 3
Registers
Timer 0
7. Continuously single step through the program by holding the F11 key. Notice that Port 1 pin 0 toggles.
8. Simulate an external input by unchecking (clearing) Port 3 pin 2. Single step the program until the
program pointer completes the external0 function and is returned to the main loop. Notice that Port 2
updates.
9. Practice simulating the program until you understand how the interrupts are triggered and how the
interrupt functions act independently of the main function. Toggle Port 3 pin 2 several more times, and
watch that Port 2 updates as you single step through the program.
10. When you understand how the interrupts are triggered, continue to the next step.
11. Ensure that Port 2 is connected to the LED bank.
12. Connect an oscilloscope probe to Port 1 pin 0 and configure it to measure a 5 kHz signal.
98
04/19/2012
GRADED ASSIGNMENTS
13. Connect a manual momentary press to make switch and debounce the circuit to Port 3 pin 2 (INT0).
99
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Program the 8051 to perform Timer operations with interrupts using the C programming
language.
Program the 8051 to perform external hardware interrupts using the C programming language.
Program the 8051 to perform serial communications with interrupts using the C programming
language.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of Experimental Procedure: Provide a detailed summary of how you performed the
lab.
Deliverables
100
04/19/2012
GRADED ASSIGNMENTS
Unit 9 Lab 1: Interfacing and Programming LCDs with the 8051 (Part 1)
Course Objectives and Learning Outcomes
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Program the 8051 to perform I/O operations using the C programming language.
Power supply
10K Potentiometer
Introduction
Providing user information can be accomplished using several methods. Indicator lights can be used to
provide basic status information; however, this provides very limited information. Using seven-segment
LEDs to display ASCII messages requires significant circuitry for addressing and decoding. For example,
how would you connect 10 seven-segment displays? Serial communications can provide terminal
messages to a computer, but PCs are not always available, cost effective, or practical.
The LCD is more commonly used in todays embedded systems than the LED. Declining prices and ease
of interfacing make LCDs ideal for displaying prices for items in grocery and other retail stores. In contrast
to laptop computers, most embedded systems use a small LCD, such as 20x2 or 40x4 lines, to display
data. In this lab, you will interface an LCD to a microcontroller and program the system to send messages
to the LCD display.
101
04/19/2012
GRADED ASSIGNMENTS
Recommended Procedures
Activity 1: Interface the LCD Display
In this activity, you will examine your LCD module and correctly interface it to the microcontroller.
1. The following image is an example of how the display will appear when in use. Compare your LCD
module to the image. They should be similar (except for the message).
3. Examine the back of the LCD module. You should see the pin description to the right. Also, pin 1 and
pin 16 should be identified.
4. The pins should be as follows:
102
04/19/2012
GRADED ASSIGNMENTS
5. Use Table 12-1, Pin Descriptions for LCD, on page 352 to identify the pin descriptions.
a. Remember that to enable pin E requires a High to Low transition.
6. If your module is different from the images, use its datasheet to identify the pin out and the values for
VCC and VSS.
7. Examine the following connection diagram.
8. Notice that:
a. Port 1 is used to provide the data.
b. Port 2 is used to provide the control signals.
c.
VSS is ground.
Pins 15 and 16 are not required; however, they can be used to apply backlighting.
9. Connect the VCC and VSS. Connect the 10K potentiometer to provide contrast required. Preset the
potentiometer to its center value, 5K ohms.
10. Verify the VCC power supply is set to the correct value (should be 5 VDC), and apply power.
11. If your display shows very dark solid blocks, then VEE is too high; adjust the 10K potentiometer until
the contrast is correct.
12. Remove power.
103
04/19/2012
GRADED ASSIGNMENTS
13. Connect Port 1 to D4 through D7 to use the 4-bit option. You can connect D0 thru D7; however, the
test program only requires D4 thru D7.
14. Connect Port 2 to RS, R/W, and E.
15. The LCD should be ready to program and use.
Activity 2: Test the LCD Display Program
Once the LCD is properly connected and the contrast is correct, you should use a test program to verify
operation. This activity will display YES on the LCD.
1. Create a working project by following these steps:
a. Create New Project
b. Select Processor Dallas DS89C450 (do not copy startup code)
c.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
#include <reg51.h>
#include <reg51.h>
sfr Port1 = 0x90; //P1=LCD data pins (Fig. 12-2)
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
sbit busy = P1^7;
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void lcdready();
void MSDelay(unsigned int);
void main()
{
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x0F);
lcdcmd(0x38);
lcdcmd(0x85);
lcddata('L');
lcddata('C');
lcddata('D');
lcddata('s');
104
04/19/2012
GRADED ASSIGNMENTS
lcddata('!');
while(1); //do it once and stop
}
//functions on next page
105
04/19/2012
GRADED ASSIGNMENTS
TH1=0xFC;
TL1=0x67;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
}
3. Notice:
a. Port1 points to the address of Port 1.
b. Command bits RS, RW, and E are assigned to Port 2 bits 0, 1, and 2.
c.
Notice the lcdcmd4 values passed to the function; use the text to determine the purpose of
each command.
d. Function lcdcmd configures the command bits and assigns data to Port1.
e. Function lcddata configures the command bits and assigns data to Port1.
4. Build and start Debug, and make sure the following windows are open:
Port 2
Port 3
Registers
5. Single step through the program by holding the F11 key and notice:
a. The enable signal must toggle from high to low with a small delay to latch the data into the
LCD module.
b. The MSDelay value will cause a specific number of milliseconds to be used.
c.
When entering the MSDelay function, save time by pressing ctrll-F11 to step out of the
function.
6. Single step through the program repeatedly until you understand the purpose of each command and
can explain how the data port is used to cycle instructions and data to the LCD module.
Activity 3: Program the LCD Module
In this activity, you will download the previous program and examine the function of the LCD module.
1. You will use the program you created in Activity 2.
2. Build and save the executable hex code.
3. Establish a connection, download the program, and run the program.
4. You should see the LCD module display the message LCDs!
106
04/19/2012
GRADED ASSIGNMENTS
107
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Program the 8051 to perform I/O operations using the C programming language.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of Experimental Procedure: Provide a detailed summary of how you performed the
lab.
Deliverables
108
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Power supply
Function Generator
Oscilloscope
ADC0809
1k ohm resistor
10K Potentiometer
LM34 or LM35
Introduction
Real-world physical quantities, such as temperature, pressure, and moisture, are analog in nature. A wide
variety of sensors exist to convert these natural quantities to analog signals. To use a microcontroller to
monitor and control processes, the signals fed into it need to be converted from analog to digital (binary).
The use of an ADC (analog-to-digital converter) for digitizing natural quantities is the first step in the field
of data acquisition and control.
In this lab, you will interface an ADC and sensors to the microcontroller. These sensors will provide an
analog input to the ADC; the input will be digitized and displayed on the LED bank.
109
04/19/2012
GRADED ASSIGNMENTS
Reference the ADC0808/0809 Data Sheet (ET2640.U10.HO3) and LM34 or LM35 Data Sheet
(ET2640.U10.HO4) as needed while performing the lab.
110
04/19/2012
GRADED ASSIGNMENTS
Recommended Procedures
Activity 1: Interface an ADC 0808/09 to the 8051
1. In this activity, you will examine the datasheet for the ADC0808/09 and correctly interface it to the
microcontroller.
2. Obtain a datasheet of the ADC0808/09 Analog to Digital Converter for reference.
3. Examine Figure 10-1. You will apply an adjustable DC level to the ADC Input Channel 1. The
ADC808/809 is a single chip that can accommodate up to eight analog inputs. These eight inputs are
called channels, and each is selected by an address.
4. A 5V reference is applied to ensure the correct step sizesee page 380 for the reason. This will
provide an output of 0x00 with a 0 V input and 0xFF with a 5 V input.
5. The 8051 Port 2 provides the addressing and control signals. To understand the function of the
control signals, review the ADC0808/09 datasheet.
6. The 8051 Port 1 will receive the digital signal from the ADC.
7. The 8051 Port 3 will output the signal to the LED bank.
8. The ADC requires a clock pulse.
Figure 10-1
9. Set the function generators TTL/CMOS output to 5 V at 25 kHz. Use the oscilloscope to verify the
waveform, set to DC coupling, to ensure no negative (destructive) voltages are sent to the trainer.
Give the function generator time to stabilize. Do not connect the function generator signal to the ADC
input until power has been applied to the circuit.
111
04/19/2012
GRADED ASSIGNMENTS
Activate OE (output enable) to read data out of the ADC chip. An L-to-H pulse to the OE pin
will bring digital data out of the chip.
Add Files to Source Group (to set speed to 11.0592 and to set output to Hex)
112
04/19/2012
GRADED ASSIGNMENTS
#include <reg51.h>
sbit ADDR_A = P2^0;
sbit ADDR_B = P2^1;
sbit ADDR_C = P2^2;
sbit ALE = P2^4;
sbit OE = P2^5;
sbit SC = P2^6;
sbit EOC = P2^7;
sfr Port1_IN = 0x90; //Port 1
sfr Port3_OUT = 0xB0; //Port 3
void MSDelay(unsigned int);
void main()
{
//Initial Conditions
Port1_IN = 0xFF; //make P1 an input
EOC = 1; //make EOC an input
ALE = 0; //clear ALE
OE = 0; //clear OE
SC = 0; //clear SC
while(1)
{
ADDR_C = 0; //C=0
ADDR_B = 0; //B=0
ADDR_A = 1; //A=1 (Select Channel 1)
MSDelay(1); //delay for fast DS89C4x0
ALE = 1;
MSDelay(1);
SC = 1;
MSDelay(1);
ALE = 0;
SC = 0; //start conversion
while(EOC==1); //wait for data conversion
while(EOC==0);
OE = 1; //enable RD
MSDelay(1);
Port3_OUT = Port1_IN; //get the data
OE = 0; //disable RD for next round
}
}
113
04/19/2012
GRADED ASSIGNMENTS
4. Notice:
a. The command bits are assigned to Port 2 bits.
b. Port 1 and Port 3 are defined as DATA_IN and DATA_OUT.
c.
The MSDelay function will delay in milliseconds (approximately). If you want a 1 millisecond
delay, enter MSDelay(1); if you want a 100 millisecond delay, enter MSDelay(100).
5. Examine the program, and identify where each of the following steps were implemented.
EOC is monitored. While high, the conversion is in progress. When low, it is ready to be
latched into the output buffer. When it toggles high again, it is ready to output.
OE is activated; L-to-H pulse to the OE pin will bring digital data out of the chip.
6. Build and start Debug and make sure the following windows are open:
Port 1
Port 2
Port 3
Registers
Memory 1 set to D0x90 to view the contents of Port 1, Port 2, and Port 3.
114
04/19/2012
GRADED ASSIGNMENTS
12. Establish a connection, download the program, and run the program.
a. Note: Port 3 also interfaces with the MAX232 serial controller. If you cannot establish serial
communications, disconnect the LED bank from Port 3 until the program is downloaded to the
processor. You can then reconnect the LED bank.
13. The LED bank should illuminate. As the 10K pot is adjusted, the LED bank should track. As the
voltage into the ADC increases, the binary value should increase; as the voltage into the ADC
decreases, the binary value should decrease.
Activity 3: Use the LM34/35 Temperature Sensor with the ADC
In this activity, you will connect the LM34/35 Celsius temperature sensor to the ADC channel 1.
1. Obtain the datasheet for the LM34/35 sensor, and verify the sensors range (-55 to 150C). The sensor
changes 10 mv for every degree of temperature change, with its maximum output at 2.56 V.
2. Remove power from the circuit, and replace the potentiometer with the sensor, per Figure 10-2. Use the
LM34/35s datasheet to determine proper connections.
3. Change the Vref+ signal from 5 VDC to 2.56 VDC to scale the ADC output from 0x00 with a 0 V input and
0xFF with a 5 V input.
4. Nothing else should change.
Figure 10-2
5. You will use the program you created in Activity 2.
115
04/19/2012
GRADED ASSIGNMENTS
6. Reapply power and you should see the LED bar illuminate.
7. Use a cup of ice to cool the sensor; the LED bar should indicate a smaller binary number. Use a heat gun
or soldering iron (apply quickly to prevent damage) to heat the sensor; the output should increase.
Deliverables
116
04/19/2012
GRADED ASSIGNMENTS
Demonstrate the ability to use oscilloscopes, other test equipment, and external inputs for
troubleshooting and verification purposes.
Program the 8051 to perform I/O operations using the C programming language.
None
Recommended Procedures
Write a lab report that contains each of the following topics:
Explanation of Experimental Procedure: Provide a detailed summary of how you performed the
lab.
Deliverables
117
04/19/2012