Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Objectives
When you finish this class, you will understand PIC18:
Architecture Interrupts Interrupt Handlers Basic Peripherals Peripheral Configuration Programming in C using MPLAB C18
MCU 2121
Slide 2
Agenda
Architecture Overview
Programmers Model
Program Memory Stack Data Memory
PIC18 Interrupts
MCU 2121
Slide 3
Agenda (continued)
Development Tools
MPLAB IDE, ICD2, PICDEM2 Plus MPLAB C18 C Language Extensions
Program Memory Variables SFR Access Inline Assembly Mixing C and Assembly Defining Sections Configuration Settings Support Interrupt Support
MCU 2121
Slide 4
Agenda (continued)
Peripherals and Hands-On Exercises
I/O Ports & External Interrupts Analog: Comparators, VREF, and ADC Timers CCP (Capture, Compare & PWM)
MCU 2121
Slide 5
Agenda (continued)
MSSP (I2C, SPI and Microwire) USART
MCU 2121
Slide 6
PIC18 Architecture
Overview
Harvard Architecture
8-bit microcontroller 16-bit Instruction width Data Transfer Mechanism between PM and DM
8 bit window
PIC18
Data Memory
16
RISC CPU
(Up to 4KB)
MCU 2121
Slide 8
PC<0> always = 0
16-bit Program Memory is Byte Addressable
MCU 2121
Slide 9
Stack Pointer:
Stack grows up
STKPTR Register
R/C-0 bit 7 R/C-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 bit 0
bit 7
STKFUL: Stack Full Flag Bit 1 = Stack full or overflow occurred 0 = Reset or cleared by user software STKUNF: Stack Underflow Flag Bit 1 = Stack underflow occurred 0 = Reset or cleared by user software SP4:SP0: Stack Pointer Bits
MCU 2121 Slide 11
bit 6
bit 4-0
Programmers Model
MCU 2121
Slide 12
Table Pointer
Program Memory Data Access (R/W)
Program memory (16 bits) TBLPTR 21-bit 21Address TABLAT Data 055h TBLPTRU TBLPTRH TBLPTRL TABLAT Register file (8 bits)
High byte
Low byte
TBLPTR registers are used to address program memory TBLRD instruction is used to read a byte
2 cycles not incl. TBLPTR set-up Data is latched in TABLAT register
Table Pointer
Assembly Operations
TBLRD/TBLWT *
no change to table pointer
TBLRD/TBLWT *+
auto post-increment of table pointer
TBLRD/TBLWT +*
auto pre-increment of table pointer
MCU 2121
Slide 14
Access RAM Access RAM Bank 0 GPR Bank 0 GPR Bank 1 Bank 1 GPR GPR
Data Memory up to 4k bytes Divided into 256 byte banks Half of bank 0 and half of bank 15 form a virtual bank that is accessible no matter which bank is selected BSR determines which bank is currently selected
1FFh 200h
D00h
Access SFR Access SFR Bank 13 Bank 13 GPR GPR 256 Bytes
DFFh E00h
Arithmetic, Logic, Shift Instructions Single Cycle 8 x 8 Multiply (100ns) Powerful Bit Manipulation
Single cycle bit set, clear or toggle Operate directly on all registers including I/O
MCU 2121
Slide 17
MCU 2121
Slide 18
Description
Result:
Reduced code size & execution time of Cbased programs
MCU 2121
Slide 20
MCU 2121
Slide 22
MCU 2121
Slide 23
Storage Qualifiers
rom near Program memory address < 64K ram In access bank
far
rom Qualifier rom object stored in program memory Compiler inserts TBLRD and TBLWT
instructions to access Can be used on any data type Can be a pointer
16-bits for near qualifier 24-bits for far qualifier
MCU 2121
Slide 25
Pointer Example
struct personal { char age; char weight; }; variable in data memory struct personal Jane = { 84, 110 }; variable in program memory rom struct personal Bob = { 24, 180 };
rom pointer to data memory struct personal *rom person1 = & Jane; ram pointer to data in program memory rom struct personal *person2 = & Bob; rom pointer to data in program memory rom struct personal *rom person3 = & Bob;
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 26
String Constants
String literals (e.g., Hello World) are of type: rom const char []; Multiple versions of standard C string libraries are provided to help accommodate this: strcpypgm2ram( myvar, Wait... ); strlenpgm( Volume Control );
MCU 2121
Slide 27
MCU 2121
Slide 28
MCU 2121
Slide 29
Inline Assembly
Syntax:
Specify all operands Directives are not supported C style comments Labels require a colon Use C-style literals (0x12 or 18, not H12) Note: optimizations are disabled in functions with inline assembly.
void add5 (void) { static char foo=0; _asm // foo += 5 movlb foo movlw 0x5 addwf foo,1,1 _endasm }
MCU 2121
Slide 30
MCU 2121
Slide 31
MCU 2121
Slide 32
C file
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
Assembly file
Slide 33
MCU 2121
Slide 34
Section Attributes
access
Locate section in access RAM no banking required variables must be declared with near qualifier
overlay
Permit other sections to occupy same address May be combined with access Each section must reside in separate .c file All must have same name and address
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 37
MCU 2121
Slide 38
Ex. watchdog timer, brown out reset, oscillator settings, PLVD, etc Setting list in: C:\MCC18\doc\hlpPIC18ConfigSet.chm
MCU 2121
Slide 39
PIC18 Interrupts
Interrupt Overview
Multiple Internal and External Sources
Global Enable Bit Individual Enable, Flag and Priority Bits Two Modes of Operation
Legacy: RCON.IPEN = 0 (Default) Priority: RCON.IPEN = 1
Most Interrupts Wake PIC from Sleep Fast Context Save/Restore Latency of 3-4 Instruction Cycles
MCU 2121
Slide 41
Interrupt Logic
Legacy Mode
TMR0IF TMR0IE
Other Core Interrupts Core Interrupts
Wakeup to CPU
Peripheral Interrupts
TMR1IF TMR1IE
GIE
PEIE
MCU 2121
Slide 42
Interrupt Logic
Priority Mode
IP IE IF INT0IF INT0IE GIEH Vector to 0x0008 High Priority Interrupt to CPU Wakeup to CPU Low Priority Interrupt to CPU Vector to 0x0018 GIEL
IP IE IF
MCU 2121
Slide 43
Interrupt Overview
Interrupt Sources
3 or 4 External Interrupts (INT0-INT3)
Edge Triggered Rising or Falling selected in INTCON2 register
PORTB Interrupt on Change (RB4-RB7) Timer Rollover/Overflow Events Comparator Output Change A/D Conversion Complete Communication Channel Events Other Peripheral Events
MCU 2121
Slide 44
Enabling Interrupts
(slide 1 of 7)
RCON Register
IPEN
SBOREN
---
RI
TO
PD
POR
BOR
IPEN: Interrupt Priority Enable 1 = Enable priority levels on interrupts 0 = Disable priority levels on interrupt (PIC16 compatibility mode)
MCU 2121
Slide 45
Enabling Interrupts
(slide 2 of 7) Set Peripheral Interrupt Priority
1 = High Priority, 0 = Low Priority
IPR1 Register
PSPIP
PSPIP: ADIP: RCIP: TXIP:
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
Parallel Slave Interrupt Priority A/D Converter Interrupt Priority EUSART Rcv Interrupt Priority EUSART Tx Interrupt Priority
MSSP Interrupt Priority CCP1 Interrupt Priority Timer2 Interrupt Priority Timer1 Interrupt Priority
IPR2 Register
OSCIP
OSCIP: CMIP: ---EEIP:
CMIP
---
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
Oscillator Fail Interrupt Priority Comparator Interrupt Priority Unimplemented Bit Data EEPROM/Flash Write Operation Interrupt Priority
Bus Collision Interrupt Priority High/Low Voltage Detect Interrupt Priority Timer3 Interrupt Priority CCP2 Interrupt Priority
MCU 2121
Slide 46
Enabling Interrupts
(slide 3 of 7)
PSPIE
PSPIE: ADIE: RCIE: TXIE:
ADIE
RCIE
TXIE
SSPIE
SSPIE: CCPIE: TMR2IE: TMR1IE:
CCPIE
TMR2IE
TMR1IE
Parallel Slave Interrupt Enable A/D Converter Interrupt Enable EUSART Rcv Interrupt Enable EUSART Tx Interrupt Enable
MSSP Interrupt Enable CCP1 Interrupt Enable Timer2 Interrupt Enable Timer1 Interrupt Enable
PIE2 Register
OSCIE
OSCIE: CMIE: ---EEIE:
CMIE
---
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
Oscillator Fail Interrupt Enable Comparator Interrupt Enable Unimplemented Bit Data EEPROM/Flash Write Operation Interrupt Enable
Bus Collision Interrupt Enable High/Low Voltage Detect Interrupt Enable Timer3 Interrupt Enable CCP2 Interrupt Enable
MCU 2121
Slide 47
Enabling Interrupts
(slide 4 of 7)
RBPU
---
TMR0IP
---
RBIP
TMR0IP: TMR0 Overflow Interrupt Priority RBIP: RB Port Change Interrupt Priority
INTCON3 Register
INT2IP
INT2IP: INT1IP:
INT1IP
---
INT2IE
INT1IE
---
INT2IF
INT1IF
NOTE: INT0 does not have an IP bit it is always a high priority interrupt
MCU 2121
Slide 48
Enabling Interrupts
(slide 5 of 7)
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
TMR0IE: TMR0 Overflow Interrupt Enable INT0IE: INT0 External Interrupt Enable RBIE: RB Port Change Interrupt Enable INTCON3 Register
INT2IP
INT1IP
--INT2IE: INT1IE:
INT2IE
INT1IE
---
INT2IF
INT1IF
MCU 2121
Slide 49
Enabling Interrupts
(slide 6 of 7)
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
PEIE/GIEL: Peripheral Interrupt Enable When IPEN = 0: 1 = Enables all unmasked peripheral interrupts 0 = Disables all peripheral interrupts When IPEN = 1: 1 = Enables all low priority peripheral interrupts 0 = Disables all low priority peripheral interrupts
MCU 2121
Slide 50
Enabling Interrupts
(slide 7 of 7)
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
GIE/GIEL: Peripheral Interrupt Enable When IPEN = 0: 1 = Enables all unmasked interrupts 0 = Disables all interrupts When IPEN = 1: 1 = Enables all high priority interrupts 0 = Disables all interrupts
MCU 2121
Slide 51
MCU 2121
Slide 52
Interrupt Support
Programmer responsibility:
Provide code for interrupt vector Provide Interrupt Service Routine (ISR)
Compiler saves:
WREG, BSR, STATUS registers (v3.xx and above)
Programmer saves:
Any additional information
MCU 2121
Slide 53
MCU 2121
Slide 54
MCU 2121
Slide 55
MCU 2121
Slide 56
Preserving Compiler Resources Prior to C18 v3.00 users were required to preserve compiler resources
#pragma interrupt isr \ save=section(.tmpdata), PROD void isr (void) { int result; result = poll_device(); ... }
MCU 2121
Slide 57
Preserving Resources v3.xx automatically saves and restores context May save variables and sections:
#pragma interrupt \save= myint, section(mydata) isr void isr (void) { int result; result = poll_device(); ... }
MCU 2121
Slide 58
PIC18 Peripherals
MCU 2121
Slide 60
After reset:
Digital I/O default to Input (Hi-Z) Analog capable pins default to analog
MCU 2121
Slide 62
VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0 VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0
MCU 2121
Slide 63
Bit n in TRISx controls the data direction of Bit n in PORTx 1 = Input, 0 = Output
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 64
Read LATX
Read PORTX
PORTX
I/O Pins
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 65
Q1
Q2
Q3
Qclks
Port B Options
Configuring PORTB Interrupt on Change
bit 7
GIE/GIEH: Global Interrupt Enable bit 1 = Enables all unmasked (IPEN=0) / high priority (IPEN=1) interrupts 0 = Disables all interrupts RBIE: RB Port Change Interrupt Enable bit 1 = Enables the RB port change interrupt 0 = Disables the RB port change interrupt RBIF: RB Port Change Interrupt Flag bit 1 = At least one of the pins RB7:RB4 changed state (must be cleared in software) 0 = None of the RB7:RB4 pins changed state
MCU 2121 Slide 67
bit 3
bit 0
PORTB Options
All PORTB pins have weak internal pull up.
One bit controls all pins
INTCON2 Register
R/W-1 bit 7 R/W-1 R/W-1 R/W-1 U-0 R/W-1 U-0 R/W-1 bit 0
bit 7
bit 0
MCU 2121
Slide 68
Development Tools
MPLAB IDE, ICD2, PICDEM2 Plus
MPLAB IDE
MPLAB Integrated Development Environment Integrates Microchip and 3rd Party Tools
Code Editor Assembler and Compilers Linker and Librarian Simulator, Debuggers and Emulators Programmers
MCU 2121
Slide 70
USB
MCU 2121
Slide 72
Analog:
Comparators
Two comparators Operates in sleep mode Generates interrupt / wakeup on output change Comparator output pins available Eight programmable modes
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 74
Analog Comparator
Key Registers
CMCON Register
R-0 bit 7 R-0 R/W-0 R/W-0 R/W-0 R/W-1 R/W-1 R/W-1 bit 0
C1INV: Comparator 1 Inversion CIS: Comparator Input Switch CM2:CM0: Comparator Mode
CVRCON Register
R/W-0 bit 7 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 bit 0
CVRSS: Comparator Vref Source CVREN: Comparator Vref Enable Selection CVROE: Comparator Vref Output Enable CVRR: Comparator Vref Range Selection CVR3:CVR0: Comparator Vref Value Selection
MCU 2121
Slide 75
Comparators Reset
R/W-0 R/W-0 R/W-1 R/W-1 R/W-1 bit 0
C1OUT C1OUT
RA0/AN0 VINVIN+
Off (Read as 0)
C1 + C2 +
State Change
VINVIN+
Off (Read as 0)
Slide 76
C1 + C2 +
State Change
VINVIN+
C1 + C2 +
State Change
VINVIN+
C1 + C2 +
RA4/C1OUT
State Change
VINVIN+
C1 + C2 +
RA4/C1OUT
State Change
VINVIN+
RA5/C2OUT
C1 + C2 +
RA4/C1OUT
State Change
VINVIN+
RA5/C2OUT
C1 + C2 +
From VREF Module
State Change
CM0 = 0 CM0 = 0
RA2/AN2
C2OUT C2OUT
CIS = 0 CIS = 0
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 82
Comparators Off
C1OUT C1OUT
RA0/AN0 VINVIN+
Off (Read as 0)
C1 + C2 +
State Change
VINVIN+
Off (Read as 0)
Slide 83
Analog:
Comparator Vref
MCU 2121
Slide 85
CVR3:0 CVR3:0
CVREN CVREN
16 Steps Source Select
R R
16:1 Mux
CVREF
CVRSS = 0 CVRSS = 0
Range Select
R R 8R
RA2/AN2/CVREF
CVRR CVRR
VREF-
Output Enable
CVOE CVOE
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 86
Analog:
10-bit A/D Converter Module
CHS3:CHS0 CHS3:CHS0
AN12 AN12 AN10 AN9 AN8 AN7* AN6* AN5* AN4
In ADCON0 Register
VAIN
ADC
VREF+ VREF-
ADRESH
ADRESL
VDD
x0 x1 1x 0x
time VC
ADRES VIN
10
time
SOURCE
ADC
CHOLD VSS
2008 Microchip Technology Incorporated. All Rights Reserved.
+ VC -
Acquisition Time is determined RS < 10k by Acquisition Time allows Pin Capacitance and time to Hold Capacitor Source impedance fully charge to VIN V (recommend <10k)
MCU 2121
Slide 89
Conversion Time
Acquisition Time Conversion Time Acquisition Time
time VC
ADRES VIN
ADC Result
10
ADC
CHOLD VSS
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
time
+ VC -
Slide 90
MCU 2121
Slide 91
Step 4: Set GO/DONE bit to start the conversion Step 5: Wait for the A/D conversion to complete
Wait for GO/DONE bit to be cleared OR Wait for A/D interrupt
Step 6: Read A/D result registers Step 7: Clear the ADIF flag Wait at least 2 T AD before next acquisition
MCU 2121
Slide 92
bit 5-2
bit 1
bit 0
MCU 2121
Slide 93
bit 5
bit 4
MCU 2121
Slide 94
MCU 2121
Slide 95
NOTE: This parameter determines the value of TAD: the conversion time per bit. bit 2-0 ADCS2:ADCS0: A/D Conversion Clock Selection bits
111 = FRC (Clock derived from A/D RC oscillator) 110 = FOSC/64 101 = FOSC/16 100 = FOSC/4 011 = FRC (Clock derived from A/D RC oscillator) 010 = FOSC/32 001 = FOSC/8 000 = FOSC/2
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 96
Minimum T AD = 0.7s
2008 Microchip Technology Incorporated. All Rights Reserved.
TAMP = 2 s
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 99
MCU 2121
Slide 103
bit 7
ADRESL
b7 b6 b7 b6 b5 b5 b4 b3 b4 b3 b2 b2 b1 b1 b0 b0
ADRESL
b1 b0 b1 b0
MCU 2121
Slide 104
Timers
Timer 0, Timer 1 and Timer 3 Modules
Timer Comparison
TIMER 0 SIZE OF REGISTER 8-bits or 16-bits Fosc/4 TIMERS 1 & 3 16-bits TIMERS 2 & 4 8-bits
CLOCK SOURCE (Internal) CLOCK SOURCE (External ) CLOCK SCALING AVAILABLE (Resolution) INTERRUPT EVENT
Fosc/4
Fosc/4
T0CKI pin
None
On overflow FFh00h NO
NO
MCU 2121
Slide 106
Timer 0
PIC16 Compatibility Mode
8-bit Timer/Counter 8-bit Programmable Prescaler Internal or External Clock Source Interrupt on Overflow from FF to 00
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1
TMR0L
Programmable Prescaler
T0SE T0SE
MCU 2121
Slide 107
Timer 0
16-bit Mode
16-bit Timer / Counter 16-bit Read / Write Mode Interrupt on overflow from FFFF to 0000 Same basic features as compatibility mode
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
Timer 0 Operation
TMR0IF TMR0IF FOSC/4 T0CKI
0 0 1
Programmable Prescaler
Clock Sync
1
TMR0L
T0SE T0SE
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
Timer 0 Operation
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
Timer 0 Operation
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
MCU 2121
Slide 111
Timer 0 Operation
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
Source Edge Select 1 = increment TMR0 on rising edge 0 = increment TMR0 on falling edge
MCU 2121
Slide 112
Timer 0 Operation
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
Timer 0 Operation
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
PS2 0 0 0
PS1 0 0 1 1 0 0 1 1
PS0 0 1 0 1 0 1 0 1
TMR0 RATE 1:2 1:4 1:8 1:16 1:32 1:64 1:128 1:256
Slide 114
0 1 1 1 1
MCU 2121
Timer 0 Operation
FOSC/4 T0CKI
0 0 1
TMR0IF TMR0IF
Clock Sync
1 READ TMR0L
High Byte
TMR0L
WRITE TMR0L
Programmable Prescaler
T0SE T0SE
TMR0H
DATA BUS
T0CON Register
TMR0ON T08BIT T0CS T0SE PSA T0PS2:0
TMR0ON 1 = ON 0 = STOPPED
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 115
MCU 2121
Slide 116
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
Clear Timer 1 (CCP Special Event Trigger)
READ TMR1L
High Byte
TMR1L
WRITE TMR1L
TMR1H
T1RUN
TCKPS1:0
T1OSCEN
T1SYNC
TMR1CS TMR1ON
Bit 0
T1CON Register
MCU 2121
Slide 117
Prescaler 1, 2, 4, 8
0
16-bit Read/Write Mode Enable 1 = Read/Write of Timer in one operation 0 = Read/Write of Timer in two 8-bit operations
TMR1IF TMR1IF
TMR1H Clear Timer 1 (CCP Special Event Trigger) TMR1L
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
T1OSCEN
T1SYNC
TMR1CS TMR1ON
Bit 0
Slide 118
Prescaler 1, 2, 4, 8
0
16-bit Read/Write Mode Enable 1 = Read/Write of Timer in one operation 0 = Read/Write of Timer in two 8-bit operations
TMR1IF TMR1IF
TMR1L
WRITE TMR1L
TMR1H
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
Clear Timer 1 (CCP Special Event Trigger) High Byte
READ TMR1L
TMR1L
WRITE TMR1L
TMR1H
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
Clear Timer 1 (CCP Special Event Trigger) High Byte
READ TMR1L
TMR1L
WRITE TMR1L
TMR1H
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
High Byte
TMR1L
WRITE TMR1L
TMR1H
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
Clear Timer 1 (CCP Special Event Trigger) High Byte
READ TMR1L
TMR1L
WRITE TMR1L
Timer1 System Clock Status Read Only Bit 1 = Device clock derived from Timer1 OSC 0 = Device clock derived from another source
TMR1H
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
Timer1 External Clock Synchronization Bit only used when TMR1CS = 1 1 = Synchronize external clock input 0 = Do not synchronize
High Byte
TMR1L
WRITE TMR1L
TMR1H
T1CON Register
RD16
Bit 7
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121
T1RUN
TCKPS1:0
Prescaler 1, 2, 4, 8
0
TMR1IF TMR1IF
High Byte
TMR1L
WRITE TMR1L
TMR1H
T1CON Register
RD16
Bit 7
T1RUN
TCKPS1:0
MCU 2121
Slide 125
CCP Module
PWM Mode
Timer 2
8-bit Timer with prescaler and postscaler Used as PWM time base TMR2 is readable & writable TMR2 resets on match with PR2 Match with PR2 generates interrupt Used as baud clock for MSSP (SPI)
MCU 2121
Slide 127
Timer 2
T2CON Register
R/W-0 -bit 7 R/W-0 T2OUTPS3 T2OUTPS3 R/W-0 T2OUTPS2 T2OUTPS2 R/W-0 T2OUTPS1 T2OUTPS1 R/W-0 T2OUTPS0 T2OUTPS0 R/W-0 TMR2ON TMR2ON R/W-0 T2CKPS1 T2CKPS1 R/W-0 T2CKPS0 T2CKPS0 bit 0
Reset
TMR2/PR2 Match
FOSC/4
TMR2
Comparator
MCU 2121
Slide 128
MCU 2121
Slide 129
CCP2
PWM Mode
CCPxCON<5:4> Master CCPR2L 5 4
Slave
R S
TB0 TB1 TMR2 * * Corresponding TRIS Bit TMR2 = PR2 TMR2 = PR2
Period
Comparator
PR2
Period
MCU 2121
Slide 130
Set PWM Period by writing to PR2 register Set PWM Duty Cycle by writing to CCPRxL and CCPxCON<5:4> bits Make the CCPx pin an output by clearing the appropriate TRIS bit Set the TMR2 prescale value, then enable TMR2 by writing to T2CON Configure the CCPx module for PWM mode
3.
4.
5.
MCU 2121
Slide 131
MCU 2121
Slide 132
PR2 =
-1
Choose TMR2PRE to ensure that PR2 is in the range of 0 to 255 for the desired PWM frequency.
MCU 2121
Slide 134
Choose TMR2PRE to ensure that CCPR2L:CCP2CON<5:4> is in the range of 0 to 1023 for the desired PWM duty cycle.
MCU 2121
Slide 135
CCPxCON Register
R/W-0 -bit 7 R/W-0 -R/W-0 DCxB1 DCxB1 R/W-0 DCxB0 DCxB0 R/W-0 CCPxM3 CCPxM3 R/W-0 CCPxM2 CCPxM2 R/W-0 CCPxM1 CCPxM1 R/W-0 CCPxM0 CCPxM0 bit 0
MCU 2121
Slide 136
RB3/AN9/CCP2(1)
TRISB<3> = 0 TRISB<3> = 0
RC1/T1OSI/CCP2(1)
(1)
TRISC<2> = 0 TRISC<2> = 0
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 137
bit 2
MCU 2121
Slide 138
bit 5-4 DC2B1:DC2B0: PWM Duty Cycle bit 1 and bit 0 for CCP2
These bits are the two LSbs of the 10-bit PWM duty cycle. The 8 MSbs (DCx9:DCx2) of the duty cycle are found in CCPR2L.
DC2B1:DC2B0 values determined when calculating PWM duty cycle DC2B1:DC2B0 values determined when calculating PWM duty cycle
CCP Module
Capture Mode
CCP Module
Capture Mode Applications
Event arrival time recording Period measurement Pulse-width measurement Interrupt generation Event counting Time reference* Duty cycle measurement*
MCU 2121
Slide 141
T3CCP2
TCKPS1:0
T3CON Register
*T3CCP2:1 Timer3 and Timer1 to CCP Enable 1x = Timer3 is clock source for CCP modules 01 = Timer3 is clock source for CCP2 Timer1 is clock source for CCP1 00 = Timer1 is clock source for CCP modules
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 142
CCP Module
Input Capture Mode
CCPxCON Register: CCPx Mode Select Bits 0100: Capture every falling edge 0110: Capture every 4th rising edge 0101: Capture every rising edge 0111: Capture every 16th rising edge T3CON Register Timer Mode Select bits CCPxM3:0 CCPxM3:0 T3CCP2:1 T3CCP2:1 TMR3H TMR 3 Enable 1:1, 1:4, 1:16 Prescaler CCPRxH TMR 1 Enable TMR1H CCPx Pin
2008 Microchip Technology Incorporated. All Rights Reserved.
TMR3L
Edge detect
CCPRxL
TMR1L
Set CCPxIF
MCU 2121 Slide 143
CCP Module
Compare Mode Applications
Generate
Single pulse Train of pulses Periodic waveform
MCU 2121
Slide 144
CCP Module
Output Compare Mode
Set CCPIF Special Event Trigger CCPR1H CCPR1L CCPx output pin
Comparator
O/P Logic
S Q R
CCPxM3:0 CCPxM3:0
TMR3 CCPxCON Register: CCPx Mode Select Bits 0010: Toggle CCPx pin on match (CCPxIF bit set) 1000: CCPx pin high on match (CCPxIF bit set) 1001: CCPx pin low on match (CCPxIF bit set) 1010: Generate s/w interrupt on match (CCPx pin not affected) 1011: Trigger special event on match (CCPIF bit set)
MCU 2121
Slide 145
MSSP
(Master Synchronous Serial Port) (Master Synchronous Serial Port)
I2CTM Mode
Standard (100 kHz), Fast (400 kHz)
Microchip 1 MHz proprietary protocol
SPI is a trademark of Motorola Semiconductor I2C is a trademark of Philips Semiconductors Microwire is a trademark of National Semiconductor
MCU 2121
Slide 147
MSSP
I2C Slave Mode
Data Bus
Write
SSPSR
LSb
Match Detect
SSPADD
MCU 2121
Slide 148
MSSP
I2C Master Mode
SSPADD<6:0> Data Bus SSPM3:SSPM0 SSPM3:SSPM0 Write SSPBUF
SDA In
Read
SDA SSPSR
MSb
Receive Enable
LSb
Bus Collision
SCL
SCL In
Start, Stop, WCOL Detect Clock Arbitration State Counter for end of TX/RX
Set/Reset, S, P, WCOL (SSPSTAT) Set SSPIF, BCLIF Reset ACKSTAT, PEN (SSPCON2)
MCU 2121
Clock Ctrl
Slide 149
=Synchronous Serial Port Enable Bit = Write Collision Detect Bit = Receive Overflow Indicator = Clock Polarity Bit (slave mode only)
SSPM<3:0>: Synchronous Serial Port Mode Select Bits 1111 = I2C Slave Mode, 10-Bit Address w/ Start/Stop interrupts 1110 = I2C Slave Mode, 7-Bit Address w/ Start/Stop interrupts 1011 = I2C Firmware controlled master mode (Slave Idle) 1000 = I2C Hardware master mode, clock = FOSC / (4 *(SSPADD+1)) 0111 = I2C Slave mode, 10-bit address 0110 = I2C Slave mode, 7-bit address
MCU 2121
Slide 150
General Call Enable (Slave mode only) Start condition enable Clock stretching enable MASTER MODE ONLY Acknowledge status bit (Master xmit only) Acknowledge data bit (Master rcv) only Acknowledge sequence enable (Master rcv only) Receive Enable Stop condition enable Repeated start enable
MCU 2121 Slide 151
SMP CKE
Read / Write bits = Slew Rate Control for High Speed Enable = SMbus select
D/A P S R/W UA BF
Read Only bits = Data / Not Address Bit (Slave Mode only) = Stop Bit = Start Bit = Read / Not Write Bit = Update Address Bit (for 10-bit slave address mode) = Buffer Full
MCU 2121
Slide 152
SSPADD Register
SSPADD Register
ADD7 ADD6 ADD5 ADD4 ADD3 ADD2 ADD1 ADD0
Reserve Addresses
B 0000 000 General Call Address 000 B0000 001 CBUS address 001 B0000 010 Reserved for different bus format 010 B0000 011 Future Purposes 011 B0000 1XX Hs-mode master code 1XX HsB1111 1XX Future Purposes 1XX B1111 0XX 10-bit Slave Address 0XX 10-
SLAVE: Holds slave device address MASTER: Lower 7 bits used by baud rate generator
MCU 2121
Slide 153
MCU 2121
Slide 154
MSSP
I2C Master Mode Baud Rate Generator
SSPADD<6:0> SSPM3:SSPM0 SSPM3:SSPM0 SSPADD<6:0>
SCL
Reload
FOSC/4
FCY FCY
10 MHz 10 MHz 10 MHz 10 MHz 10 MHz 10 MHz 4 MHz 4 MHz 4 MHz 4 MHz 4 MHz 4 MHz 1 MHz 1 MHz 1 MHz 1 MHz 1 MHz 1 MHz
2008 Microchip Technology Incorporated. All Rights Reserved.
2*FCY 2*FCY
20 MHz 20 MHz 20 MHz 20 MHz 20 MHz 20 MHz 8 MHz 8 MHz 8 MHz 8 MHz 8 MHz 8 MHz 2 MHz 2 MHz 2 MHz 2 MHz 2 MHz 2 MHz
MCU 2121
(2 Rollovers of BRG) (2 Rollovers of BRG) 400 kHz 400 kHz 312.5 kHz 312.5 kHz 100 kHz 100 kHz 400 kHz 400 kHz 308 kHz 308 kHz 100 kHz 100 kHz 333 kHz 333 kHz 100 kHz 100 kHz 1 MHz 1 MHz
Slide 155
FSCL FSCL
What is it?
It allows a PIC MCU to ACK more than one address.
PIC18F45K20
MCU 2121
Slide 156
Address Masking Feature Example A 0 in the SSPMSK register ignores the corresponding address bit Example: SSPADD = 0b1010111R SSPMSK = 0b1111100R Module will respond to addresses:
0b1010111R, 0b1010110R, 0b1010101R, 0b10101100R
MCU 2121
Slide 157
Write
1. Start Condition detected 2. Address shifted into SSPSR 3. SSPSR<7:1> compared to SSPADD<7:1> IF match AND no overflow
RC3/SCK/SCL RC4/SDI/SDA
MSb
SSPSR
LSb
Match Detect
SSPADD
1. SSPSR loaded into SSPBUF 2. BF = 1; read SSPBUF to clear 3. ACK generated 4. SSPIF = 1 (must be cleared via software) 5. R/W bit updated in SSPSTAT
MCU 2121
Slide 158
Write
Data is shifted into SSPSR 8-bits => latch to SSPBUF IF BF and SSPOV = 0, ACK is sent Read SSPBUF to clear BF SSPIF = 1 (clear in s/w)
RC3/SCK/SCL RC4/SDI/SDA
MSb
SSPSR
LSb
Match Detect
OVERFLOW Data fills SSPSR before SSPBUF is read SSPOV set and NACK sent Master sends STOP Addr Match
SSPIF
SSPADD
MCU 2121
Slide 159
Write
RC3/SCK/SCL RC4/SDI/SDA
MSb
SSPSR
LSb
Addr Match
Match Detect
SSPADD
MCU 2121
Slide 160
I2C Done?
YES
I 2C
YES
Done?
NO
ACK?
YES
I 2C
Done?
YES
Xmit Done?
YES
ACK?
YES
MCU 2121
Slide 161
1;
//Set PORTC to input, especially RC3 and RC4 //Set PORTB to output LEDs used to indicate error //Configure MSSP for Hardware Master Mode and enable //Disable slew rate and select I2C bus levels //Setup baud rate generator //Initiate START condition
MCU 2121
Slide 162
void ack_error(void) { SSPCON2bits.PEN = 1; //Send STOP condition PORTB = 0xFF; //Turn on LEDs to indicate error while(1); //In real code this would be handled better } void Wait_I2C(void) { while(!SSPIF); //SSPIF is set after all I2C operations, wait for flag SSPIF = 0; //Clear flag } MCU 2121 2008 Microchip Technology Incorporated. All Rights Reserved.
Slide 163
Done?
NO
Done?
YES
YES
Read Another?
YES
Send NACK
NO
Send ACK
NO
I2C Done?
YES
I 2C
Done?
YES
ACK?
YES
Send Restart
MCU 2121 Slide 164
EUSART
Enhanced Universal Synchronous Receiver/Transmitter
EUSART
Summary
Full-duplex asynchronous -or- half-duplex synchronous 8- or 9-bit data Double-buffered transmit and receive buffers 9-bit addressable mode Auto-Wake-up on character reception Auto-Baud calibration Max baud rates @ 40 MHz Synchronous: 10 Mbaud Asynchronous: 625 Kbaud/2.5 Mbaud
MCU 2121
Slide 166
EUSART
Block Diagram
TX9D TXIE Interrupt 9 TX8/9 TXDATA
TXREG
TxSTA
RxSTA
TXIF
TSR
TXCLK
OERR FERR
RXDATA RCCLK ADDEN RC8/9 RX9D 9
RSR
C O N T R O L
Baud Rate Clock
RC6/TX/CK
RC7/RX/DT
RCREG
RCIF
RCIE
Tcy
MCU 2121
Slide 167
TRANSMIT CONTROL = Clock Source Select (synchronous modes only) = Enable 9-bit transmission = Enable transmit = Select synchronous or asynchronous mode = High baud rate select (asynchronous mode only) TRANSMIT STATUS = Transmit shift register status (0= full, 1=empty) = Ninth bit of transmit data
TRMT TXD9
MCU 2121
Slide 168
RECEIVE CONTROL SPEN = Serial port enable RX9 = Enable 9-bit reception SREN = Single receive enable (synchronous mode only) CREN (asynch) = Enable receiver CREN (sync) = Enable continuous receive (overrides SREN) ADDEN = Enable address detect (asynch = 1) RECEIVE STATUS = Framing error = Overrun error (cleared by clearing CREN) = Ninth bit of receive data
MCU 2121
Slide 169
SYNC 0 0 1 1
BRG16 0 1 0 1
MCU 2121
Slide 170
SPBRG = 4 X 106 / (16 * 9600)) 1 SPBRG = 25.042 Load SPBRG with nearest integer value: SPBRG=> 0x19 SPBRGH => 0x00
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 171
MCU 2121
Slide 172
= Auto Baud Rate acquisition rollover status = Receive Operation Idle = Clock polarity (synch mode only) = Enable 16-bit baud rate register = Wake up enable (asynch mode only) = Auto baud detection enable (asynch mode only)
MCU 2121
Slide 173
EUSART
Transmit Operation
TXREG empty, will set TXIF Loading TXREG resets TXIF TSR empty will set TRMT Loading TSR resets TRMT If TXREG is loaded and TRMT is set, then data is immediately loaded to TSR. Serial data shifting starts and TXIF will be set
TXIF
TXREG
TRMT
TSR
RC6/TX/CK
MCU 2121
Slide 174
EUSART
Receive Operation
RSR receives data with valid start/stop Data loaded into RCREG FIFO and RCIF = 1 If 2nd byte is received before the 1st has been serviced, then new data will be placed in the 2nd location on the FIFO When servicing the receive interrupt, after reading the 1st byte, if another byte is still in the FIFO, then a second RCIF interrupt is generated
RSR ADDEN
RC7/RX/DT
RCREG RCIF
2 deep FIFO
MCU 2121
Slide 175
Secondary
Timer1 Oscillator - fixed frequency Required for Real Time Clock time base
Internal RC Oscillator
INTOSC (8 MHz) source
4, 2, 1 MHz, 500, 250, 125 and 31 kHz
MCU 2121
Slide 177
MCU 2121
Slide 178
MCU 2121
Slide 179
3 Categories
RUN - 3 clock sources IDLE - 3 clock sources SLEEP - no clocks Total = 7 Modes
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 180
SEC_RUN Mode
Clock switching mechanism in other PIC18 controllers Timer1 source, Primary oscillator is disabled
RC_RUN Mode
IRCF<2:0> selects clock speed IOFS set after 1us (typ.) delay if Freq 31 kHz
2008 Microchip Technology Incorporated. All Rights Reserved. MCU 2121 Slide 181
MCU 2121
Slide 183
MCU 2121
Slide 184
Sleep Mode
The processor can be put into a power-down mode by executing the SLEEP instruction
System oscillator is stopped Processor status is maintained (static design) Watchdog timer continues to run, if enabled Minimal supply current is drawn - mostly due to leakage (0.1 - 2.0A typical)
Events that wake processor from sleep Events that wake processor from sleep
MCLR MCLR WDT WDT INT INT TMR1 TMR1 ADC ADC CMP CMP CCP CCP PORTB PORTB SSP SSP PSP PSP Master Clear Pin Asserted (pulled low) Master Clear Pin Asserted (pulled low) Watchdog Timer Timeout Watchdog Timer Timeout INT Pin Interrupt INT Pin Interrupt Timer 1 Interrupt (or also TMR3 on PIC18) Timer 1 Interrupt (or also TMR3 on PIC18) A/D Conversion Complete Interrupt A/D Conversion Complete Interrupt Comparator Output Change Interrupt Comparator Output Change Interrupt Input Capture Event Input Capture Event PORTB Interrupt on Change PORTB Interrupt on Change Synchronous Serial Port (I2C Mode) Start // Stop Bit Detect Interrupt Synchronous Serial Port (I2C Mode) Start Stop Bit Detect Interrupt Parallel Slave Port Read or Write Parallel Slave Port Read or Write
MCU 2121 Slide 185
Watchdog Timer
Helps recover from software malfunction Uses its own free-running on-chip RC oscillator WDT is cleared by CLRWDT instruction Enabled WDT (WDTEN) cannot be disabled by software WDT overflow resets the chip Programmable timeout period: 18ms to 3.0s typical Operates in SLEEP; on time out, wakes up CPU
MCU 2121
Slide 186
Pin Pin VPP VPP VDD VDD VSS VSS RB6 RB6 RB7 RB7
Function Function Programming Voltage = 13V Programming Voltage = 13V Supply Voltage Supply Voltage Ground Ground Clock Input Clock Input Data I/O & Command Input Data I/O & Command Input
PIC18Fxxxx
ICSP Connector
MCU 2121
Slide 187
MCU 2121
Slide 188
MCU 2121
Slide 189
MCU 2121
Slide 190
Internal VREF
MCU 2121
Slide 191
PIC18 RESETS
Power-on Reset (POR) MCLR Reset during normal operation Programmable Brown-out Reset (BOR) Watchdog Timer (WDT) Reset (during execution) RESET Instruction Stack Full Reset Stack Underflow Reset
MCU 2121
Slide 192
After RESET PC will have the address 0x000000 Following bits will be affected after each RESETS
POR = 0: Power On RESET BOR = 0 & POR = 1: BOR RESET TO = 0: WDT RESET RI = 0: RESET Instruction STKFUL = 1: Stack over flow RESET STKUNF = 1: Stack under flow RESET POR, BOR, TO & RI = 1 and STKFUL & STKUNF = 0: MCLR RESET
MCU 2121
Slide 193
Summary
Summary
We discussed PIC18 Architecture
Programmers Model Instruction set Overview Interrupt Handling and latency
MCU 2121
Slide 195
Summary
Timers CCP (Capture, Compare & PWM) MSSP (I2C & SPI / Microwire) USART
Discussed
Oscillator and power saving modes Special features of PIC18F controllers
MCU 2121
Slide 196
Summary
Carried out the Labs on
Basic C18 Project Creation IO Port initialization and reading and writing to the Ports Initialization and ADC conversion Timer configuration Generating PWM Capturing the input signal I2C module configuration EUSART configuration
MCU 2121
Slide 197
References
PIC18F4520 Datasheets PIC18F family reference manual PICDEM2 Plus Users Guide MPLAB C18 C Compiler Users Guide MPLAB C18 C Compiler Getting Started Guide MPLAB C18 C Compiler Libraries Guide Other PIC18-based classes
101_TLS: Introduction to MPLAB IDE, ICD2 102_ASP: Introduction to PIC18F Arch. 202_ASP: PIC18 Peripherals in Asm. 390_USB: USB From Scratch
MCU 2121
Slide 198
Trademarks The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KeeLoq, microID, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart, rfPIC and SmartShunt are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, SEEVAL, SmartSensor and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, Linear Active Thermistor, Mindi, MiWi, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool, REAL ICE, rfLAB, rfPICDEM, Select Mode, Smart Serial, SmartTel, Total Endurance, UNI/O, WiperLock and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies.
MCU 2121
Slide 200