Download presentation
Presentation is loading. Please wait.
1
Microprocessor and Microcontroller Fundamentals
Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 1
2
Microcontrollers Embedded Systems Microcontroller (MCU)
Operations managed behind the scenes by a microcontroller Microcontroller (MCU) Integrated electronic computing device that includes three major components on a single chip Microprocessor (MPU) Memory I/O (Input/Output) ports 330_01 2 ELEC 330 2
3
Microcontrollers Support Devices Common communication lines Timers
A/D converter Serial I/O Common communication lines System Bus 330_01 3 ELEC 330 3
4
Block Diagram 330_01 4 ELEC 330 ELEC 330 4
5
Microprocessor (MPU) MPU (CPU) Read instructions Process binary data
330_01 5 ELEC 330 5
6
Memory Storage Device Major Categories Read/Write Memory (R/W)
Addresses Registers Major Categories Read/Write Memory (R/W) Read-only-Memory (ROM) D7 D0 330_01 6 ELEC 330 6
7
Input/Output (I/O) Input Devices Output devices Switches and Keypads
Provide binary information to the MPU Output devices LEDs and LCDs Receive binary information from the MPU 330_01 7 ELEC 330 7
8
Microprocessor-Based Systems
330_01 8 ELEC 330 8
9
Microprocessor Architecture
MPU communicates with Memory and I/O using the System Bus Address bus Unidirectional Memory and I/O Addresses Data bus Bidirectional Transfers Binary Data and Instructions Control lines Read and Write timing signals 330_01 9 ELEC 330 9
10
Microprocessor-Based System
330_01 10 ELEC 330 10
11
Example Microprocessor System
330_01 11 ELEC 330 11
12
Software Machine Language Binary Instructions
Difficult to decipher and write Error-prone All programs converted into machine language for execution Instruction Hex Mnemonic Description Processor 80 ADD B Add reg B to Acc Intel 8085 28 ADD A, R0 Add Reg R0 to Acc Intel 8051 1B ABA Add Acc A and B Motorola 6811 330_01 12 ELEC 330 12
13
Software Assembly Language
Machine instructions represented in mnemonics One-to-one correspondence Efficient execution and use of memory Machine-specific 330_01 13 ELEC 330 13
14
Software High-Level Languages BASIC, C, and C++
Written in statements of spoken languages Machine independent Easy to write and troubleshoot Larger memory and less efficient execution 330_01 14 ELEC 330 14
15
Data Format (8-bit) Unsigned Integers
All eight bits represent the magnitude of a number Bit7 to Bit0 Range 00H to FFH (010 to 25510) 330_01 15 ELEC 330 15
16
Data Format (8-bit) Signed Integers 2's Complement
Bit7 is sign bit Positive numbers: 00H to 7FH (010 to 12710) Negative numbers: 80H to FFH (-110 to ) 330_01 16 ELEC 330 16
17
Data Format (8-bit) Binary Coded Decimal Numbers (BCD)
8-bit number divided into two groups of four Each group represents a decimal digit from 0 to 9 AH through FH are invalid Example: BCD = 2510 330_01 17 ELEC 330 17
18
Data Format (8-bit) American Standard Code for Information Interchange (ASCII) 7-bit alphanumeric code with 128 combinations (00H to 7FH) Represents English alphabet, decimal digits from 0 to 9, symbols, and commands 330_01 18 ELEC 330 18
19
MPU-Based Systems System hardware System software Discrete components
Microprocessor, Memory, and I/O Components connected by buses Address, Data, and Control System software Group of programs that monitors the functions of the entire system 330_01 19 ELEC 330 19
20
MPU-Based System 330_01 20 ELEC 330 20
21
MCU-Based Systems Microprocessor, memory, I/O ports, and support devices on a single chip Buses generally not available to a system designer I/O ports generally multiplexed and can be programmed to perform different functions 330_01 21 ELEC 330 21
22
MCU-Based System 330_01 22 ELEC 330 22
23
Computer Architectures
Princeton versus Harvard Architecture CISC versus RISC processors Microprocessors and Microcontrollers 330_01 23 ELEC 330 ELEC 330 23
24
Microcontroller Architecture PIC18F Family
ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 24
25
PIC18F Microcontrollers
Microcontroller Unit (MCU) Microprocessor unit (MPU) Harvard Architecture Program memory for instructions Data memory for data I/O ports Support devices such as timers 330_02 25 ELEC 330 25
26
Microcontroller Unit 330_02 26 ELEC 330 26
27
PIC18F – MPU and Memory 330_02 27 ELEC 330 27
28
Microprocessor Unit Includes Arithmetic Logic Unit (ALU), Registers, and Control Unit Arithmetic Logic Unit (ALU) Instruction decoder 16-bit instructions Status register that stores flags 5-bits WREG – working register 8-bit accumulator 330_02 28 ELEC 330 28
29
Microprocessor Unit Registers Control unit Program Counter (PC)
21-bit register that holds the Program Memory address Bank Select Register (BSR) 4-bit register used in direct addressing the Data Memory File Select Registers (FSRs) 12-bit registers used as memory pointers in indirect addressing Data Memory Control unit Provides timing and control signals Read and Write operations 330_02 29 ELEC 330 29
30
PIC18F - Address Buses Address bus
21-bit address bus for Program Memory Addressing capacity: 2 MB 12-bit address bus for Data Memory Addressing capacity: 4 KB 330_02 30 ELEC 330 30
31
Data Bus and Control Signals
16-bit instruction/data bus for Program Memory 8-bit data bus for Data Memory Control signals Read and Write 330_02 31 ELEC 330 31
32
PIC18F452/4520 Memory Program Memory: 32 K Data Memory: 4 K
Address range: 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 330_02 32 ELEC 330 32
33
PIC18F452/4520 Memory Program Memory Data Memory 330_02 33 ELEC 330 33
34
Data Memory Banks 330_02 34 ELEC 330 34
35
PIC18F452 I/O Ports Five I/O ports PORT A through PORT E
Most I/O pins are multiplexed Generally have eight I/O pins Addresses already assigned to these ports Each port is identified by its assigned SFR 330_02 35 ELEC 330 35
36
I/O Ports A and B 330_02 36 ELEC 330 36
37
Data Transfer 330_02 37 ELEC 330 37
38
MCU Support Devices Timers Serial Communications A/D converter
Capture, Compare and PWM (CCP Modules) Serial Communications Master Synchronous Serial Port (MSSP) Addressable USART A/D converter Parallel Slave Port (PSP) Data EEPROM 330_02 38 ELEC 330 38
39
MCU Support Devices 330_02 39 ELEC 330 39
40
PIC18F Special Features Sleep mode Watchdog timer (WDT)
Code protection In-circuit serial programming In-circuit debugger 330_02 40 ELEC 330 40
41
PIC18F4X2 Architecture Block Diagram
41 ELEC 330 41
42
PIC18F452 Programming Model
330_02 42 ELEC 330
43
330_02 43 ELEC 330 43
44
PIC18F Instructions 77 assembly language instructions
Earlier PIC families have 33 or 35 instructions PIC18F instruction set Most instructions are 16-bit word length Four instructions are 32-bit length 330_02 44 ELEC 330 44
45
Instruction Descriptions
Copy (Move) 8-bit number (Literal) into W register Mnemonics: MOVLW 8-bit Binary format: XXXX XXXX (any 8-bit number) Copy (Move) contents of W register into PORTC (File) Mnemonics: MOVWF PORTC, a (‘a’ indicates that PORTC is in the Access Bank) (82H is PORTC address) 330_02 45 ELEC 330 45
46
Illustrative Program Problem statement: Hardware:
Write instructions to light up alternate LEDs at PORTC Hardware: PORTC Bidirectional (input or output) port Setup as output port for display Logic 1 will turn on an LED 330_02 46 ELEC 330 46
47
Illustration Interfacing LEDs to PORTC 330_02 47 ELEC 330 47
48
Illustration Program (software)
Logic 0 to TRISC sets up PORTC as an output port Byte 55H turns on alternate LEDs MOVLW 00 ;Load W register with 0 MOVWF TRISC ;Set up PORTC as output MOVLW 0x55 ;Byte 55H to turn on LEDS MOVWF PORTC ;Turn on LEDs SLEEP ;Power down 330_02 48 ELEC 330 48
49
Illustration Address Hex Mnemonics Comments 000000 0E00 MOVLW 00
;Load W with 0s 000002 6E94 MOVWF TRISC ;Set PORTC as output 000004 0E55 MOVLW 0x55 ;Load 55 to turn on LEDs 000006 6E82 MOVWF PORTC ;Turn on LEDs 000008 0003 SLEEP ;Power Down 330_02 49 ELEC 330 ELEC 330 49
50
Illustration Execution of the instruction: MOVWF PORTC 330_02 50
ELEC 330 50
51
PIC18 Simulator IDE 330_02 51 ELEC 330 ELEC 330 51
52
Embedded System MCU-based System 330_02 52 ELEC 330 52
53
PIC18F Programming Model and Instruction Set
ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 53
54
Review: MPU and Memory 330_02 54 ELEC 330 54
55
Review: Data Memory 330_02 55 ELEC 330 55
56
PIC18F Programming Model
Representation of the internal architecture necessary to write assembly language programs Divided into two groups Arithmetic Logic Unit (ALU) and Registers From Microprocessor Unit (MPU) Special Function Registers (SFRs) From Data (File) Memory 330_03 56 ELEC 330 56
57
PIC18F Programming Model
330_03 57 ELEC 330 57
58
ALU Instruction Decoder Table Latch STATUS: Flag Register
16-bit Instructions Table Latch 8-bit Data STATUS: Flag Register 5 individual bits called flags WREG (W): Working Register 8-bit Accumulator Product 16-bit Product of 8-bit by 8-bit Multiply 330_03 58 ELEC 330 58
59
Flags in Status Register
N (Negative Flag) Set when bit B7 is one as the result of an arithmetic/logic operation OV (Overflow Flag) Set when result of an operation of signed numbers goes beyond 7-bits Z (Zero Flag) Set when result of an operation is zero DC (Digit Carry Flag) (Half Carry) Set when carry generated from Bit3 to Bit4 in an arithmetic operation C (Carry Flag) Set when an addition generates a carry (out) 330_03 59 ELEC 330 59
60
Registers Program Counter (PC)
21-bit register used as a pointer to Program Memory during program execution Table Pointer 21-bit register used as a pointer to copy bytes between Program Memory and data registers Stack Pointer (SP) 5-bit register used to point to the stack Stack 31 registers used for temporary storage of memory addresses during execution of a subroutines 330_03 60 ELEC 330 60
61
Registers BSR: Bank Select Register (0H to FH)
4-bit Register Provides upper 4-bits of 12-bit address of Data Memory FSR: File Select Registers FSR0, FSR1, and FSR2 FSR: composed of two 8-bit registers FSRH and FSRL Used as pointers for Data Memory Holds 12-bit address of data register 330_03 61 ELEC 330 61
62
Special Function Registers
SFRs: Table 3-1 Data registers associated with I/O ports, support devices, and processes of data transfer I/O Ports (A to E) Interrupts EEPROM Serial I/O Timers Capture/Compare/PWM (CCP) Analog-to-Digital (A/D) Converter 330_03 62 ELEC 330 ELEC 330 62
63
PIC18 Instruction Set Includes 77 instructions
73 one word (16-bit) long 4 two words (32-bit) long Divided into seven groups Move (Data Copy) and Load Arithmetic Logic Program Redirection (Branch/Jump) Bit Manipulation Table Read/Write Machine Control 330_03 63 ELEC 330 63
64
Addressing Modes Method of specifying of an operand
Immediate (Literal) addressing The operand is a number that follows the opcode Direct addressing The address of the operand is a part of the instruction Indirect addressing An address is specified in a register (pointer) and the MPU looks up the address in that register 330_05 64 ELEC 330 64
65
Move and Load Instructions
MOVLW 8-bit ;Load an 8-bit Literal into WREG MOVLW 0xF2 ;Load F2H into W MOVWF F,a ;Copy WREG into File (Data) Reg. ;If a = 0, F is in Access Bank ;If a = 1, Bank is specified by BSR MOVWF 0x25,0 ;Copy W into F Reg25H MOVWF 0x25 ;Alternate format MOVFF fs,fd ;Copy from one File Reg. to ;another File Reg. MOVFF 0x20,0x30 ;Copy F Reg20H into Reg30H 330_03 65 ELEC 330 65
66
Move and Load Instructions
MOVF F,d,a ;Copy F into itself or W ;If d = 0 (or W), destination is W ;If d = 1 (or F), destination is F ;Affects N & Z flags MOVF 0x25,0,0 ;Copy F Reg25H into W MOVF 0x25,W ;Alternate format CLRF F,a ;Clear F Reg. ;Sets Z flag CLRF 0x25 ;Clear Reg25H SETF F,a ;Sets all bits to 1 in F Reg. 330_03 66 ELEC 330 66
67
Points to Remember Each instruction has two parts
Opcode and Operand When instructions copy data from one register to another, the source is not modified In general, these instructions do not affect flags Except CLRF and MOVF 330_03 67 ELEC 330 ELEC 330 67
68
Arithmetic Instructions
ADDLW 8-bit ;Add 8-bit number to W & set flags ADDLW 0x32 ;Add 32H to W ADDWF F,d,a ;Add W to F & set flags ;Save result in W if d = 0 (or W) ;Save result in F if d = 1 (or F) ADDWF 0x20,0 ;Add W to REG20H and ;save result in W ADDWF 0x20,W ;Alternate format ADDWF 0x20,1 ;Add W to REG20H and ;save result in REG20H ADDWF 0x20,F ;Alternate format 330_03 68 ELEC 330 68
69
Arithmetic Instructions
ADDWFC F,d,a ;Add W to F with carry ;and save result in W or F SUBLW 8-bit ;Subtract W from literal SUBWF F,d,a ;Subtract W from F SUBWFB F,d,a ;Subtract W from F with borrow INCF F,d,a ;Increment F DECF F,d,a ;Decrement F NEGF F,a ;Take 2’s Complement of F 330_03 69 ELEC 330 69
70
Arithmetic Instructions
MULLW 8-bit ;Multiply 8-bit Literal and W ;Save result in PRODH:PRODL MULWF F,a ;Multiply W and F ;Save result in PRODH:PRODL DAW ;Decimal adjust W for BCD ;Addition 330_03 70 ELEC 330 70
71
Points to Remember Arithmetic instructions
Can perform operations on W and 8-bit literals Save the result in W Can perform operations an W and F Save the result in W or F In general, affect all flags 330_03 71 ELEC 330 ELEC 330 71
72
Logic Instructions COMF F,d,a ;Complement (NOT) F
;and save result in W or F ANDLW 8-bit ;AND Literal with W ANDWF F,d,a ;AND W with F and ;save result in W or F IORLW 8-bit ;Inclusive OR Literal with W IORWF F,d,a ;Inclusive OR W with F ;and save result in W or F IORWF 0x12,F ;OR W with REG12H and ;save result in REG12H XORLW 8-bit ;Exclusive OR Literal with W XORWF F,d,a ;Exclusive OR W w/ F ;and save result in W or F 330_03 72 ELEC 330 72
73
Points to Remember Logic instructions
Can perform operations on W and 8-bit literals Save the result in W Can perform operations an W and F Save the result in W or F In general, affect only two flags: N and Z 330_03 73 ELEC 330 ELEC 330 73
74
Branch Instructions BC n ;Branch if C flag = 1, + or – 64 Words
;to PC+2+2n BC 5 ;Branch on Carry to PC+2+10 BC Label ;Alternate: Branch to Label BNC n ;Branch if C flag = 0 BZ n ;Branch if Z flag = 1 BNZ n ;Branch if Z flag = 0 BN n ;Branch if N flag = 1 BNN n ;Branch if N flag = 0 BOV n ;Branch if OV flag = 1 BNOV n ;Branch if OV flag = 0 BRA nn ;Branch always, + or – 512 Words 330_03 74 ELEC 330 74
75
Branch Example Address Label Opcode Operand Comment 000020 START:
MOVLW BYTE1 ;Load BYTE1 into W 000022 MOVWF REG0 ;Save into REG0 000024 BYTE2 ;Load BYTE2 into W 000026 REG1 ;Save into REG1 000028 ADDWF REG0,W ;Add REG0 to REG1 00002A BNC SAVE ;Branch if no carry 00002C 0x00 ;Clear W 00002E SAVE: REG2 ;Save Result 000030 SLEEP 330_03 75 ELEC 330 ELEC 330 75
76
Call and Return Instructions
RCALL nn ;Relative Call subroutine ;within + or – 512 words CALL 20-bit,s ;Call subroutine ;If s = 1, save W, STATUS, BSR RETURN s ;Return subroutine ;If s = 1, retrieve W, STATUS, BSR RETFIE s ;Return from interrupt 330_03 76 ELEC 330 76
77
Points to Remember Eight conditional relative branch instructions
Based on four flags Range is 64 words Unconditional relative branch instruction Range is 512 words If the operand is positive, the jump is forward If negative, the jump is backward 330_03 77 ELEC 330 ELEC 330 77
78
Bit Manipulation Instructions
BCF F,b,a ;Clear bit b of F, b = 0 to 7 BCF 0x2,7 ;Clear bit 7 of Reg2 BSF F,b,a ;Set bit b of F, b = 0 to 7 BTG F,b,a ;Toggle bit b of F, b = 0 to 7 RLCF F,d,a ;Rotate bits left in F through ;carry and save in W or F RLNCF F,d,a ;Rotate bits left in F ;and save in W or F RRCF F,d,a ;Rotate bits right in F through RRNCF F,d,a ;Rotate bits right in F 330_03 78 ELEC 330 78
79
Points to Remember Any bit in a File (data) register
Set, reset, or complemented There are two types of rotate instructions 8-bit and 9-bit (include C) Any file (data) register can be rotated left or right Saved in W or F 330_03 79 ELEC 330 ELEC 330 79
80
Test and Skip Instructions
BTFSC F,b,a ;Test bit b in F and skip the ;next instruction if bit is cleared (bit=0) BTFSC 0x2,7 ;Test bit B7 in REG2 ;if B7=0 then skip next instruction BTFSS F,b,a ;Test bit b in F and skip the ;next instruction if bit is set (bit=1) CPFSEQ F,a ;Compare F with W, skip if F = W CPFSGT F,a ;Compare F with W, skip if F > W CPFSLT F,a ;Compare F with W, skip if F < W TSTFSZ F,a ;Test F, skip if F = 0 330_03 80 ELEC 330 80
81
Increment/Decrement and Skip Next Instruction
DECFSZ F,d,a ;Decrement F and skip the ;next instruction if F = 0 DECFSNZ F,d,a ;Decrement F and skip the ;next instruction if F ≠ 0 INCFSZ F,d,a ;Increment F and skip the ;next instruction if F = 0 INCFSNZ F,d,a ;Increment F and skip the ;next instruction if F ≠ 0 330_03 81 ELEC 330 81
82
Points to Remember Any File (data) register or single bit in a File (data) register can be tested for 0 A File (data) register can be compared with W for equality, greater than, and less than A File (data) register can be incremented or decremented and tested for 0 If a condition is met, the next instruction is skipped (no flags are affected) 330_03 82 ELEC 330 ELEC 330 82
83
Table Read/Write Instructions
TBLRD* ;Read Program Memory pointed by TBLPTR ;into TABLAT TBLRD*+ ;Read Program Memory pointed by TBLPTR ;into TABLAT and increment TBLPTR TBLRD*- ;Read Program Memory pointed by TBLPTR ;into TABLAT and decrement TBLPTR TBLRD+* ;Increment TBLPTR and Read Program ;Memory pointed by TBLPTR into TABLAT 330_03 83 ELEC 330 83
84
Table Read/Write Instructions
TBLWT* ;Write TABLAT into Program Memory pointed ;by TBLPTR TBLWT*+ ;Write TABLAT into Program Memory pointed ;by TBLPTR and increment TBLPTR TBLWT*- ;Write TABLAT into Program Memory pointed ;by TBLPTR and decrement TBLPTR TBLWT+* ;Increment TBLPTR and Write TABLAT into ;Program Memory pointed by TBLPTR 330_03 84 ELEC 330 84
85
Machine Control Instructions
CLRWDT ;Clear Watchdog Timer RESET ;Reset all registers and flags SLEEP ;Go into standby mode NOP ;No operation 330_03 85 ELEC 330 85
86
Instruction Format The PIC18F instruction format divided into four groups Byte-Oriented operations Bit-Oriented operations Literal operations Branch operations 330_03 86 ELEC 330 86
87
Instruction Format Byte-oriented instruction – ADDWF F, d, a
ADDWF 0x1,F ;Add W to REG1, save in REG1 Bit-oriented instruction – BCF F, b, a BCF 0x15,7 ;Clear bit7 in REG15H 330_03 87 ELEC 330 87
88
Instruction Format Literal instruction — MOVLW k
MOVLW 0x7F ;Load 7FH into W Branch instruction — BC n BC 0x15 ;Branch if carry +15H words 330_03 88 ELEC 330 88
89
Illustration: Addition
Problem Statement Load two bytes (37H and 92H) in registers REG0 and REG1 Add the bytes and store the sum in REG2 Address Hex Opcode Operand Comments 0020 0E37 MOVLW 0x37 ;Load first byte in W 0022 6E00 MOVWF REG0 ;Save first byte in REG0 0024 0E92 0x92 ;Load second byte in W 0026 6E01 REG1 ;Save second byte in REG1 0028 2400 ADDWF REG0,W ;Add bytes and save sum in W 002A 6E02 REG2 ;Save sum in REG2 002C 0003 SLEEP ;Power Down 330_03 89 ELEC 330 ELEC 330 89
90
Bus Contents Execution of the instruction: MOVLW 0x37 330_03 90
ELEC 330 90
91
Pipeline Fetch and Execution
330_03 91 ELEC 330 91
92
Programming and Problem Solving
ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 92
93
Problem Solving Modular Design Subroutines Flowchart
Divide problem into various tasks Subroutines Independent units that can be combined Flowchart Graphical representation of processes (tasks) Sequence to be followed 330_04 93 ELEC 330 93
94
Flowcharting 330_04 94 ELEC 330 94
95
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 two bytes in data registers REG0 and REG1 Add the bytes Save the sum in data register REG2 330_04 95 ELEC 330 95
96
Example 4.1 330_04 96 ELEC 330 96
97
Assembly Language Programming
Analyze the problem Draw a flowchart Convert the flowchart into mnemonics Look up Hex code and assign memory addresses Enter the Hex code into memory Execute the program Debug the program if necessary 330_04 97 ELEC 330 97
98
Illustrative Program Addition With Carry Check
Write instructions to load two bytes (F2H and 32H) in data registers REG0 and REG1 and add the bytes. If the sum generates a carry, clear the data register REG2; otherwise, save the sum in REG2. 330_04 98 ELEC 330 98
99
Illustrative Program 330_04 99 ELEC 330 99
100
Assembly Language Label Opcode Operand Comments START: MOVLW 0xF2
;Load first byte in W MOVWF REG0 ;Save first byte in REG0 0x32 ;Load second byte in W REG1 ;Save second byte in REG1 ADDWF REG0,W ;Add bytes and save sum in W BNC SAVE ;Branch if no carry ;Clear W SAVE: REG2 ;Save sum in REG2 SLEEP ;Power Down 330_04 100 ELEC 330 ELEC 330 100
101
Code Look-up? Address Hex Label Opcode Operand 000020 0EF2 START:
MOVLW 0xF2 000022 6E00 MOVWF REG0 000024 0E32 0x32 000026 6E01 REG1 000028 2400 ADDWF REG0,W 00002A E301 BNC SAVE 00002C 0E00 00002E 6E02 SAVE: REG2 000030 0003 SLEEP 330_04 101 ELEC 330 ELEC 330 101
102
Execute the Program Hardware Simulation MPLAB IDE
PICDEM 2 Plus Demo Board Microchip PIC18F4520 MPU MPLAB ICD2 Simulation PIC18 Simulator IDE Microchip PIC18F452 MPU 330_04 102 ELEC 330 ELEC 330 102
103
PIC18 Simulator IDE Integrated Development Environment Editing
Assembling Linking Downloading Executing Simulation Debugging 330_04 103 ELEC 330 ELEC 330 103
104
Using an Assembler Assembly Language Programs
Program in Mnemonics Assembler Directives Comments Assembly Language Statements (4 fields) Label Opcode Operand (data, register, or memory address) Comment 330_04 104 ELEC 330 104
105
Assembly Language Format
Label Opcode Operand Comment START: MOVLW 0xF2 ;Load F2H in W ↑ ↑ ↑ ↑ Tab Tab Tab Semicolon 330_04 105 ELEC 330 105
106
Assembler Directives #INCLUDE Include resources from library
ORG Origin EQU Equate DB Data byte END End of assembly 330_04 106 ELEC 330 106
107
Radix Formats Type Format Hexadecimal 0x4F H’4F’ 4F 4FH Decimal D’100’
Binary B’ ’ ASCII ‘Susan’ A’Susan’ 330_04 107 ELEC 330 ELEC 330 107
108
PIC18 Simulator IDE Editor Debugger Simulator I/O Modules Assembler
Breakpoint Manager Watch Variables Simulator Run and Step I/O Modules LED Board 7-Segment Display Keypad Matrix 330_04 108 ELEC 330 108
109
Assembler (Editor) 330_04 109 ELEC 330 ELEC 330 109
110
Understanding the List File
List file generated primarily for documentation Includes seven columns Line numbers Memory addresses where binary code is stored Hex code Contents of source file Labels Opcode Operands Comments 330_04 110 ELEC 330 110
111
Assembler Listing 330_04 111 ELEC 330 ELEC 330 111
112
Executing a Program 330_04 112 ELEC 330 112
113
Debugging a Program Single-step technique Breakpoint technique
Execute one instruction at a time Observe registers for expected results Breakpoint technique Execute a group of instructions 330_04 113 ELEC 330 113
114
Breakpoint Manager Simulation automatically switches to step-by-step mode when reaching a breakpoint 330_04 114 ELEC 330 ELEC 330 114
115
Special Breakpoints Switch to step-by- step when the value of a register has changed or equals a predefined value 330_04 115 ELEC 330 ELEC 330 115
116
Assembly Language Instructions
ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 116
117
Data Copy Operations Load 8-bit data directly in WREG
Copy data between WREG and data (file) register including I/O ports Copy data from one data (file) register to another data (file) register Clear or set all data bits in data (file) register Exchang low-order four bits (nibble) with high- order four bits in data (file) register 330_05 117 ELEC 330 117
118
Frequently Used Registers
330_05 118 ELEC 330 118
119
Addressing Modes Method of specifying of an operand
Immediate (Literal) addressing The operand is a number that follows the opcode Direct addressing The address of the operand is a part of the instruction Indirect addressing An address is specified in a register (pointer) and the MPU looks up the address in that register 330_05 119 ELEC 330 119
120
MOV (Copy) Operations Instructions MOVLW 8-bit MOVWF F,a MOVF F,d,a
MOVFF Fs,Fd Examples MOVLW 0xF2 Load F2H into WREG MOVWF REG1 Copy WREG into REG1 MOVF REG1,F Copy REG1 into REG1 MOVF REG1,W Copy REG1 into WREG MOVFF REG1,REG2 Copy REG1 into REG2 330_05 120 ELEC 330 120
121
SET/CLR Instructions Instructions CLRF F,a SETF F,a SWAPF F,d,a
Examples CLRF REG1 Clear REG1 SETF REG1 Set all bits in REG1 SWAPF REG1,F Exchange low and high nibbles in REG1 330_05 121 ELEC 330 121
122
Points to Remember When instructions copy data from one register to another, the source is not modified In general, these instructions do not affect flags Except CLRF and MOVF F The letter “d” represents the destination If d = 0 or W, result saved in WREG If d = 1 or F, result saved in File (data) register 330_03 122 ELEC 330 ELEC 330 122
123
File Select Registers as Pointers
Three registers: FSR0, FSR1, and FSR2 LFSR F,12-bit Load 12-bit address into FSR Each can be used in five different formats INDF0 Use FSR0 as pointer POSTINC0 Use FSR0 as pointer and increment FSR0 POSTDEC0 Use FSR0 as pointer and decrement FSR0 PREINC0 Increment FSR0 first and use as pointer PLUSW0 Add W to FSR0 and use as pointer 330_05 123 ELEC 330 123
124
Pointer Example Opcode Operands Comments LFSR FSR1,0x0120
;Load 120H into FSR1 FSR2,0x0150 ;Load 150H into FSR2 MOVFF POSTINC1,POSTINC2 ;Copy data in register 120H into register 150H and increment both FSRs 330_05 124 ELEC 330 ELEC 330 124
125
Indirect Addressing 330_05 125 ELEC 330 125
126
Using Table Pointers to Copy Data
TBLRD* Copy from Program Memory into Table Latch Using Table Pointer TBLRD*+ Copy from Program Memory into Table Latch and Increment Table Pointer 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 330_05 126 ELEC 330 126
127
Copying Data 330_05 127 ELEC 330 127
128
Example 5.3 The program memory location BUFFER (at address H) holds the byte F6H. Write the assembly language instructions to copy the byte from BUFFER to the data register REG10 (at address 010H) 330_05 128 ELEC 330
129
Table Pointer Example Label Opcode Operand Comment REG10 EQU 0x10
;Data Register MOVLW UPPER BUFFER ;Load upper bits of BUFFER MOVWF TBLPTRU HIGH BUFFER ;Load high byte of BUFFER TBLPTRH LOW BUFFER ;Load low byte of BUFFER TBLPTRL TBLRD* ;Copy data to Table Latch MOVF TABLAT,W ;Copy Table Latch to WREG ;Copy WREG to REG10 SLEEP ORG 0x40 BUFFER DB 0xF6 ;Data Byte END 330_05 129 ELEC 330 ELEC 330 129
130
Arithmetic Operations
PIC18F MPU Add Subtract Multiply Negate (2s complement) Increment Decrement 330_05 130 ELEC 330 130
131
Points to Remember Arithmetic instructions
Can perform operations on W and 8-bit literals Save the result in W Can perform operations an W and F Save the result in W or F In general, affect all flags 330_03 131 ELEC 330 ELEC 330 131
132
Redirection of Program Execution
Three groups of instructions that change the direction of execution Branch Skip Call (discussed in Chapter 7) 330_05 132 ELEC 330 132
133
Branch Instructions 330_05 133 ELEC 330 133
134
Points to Remember Branch instructions use relative addressing
If the operand is positive, the jump is forward If negative, the jump is backward These instructions do not affect flags Flags set by previous instructions used to make decisions 330_05 134 ELEC 330 ELEC 330 134
135
Arithmetic Instructions with Skip
Compare File (Data) Register with W: CPFSEQ F,a ;Skip next instruction if F = W CPFSGT F,a ;Skip next instruction if F > W CPFSLT F,a ;Skip next instruction if F < W Increment File (Data) Register: INCFSZ F,d,a ;Skip next instruction if F = 0 INFSNZ F,d,a ;Skip next instruction if F ≠ 0 Decrement File (Data) Register: DECFSZ F,d,a ;Skip next instruction if F = 0 DCFSNZ F,d,a ;Skip next instruction if F ≠ 0 330_05 135 ELEC 330 135
136
Flowchart for Loop 330_05 136 ELEC 330 136
137
Loop Example Label Opcode Operand Comments COUNT1 EQU 0x01
;Counter is REG01 ORG 0x20 START: MOVLW 0x05 ;Initialize Counter to 5 MOVWF LOOP1: DECF COUNT1,F ;Decrement Counter BNZ LOOP1 ;Count1 = 0? SLEEP ;Done END 330_05 137 ELEC 330 ELEC 330 137
138
Loop Example2 Label Opcode Operand Comments COUNT1 EQU 0x01
;Counter is REG01 ORG 0x20 START: MOVLW 0x05 ;Initialize Counter to 5 MOVWF LOOP1: DECFSZ COUNT1,F ;Decrement Counter, Skip if 0 BRA LOOP1 SLEEP ;Done END 330_05 138 ELEC 330 ELEC 330 138
139
Illustrative Program Problem Statement
Write a program to copy five data bytes from program memory (with the starting location H called SOURCE) to data registers (with beginning address 010H called BUFFER). When the copying process is complete, turn on all LEDs at PORTC. Data Bytes: F6H, 67H, 7FH, A9H, 72H 330_05 139 ELEC 330 ELEC 330 139
140
Copy Data Program 330_05 140 ELEC 330 140
141
Copy Data Program Label Opcode Operand Comments BUFFER EQU 0x10
;Begin Data Registers COUNTER 0x01 ;Counter is REG01 ORG 0x00 ;Reset Vector GOTO START 330_05 141 ELEC 330 ELEC 330 141
142
Copy Data Program Label Opcode Operand Comments ORG 0x20 START: MOVLW
;Init PORTC as Output MOVWF TRISC 0x05 ;Init COUNTER = 5 COUNTER LFSR FSR0, BUFFER ;Init FSR0 Pointer UPPER SOURCE ;Init Table Pointer TBLPTRU HIGH SOURCE TBLPTRH LOW SOURCE TBLPTRL 330_05 142 ELEC 330 ELEC 330 142
143
Copy Data Program Label Opcode Operand Comments NEXT: TBLRD*+
;Copy to Table Latch ;Inc Table Pointer MOVF TABLAT,W ;Copy data to W MOVWF POSTINC0 ;Copy to Register ;Inc FSR0 Pointer DECF COUNTER,F ;Dec COUNTER BNZ NEXT ;COUNTER = 0? MOVLW 0xFF ;Set W PORTC ;Turn ON LEDs SLEEP 330_05 143 ELEC 330 ELEC 330 143
144
Copy Data Program Label Opcode Operand Comments ORG 0x50 SOURCE DB
0xF6,0x67,0x7F,0xA9,0x72 END 330_05 144 ELEC 330 ELEC 330 144
145
Program Execution and Troubleshooting (Debugging)
330_05 145 ELEC 330 ELEC 330 145
146
Logic Operations COMF F,d,a ;Complement (NOT) F
;and save result in W or F ANDLW 8-bit ;AND Literal with W ANDWF F,d,a ;AND W with F and ;save result in W or F IORLW 8-bit ;Inclusive OR Literal with W IORWF F,d,a ;Inclusive OR W with F ;and save result in W or F XORLW 8-bit ;Exclusive OR Literal with W XORWF F,d,a ;Exclusive OR W w/ F ;and save result in W or F 330_05 146 ELEC 330 146
147
Application Example The W register holds a packed BCD number 68H.
Write the instructions to mask the high-order four bits (7-4), preserve the low-order bits (3-0), and save the result in REG1. Label Opcode Operand Comments ANDLW B’ ’ ;And with Mask MOVWF REG1 ;Save Result 330_05 147 ELEC 330 ELEC 330 147
148
Points to Remember Each bit (7-0) of W is logically operated with the corresponding bit of the operand When the operand is a File (data) register, the result can be saved in either W or F using the “d” parameter These instructions affect only the N and Z flags 330_05 148 ELEC 330 ELEC 330 148
149
Bit Set, Clear, and Toggle
Instructions BCF F,b,a BSF F,b,a BTG F,b,a These instructions can set, reset, or toggle any (single) bit in a data register. Examples BCF REG1,7 Clear Bit7 in REG1 BSF REG2,4 Set Bit4 in REG2 BTG REG5,0 Toggle Bit0 in REG5 330_05 149 ELEC 330 149
150
Bit Test and Skip Instructions
Instructions test a bit in a data register for set or reset condition; if conditions met, MPU skips next instruction. Instructions Examples BTFSC F,b,a BTFSC REG1,7 Test BIT7 in REG1 and if the bit is zero, skip the next instruction BTFSS F,b,a BTFSS REG1,5 Test Bit5 in REG1 and if the bit is one, skip the next instruction 330_05 150 ELEC 330 150
151
Application Example The MPU checks RC1 (Bit1) at PORTC.
If the switch is open (RC1=1), it stays in the loop and continues to check the switch. When the switch is closed (RC1=0), the MPU skips the branch instruction, and turns on the LED. Label Opcode Operand CHECK: BTFSC PORTC,1 BRA CHECK BSF PORTC,0 330_05 151 ELEC 330 151
152
Bit Rotation The instruction set includes four instructions that can shift a bit to the adjacent position 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 330_05 152 ELEC 330 152
153
Rotate Instructions 330_05 153 ELEC 330 153
154
Rotate Left Example 330_05 154 ELEC 330 154
155
Unpacking Example Write a program to unpack a packed BCD byte into two buffers, so it can be displayed on two seven-segment displays. 330_05 155 ELEC 330 ELEC 330 155
156
Unpacking Example Label Opcode Operand Comments BCD0 EQU 0x10
;Define Data Registers BCD1 0x11 REG1 0x01 ORG 0x00 ;Reset Vector GOTO START 330_05 156 ELEC 330 ELEC 330 156
157
Unpacking Example Label Opcode Operand Comments ORG 0x20 START: MOVLW
;Load packed byte MOVWF REG1 ANDLW 0x0F ;Mask high order digit BCD0 ;Save low order digit MOVF REG1,W ;Reload byte 0xF0 ;Mask low order digit RRNCF WREG,W ;Rotate 4 times BCD1 ;Save high order digit SLEEP 330_05 157 ELEC 330 ELEC 330 157
158
Illustrative Program Find the Highest Temperature in a Data String
Data string includes positive and negative 8-bit readings. Terminated in null character 00. To find the highest temperature. Get a reading and check whether it is zero. Check whether the byte is negative. Is the byte larger than the previously saved data? If yes, replace the existing byte. Go back to get the next byte. 330_05 158 ELEC 330 158
159
Flow Chart 330_05 159 ELEC 330 159
160
Highest Temperature Label Opcode Operand Comments REG0 EQU 0x00
;Define Data Registers BUFFER 0x10 ORG ;Reset Vector GOTO START 330_05 160 ELEC 330 ELEC 330 160
161
Highest Temperature Label Opcode Operand Comments ORG 0x20 START: CLRF
REG0 ;Init REG0 LFSR FSR0,BUFFER ;Init Pointer NEXT: MOVF POSTINC0,W ;Get Data & Inc Pointer BZ FINISH ;Data = 0? BTFSC WREG,7 ;Data > 0? BRA NEXT CPFSLT ;REG0 < Data? MOVWF ;Save Larger Data SLEEP 330_05 161 ELEC 330 ELEC 330 161
162
Program Execution N Z 330_05 162 ELEC 330 ELEC 330 162
163
Stacks and Subroutines
ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 163
164
Subroutine A group of instructions that performs a specified task
Written independent of main program Can be called multiple times to perform task By main program By another subroutine 330_07 164 ELEC 330 164
165
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) Keeps track of available stack locations 330_07 165 ELEC 330 165
166
PIC18 Microcontroller Stack
Hardware Stack 31 registers 21-bits wide Not part of program memory or data registers Stack Pointer (STKPTR) 5-bit address Top of the Stack (TOS) Pointed to by the stack pointer Copied into three special function registers TOSU (Upper), TOSH (High), and TOSL (Low) 330_07 166 ELEC 330 166
167
STKPTR Register SP4-SP0: Stack Address STKOF: Stack overflow
Attempt to use more than 31 registers to store information on the stack STKUNF: Stack underflow Attempt to retrieve more information than was stored previously on the stack 330_07 167 ELEC 330 167
168
Stack Instructions PUSH POP
Increment the memory address in the stack pointer and store the contents of the program counter (PC+2) on the top of the stack POP Discard the address of the top of the stack and decrement the stack pointer by one 330_07 168 ELEC 330 168
169
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 By another subroutine Call and Return instructions Call a subroutine Return from the subroutine 330_07 169 ELEC 330 169
170
Call and Return Instructions
CALL Label, s ;Call subroutine at Label CALL Label, FAST ;FAST equivalent to s = 1 If s = 0: Increment the stack pointer Store the return address (PC+4) on the TOS Go to the subroutine located at Label If s = 1: Also copy W, STATUS, and BSR to their respective shadow registers RCALL, n ;Relative call to subroutine Increment the stack pointer Store the return address (PC+2) on the TOS Branch to the location Label ± 512 words 330_07 170 ELEC 330 170
171
Call and Return Instructions
RETURN, s ;Return from subroutine RETURN FAST :FAST equivalent to s = 1 If s = 0: Get the return address from the TOS (to PC) Decrement the stack pointer If s = 1: Also retrieve W, STATUS, and BSR from their shadow registers RETLW 8-bit ;Return literal to WREG Get the return address from the TOS (to PC) Decrement the stack pointer Return 8-bit literal to WREG 330_07 171 ELEC 330 171
172
Example 7.2 Program Listing with Memory Addresses 330_07 172 ELEC 330
173
Parameter Passing Parameter passing
Information exchanged between a calling program and a subroutine Subroutine documentation should include Function of the subroutine Input parameters Output parameters Registers modified List of subroutines called 330_07 173 ELEC 330 173
174
Illustrative Program Problem Statement
Write a program to copy a given number of unsigned 8-bit numbers from program memory into specified data registers Add the numbers and display the 16-bit sum at PORTB and PORTC The program should be subdivided into general purpose subroutines 330_07 174 ELEC 330 ELEC 330 174
175
Application Programs ELEC 330 Digital Systems Engineering
Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 175
176
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 ELEC 330 176
177
BCD to Binary Conversion
Data often entered in decimal format A through F are invalid Special care taken to process the data in BCD Sometimes necessary to convert the BCD data into binary numbers ELEC 330 177
178
BCD to Binary Conversion
Problem statement Given a packed BCD number in WREG Write a subroutine to unpack the number Convert it into its equivalent binary number Return the number in WREG ELEC 330 178
179
BCD to Binary Conversion
Problem analysis The BCD numbers include only ten digits from 0 to 9 The value of the digit is based on its position Example, in decimal number 97, the value of 9 is 90 To find the binary value of 97BCD = Unpack the number 9 ( ) and 7 ( ) Multiply high-order digit by 10 and add low-order digit ( ) x 10 + ( ) ELEC 330 179
180
Unpack Opcode Operands Comments MOVWF REG1 ;Save BCD ANDLW 0x0F
;Mask high digit BCD0 ;Save low digit MOVF REG1,W ;Get saved BCD SWAPF WREG,W ;Swap digits ;Mask low digit BCD1 ;Save high digit 330_01 180 ELEC 330 ELEC 330 180
181
Convert Opcode Operands Comments MULLW D’10’ ;Multiply high digit
MOVFF PRODL,WREG ;Move product ADDWF BCD0,W ;Add low digit RETURN 330_01 181 ELEC 330 ELEC 330 181
182
PIC18 Simulator 330_01 182 ELEC 330 ELEC 330 182
183
Input/Output Ports and Interfacing
ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 183
184
Basic I/O Concepts Peripherals such as LEDs and keypads are essential components of microcontroller-based systems 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 display, LCD, and printer Devices are interfaced to an MPU using I/O ports 330_09 184 ELEC 330 184
185
I/O Interfacing 330_09 185 ELEC 330 185
186
Interfacing and Addressing
I/O ports Buffers and latches on the MCU chip Assigned binary addresses by decoding the address bus Generally bidirectional Internal data direction registers To read binary data from an input peripheral MPU places the address of an input port on the address bus Enables the input port by asserting the RD signal Reads data using the data bus To write binary data to an output peripheral MPU places the address of an output port on the address bus Places data on data bus Asserts the WR signal to enable the output port 330_09 186 ELEC 330 186
187
PIC18F452/4520 I/O Ports MCU includes five I/O ports
PORTA, PORTB, PORTC, PORTD, PORTE Ports are multiplexed Can be set up to perform various functions Each I/O port is associated with several SFRs PORT Functions as a latch or a buffer TRIS Data direction register Logic 0 sets up the pin as an output Logic 1 sets up the pin as an input LAT Output latch similar to PORT 330_09 187 ELEC 330 187
188
PIC18F452/4520 I/O Ports PORTA: Example of Multiple Fns
Digital I/O: RA6-RA0 Analog Input: AN0-AN4 V REF+ : A/D Reference Plus V V REF- : A/D Reference Minus V TOCK1: Timer0 Ext. Clock SS: SPI Slave Select Input LVDIN: Low V Detect Input 330_09 188 ELEC 330 188
189
PIC18F452/4520 I/O Ports PORTB 330_09 189 ELEC 330 189
190
I/O Example Write instructions to set up pins RB7-RB4 of PORTB as inputs and pins RB3-RB0 as outputs Opcode Operands Comments MOVLW 0xF0 ;Load B’ ’ into WREG MOVWF TRISB ;Set PORTB TRIS Reg 330_09 190 ELEC 330 ELEC 330 190
191
Interfacing Output Peripherals
Commonly used output peripherals in embedded systems LEDs Seven-Segment Displays LCDs Two ways of connecting LEDs to I/O ports Common Cathode LED cathodes are grounded Logic 1 from the I/O port turns on the LEDs Current is supplied by the I/O port called current sourcing Common Anode LED anodes are connected to the power supply Logic 0 from the I/O port turns on the LEDs Current is received by the chip called current sinking 330_09 191 ELEC 330 191
192
Interfacing Output Peripherals
Common Cathode Common Anode 330_09 192 ELEC 330 192
193
Seven-Segment Display
Seven-segment Displays Used to display BCD digits 0 thru 9 A group of 7 LEDs physically mounted in the shape of the number eight Plus a decimal point Each LED is called a segment ‘a’ through ‘g’ Two types Common anode Common cathode 330_09 193 ELEC 330 193
194
Seven-Segment Display
Common Anode All anodes are connected together to a power supply Cathodes are connected to data lines Logic 0 turns on a segment Example: To display the digit 1 All segments except b and c should be off = F9H Common Anode 330_09 194 ELEC 330 194
195
Seven-Segment Display
Common Cathode All cathodes are connected together to ground Anodes are connected to data lines Logic 1 turns on a segment Example: To display digit 1 All segments except b and c should be off = 06H 330_09 195 ELEC 330 195
196
Example 9.4 Interfacing Seven-Segment Display to PORTB Common Anode
Table Look-Up 330_09 196 ELEC 330 196
197
Illustrative Program Problem Statement
Interface two common-anode seven-segment displays to PORTD and PORTC of the PIC18F Write instructions to implement an up-counter, counting from 00 to 59 Display the count on the two seven-segment displays 330_09 197 ELEC 330 ELEC 330 197
198
Interfacing Input Peripherals
Commonly used input peripherals DIP switches, push-button keys, keyboards, and A/D converters DIP switch One side of the switch is tied high To a power supply through a resistor called a pull-up resistor The other side is grounded The logic level changes when the position is switched Push-button key Same as the DIP switch except that contact is momentary 330_09 198 ELEC 330 198
199
Interfacing Dip Switches
330_09 199 ELEC 330 199
200
Reading from an I/O Port
Read input switches on PORTB (RB7-RB4) RB0 set HI (1) Switches Open = LOW (0) Switches Closed = HIGH (1) Display on PORTC Opcode Operands Comments MOVLW 0xF0 ;Load B’ ’ into WREG MOVWF TRISB ;Set PORTB TRIS Reg CLRF TRISC ;Set PORTC as Output BSF PORTB,0 ;Set RB0 High MOVF PORTB,W ;Read PORTB PORTC ;Display on PORTC 330_09 200 ELEC 330 200
201
Internal Pull-Up Resistor
Turning off the internal FET provides a pull-up resistor Bit7 (RBPU) in the INTCON2 register enables or disables the pull-up resistor Instruction to Enable Pull Up Resistors: BCF INTCON2,7 330_09 201 ELEC 330 201
202
Interfacing Push-Button Keys
When a key is pressed (or released), mechanical metal contact bounces momentarily and can be read as multiple inputs Key debounce Eliminating reading of one contact as multiple inputs Hardware or Software 330_09 202 ELEC 330 202
203
Key Debounce Techniques
Hardware technique Two NAND gates S-R latch The output of the latch is a pulse without a bounce Software technique Wait for 10 to 20 ms after detection of a switch closure If the reading is still the same it is accepted 330_09 203 ELEC 330 203
204
Interfacing a Matrix Keypad
Problem statement Interface a 4 x 4 Hex keypad to PORTB Write a program to recognize a key pressed and encode the key in its binary value Display binary code on PORTC 330_09 204 ELEC 330 204
205
Interfacing a Matrix Keypad
Hardware (PIC18 Simulator) 4 x 4 matrix keypad organized in the row and column format Four columns are connected to the lower half of PORTB (RB0-RB3) Four rows are connected to upper half of PORTB (RB4-RB7) When a key is pressed, it makes a contact with the corresponding row and column 330_09 205 ELEC 330 205
206
Interfacing a Matrix Keypad
PIC18 Simulator Keypad Matrix 1 4 7 A 2 5 8 3 6 9 B C D E F 330_09 206 ELEC 330 206
207
Interfacing a Matrix Keypad
Software To recognize and encode the key pressed Set all the columns High by sending ones Check for any key pressed (non-zero) Set one column High at a time Check all the rows in that column Once a key is identified Encode based on its position in the column 330_09 207 ELEC 330 207
208
Time Multiplex Scanning Technique
Problem statement Interface four common cathode seven-segment displays to PORTB and PORTC using the time multiplex scanning technique. Write instructions to display a four-digit number stored in data registers. 330_09 208 ELEC 330 208
209
Time Multiplex Scanning
Hardware Eight data lines of PORTB are connected to the anodes of each display Each cathode is connected to PORTC (RC3-RC0) through a transistor Transistors (and LEDs) can be turned on by sending logic 1 Each display is turned on and off in a sequence to display a digit 330_09 209 ELEC 330 209
210
Time Multiplex Scanning
330_09 210 ELEC 330 210
211
Time Multiplex Scanning
Software Codes of the numbers to be displayed are stored in data registers in sequence The program gets the codes from the data registers by using the pointer (FSR0) and sends them out to the LED segments through PORTB One display at a time is turned on by sending logic 1 to the corresponding transistor connected to PORTC After an appropriate delay, the first display is turned off and the next display is turned on Turning displays on/off is repeated in sequence 330_09 211 ELEC 330 211
212
Interfacing LCD Problem statement
Interface a 2-line x 20 character LCD module with the built-in HD44780 controller to I/O ports of the PIC18 microcontroller. Explain the control signals necessary to read from and write to the LCD. Write a program to display ASCII characters. 330_09 212 ELEC 330 212
213
Interfacing LCD Hardware 20 x 2-line LCD display
Two lines with 20 characters per line LCD has a display Data RAM Stores data in 8-bit character code Each register in Data RAM has its own address Corresponds to its position on the line Line 1 is 00H to 13H Line 2 is 40H to 53H 330_09 213 ELEC 330 213
214
Interfacing LCD 330_09 214 ELEC 330 214
215
Interfacing LCD Driver HD44780 8-bit data bus (RD7-RD0)
Three control signals RS – Register Select (RA3) R/W – Read/Write (RA2) E – Enable (RA1) Three power connections Power, ground, and variable resistor to control brightness 330_09 215 ELEC 330 215
216
Interfacing LCD Can be interfaced either in 8-bit mode or 4-bit mode
In 8-bit mode, all eight data lines are connected In 4-bit mode, only four data lines are connected Two transfers per character (or instruction) are needed Driver has two 8-bit internal registers Instruction Register (IR) to write instructions to set up LCD Table 9-3 Data Register (DR) to write data (ASCII characters) 330_09 216 ELEC 330 216
217
Interfacing LCD LCD Operation
When the MPU writes an instruction to IR or data to DR, the controller: Sets DB7 high indicating that the controller is busy Sets 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 330_09 217 ELEC 330 217
218
Interfacing LCD Writing to or Reading from LCD (Table 9-4) 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 330_09 218 ELEC 330 218
219
Interfacing LCD Timing diagram: writing to LCD 330_09 219 ELEC 330 219
220
Interfacing LCD Software To write into the LCD
Send the initial instructions to set up the LCD 4-bit or 8-bit mode Continue to check DB7 until it goes low Write instructions to IR to set up LCD parameters Number of display lines and cursor status Write data to display a message 330_09 220 ELEC 330 220
221
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 221
222
Basic Concepts of Interrupts
An interrupt is a communication process A device Requests the MPU to stop processing Internal or external The MPU Acknowledges the request Attends to the request Goes back to processing where it was interrupted 330_10 222 ELEC 330 222
223
Types of Interrupts 330_10 223 ELEC 330 223
224
MPU Response to Interrupts
When interrupts are enabled MPU checks interrupt request flag at the end of each instruction If interrupt request is present, the MPU Resets the interrupt flag Saves the return address on the stack MPU redirected to appropriate memory location Interrupt vectors Interrupt service routine (ISR) meets request MPU returns to where it was interrupted Specific return instruction 330_10 224 ELEC 330 224
225
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 hardware Has a priority scheme divided into two groups High priority and low priority Uses many Special Function Registers (SFRs) to implement the interrupt process 330_10 225 ELEC 330 225
226
PIC18 Interrupt Sources External sources Internal peripheral sources
Three pins of PORTB RB0/INTO, RB1/INT1,and RB2/INT2 Can be used to connect external interrupting sources Keypads or switches PORTB Interrupt (RBI) Change in logic levels of pins RB4-RB7 Internal peripheral sources Examples Timers A/D Converter Serial I/O 330_10 226 ELEC 330 226
227
PIC18 Interrupt Sources Special Function Registers (SFRs)
RCON Priority Enable INTCON External interrupt sources IPR, PIE, and PIR Internal peripheral interrupts Valid interrupt Interrupt request bit (flag) Interrupt enable bit Priority bit 330_10 227 ELEC 330 227
228
Interrupt Priority Interrupt priorities
High-priority interrupt vector H Low-priority interrupt vector H A high-priority interrupt can interrupt a low-priority interrupt in progress. Interrupt priority enable Bit7 (IPEN) in RCON register RCON 330_10 228 ELEC 330 228
229
External Interrupts INTCON Register INTCON2 Register INTCON3 Register
330_10 229 ELEC 330 229
230
Interrupt Service Routine (ISR)
Similar to a subroutine Attends to the request of an interrupting source Clears the interrupt flag Should save register contents that may be affected by the code in the ISR Must be terminated with the instruction RETFIE When an interrupt occurs, the MPU: Completes the instruction being executed Disables global interrupt enable Places the return address on the stack 330_10 230 ELEC 330 230
231
Interrupt Service Routine (ISR)
High-priority interrupts The contents of W, STATUS, and BSR registers are automatically saved into respective shadow registers. Low-priority interrupts These registers must be saved as a part of the ISR If they are affected RETFIE [s] ;Return from interrupt RETFIE FAST ;FAST equivalent to s = 1 If s =1: MPU also retrieves the contents of W, BSR, and STATUS registers 330_10 231 ELEC 330 231
232
Internal Interrupts PIC18 MCU internal interrupt sources
Timers A/D converter Serial I/O Each interrupt has three bits Interrupt priority bit Interrupt enable bit Interrupt request bit (flag) Interrupt registers IPR: Interrupt Priority Register PIE: Peripheral Interrupt Enable PIR: Peripheral Interrupt Request (Flags) 330_10 232 ELEC 330 232
233
Interrupt Registers IPR1 PIE1 PIR1 330_10 233 ELEC 330 233
234
Multiple Interrupt Sources
All interrupt requests are directed to one of two memory locations (interrupt vectors) 000008H (high-priority) 000018H (low-priority) When there are multiple requests The interrupt source must be identified by checking the interrupt flags 330_10 234 ELEC 330 234
235
Illustration Problem Statement
INT1 set up as a high-priority interrupt Timer1 and Timer2 set up as low-priority Identify the interrupt sources Execute the appropriate interrupt service routines 330_09 235 ELEC 330 ELEC 330 235
236
Illustration Label Opcode Operand Comments S_TMP EQU 0x100
;Temp Registers W_TMP 0x101 ORG 0x00 GOTO MAIN 0x08 ;High-Priority Interrupt Vector INTCHK: INT1_ISR 0x18 ;Low-Priority Interrupt Vector TIMERCHK: BTFSC PIR1,TMR1IF ;Timer1 Flag, Skip if Clear TMR1_ISR PIR1,TMR2IF ;Timer2 Flag, Skip if Clear TMR2_ISR RETFIE 330_04 236 ELEC 330 ELEC 330 236
237
Illustration Label Opcode Operand Comments MAIN:
;Main Program goes here ;Do Something HERE: GOTO HERE ;Wait for an Interrupt ORG 0x100 INT1_ISR: BCF INTCON3,INT1IF ;Clear Flag RETFIE FAST ;Retrieve registers and Return 330_04 237 ELEC 330 ELEC 330 237
238
Illustration Label Opcode Operand Comments TMR1_ISR: MOVFF
STATUS, S_TMP MOVWF W_TMP ;Save Registers BCF PIR1,TMR1IF ;Clear Flag ;Do Something MOVF W_TMP,W ;Retrieve Registers S_TMP,STATUS RETFIE ;Return from interrupt TMR2_ISR ;Similar to Timer1 330_04 238 ELEC 330 ELEC 330 238
239
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 239
240
Hardware Counters and Timers
Counter (register) Can be decremented or incremented per clock cycle Time calculation Find difference between beginning count and last count Multiply the count difference by the clock period 330_11 240 ELEC 330 240
241
Hardware Counters and Timers
Counter (register) Replace the clock with a signal from an event Count is incremented (or decremented) Total number of events can be counted 330_11 241 ELEC 330 241
242
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 330_11 242 ELEC 330 242
243
Timer Applications Time delay Pulse wave generation
Pulse width or frequency measurement Timer as an event counter 330_11 243 ELEC 330 243
244
Capture, Compare, and PWM (CCP) Modules
CCP modules are common in recent MCUs Registers specially designed to perform the following functions (in conjunction with timers) Capture: The CCP pin can be set as an input to record the arrival time of a pulse. 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. 330_11 244 ELEC 330 244
245
Pulse Width Modulation
Duty cycle Percentage ratio of on time of a pulse to its period Changing of the duty cycle is defined as PWM CCP pin is set as an output Count for period and duty cycle loaded into CCP registers Varying the duty cycle generates PWM 330_11 245 ELEC 330 245
246
PIC18 Timers PIC18 timers Timer0 Timer1 (and Timer3)
All up-counters 8-bit and 16-bit Timer0 8-bit or 16-bit timer Timer1 (and Timer3) 16-bit timers Timer2 (and Timer4) 8-bit timer SFRs T0CON-T2CON 330_11 246 ELEC 330 246
247
Timer0 8-bit or 16-bit timer Readable and writable
Parameters in T0CON register Eight pre-scale values (Bit2-Bit0) Clock source (Bit5) Internal (instruction cycle) External clock connected to pin RA4/T0CK1 Rising edge or falling edge (Bit4) Generates an interrupt or sets a flag when it overflows TMR0IF Flag must be cleared to start the timer again 330_11 247 ELEC 330 247
248
Timer0 T0CON 330_11 248 ELEC 330 248
249
Timer1 16-bit counter/timer Two 8-bit registers (TMR1H and TMR1L)
Four prescale values (Bit5-Bit4) Clock source (Bit1) Internal (instruction cycle) External (pin RC0/T13CK1) on rising edge 330_11 249 ELEC 330 249
250
Timer1 Interrupt Resetting Timer1 using CCP module
Generates an interrupt or sets a flag when it overflows TMR1IF Flag must be cleared to start the timer again Resetting Timer1 using CCP module CCP1 in the Compare mode Timer1 and CCP1compared at every cycle When a match is found, Timer1 is reset 330_11 250 ELEC 330 250
251
Timer2 8-bit timer (TMR2) 8-bit period register (PR2)
TMR2 and PR2 are readable and writable Three prescale values (Bit1-Bit0) 16 postscale values (Bit6-Bit3) Flag (TMR2IF) is set when TMR2 matches PR2 Can generate an interrupt 330_11 251 ELEC 330 251
252
Timer2 Timer2 operation 8-bit number is loaded in PR2
When TMR2 and PR2 match Output pulse is generated and the timer is reset Output pulse goes through postscaler Sets the flag TMR2IF 330_11 252 ELEC 330 252
253
CCP Modules Capture, Compare, and Pulse Width Modulation (PWM)
CCPR1H (high) and CCPR1L (low) 16-bit Capture register 16-bit Compare register Duty-cycle PWM register Timer1 used as clock for Capture and Compare Timer2 used as clock for PWM 330_11 253 ELEC 330 253
254
CCP in the Capture Mode CCPR1 captures the 16-bit value of Timer1
When an event occurs on pin RC2/CCP1 Interrupt request flag bit CCP1IF is set Must be cleared for the next operation To capture an event Set up pin RC2/CCP1 of PORTC as the input Initialize Timer1 T1CON register Initialize CCP1 CCP1CON register 330_11 254 ELEC 330 254
255
CCP in the Compare Mode CCPR1 constantly compared with TMR1
When a match occurs Pin RC2/CCP1 on PORTC Driven high, low, or toggled Interrupt flag bit CCP1IF is set To set up CCP1 in Compare mode Set up pin RC2/CCP1 of PORTC as output Initialize Timer1 and CCP1 Clear the flag CCP1IF 330_11 255 ELEC 330 255
256
PWM Mode CCP module with Timer2 Duty cycle Period
Output a pulse wave form for a given frequency/duty cycle Duty cycle CCPR1 register Period PR2 register When TMR2 is equal to PR2 TMR2 is cleared Pin RC2/CCP1 of PORTC is set high PWM duty-cycle byte loaded into CCPR1 330_11 256 ELEC 330 256
257
Data Converters ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 257
258
Basic Concepts Analog Signals Digital Signals Continuous
Infinite values in a given range Example Clock face with hands Digital Signals Discrete values On/Off 1/0 Digital clock 330_12 258 ELEC 330 258
259
Basic Concepts Limitations of analog signals
Pick up noise as they are amplified Difficult to store Advantages of digital signals Noise margin Easily stored in memory Limitations of digital signals How accurately analog signals are represented Quantization 330_12 259 ELEC 330 259
260
Embedded System Typical analog/digital system Transducer A/D converter
Converts non-electrical signals into electrical signals A/D converter Converts analog signals into digital signals Digital processor Processes digital data D/A converter Converts digital signals into equivalent analog signals Converts electrical signals into non-electrical signals 330_12 260 ELEC 330 260
261
Analog-to-Digital Conversion
A/D, ADC, or A-to-D Converting a continuous varying signal, such as voltage or current, into discrete digital quantities that represent the magnitude of the signal compared to standard or reference 261 ELEC 330 261
262
A/D Conversion Flash Integrator Successive Approximation Counter
Uses multiple comparators in parallel High-speed, high cost Integrator Charges a capacitor for a given amount of time Slow, but high accuracy and low noise Successive Approximation Effective compromise among resolution, speed, and cost Counter Similar to successive approximation circuit Slower, with variable conversion times 330_12 262 ELEC 330 262
263
Successive Approximation
330_12 263 ELEC 330 263
264
PIC18F A/D Converter Module
The PIC18F4520 10-bit A/D converter 13 channels AN0 - AN12 Three control registers ADCON0, ADCON1, and ADCON2 The PIC18F452 8 channels AN0 - AN7 Two control registers ADCON0 and ADCON1 330_12 264 ELEC 330 264
265
PIC18F4520 A/D Converter 330_12 265 ELEC 330 265
266
A/D Control Register0 ADCON0 Register
Select channel for input analog signal Start conversion Indicate end of conversion 330_12 266 ELEC 330 266
267
A/D Control Register1 ADCON1 Register Set up I/O pins
Analog signal or digital signals Select VREF voltages 330_12 267 ELEC 330 267
268
A/D Control Register2 ADCON2 Register
Select acquisition time and clock frequency Right or left justify output reading 330_12 268 ELEC 330 268
269
PICDEM 2 Plus 330_12 269 ELEC 330 ELEC 330 269
270
Digital to Analog Conversion
D/A, DAC, or D-to-A Converting discrete signals into analog values that represent the magnitude of the input signal compared to a standard or reference Output is discrete analog steps Increasing the resolution (number of bits) Step size is reduced Approximates a continuous analog signal 330_12 270 ELEC 330 270
271
D/A Converter Circuits
330_12 271 ELEC 330 271
272
IC D/A Converters 330_12 272 ELEC 330 272
273
Serial I/O ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Images Courtesy of Ramesh Gaonkar and Delmar Learning ELEC 330 ELEC 330 273
274
Serial I/O Serial I/O (Communication) To transmit data serially
One bit transferred at a time Over one line To transmit data serially Microcontroller must convert its parallel word into a stream of serial bits Parallel-to-serial conversion To receive data serially Microcontroller must convert a stream of serial bits into a parallel word Serial-to-parallel conversion 330_13 274 ELEC 330 274
275
Basic Concepts Transmission issues Synchronous vs Asynchronous
Simplex vs Duplex Data Rate Errors and Error Checks Standards and Protocol 330_13 275 ELEC 330 275
276
Synchronous vs Asynchronous
330_13 276 ELEC 330 276
277
Simplex vs Duplex Simplex Full duplex Half duplex
Data flow in only one direction Such as from a PC to its peripheral Full duplex Data flow in both directions simultaneously Such as a telephone conversation or communication via a modem Half duplex Data flow in both directions, only one direction at a time Such as a conversation over a CB radio 330_13 277 ELEC 330 277
278
Rate of Transmission Baud and bits per second (BPS)
Baud is a measure of the “signaling rate” 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 Baud and bits per second are equal At higher speeds Multiple bits are encoded in one electrical change Generally expressed in bits per second (bps) 330_13 278 ELEC 330 278
279
Transmission Errors Framing error Overrun error
Start and Stop bits improperly frame a character Recognized when the Stop bit is zero Expected to be one Flag in control register Overrun error New byte overwrites the earlier byte Before the receiver completes reading 330_13 279 ELEC 330 279
280
Error Checks Parity CheckSum Receiver recalculates Parity or CheckSum
Last bit is used as a parity bit Either an even or odd number of 1s CheckSum Used when blocks of data are transmitted All bytes transmitted are XORed Sent as the last byte Receiver recalculates Parity or CheckSum Compares to the one transmitted No match generates an error 330_13 280 ELEC 330 280
281
Standards and Protocols
EIA-232 (formerly known as RS-232) Serial Peripheral Interface (SPI ) Inter-Integrated Circuit (I2C) One-wire (1-Wire®) Bus Controller Area Network Bus (CAN) Local Interconnect Network (LIN) 330_13 281 ELEC 330 281
282
PIC18 Serial Communications
Includes two serial communication modules Universal Synchronous Asynchronous Receiver Transmitter (USART) Implements EIA-232 Also known as Serial Communications Interface (SCI) Master Synchronous Serial Port (MSSP) Also known as Serial Peripheral Interface (SPI) Also implements I2C mode 330_13 282 ELEC 330 282
283
EIA-232 Serial I/O Standard
Negative logic Logic 1: –3 V to –25 V or Mark Logic 0: +3 V to + 25 V or Space Connector DB25 subminiature Twenty-five pins DB9 connector Nine pins 330_13 283 ELEC 330 283
284
EIA-232 Serial I/O Standard
EIA 232 DTE and DCE Minimum Connection 330_13 284 ELEC 330 284
285
Framing ASCII Character Y (59H) – 9600 BAUD 330_13 285 ELEC 330 285
286
PIC18 USART Module Configuration Modes Asynchronous Synchronous
Full duplex PC serial communication EIA-232 protocol Synchronous Master or slave half duplex Used to communicate with peripherals A/D and D/A converters Serial EEPROM 330_13 286 ELEC 330 286
287
PIC18 USART Module Control Registers SPBRG TXSTA RCSTA BAUDCON
Baud Rate Generator TXSTA Transmit Status and Control RCSTA Receive Status and Control BAUDCON Baud Rate Control 330_13 287 ELEC 330 287
288
USART Transmission 330_13 288 ELEC 330 288
289
USART Reception 330_13 289 ELEC 330 289
290
Interrupts PIE1 PIR1 TXIE (Bit4) RCIE (Bit5) TXIF (Bit4) RCIF (Bit5)
Transmit Interrupt Enable RCIE (Bit5) Receive Interrupt Enable PIR1 TXIF (Bit4) Transmit Interrupt Flag Bit is 1 when TXREG (Transmit Register) is empty RCIF (Bit5) Receive Interrupt Flag Bit is 1 when RCREG (Receive Register) is full 330_13 290 ELEC 330 290
291
Master Synchronous Serial Port
Master Synchronous Serial Port (MSSP) Serial interface used in communicating with other peripheral devices Can operate in one of two modes Serial Peripheral Interface (SPI) Inter-Integrated Circuit (I2C) 330_13 291 ELEC 330 291
292
Serial Peripheral Interface (SPI)
Serial synchronous data exchange protocol High-speed serial communication Between a microcontroller and its peripheral devices Between a master and a slave device EEPROMs, data converters, and display drivers Four-wire Interface Clock, data in, data out, and slave select Synchronous protocol Clock signal is provided and controlled by the master device Master-Slave protocol Can communicate with multiple slave devices Data Exchange protocol Each device has two data lines 330_13 292 ELEC 330 292
293
MSSP: SPI Mode Transmits and receives data simultaneously
Uses following pins 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 Internal Registers SSPSR (Shift Register) SSPBUF (Buffer Register) 330_13 293 ELEC 330 293
294
SPI Mode Write and Read operations Must be performed for each byte
Some of data bytes may not have any use in a given application Transmission Master sends data and receives dummy data from slave device Transmission and Reception Both exchange data Reception Master receives data and sends dummy data to slave device 330_13 294 ELEC 330 294
295
SPI Applications SPI-compatible peripherals include
Converters (ADC and DAC) Memories (EEPROM and Flash) Sensors (temperature and pressure) Real Time Clock (RTC) Display (LCD) Shift registers Master-slave configuration One master device and multiple slave devices 330_13 295 ELEC 330 295
296
Master and Multiple Independent Slave Connection
330_13 296 ELEC 330 296
297
Example Interfacing 74HC – Shift Register in SPI Mode 330_13 297
ELEC 330 297
298
Inter-integrated Circuit Protocol
Inter-integrated Circuit (I2C) Two-wire interface Two open drain/collector lines One for clock and one for data Synchronous Data are transferred with a synchronous clock initiated by the master device Rate of data transfer is 100 kbps in standard mode 400 kbps in fast mode Master/Slave or Many Masters Bus may have one master and many slaves or multiple masters 330_13 298 ELEC 330 298
299
I2C Protocol 330_13 299 ELEC 330 299
300
MSSP: I2C Mode Implements all master and slave functions
Provides interrupts on Start and Stop bits Uses the following pins SCL (Serial Clock) pin RC3/SCK/SCL on PORTC SDA (Serial Data) pin RC4/SDI/SDA on PORTC Internal Registers SSPSR (Shift Register) SSPBUF (Buffer Register) SSPADD (Address Register) 330_13 300 ELEC 330 300
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.