PIC18 Study Guide CIE
PIC18 Study Guide CIE
PIC18 Study Guide CIE
MICROCONTROLLERS AND
EMBEDDED SYSTEMS COURSE
For use with Fundamentals of Microcontrollers and Applications in Embedded Systems (with the
PIC18 Microcontroller Family) by Ramesh S. Gaonkar (ISBN: 978-1-4018-7914-3)
Table of Contents
Welcome to the exciting world of microcontrollers. In this course we will delve into the
PIC18 microcontroller family and learn about the fundamentals of microcontrollers and their
application in embedded systems. This course contains ten lesson assignments covering material
The textbook does contain a few errors and the author did provide an errata sheet
indicating the corrections, which have been included in this study guide. You will find them as a
Note *** so if you have any questions regarding the errors please let us know. If you encounter
other errors not mentioned in the study guide please let us know and we will contact the author.
A CD does accompany the textbook and it includes a simulator for the PIC18, source
codes and data sheets. In Chapter 4 the PIC18 Simulator is discussed and you are welcome to
experiment with the simulator but another study has been prepared by the Instructors at CIE that
will use the simulator to write programs. If you are enrolled in a diploma program with CIE you
will receive the study guide after completing this study guide automatically. If you are taking this
course through CIE’s Bookstore then the laboratory study guide is also available from the
Bookstore.
If you have any questions please contact the Instruction Department at 1-800-243-6446
(216-781-9400) or email at [email protected] for assistance. We hope you enjoy this course
and we know you will come away with a better understanding of how embedded systems and
Assignment Checklist
Read Chapter 1 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8102 in this Study Guide
Take the Examination for this lesson
Chapter 1 Lecture/Discussion
The microcontroller is an integrated electronic computing and logic device that includes three
major components on a single chip
Microprocessor
Memory
I/O ports
The four terms of CPU, MPU, processor and microprocessor are synonymous so the textbook
will use them interchangeably.
Memory-A semiconductor storage device consisting of registers that store binary bits
Two major categories
– Read/Write Memory (R/WM)
– Read-only-Memory (ROM)
Input/Output (I/O)
Input devices such as switches and keyboards provide binary information to the microprocessor
(MPU) Output devices such as LEDs, video screens, and printers receive information from the
microprocessor (MPU).
6
Bus
The three components – MPU, memory, and I/O – are connected by a group of wires called the
bus. It is a common communication path between these components.
– Wires function as a group
– Same wires connected to multiple devices
Microprocessor Architecture
The MPU communicates with memory and I/O using the system bus consisting of:
– Address bus: unidirectional and carries memory and I/O addresses
– Data bus: bidirectional; transfers binary data and instructions between MPU and
memory and I/O
– Control lines: Read and Write timing signals asserted by MPU
The microprocessor (MPU) is a computing and logic device that executes binary instructions in a
sequence stored in memory.
– General purpose
– Binary
– Register-based
– Clock-driven
– Programmable
I/O (Input/Output)
Peripherals
– Input devices
• switches, keyboard, mouse, scanner, and digital camera
– Output devices
• LEDs, LCD, video screen, and printer
– Interfacing devices
• buffer and latch
7
Signed Integers: Seven bits (Bit 0 to Bit 6) represent the magnitude of a number
– The eighth bit (Bit 7) represents the sign of a number. The number is positive
when Bit7 is zero and negative when Bit7 is one.
– Positive numbers: 0 to 7F (0 to 127)
– Negative numbers: 80 to FF (-1 to -128)
– All negative numbers are represented in 2’s complement
MPU-Based Systems
System hardware
– Includes discrete components such as a microprocessor, memory, and I/O
– Components connected by buses
• address, data, and control
System software
– A group of programs that monitors the functions of the entire system
8
MCU-Based Systems
Includes microprocessor, memory, I/O ports, and support devices (such as timers) on a
single semiconductor chip
Buses are generally not available to a system designer
I/O ports are generally multiplexed and can be programmed to perform different
functions
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
9. In 8 K-byte (KB) of memory there are ____________ registers with the hexadecimal
address of the last register being ____________.
(1) 8192, 8191H (3) 8000, 1FFFH
(2) 8192, 2000H (4) 8192, 1FFFH
11. The last address of a memory chip is 07FFH. What is the size of the chip?
(1) 1 KB (3) 4KB
(2) 2 KB (4) None of the above
12. A range of memory extends from 00000H to 1FFFFH. How big is this range?
(1) 2 KB (3) 6 KB
(2) 4 KB (4) 8 KB
13. A microcontroller has a R/W memory range that starts at 2000H and ends at 21FFH. How
many memory locations would this be?
(1) 512 (3) 511
(2) 1024 (4) 1023
17. The decimal number 138 would be __________ in hex and ___________ in binary.
(1) 76H, 100010102 (3) 8AH, 101010102
(2) 8AH, 100010102 (4) 76H, 101010102
19. If the ASCII code is 5AH, what letter or character does it represent?
(1) a (lower case) (3) Z (upper case)
(2) @ (4) %
11
20. Which bit differentiates an upper case ASCII letter from a lower case letter?
(1) Bit 1 (3) Bit 3
(2) Bit 7 (4) Bit 5
22. The end result of an operation is 98H. What would the decimal value be if it were signed?
(1) 152 (3) -98
(2) -102 (4) -66
24. In 4 MB of memory there are ___________ registers, with the hex address of the last
register being ___________.
(1) 4194304, 3FFFFF (3) 4194304, 400000
(2) 4194304, 4194303 (4) 4000000, 3FFFFF
25. An 8 bit processor would be able to represent the hex value -138 in decimal.
(1) True (2) False
End of Examination
12
Assignment Checklist
Read Chapter 2 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Chapter 3 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Review Appendix A PIC18FXXX/XXXX Instruction Set
Read Lesson 8104 in this Study Guide
Take the Examination for this lesson
Chapter 2 Lecture/Discussion
PIC18F Microcontroller Families
• PIC microcontrollers are designed using the Harvard Architecture which includes:
– Microprocessor unit (MPU)
– Program memory for instructions
– Data memory for data
– I/O ports
– Support devices such as timers
Microprocessor Unit
• Includes Arithmetic Logic Unit (ALU), Registers, and Control Unit
– Arithmetic Logic Unit (ALU)
• WREG – working register
• Status register that stores flags
• Instruction decoder
• Registers
– Bank Select Register (BSR)
• 4-bit register used in direct addressing the data memory
– File Select Registers (FSRs)
• 16-bit registers used as memory pointers in indirect addressing data
memory
– Program Counter (PC)
• 21-bit register that holds the program memory address while executing
programs
• Control unit
– Provides timing and control signals to various Read and Write operations
PIC18F452/4520 Memory
• Program Memory: 32 K
– Address range: 000000 to 007FFFH
• Data Memory: 4 K
– Address range: 000 to FFFH
• Data EEPROM
– Not part of the data memory space
– Addressed through special function registers
***Note: The textbook has an error with the caption of Figure 2-3 listing it as PIC252***
***Note: On page 53 the second line in the information box contains an error in the
Comments column. In the textbook it reads “Set up PORTB as output” but it should have
been “Set up PORTC as output” ***
15
Chapter 3 Discussion
PIC18F Programming Model
• The representation of the internal architecture of a microprocessor, necessary to write
assembly language programs
• Divided into two groups
– ALU Arithmetic Logic Unit (ALU)
– Special Function Registers (SFRs) from data memory
Registers
• WREG
– 8-bit Working Register (equivalent to an accumulator)
• BSR: Bank Select Register (0 to F)
– 8-bit Register
• Only low-order four bits are used to provide MSB four bits of a12-bit
address of data memory.
• STATUS: Flag Register
Instruction Format
• The PIC18F instruction format divided into four groups
– Byte-Oriented operations
– Bit-Oriented operations
– Literal operations
– Branch operations
Execution of an Instruction
Instruction: MOVLW 0x37 ; Load 37H in W
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
6. The MCU consists solely of the MPU and the I/O ports.
(1) True (2) False
7. Harvard architecture in in the MPU means there is separate memory storage for
instructions and data.
(1) True (2) False
8. The W register in the PIC18 MCU is the same as the ___________ in other
microcontrollers.
(1) Program counter (3) Accumulator
(2) Register (4) Control unit
10. The FSR registers are 21 bits wide in order to hold the 12 bit address of the data register
and the 4 bit address of the memory bank.
(1) True (2) False
12. What is the address range (beginning to ending addresses) of the special function
registers in the PIC18F MCU?
(1) 00H to FFH (3) 80H to FFH
(2) 00H to 80H (4) None of the above
16. Which of these will not generate an interrupt for the PIC18F MCU?
(1) Peripheral devices (3) A/D Converters
(2) Timers (4) RAM
17. Which is not a typical support device included on the MCU chip?
(1) Timers (3) A/D converter module
(2) Flash memory (4) Serial module
MOVLW 0xFA
ADDLW 0x38
18. After the MOVLW 0xFA is executed what is the contents of the W register?
(1) 00H (3) 38H
(2) 32H (4) FAH
19. After the ADDLW 0x38 is executed what is the contents of the W register?
(1) 00H (3) 38H
(2) 32H (4) FAH
20
20. If the numbers were unsigned, what would be the result of the addition?
(1) 138H (3) 38H
(2) FAH (4) 32H
25. Peripheral devices can process data faster than the MPU can send it.
(1) True (2) False
End of Examination
21
Assignment Checklist
Read Chapter 4 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8106 in this Study Guide
Take the Examination for this lesson
Chapter 4 Lecture/Discussion
Flowcharting
• Flowchart
– A graphical representation of processes (tasks) to be performed and the sequence
to be followed in solving computational problem
• Symbols shown in Figure 4.1 are used commonly to draw a flowchart
Example 4.1
• Write instructions to load two bytes (37H and 92H) in data registers REG0 and REG1.
Add the bytes and store the sum in REG2.
• Steps
– Load the two bytes in data registers REG0 and REG1.
– Add the bytes.
– Save the sum in data register REG2.
Assembler Directives
• ORG Origin
• END End of assembly
• EQU Equate
• SET Defines an assembler variable
• #INCLUDE Include resources from available library
• RADIX Number format
• DB Define byte
• DW Define word
• CBLOCK Define a block of constants
• ENDC End of block of constants
• RES Reserve memory
***A Laboratory Study Guide will cover the following procedure along with several simulation
exercises***
25
– Summary Finish
Project Window
28
***On Page 112 there are a couple of errors in the program at the top of the page. On Program
Line 000024 the Text portion has ;Load 32H into W and the missing information is ;Load 32H
into W register so register is missing. And the next line below this has register26 6E01 and the
word register should not have been there but the corrected information is 000026 6E01.
Debugging a Program
• Single-step technique
– Enables user to execute one instruction at a time and observe registers for
expected results
• Breakpoint technique
– Enables user to execute a group of instructions at a time and observe registers for
expected results
• Tracing code
– MPLAB can track execution of each instruction and display data which can be
examined for errors
30
Breakpoint Technique
Tracing Code
31
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
2. A(n) ___________ is a program that converts the mnemonics of a source program into
binary machine language, equivalent Hex codes, and assigns memory address to these
codes.
(1) program (3) flowchart
(2) flow diagram (4) assembler
4. What would be the contents of the W register after the program has been run?
(1) 100H (3) 9AH
(2) AAH (4) 34H
5. After the addition has taken place, what flags would be set?
(1) N and OV (3) N, OV, Z and C
(2) N, OV, and Z (4) No flags were set
7. What would be the contents of the W register after the program has been run?
(1) 09H (3) F1H
(2) 0FH (4) FFH
8. What flags would be set after the subtraction has taken place?
(1) N and OV (3) N and DC
(2) N and Z (4) All of the flags would be set
11. An assembly language program can contain more than one ORG statement?
(1) True (2) False
13. The flowchart symbol for the beginning of a program is the _______________?
(1) Rectangle (3) Oval
(2) Diamond (4) Circle with Arrow
14. The flowchart symbol for the decision point of a program is the _______________?
(1) Rectangle (3) Oval
(2) Diamond (4) Circle with Arrow
End of Examination
33
Assignment Checklist
Read Chapter 5 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Chapter 6 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8108 in this Study Guide
Take the Examination for this lesson
Chapter 5 Lecture/Discussion
Data Copy (Move) and Set/Clear Operations
The data copy operations are classified as:
• Loading 8-bit data directly in WREG
• Copying data between WREG and data (file) register including I/O ports
• Copying data from one data (file) register to another data (file) register
• Clearing or setting all data bits in data (file) register
• Exchanging low-order four bits (nibble) with high-order four bits in data (file) register
Addressing Modes
• A way of specifying of an operand
– Direct addressing
• The operand is a part of the instruction
– Indirect addressing
• An address is specified in a register and the MPU looks up the address in
that register
****Understanding the various instruction sets is very important to understanding coding. You
do not need to memorize the instruction sets but simply have a general idea of the operation as it
helps in programming. Appendix A is used for reference so that way you don’t have to memorize
every instruction set.
Using Table Pointers to Copy Data from Program Memory into Table Latch
• TBLRD*
– Copy from Program Memory into Table Latch Using Table Pointer
• TBLRD*+
– Copy from Program Memory into Table Latch and Increment Table Pointer
34
• TBLRD*-
– Copy from Program Memory into Table Latch and Decrement Table Pointer
• TBLRD+*
– Increment Table Pointer first and then copy from Program Memory into Table
Latch
Using Table Pointers to Copy Data from Table Latch into Program Memory
• TBLWT*
– Copy from Table Latch into Program Memory Using Table Pointer
• TBL WT*+
– Copy from Table Latch into Program Memory and Increment Table Pointer
• TBL WT*-
– Copy from Table Latch into Program Memory and Decrement Table Pointer
• TBLWT+*
– Increment Table Pointer first and then copy from Table Latch into Program
Memory
Arithmetic Operations
• The PIC18F MPU performs the following arithmetic operations:
– Add
– Subtract
– Multiply
– Complement (1s and 2s)
– Increment
– Decrement
***Example 5.4 on Page 136 contains an error in the Results: section. It should read as follows:
WREG 0 1 0 0 1 1 1 1 (4FH)
+
REG20 0 1 0 0 1 0 0 0 (48H)
Carry 1 1
------------------------------------
1 0 0 1 0 1 1 1 (97H)
****On Page 156 the 5.6.3 Program contains a few errors. The second line of the program has:
COUNTER EQU 0x010 when it should be COUNTER EQU 0x01
Line 4 has GOTO START0 when it should be GOTO START
Line 5 has ORG 0x200 when it should be ORG 0x20
****On Page 164 the 5.7.3 Program contains an error. Toward the end under SKIP: line 3 has:
MOVWF ORTC when it should be MOVWF PORTC
35
Chapter 6 Lecture/Discussion
Logic Instructions: AND, IOR (Inclusive OR), and XOR (Exclusive OR)
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
Bit Rotation
• The instruction set includes four instructions that can shift a bit to the adjacent position—
either left or right.
• The instructions are further classified as 8-bit rotation and 9-bit rotation.
– In 9-bit rotation, carry flag becomes the ninth bit.
****On the top of Page 193, under section 6.3-Multiply and Divide Operations, the last line of
the instructions has an extra L in MULLWF F, a and its example. It should be:
MULWF F, a: MULWF REG1,0
Division
• The PIC18F instruction set does not include any instruction that divides two numbers.
• Commonly used algorithms to divide numbers
– Using the instruction Rotate Right
– Repeated Subtraction
****The 6.5.3 Program begins on Page 199 and continues to Page 200. On Page 200 the
program contains an error. The 5th line up from END has a wrap around from the comments line.
The HI_SUM goes with the comment line being part of decrement HI_SUM.
38
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
1. If two unsigned numbers 3FH and 47H are added together what is their result in the W
register?
(1) 86H (3) 134H
(2) 206H (4) None of these are correct
3. The STATUS register would have the Hex value of ____________ after the addition of
the two unsigned numbers in Question 1.
(1) 18H (3) 00H
(2) 08H (4) Cannot be determined
4. If the numbers in Question 1 were signed numbers, the result would be the same.
(1) True (2) False
5. If the instructions were writing for subtracting the byte 7FH from the byte 25H what
would be the final result after the program was executed?
(1) 5AH (3) A6H
(2) A5H (4) None of these are correct
6. Which flag or flags would be set after the subtraction in Question 5 was completed?
(1) N (3) OV, DC and C
(2) N and OV (4) All of the flags would be reset
7. If two bytes 78H and F2H were added together, the W register would be ___________?
(1) 22AH (3) 6AH
(2) 16AH (4) Cannot be determined
39
(1) 1 (3) 50
(2) 32 (4) 82
9. In the program of Question 8, if the instruction BNZ was changed to BZ how many times
is LOOP1 executed?
(1) 1 (3) 50
(2) 32 (4) 82
10. Calculate the time delay of LOOP1 in the program of Question 8 if the clock frequency is
10-MHz. Ignore the difference in execution of the last cycle of the BNZ instruction.
(1) 0.6-μS (3) 60-μS
(2) 6-μS (4) 600-μS
11. If the bytes 97H and 68H were ANDed what would be the result?
(1) FFH (3) 07H
(2) D1H (4) 00H
12. If the bytes F8H and 31H were ANDed what would be the result?
(1) 129H (3) 39H
(2) A7H (4) 30H
13. After the ANDing has taken place in Question 12, which flag or flags are affected?
(1) None, all are reset (3) N is set
(2) N and Z are reset (4) N and Z are set
14. If the bytes F8H and 37H are ORed what would be the result?
(1) FFH (3) 12FH
(2) C1H (4) 1CDH
15. After the ORing has taken place in Question 14, which flag or flags are affected?
(1) None, all are set (3) N is reset
(2) N is set (4) N and C are reset
16. If the bytes 97H and 67H were ORed what would be the result?
(1) FFH (3) 0FH
(2) F7H (4) 00H
40
17. What is the instruction to reset the OV flag in the STATUS register?
(1) BCF STATUS, 3 (3) BCF STATUS, 5
(2) BCF STATUS, 2 (4) BCF STATUS, 7
19. If the bytes 31H and C8H were exclusive ORed, the results would be?
(1) Cannot exclusively OR bytes in PIC MPU.
(2) Exclusive OR these two bytes would produce an erroneous result.
(3) The result would be F9H.
(4) The result would be 00H.
20. Which flag or flags would be set after 31H and C8H were exclusive ORed?
(1) All of the flags. (3) N would be reset.
(2) None of the flags. (4) N would be set.
End of Examination
41
Assignment Checklist
Read Chapter 7 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8110 in this Study Guide
Take the Examination for this lesson
Chapter 7 Lecture/Discussion
Stack
• Temporary memory storage space used during the execution of a program
• Can be part of R/W memory or specially designed group of registers
• Stack Pointer (SP)
– The MPU uses a register called the stack pointer, similar to the program counter
(PC), to keep track of available stack locations.
Subroutine
• A group of instructions that performs a specified task
• Written independent of a main program
• Can be called multiple times to perform task by main program or by another subroutine
• Call and Return instructions used to call a subroutine and return from the subroutine
Example 7.2
• Program Listing with Memory Addresses
****On Page 225 the last box of Program 7.5.3 contains an error. The box begins with:
MOVWF LO_SUM
MOVLW 0x03 ;
Shifting right three times – set up counter for 3
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
2. The __________ is a register in a MPU that is used to hold an address of the memory
location that is available to store the information.
(1) Program counter (3) Stack
(2) Stack pointer (4) Subroutine
3. The ___________ holds the address of the memory location of the next code to be
executed.
(1) Program counter (3) Stack
(2) Stack pointer (4) Subroutine
6. The stack pointer register is the same size as the program counter.
(1) True (2) False
10. The term output parameter to a subroutine is explained as the information needed to the
subroutine by the calling program.
(1) True (2) False
11. The advantage of a subroutine is that it is a more efficient execution than a macro.
(1) True (2) False
12. A disadvantage with a macro is that its code is included in the program every time it is
invoked.
(1) True (2) False
13. The stack pointer that holds the address of the TOS register is ______ bits wide.
(1) 8 (3) 21
(2) 31 (4) 5
14. When the user tries to use more registers than the PIC18 microcontroller has for the
stack, then this is called _____________?
(1) overflow (3) push
(2) underflow (4) pull
15. The bit number in the STKPTR Register that is associated with the answer to Question 14
is _____________?
(1) B4 (3) B6
(2) B5 (4) B7
End of Examination
46
Assignment Checklist
Read Chapter 8 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8112 in this Study Guide
Take the Examination for this lesson
Chapter 8 Lecture/Discussion
Application Programs
• BCD to Binary Conversion
• Binary to BCD Conversion
• ASCII Code to Binary Conversion
• Binary to ASCII Code Conversion
• Multiplication of 16-bit Numbers
• Division of Two Unsigned Numbers
****On Page 238 the textbook has an error in Section 8.4.1 as the Problem Statement should
read as stated below:
• Problem statement
– Write a subroutine to convert an 8-bit binary number given in WREG into its
ASCII characters.
• Problem analysis
– An 8-bit number must be first unpacked to get two digits.
– The process of finding ASCII code for the unpacked digits is just the opposite to
that of ASCII to binary conversion.
– If the digit is between 0 and 9, add 30H.
– If the digit is larger than 9 (from A to F), add additional 07H.
• In the above multiplication, two digits are multiplied at a time with all four combinations.
49
• The addition is done by shifting the multiplication results to the left position based on the
positions of the digits.
****The 8.5.3 Program has a few errors in the code. The first four lines should be stated as:
Line 1 NUM1LO EQU 0x02 ;16-bit number1-0102H
Line 2 NUM1HI EQU 0x01
Line 3 NUM2LO EQU 0x05 ;16-bit number1-0205H
Line 4 NUM1HI EQU 0x02
Software Design
• Project statement
– Design a software program to meet the following specifications:
• Given a string of temperature readings that terminates in a null character
00, find the average temperature.
• Convert the average temperature reading in BCD digits into ASCII
characters to be displayed by some other subroutine.
• A new data set of temperature readings is recorded every 100 ms and it is
indicated by setting Bit0 (carry flag) in STATUS register.
• Project analysis
– By examining the project statements carefully, many clues can be found to divide
the project in small segments.
– These small segments can be written as independent modules or subroutines.
– The project is divided as follows:
• In this project, the number of bytes is variable and determined by the last
null character.
• A separate subroutine can be written to check for the null character and
count the number of bytes in the string.
• To find the average reading, these readings must be added first that may
generate carries, and the sum will require more than one 8-bit register.
– To find the average temperature reading, the sum must be divided by the number
of readings.
– The number of bytes in the string counted in the previous subroutine must be
passed on to this division process.
– The repeated subtraction method to divide a 16-bit sum by an 8-bit divisor can be
used.
– The average temperature is in binary (Hex).
• The reading must be converted in BCD numbers.
• The subroutine in Section 8.2 converts an 8-bit Hex number in three BCD
numbers.
– To display these digits at ASCII peripherals, these digits must be converted into
ASCII codes by using the subroutine BINASC discussed in Section 8.4.
– New data string is available every 100 ms, and the availability of new data set is
indicated by setting the carry flag in the STATUS register.
– This process must be repeated every 100 ms.
– This program should check for the carry flag at the beginning before it begins the
execution and stays in a continuous loop until the carry flag is set.
50
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
2. The label to denote where the subroutine program resides is important to the operation of
the program.
(1) True (2) False
3. In the following subroutine program called UNPACK, which line is responsible for
masking the high-order nibble?
5. In the following subroutine program called UNPACK, which line is responsible for
masking the low-order nibble?
(1) ANDLW 0x0F (the first usage (3) ANDLW 0x0F (the second usage
(2) MOVWF BUFFER1 (4) RRNCF REG1, 1
6. The difference between the subroutine in problem 3 and the one in problem 5 is that we
used three RRNCF instructions to replace the SWAPF instruction
(1) True (2) False
7. If we analyze the program fragment from Section 8.4 in the text, how many subroutines
will the microprocessor encounter?
(1) One (4) There are no subroutines in the
(2) Two referenced program fragment
(3) Three
8. In Section 8.5 of the text, the program used multiplies two 16-bit numbers together.
Which section of the program is responsible for multiplying NUM1LO and NUM2HI?
(1) SECT1 (3) SECT2
(2) SECT3 (4) SECT4
9. If we analyze the program fragment from Section 8.7 in the text, how many subroutines
will the microprocessor encounter?
(1) Two (3) Four
(2) Three (4) Five
10. The program shown in Section 8.6 of the text has to clear the carry flag and the register
REMAINDR before executing any of the division process.
(1) True (2) False
11. Ensuring that the labels used for subroutines are not duplicated or undefined is one thing
we as programmers really do not have to worry about happening.
(1) True (2) False
52
12. In Section 8.5 of the text, the program used multiplies two 16-bit numbers together.
Which section of the program is responsible for multiplying NUM1LO and NUM2LO?
(5) SECT1 (7) SECT2
(6) SECT3 (8) SECT4
13. What is the code used to communicate between video screens or printers and the
microcontroller?
(1) Binary (3) ASCII
(2) Hexadecimal (4) BCD
14. If we were to modify the addition subroutine from Section 8.7 like this, what would the
result be?
ADDITION: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Function: Adds the data bytes in the data string ;
;Input: Address in FSR0 to point to data string and ;
; Count of data bytes in COUNTER register ;
;Output: 16-bit sum-High byte in CYREG and low byte in W ;
;Registers Changed: COUNTER and FSR0 ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15. In the breakout sections of each major section is an area entitled Program Execution and
Troubleshooting. How does the author recommend you check each program?
(1) By setting breakpoints and then running the program
(2) By single stepping through the program
(3) By just running the entire program
(4) The author uses each one of these but applies them strategically
End of Examination
53
Assignment Checklist
Read Chapter 10 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Chapter 11 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8114 in this Study Guide
Take the Examination for this lesson
Chapter 10 Lecture/Discussion
Basic Concepts in Interrupts
• An interrupt is a communication process set up in a microprocessor or microcontroller in
which:
– An internal or external device requests the MPU to stop the processing
– The MPU acknowledges the request
– Attends to the request
– Goes back to processing where it was interrupted
Types of Interrupts
• Hardware interrupts (Figure 10-1)
– Maskable: can be masked or disabled
– Two groups: external and internal interrupts
– Non-maskable: cannot be disabled
– Software interrupts: generally used when the situation requires stop processing
and start all over
– Examples: divide by zero or stack overflow
– Generally, microcontrollers do not include software interrupts
MPU Response to Interrupts
• When the interrupt process is enabled, the MPU, during execution, checks the interrupt
request flag just before the end of each instruction.
• If the interrupt request is present, the MPU:
– Completes the execution of the instruction
– Resets the interrupt flag
– Saves the address of the program counter on the stack
• Some interrupt processes also save contents of MPU registers on the stack.
– Stops the execution
• To restart the execution, the MPU needs to be redirected to the memory location where
the interrupt request can be met.
– Accomplished by interrupt vectors
• The set of instructions written to meet the request (or to accomplish the task) is called an
interrupt service routine (ISR).
• Once the request is accomplished, the MPU should find its way back to the instruction,
next memory location where it was interrupted.
– Accomplished by a specific return instruction
54
Interrupt Vectors
• Direct the MPU to the location where the interrupt request is accomplished.
• They are:
– Defined memory location where a specific memory location/s is assigned to the
interrupt request
– Defined vector location where specific memory locations assigned to store the
vector addresses of the ISRs
– Specified by external hardware: The interrupt vector address (or a part of it) is
provided through external hardware using an interrupt acknowledge signal.
Interrupt Priorities
• Rationale for priorities
– Multiple interrupt sources exist in a system, and more than one interrupt requests
can arrive simultaneously.
– When one request is being served (meaning when the MPU is executing an ISR),
another request can arrive.
– Therefore, the interrupt requests must be prioritized. Most MCUs (and MPUs)
include an interrupt priority scheme. Some are based on hardware and some use
software.
PIC18 Interrupts
• PIC18 Microcontroller family
– Has multiple sources that can send interrupt requests
– Does not have any non-maskable or software interrupts; all interrupts are
maskable
– Has a priority scheme divided into two groups
• High priority and low priority
– Uses many Special Function Registers (SFRs) to implement the interrupt process
55
****Note in Figure 10-4 (a) on Page 319 at the top of the page the information in B7 and the
first line of the register description is incorrect. It should be GIE/GIEH and not GIE/GEIH as
shown.
56
****Note on Page 321 in Example 10.1 the section INT1_ISR: has an error in the first line. It
should be BCF INTCON3, INT1IF
PIC18 Resets
• When the reset signal is activated:
– The MPU goes into a reset state during which the initial conditions are
established.
– The program counter is cleared to 000000 which is called the reset vector.
– The MPU begins the execution of instructions from location 000000.
• PIC18 MCU can be reset by external source such as the push-button key, or when power
is turned-on, or by various internal sources.
– Resets categorized as follows:
• External Manual Reset Key
• Power-on Reset (POR)
• Watchdog Timer Reset (WDT)
• Programmable Brown-Out Reset (BOR)
• RESET and SLEEP Instructions
• Stack Full and Underflow Reset
****Note there are two errors to mention on Page 330. The 10.3.3 Program contains an error in
section MAIN: on line 9 and it should be
BSF INTCON3, INT1IE ;Enable Interrupt 1-
;INTCON3 <3>
Chapter 11 Lecture/Discussion
Basic Concepts in Counters and Timers
• In digital systems
– Counting is a fundamental concept.
– Clock is an essential element.
– Count is in synchronization with the clock.
– Count is converted in time by multiplying the count and the clock period.
• The register can also be used as a counter by replacing the clock with a signal from an
event.
• When a signal from an event arrives, the count in the register is incremented (or
decremented); thus, the total number of events can be counted.
Types of Counters
• Up-counter
– Counter is incremented at every clock cycle
– When count reaches the maximum count, a flag is set
– Counter can be reset to zero or to the initial value
• Down-counter
– Counter is decremented at every clock cycle
– When count reaches zero, a flag is set
– Counter can be reset to the maximum or the initial value
• Free-running counter
– Counter runs continuously and only readable
– When it reaches the maximum count, a flag is set
Timer Applications
• Time delay
• Pulse wave generation
• Pulse width or frequency measurement
• Timer as an event counter
• Compare: The CCP pin is set as an output, and at a given count, it can be
driven low, high, or toggled.
• Pulse width modulation (PWM): The CCP pin is set as an output and the
duty cycle of a pulse can be varied.
PIC18 Timers
• The PIC18 microcontroller has multiple timers, and all of them are up-counters.
• Timers are divided into two groups: 8-bit and 16-bit
• Labeled as Timer0 to Timer3 or Timer4 (if available)
– Timer0 can be set up as an 8-bit or 16-bit timer.
– Timer1 and Timer3 are 16-bit timers.
– Timer2 and Timer4 (if available) are 8-bit timers.
• Each timer associated with its Special Function Register (SFR): T0CON-T3CON or
T4CON
Timer0
– Can be set up as an 8-bit or 16-bit timer (Figures 11-2 (a) and (b)
– Is readable and writable
– Parameters are set up by bits in T0CON register
– Has eight options of pre-scale values (Bit2-Bit0-Figure 11-3)
– Can run on internal clock source (instruction cycle) or external clock connected to
pin RA4/T0CK1
– Generates an interrupt or sets a flag when it overflows from FFH to 00 in the 8-bit
mode and from FFFFH to 0000 in the 16-bit mode
– Can be set up on either rising edge or falling edge (Bit4 –Figure 11-3) when an
external clock is used
– To use prescaler, Bit3 = 0, and three bits Bit2-Bit0 specify scaler ratio from 1:2 to
1:256
• Interrupt
– When Timer0 overflows from FFH to 00 in the 8-bit mode and from FFFFH to
0000 in the16-bit mode, it sets TMR0IF (Timer0 Interrupt Flag) –Bit2 in the
INTCON register.
• Flag can be used two ways: 1) a software loop can be set up to monitor the
flag, or 2) an interrupt can be generated.
• Flag must be cleared to start the timer again.
• 16-bit mode
– When Timer0 is set in the 16-bit mode, it uses two 8-bit registers TMR0L and
TMR0H.
****Note on Page 345 in Example 11.2 Line 3 from the end “Therefore, the count should be 65,
536 – 19531 = 46,005 = B3B5H”
Also in 11.2.2 Program, last line MOVLW 0xB5 ;Low count of B3B5H
On Page 346 11.2.2 Program continues and in the section TMR0_ISR in line 1 the comment
should be ;High count of B3B5H
Timer1
• Figure 11-5
– A 16-bit counter/timer with two 8-bit registers (TMR1H and TMR1L); both
registers are readable and writable
– Four options of prescale value (Bit5-Bit4)
– Clock source (Bit1) can be internal (instruction cycle) or external (pin
RC0/T13CK1) on rising edge
– Sets flag or generates an interrupt when it overflows from FFFFH to 0000
• Timer1 Operation
– Can operate in three modes: timer, synchronous counter, and asynchronous
counter
– Bit0 enables or disables the timer
– When Bit1 = 0, it operates as a timer and increments count at every instruction
cycle.
– When Bit1 = 1, it operates as a counter and increments count at every rising edge
of the external clock.
– When Bit3 = 1, Timer1 oscillator is enabled which is used for low frequency
operations.
61
• Interrupt
– When the Timer1 overflows from FFFFH to 0000, it sets the TMR1IF (Timer1
Interrupt Flag) –Bit0 in the Peripheral Interrupt Register1 (PIR1).
– This flag can be used two ways
• A software loop can be set up to monitor the flag.
• An interrupt can be generated.
– Flag must be cleared to start the timer again.
• Resetting Timer1 using CCP module
– When a CCP module associated with Timer1 is loaded with a 16-bit number and
setup in the Compare mode, the count in Timer1 and the number in the CCP
module are compared at every cycle.
– When a match is found, Timer1 is reset.
Timer2
• Figure 11-6
– An 8-bit timer (TMR2) with an 8-bit period register (PR2)
– Registers (TMR2 and PR2) are readable and writable
– Three options of prescale values (Bit1-Bit0)
– 16 options of postscale values (Bit6-Bit3)
– A flag is set when TMR2 value matches that of PR2, which can generate an
interrupt
• Timer2 operation
– Figure 11-7 shows two 8-bit registers (TMR2 and PR2)
– An 8-bit number is loaded in PR2 and the timer is turned on, which is
incremented every instruction cycle.
– When the count in the timer register and the PR register match, an output pulse is
generated and the timer register is set to zero.
– The output pulse goes through a post scaler that divides the frequency by the scale
factor and sets the flag TMR2IF- Bit1 in the Peripheral Interrupt Register1 (PIR1)
that can be used to generate an interrupt.
Timer3
• Figure 11-8
– 16-bit counter/timer with two 8-bit registers (TMR3H and TMR3L); both
registers are readable and writable
– Can operate as a timer, a synchronous or an asynchronous counter
– Four options of prescale value (Bit5-Bit4)
– Clock source (Bit1) can be internal (instruction cycle) or external Timer1
oscillator or T1CK1 (on the rising edge after the first falling edge)
– Sets a flag or generates an interrupt when it overflows from FFFFH to 0000
• Timer1 and Timer3 are used as clock resources for Capture and Compare registers
• Timer2 and Timer4 (if available) are used as clock sources as PWM modules
• When CCPR1H and TMR2 match again for the specified duty cycle, the
CCP1 pin is cleared.
• To Initialize CCP1 in the PWM mode:
• Set up pin RC2/CCP1 of PORTC as output.
• Set up PWM period by writing to the PR2 register.
• Set up PWM duty cycle by writing to CCPR1L register and Bit5-Bit4 of
CCP1CON register.
• Set up TMR2 prescale value and Timer2 in timer mode by writing to T2CON
register.
• Enable CCP1 module in the PWM mode.
• Set up CCP1 by writing to the CCP1CON register.
Timer Calculations
• Example 11.8
•
****Note on Page 363 11.5.3 Program contains an error. In the description section above the
coding section, the 3rd line has ;characters in ;data registers. The second set of semicolons
should be removed so it should read as ;characters in data registers.
64
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
3. A(n) ____________ is an address where the MPU is directed after the acknowledgment
of an interrupt.
(1) interrupt service routine (3) interrupt hardware
(2) interrupt vector (4) interrupt subroutine
5. The type of interrupts that can be disabled and must be enabled after a system reset is the
_______________.
(1) un-maskable (3) software
(2) hardware (4) maskable
6. The PIC18 microcontroller has ____________ interrupts divided into two groups.
(1) un-maskable (3) hardware
(2) maskable (4) software
7. The two groups of interrupts the PIC18 microcontroller has are the _____________ and
_______________.
(1) high priority; medium priority (3) high priority; low priority
(2) high priority; lesser priority (4) low priority; micro priority
8. The instructions to disable the interrupt priority scheme is BCF RCOM, IPEN
(1) True (2) False
65
9. The registers that are saved on the stack when an interrupt source with high priority
interrupts the MCU are the __________________________?
(1) WREG, STATUS, and BSR (3) STATUS and BSR
(2) WREG and STATUS (4) WREG and BSR
A free-running 16-bit timer has a clock frequency of 5-MHz. The counter register in the
timer is incremented every clock cycle. When the counter reaches FFFFH it rolls over to
0000H and continues to count. The timer reading at the beginning of the event is 1FF8H
and at the end of the event is 3380H.
10. Based on the information above, the clock period would be calculated as _________?
(1) 0.5-μs (3) 1.0-μs
(2) 0.2-μs (4) 1.5-μs
11. What would be the count between the two events expressed as a decimal value?
(1) 1319210 (3) 500010
(2) 818410 (4) 50010
12. The time delay between the two events would be ________?
(1) 2.6 ms (3) 1.4 ms
(2) 1.6 ms (4) 1.0 ms
13. In a pulse waveform, the on-time is 150-μs and the off-time is 300-μs. Calculate the duty
cycle of the waveform.
(1) 33% (3) 55%
(2) 41% (4) 60%
14. Based on the information given in Question 13, calculate the frequency of the waveform.
(1) 2.22-mHz (3) 2.22-kHz
(2) 222.2-Hz (4) 22.2-kHz
End of Examination
66
Assignment Checklist
Read Chapter 12 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8116 in this Study Guide
Take the Examination for this lesson
Chapter 12 Lecture/Discussion
Data Converters-Basic Concepts
• Analog signals are continuous, with infinite values in a given range.
– Examples: A clock face with hands, a voltmeter with a needle, and audio signals.
• Digital signals have discrete values such as on/off or 0/1.
– Examples: A digital clock or a digital voltmeter.
• Limitations of analog signals
– Analog signals pick up noise as they are being amplified.
– Analog signals are difficult to store.
– Analog systems are more expensive in relation to digital systems.
• Advantages of digital systems (signals)
– Noise can be reduced by converting analog signals in 0s and 1s.
– Binary signals of 0s/1s can be easily stored in memory.
– Technology for fabricating digital systems has become so advanced that they can
be produced at low cost.
• The major limitation of a digital system is how accurately it represents the analog signals
after conversion.
Embedded System
• A typical system that converts signals from analog to digital and back to analog (Figure
12-1) includes:
– A transducer that converts non-electrical signals into electrical signals
– An A/D converter that converts analog signals into digital signals
– A digital processor that processes digital data (signals)
– A D/A converter that converts digital signals into equivalent analog signals
– A transducer that converts electrical signals into real life non-electrical signals
(sound, pressure, and video)
• Temperature calculations
– A/D converter has 10-bit resolution
– For temperature range 0ºF to +100ºF, the digital output should be divided into
1023 steps (0 to 3FFH).
– Therefore, the digital value per degrees Fahrenheit is 10.23 (1023/100 = 10.2310).
– To obtain temperature reading from a digital reading of the A/D converter, the
digital reading must be divided by the factor of 10.23.
• Software modules
– Program should be divided into the following:
• Start a conversion and read the digital reading at the end of the conversion.
• Calculate the equivalent temperature reading.
• Convert the result in BCD.
• Convert the BCD numbers in ASCII code.
where RREF is a reference resistor and A1 to An can be either 0 or 1, A1 is the MSB and
An is the LSB. However, in a microprocessor, bit B7 (or D7) is the MSB, and in the
above formula A1/2 will be replaced by B7/2.
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
4. If a 12-bit A/D converter has an analog signal from 0 to +10-V determine the LSB.
(1) 5-V (3) 9.975-V
(2) 24.4-mV (4) 10-V
5. If a 12-bit A/D converter has an analog signal from 0 to +10-V determine the MSB.
(1) 5-V (3) 9.975-V
(2) 24.4-mV (4) 10-V
6. If a 12-bit A/D converter has an analog signal from 0 to +10-V determine the full-scale
output voltage.
(1) 5-V (3) 9.975-V
(2) 24.4-mV (4) 10-V
7. If the voltage range in Question 4 changed from -5-V to +5-V the value of the LSB
would _______________?
(1) increase (3) remain the same
(2) decrease (4) become 0-V.
8. If the reference voltages in PIC18 A/D modules are VREF- = 0-V and VREF+ = +5-V, what
is the output voltage for the digital value of 80H?
(1) 625-mV (3) 2.5-V
(2) 4.97-V (4) 0-V
73
9. Based on the information provided in Question 8 what is the output voltage for the digital
value of 03FAH?
(1) 4.97-V (3) 2.5-V
(2) 625-mV (4) 0-V
10. An A/D converter will convert an energy signal into its digital signal equivalent.
(1) True (2) False
11. If the reference voltages in PIC18 A/D module are VREF- = -1-V and VREF+ = +5-V, what
would be the value of the voltage per bit?
(1) -1-V (3) 5.859-mV
(2) +5-V (4) 2.5-mV
12. If a 12-bit A/D converter has an analog signal from -5-V to +5-V determine the MSB.
(1) -5 V (3) 4.975 V
(2) 0 V (4) 10 V
14. If the reference voltages in PIC18 A/D module are VREF- = -1-V and VREF+ = +5-V,
determine the output voltage for the digital value of 55H.
(1) -502 mV (3) -3.9 mV
(2) -498 mV (4) -1 V
15. If the reference voltages in PIC18 A/D module are VREF- = -1-V and VREF+ = +5-V,
determine the output voltage for the digital value of AAH.
(1) -498 mV (3) +4.9 V
(2) -3.9 mV (4) +6 V
End of Examination
74
Assignment Checklist
Read Chapter 9 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Chapter 13 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8118 in this Study Guide
Take the Examination for this lesson
Chapter 9 Lecture/Discussion
Basic Concepts in I/O Interfacing and PIC18 I/O Ports
• I/O devices (or peripherals) such as LEDs and keyboards are essential components of the
microprocessor-based or microcontroller-based systems.
– Classified into two groups
• input devices
• output devices
• Input devices
– Provide digital information to an MPU
– Examples: switch, keyboard, scanner, and digital camera
• Output devices
– Receive digital information from an MPU
– Examples: LED, seven-segment LED, LCD, and printer
• I/O devices interfaced (connected) to an MPU using I/O ports as shown in Figure 9-1.
****Note Figure 9-5 on Page 266 mislabeled the Logic Diagram as it should say “Common
Anode” at the top of the diagram rather than common cathode.
• FIGURE 9-9 (a) shows that turning off the internal FET is equivalent to providing a pull-
up resistor.
• Bit7 (RBPU) in the INTCON2 register enables or disables the pull-up resistor (Figure 9-9
b).
• Instruction to Enable Pull Up Resistors:
BCF INTCON2 7, 0
****Note on Page 279 of Figure 9-10 (b) the caption is incorrect as it should read “Push-Button
Key Bounce”
****Note on Page 284 line 3 of 9.5.3 Program contains an error. Line 3 should be
BCF INTCON2, 7, 0
• Sets the data line DB7 low after the completion of the operation
– The MPU should always check whether DB7 is low before sending an instruction
or a data byte
– After the power up, DB7 cannot be checked for the first two initialization
instructions.
• Writing to or reading from LCD (Figure 9-15 b)
– The MPU:
• Asserts RS low to select IR
• Asserts RS high to select DR
• Reads from LCD by asserting the R/W signal high
• Writes into LCD by asserting the R/W signal low
• Asserts the E signal high and then low (toggles) to latch a data byte or an
instruction
• Software
– To write into the LCD, the program should:
• Send the initial instructions (commands) before it can check DB7 to set up
the LCD in the 4-bit or the 8-bit mode.
• Check DB7 and continue to check until it goes low.
• Write instructions to IR to set up the LCD parameters such as the number
of display lines and cursor status.
• Write data to display a message.
****Note on Page 294 of 9.6.3 Program under section LCD_DATA: line 1 should read
;Save ASCII code in TEMP register
****Note on Page 298 of 9.7.3 Program under section SETUP: line 3 should be
BCF INTCON2, 7, 0
80
Chapter 13 Lecture/Discussion
Serial I/O
• Parallel I/O
– The 8-bit microcontroller processes data in groups of 8 bits and transfers (copies)
data to peripherals using the entire data bus.
• This is called parallel I/O or parallel communication.
– However, it is not always possible to use the entire data bus to transfer data to and
from a peripheral or over long distance communication such as telephone lines.
• In serial I/O (or communication) one bit is transferred at a time over one line.
• To transfer data serially, one bit at a time, the microcontroller must convert its parallel
word into a stream of serial bits.
– Called parallel-to-serial conversion
• Similarly, to receive data serially, the microcontroller must receive one bit at a time and
convert a stream of serial bits into a parallel word.
– Called serial-to-parallel conversion
• Transmitter must let receiver know when it begins to transmit and when it
ends.
• Figure 13–1 (b) shows that the “Start” and “Stop” bits are included as a
part of the transmission.
– Typical application includes communication between a PC (personal computer)
and a modem at an agreed upon clock rate.
• Figure 13-1 (a) Synchronous Data Transfer
• Figure 13-1 (b) Asynchronous Data Transfer
Rate of Transmission
• Defined either in terms of bits per second or the unit called “baud”; sometimes they are
equal
• Baud and bits per second (BPS)
– In telecommunications and electronics, baud is a measure of the “signaling rate,”
which is the number of changes to the transmission medium per second in a
modulated signal.
– At slow speeds, only one bit of information is encoded in each electrical change.
In this case, the baud and the bits per second are equal.
– At higher speeds, multiple bits are encoded in one electrical change. Therefore,
data transmission rates are generally expressed in bits per second (bps).
– Technique is based on the principle that the characters (data) are transmitted as
either an even or odd number of 1s
– ASCII is a seven-bit alphanumeric code; the last bit, B7, of a byte is always zero,
and in some systems, the last bit is used for parity information.
Example: Parity Check
• The ASCII letter “Y—” 0101 1001 (59H)—has four 1s, which is even.
• If the transmission is set up with even parity, the letter “Y” is transmitted
as 59H. If the transmission is set up with odd parity, odd number of 1s
must be transmitted. Bit B7 is changed to 1 and the byte is sent as 1101
1001 (D9H).
• CheckSum
• Used when blocks of data are transmitted
• All bytes to be transmitted are added (without a carry), and 2’s complement of
sum is calculated, which is sent as the last byte, called checksum.
• Receiver program adds all the bytes including the checksum (2’s complement),
and if the sum is zero, the receiver accepts the data. Otherwise, the receiver
program generates an error message.
– Standard for serial binary data interconnection between DTE (data terminal
equipment) such as a computer or a terminal, and DCE (data communication
equipment) such as a modem or a router
– Standard defines electrical, mechanical, and functional characteristics for
interfacing communication equipment
• Negative logic
– Logic 1: –3 V to –25 V or Mark
– Logic 0: +3 V to + 25 V or Space.
• Connector
– DB25 subminiature with twenty-five pins
• Signals are divided into four groups
– Data, control, timing, and ground
• Signals are defined in reference to DTE (data terminal equipment)
• Modern equipment, including the PC, uses positive TTL/CMOS logic levels, and +5 V
power supply.
– These signals cannot be connected directly to EIA-232 interface circuitry.
– Signals need to be converted into negative logic and shifted to higher voltages by
using line drivers and line receivers called transceivers.
– Figure 13–2 shows minimum connections—transmit, receive, and ground—
required to connect a DTE and a DCE.
• Figure 13–2 shows that the transceiver changes:
– TTL voltage +3.4 V to EIA-232 level –9 V and again back to the TTL level at the
receiver side.
– Similarly, 0.2 V (logic 0) is changed to +9 V and back to 0.2 V at the receiver
side.
• The PC serial communication is primarily asynchronous.
• Many of the signals on the DB25 connector are unnecessary in asynchronous
communication.
• Seven to nine signals are necessary in asynchronous communication.
• DB9 connector has nine pins.
• Figure 13–3 shows the 25-pin and 9-pin connectors with the associated signals.
Data Transmission
• Serial Bit Transmission (Figure 13–5)
– Uses pin RC6 (TX) of PORTC
– Two types of protocols – 8-bit and 9-bit
– Once a data byte (character) is written into TXREG, bits are moved into transmit
shift register and clocked out onto TX pin
– Each byte is preceded by a Start bit and followed by a Stop bit
– Transmit shift register enables the MPU to write new data while the previous data
bits are being transmitted.
– TXSTA register (Figure 13–6) controls data transmission
– 9-bit protocol:
• Used in multiprocessor communication where a PC as a host
communicates with many microcontrollers
• Ninth bit must be placed in TX9D bit position of TXSTA register (Figure
13–6) before writing eight bits into TXREG register
Data Reception
• Serial Bit Reception (Figure 13–7)
– Uses pin RC7 of PORTC
– Can be either eight or nine data bits
• When USART detects Start bit, receive shift register receives either eight or nine bits,
one bit at a time.
• After checking Stop bit, USART moves data bits into buffer and then into RCREG.
• RCSTA register controls data reception (Figure 13–8).
• For the nine-bit reception, ninth bit is placed in RX9D-bit position of the RCSTA
register.
– The receive shift register and buffer enable the MPU to read data from RCREG
while receiving data in the receive shift register.
• When Bit4 is 1, it enables the interrupt, and when this bit is 0, it disables
the interrupt.
SPI Mode
• The SPI module transmits and receives data simultaneously in the synchronous mode.
• Creates a loop (Figure 13.10)
• Uses following first three pins, and fourth pin in the Slave mode of operation:
– Serial Data Out (SDO) – pin RC5/SDO on PORTC
– Serial Data In (SDI) – pin RC4/SDI/SDA on PORTC
– Serial Clock (SCK) – pin RC3/SCK/SCL on PORTC
– Slave Select (/SS) – Available pin on a PORT
• The SS pin is active low. It selects a slave device to communicate with; it
is generally used in configurations that call for multiple slaves. This pin
can be any available pin on an I/O port.
• Figure 13.10 is a simplified block diagram of the MSSP module.
88
• SSPSR
– It is a shift register; it shifts data out serially on SDO pin and receives data in on
SDI pin.
• SSPBUF
– It is a buffer register, used to write a byte to or read a byte from.
• After the exchange of a byte between the master and the slave, the module copies the
byte in the SSPBUF register, and the user program must read the SSPBUF register before
transmitting the next byte.
• The Write and Read operations must be performed for each byte; some of the data bytes
may not have any use in a given application. Therefore, the data transmission in the SPI
mode can be divided in three categories:
– Transmission: the master sends data and receives dummy data from the slave
– Transmission and Reception: both exchange data
– Reception: the master receives data and sends dummy data to the slave
– SSPOV <Bit6>
• Used to indicate an overflow in the Slave while receiving data. An
overflow occurs whenever the master writes a byte before the previous
byte has been read from the SSPBUF. If the SSPOV bit is set, it must be
cleared by the user program.
****Note on Page 423 of Example 13.5 in the section SPI_MS in the comments section move
the word pulse up one line to ;rising pulse. The word “rising” in line 7 should not be alone.
SPI Applications
• The SPI
– A synchronous serial protocol and requires only four signals (pins)
– Slower than the parallel mode, but many devices do not need high-speed
communication
• Many manufacturers have designed peripherals that are compatible with the SPI.
89
****Note on Page 436 in Example 13.9 the section TRANSMT has a small tab issue in line 4 of
the code. It has HERE: BRA HERE when it is more correct to express this line as
HERE: BRA HERE
Interfacing Serial EEPROM to the PIC18 MSSP Module in the SPI Mode
• In embedded systems, a microcontroller with limited memory can expand its memory
size by adding a serial EEPROM.
• Advantages
– Small size, low number of I/O pin requirement, byte level flexibility, low power
consumption, and low cost
– The primary benefit is that signal timings are handled by hardware rather than
software.
• This enables system to continue program execution while serial
communication is handled in the background.
• Problem statement
– Interface Microchip 25XX040 Serial EEPPROM to the PIC18F452/4520 MSSP
module in the SPI mode.
– Explain the serial communication process between the memory chip and the
microcontroller module.
91
Interfacing Serial EEPROM to the PIC18 MSSP Module in the I2C Mode
• The I2C Mode
– Two-wire interface consisting of SCL (Clock) and SDA (Data)
– Requires fewer pins than the SPI mode
– Advantages similar to those of the SPI mode
– Many devices, such as the I/O expander, D/A converter, and digital thermometers,
compatible with this bus, are available
– Primary benefit of using the MSSP module is the signal timings handled by
hardware rather than software
• Problem statement
– Interface the Microchip 24LC024 Serial EEPPROM to the PIC18F452/4520
MSSP module in the I2C mode.
– Explain the serial communication process between the memory chip and the
microcontroller module.
– Write instructions to transfer a byte from the microcontroller to the EEPROM.
• I2C Compatible Electrically Erasable PROM (EEPROM):
– The Microchip 24LC024 is an 8-pin device with 2 Kb memory organized as 256 x
8.
– The address range for 256 bytes is 00 to FFH, and the size of the page is 16 bytes.
– The data transfer between memory and the microcontroller MSSP module takes
place via the I2C bus.
– The bus requires only two signals (Figure 13–23): Serial Clock (SCL) and Serial
Data/Address (SDA).
– The power supply VCC range varies based on the version of the chip; the VCC
range for 24LC024 is 2.5 V – 5.5 V.
• It has three address lines A2–A0 that enable the user to address eight different EEPROM
chips in the circuit.
• The WP is a Write Protection pin; it should be tied to VCC to enable the write protection
or grounded to disable the function.
• The SDA and SCL are open drain pins, and require pull-up resistors as shown in Figure
13–23.
• The SDA is bidirectional, and data flow in both directions, from the master to a slave and
from a slave to the master.
• The master controls the clock and the direction of the data flow.
• Steps in Write Operation
– Send a Start bit
• Before sending the Start bit:
• Set the bit SEN <Bit 0> in the SSPCON2 register.
• Clear the SSPIF flag in the PIR1.
• Wait in the loop until the SSPIF flag is set.
– Send the control byte.
• Before sending the byte:
• Clear the SSPIF flag.
• Load the control byte (A0H) in SSPBUF.
• Wait in the loop until the SSPIF flag is set.
– Check if the Acknowledge bit (Bit ACKSTAT <6>) is cleared.
93
– The EEPROM device must acknowledge by pulling the SDA line low for the
ninth clock cycle.
– The control byte A0H is configured as follows (Figure 13-24):
• The code for the high-order nibble for this memory is A.
• The address bits are 0 because they are grounded.
• The bit <0> is low for the write operation.
– Send the address
• The 24LC024 EEPROM has only 256 bytes, and requires only one byte
address. Memory chips larger than 2 Kb would require a 16-bit or 2-byte
address. To send the address, follow the same procedure as in “Send the
Control Byte”.
– Send the data byte
• Once the address has been sent and acknowledged, the data byte can be
sent by following the same procedure as before.
– Send the Stop bit
• The procedure to send the Stop is similar to that of the Start bit except that
the bit set in the SSPCON2 register is PEN (Stop condition – Bit <2>).
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
1. The difference between serial I/O and parallel I/O is that serial I/O transfers _______ bit
or bits at a time and parallel I/O uses the entire ___________ to transfer data.
(1) one; MPU (3) several, data bus
(2) four; MPU (4) one, data bus
2. One advantage of serial I/O is that there are less bus lines resulting into smaller chips.
(1) True (2) False
5. The bit time for a fax machine that is transmitting data at 1200 bps is ______________?
(1) 0.83 ms (3) 83.3 ms
(2) 8.33 ms (4) 833 ms
6. A ________________ occurs when the Start and Stop bits improperly frame a character.
(1) framing error (3) transmission error
(2) parity error (4) overrun error
7. If a receiver receives the bits 0100 0001, and the transmission was set for even parity, the
receiver would generate a parity error message.
(1) True (2) False
11. The Hex code used to transmit the 7-bit character “a” with odd parity is _________?
(1) 61H (3) E1H
(2) C1H (4) None of the above
12. In reference to Question 11, if the receiver receives the following bits, 0110 0001, then it
would generate a parity error message.
(1) True (2) False
13. When a receiver and a transmitter operate on the same clock then they are in
_____________________ transmission.
(1) Asynchronous (3) Simplex
(2) Synchronous (4) Full Duplex
14. If the ASCII character “Z” is being transmitted at 28k baud, the bit time is __________?
(1) 17.8 μs (3) 0.357 ms
(2) 35.7 μs (4) More information is needed
End of Examination
96
Assignment Checklist
Read Chapter 14 of Fundamentals of Microcontrollers and Applications in Embedded
Systems (with the PIC18 Microcontroller Family)
Read Lesson 8120 in this Study Guide
Take the Examination for this lesson
Chapter 14 Lecture/Discussion
Features of Embedded Systems
• Specific dedicated purpose
• Space constraints
• Memory constraints
• Power constraints
• Trade-off between power and frequency
• Cost sensitivity
• Protection against software failure
• Real-time constraints
System Specifications
• The project should include the following features:
– Monitor time and room temperature
– Display the time and temperature on an LCD.
• If the temperature falls below a certain limit, the system should turn on a
heater.
• If the temperature goes above a certain limit, it should turn on a fan.
97
• Matrix LEDs
– Bright and very attractive displays
– Need display driver chip or driver would have to be written in software
– More expensive than other options
– Consume more power
• LCD display
– Comprise between price and the attractiveness
– An 8-bit or 4-bit parallel interface
– LCDs with a serial interface
• Power and frequency
– Microcontrollers are TTL logic compatible, and can run on a 5 V power supply.
Alternately, the unit can be battery operated.
– 5 V power supply
• The power line voltages are different in different countries.
– The transformers used to step down the power line voltages will have to be
selectable.
– A common practice is to step down the line voltage to unregulated 9 V and use a
semiconductor regulator chip to supply 5 V DC.
– In a battery-operated unit:
• Power consumption should be minimized
• Use a lower clock frequency
– Oscillator circuits
• An RC circuit and a crystal-driven circuit
• RC circuit is less expensive, but less reliable.
• Crystal driven is more reliable and stable.
• Final Specifications
– Temperature range: 0ºF to 99ºF
– Temperature sensor: semiconductor chip
– Clock: 12-hour
– Display: LCD
– Power supply: 5 V regulated
– Frequency: 10 MHz—crystal driven oscillator
– Clock setting: two switches
• Figure 14–1 shows block diagram of the above system.
Microcontroller Selection
• Pins requirement
– Based on specifications, the TTMS project includes an LCD module, a
temperature sensor, a fan, and a heater as output peripherals and switches for
interrupts.
• All of these devices require one pin each except the LCD.
• LCD interfacing: 8-bit interface, 4-bit interface, and serial LCD
• Size of the memory requirement
– Assembly language
– High-level language such as “C”
• Memory: R/W memory and read-only-memory (ROM)
• Questions:
– Once the product is shipped, does the user need to modify any information to run
the unit?
– Does the MPU need any memory for temporary storage during the execution of
the program?
– Is there a need for the user input?
• The TTMS unit
– Requires input from the user to set the time
– Is expected to include subroutines and the use of data registers
• Both types of memory needed
– ROM to store permanent programs
– R/W to store the inputs from the user
• ROM choices
– Masked ROM becomes cost effective
• If the production quantities are large (in thousands)
• The product does not anticipate software changes in near future
– Product designer needs to depend on a memory manufacturer that may require a
six- to twelve-week waiting period.
• OTP - one-time programmable
– Can be programmed in-house
– Somewhat cheaper than flash memory
– Cannot be reprogrammed
– Flash memory
• Can be programmed in-house
• Can be reprogrammed (1000 to 100 k erase/write cycles)
– EPROM: erasable programmable read-only-memory
• Almost obsolete because it needs to be taken out of circuit board to be
reprogrammed and exposed to ultraviolet light to be erased
– EEPROM: electrically erasable PROM
• Can be easily reprogrammed a byte at a time in the field from a remote
location
• More expensive than flash
• TTMS project may need:
– Less than 1 K of flash memory
– Fewer than 64 data registers (R/WM)
100
– No EEPROM
• Cost
– Presently, the price of an 8-bit microcontroller in quantity ranges from less than
fifty cents to more than six dollars.
– If a microcontroller meets the pin and memory requirements, the least expensive
microcontroller can be selected.
• Other considerations
– Availability of troubleshooting tools and technical support from the manufacturer
of the selected microcontroller
– In-house capability
– Availability of secondary sources
• Clock frequency directly affects power consumption, and many special features are
available in this microcontroller family to reduce the power consumption.
Figure 14-4 Connecting a Temperature Sensor to A/D Converter Input through a Scaling Circuit
****Note on Page 471 the caption for Figure 14-4 is incorrect. This is the correct wording.
• 8-bit interface
• 4-bit interface using high-order four data lines <7-4>
• 4-bit interface using low-order four data lines <3-0>
– 8-bit data interface requires less programming, but uses 11 (8 data lines and 3
control signals) I/O pins.
– 4-bit data interface uses only 7 I/O pins, but requires more programming.
– Figure 14-5 shows 8-bit interface.
• Circuit uses eight pins of PORTD for LCD data lines, and three pins from
PORTA for control signals.
****Note on Page 481 the Instructions check listed has a couple of errors. In the section
FANON: on line 2 it should be BCF PORTB, RB7
Answer the following questions based on what has been presented or discussed in the textbook
and study guide. To submit the exam please use either Word or Excel to create a two column
answer sheet. The left column is the question number and the right column would be your
answer. On the answer sheet please include your name, student number, examination/lesson
number and an email address so we can return your results. If you have questions on creating an
answer sheet or attaching the answer sheet please contact the Instruction Department at 1-800-
243-6446 or [email protected].
2. The constraint of space with the design of embedded systems stems from _________.
(1) the area the system is stored in (3) the placement of the power supply
(2) limits with the printed circuit board (4) the physical size limitations
3. The real-time embedded systems are divided into which two categories?
(1) time-critical & time-stamp (3) strict time limit & time sensitive
(2) time-sensitive & time-critical (4) real & time
4. One of the design steps that are required in the development of embedded systems that is
different from designing other electronic systems is ________________________?
(1) the partitioning of tasks between hardware and software at the beginning stage of the
design cycle
(2) the integration of accessibility for the end user
(3) maximizing the power requirements
(4) designing the system for memory sizes approaching 1 Giga bytes
6. What is the gain of the A/D converter when the temperature is 0 °F?
(1) 1.428 (3) 2
(2) 1.667 (4) 10
108
7. What is the input voltage to the A/D converter when the temperature is 0 °F?
(1) 714 mV (3) 1 V
(2) 833 mV (4) 4.28 V
8. What is the temperature reading when the converted digital reading is 32H?
(1) -50 °F (3) +125 °F
(2) -32.9 °F (4) +300 °F
9. What is the temperature reading when the converted digital reading is 200H?
(1) -50 °F (3) +125 °F
(2) -32.9 °F (4) +300 °F
10. When the temperature is +100 °F what is the digital Hex reading?
(1) 92H (3) 1B6H
(2) 124H (4) 2DBH
12. Another consideration for the design of the TTMS (Time and Temperature Monitoring
System) unit is to change the transformer ratio and the shape of the wall socket if marked
outside of the United States.
(1) True (2) False
13. The TTMS operates the fan and heater and these two devices are controlled by two
output bits. The bit ______ controls the fan and the bit _______ controls the heater.
(1) RB7, RB1
(2) RB1, RB7
(3) RB6, RB7
(4) RB7, RB6
14. In Figure 14-2, the function of the diode connected to a battery source is __________?
(1) to filter the AC from the bridge circuit
(2) to protect the fuse
(3) to protect the circuitry in the event the battery is connected with the reversed polarity
as the diode will conduct
(4) to protect the output voltages (VDD and VSS)
End of Examination
109