PIC18Fxxx Comprehensive Tutorial Containing 7Mb of Info
PIC18Fxxx Comprehensive Tutorial Containing 7Mb of Info
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 1
PIC18FXXX Hands On
Workshop Agenda
l PIC18FXXXX architecture, peripherals and special features
l PICmicro product overview including future products
l PIC18FXXXX development tool overview
l Audio Spectrum Analyzer Demo Board design
l Lab 1 - Install MPLAB 6.0, MPLAB ICD 2, MPLAB C18,
Demo Board, Create Project, Compile and Run, Display
Message
l Lab 2 - Develop a traffic light
l Lab 3 - A/D Sampling ISR, Fill A/D sample buffer
l Lab 4 - Apply DFT to A/D sample buffer, scale and display
DFT results.
l Lab 5 - Extra credit- Add Automatic Gain Control
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 2
PIC18FXXX Workshop
Appendix A-D
l The following Appendix topics are available
for your reference, but will not be presented
today:
l Appendix A: Optimizing C source code for
compiler efficiency
l Appendix B: PIC18FXXXX Instruction Set,
PIC16/17 migration
l Appendix C: PIC18FXXXX Flash Programming
Tips
l Appendix D: PIC18FXXXX Peripheral
Calculation Spreadsheet
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 3
Microchip Technology Inc.
Company Overview
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 4
Corporate Overview
l Leading semiconductor manufacturer:
l of high-performance, field-programmable
8-bit & 16-bit RISC Microcontrollers
l of Analog & Interface products
l of related Memory products
l for high-volume embedded control
applications
l $572 million in product sales in FY02
l More than 3,000 employees
l Headquartered near
Phoenix in Chandler, AZ
400
350 Memory
300 MCU
250 469 447
200 393
150 272 306
100 220
167
50 122
32 58
0
FY 93 FY 94 FY 95 FY 96 FY 97 FY 98 FY 99 FY 00 FY01 FY02
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 7
Worldwide Manufacturing Locations
Washington
Fab 3
710K sq feet
Shanghai
Assembly &
Test
80 K sq feet
Arizona Corp. HQ
Fab 1 Bangkok
270 K sq feet Assembly &
Test Facility
Fab 2 190K sq feet
178 K sq feet
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 8
Existing PICmicro®® MCU Core
and Peripheral Blocks
RF
Xmit/receive
High Voltage Telecom
Precision IR
DTMF
Digital Pot Voltage I/O’s Communication
Codec
Reference
Power Motors
Relays
Sensors Amplifiers Filters A/D Drivers Print-heads
VF
Drivers
SRAM Transceivers
Bus
Communication Digital Encryption
- RS232/485 - CAN bus
- CAN bus - USB Peripherals
PWM
(KEELOQ®)
Speech
LED
- I 2C™
- USB - SPI™
- RS422/423
Real Time Clock Co-Processing Drivers
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 9
Microcontroller Market Pyramid
32-Bit
PIC18CXXX 16-Bit
Enhanced MCU Core
PIC17CXXX
High-Performance
Family
PIC16CXX
PIC16CXX 8-Bit
Mid-Range
Mid-Range Family
Family
PIC16C5X
PIC16C5X
Baseline
Baseline Family
Family
PIC12CXXX
PIC12CXXX
8-Pin
8-Pin Family
Family 4-Bit
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 10
PICmicro® Strategic Directions
New Process Development
2.0 to 5.5 volts - 0.4 micron
ROMs 1.8 to 3.6 volts - 0.18 micron
H.V. Foundry
8 ® 1
CSICs & r
RO rive High Density Memory
Verticals M D
ech ROMless,
T
HCS101/201
HCS365/370 7 CSIC ory
2 FLASH
& Mem
HCS412 Vert
icals Large PIC18C801
PIC18F8720
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 11
PICmicro®® MCU Product Migration Path
Today
159 Products
l Enhanced FLASH, OTP (EPROM),
EEPROM and ROM program
memory
80/84-Pin
l Superior Analog functionality Family
64/68-Pin
l Industry’s strongest product and Family 8KWord - 16KWord
40/44-Pin
family migration path Family 4KWord - 16KWord
28-Pin
Family
18/20-Pin 2KWord - 16KWord
Family .5KWord - 16KWord
14-Pin
Family ation
8-Pin
.5KWord - 4KWord
Migr
Family 1KWord less
eam
.5KWord - 2KWord S
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 12
PICmicro®® 8-Pin Families
VDD VSS
GP5/OSC1/CLKIN GP0/AN0
GP4/OSC2/AN3/CLKOUT M GP1/AN1/Vref
GP3/MCLR/VPP GP2/TOCKI/AN2/INT
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 13
PICmicro®® 18-Pin Families
RA2/AN2/Vrefout RA1/AN1
RA3/AN3/CMP1/Vrefin RA0/AN0
RA4/TOCKI/CMP2 OSC1/CLKI/RA7
MCLR/VPP/RA5/THV OSC2/CLKO/RA6
VSS
RB0/INT
M VDD
RB7/T1OSI
T1OSO/T1CKI /RB1/RX/DT RB6/ T1OSO/T1CKI
T1OSI /RB2/TX/CK RB5
RB3/CCP1 RB4/PGM
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 14
PICmicro®® 20-Pin Families
RA0/AN0/OPA+ RB3/CCP1/P1A/OPA/PWM1
RA1/AN1/LVDIN/OPA- RB2/SCK/SCL/PWM0
RA4/TOCKI OSC1/CLKIN/RA7
RA5/MCLR/VPP OSC2/CLKOUT/RA6
M
VSS VDD
AVSS AVDD
RA2/AN2/Vrl/Vref-/PWM4 RB7/T1OSI/P1D/ PSMC1B
RA3/AN3/Vrh/Vref+/PWM5 RB6/T1OSO/T1CKI/P1C/PSMC1A
RB0/AN4/INT/Vr RB5/SDO/P1B/PWM3
RB1/AN5/SS/Vdac RB4/SDI/SDA/PWM2
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 15
PICmicro®® 28-Pin Families
MCLR/VPP RB7/PGD
RA0/AN0 RB6/PGC
RA1/AN1 RB5/PGM
RA2/AN2/Vrl/Vref- RB4
RA3/AN3/Vrh/Vref+ RB3/CCP2/CANRX
RA4/TOCKI RB2/INT2/CANTX
RA5/SS/AN4/AVDD/Lvdin RB1/INT1
AVSS
OSC1/CLKI
M RB0/INT0
VDD
OSC2/CLKO/RA6 VSS
RC0/T1OSO/T1CKI RC7/RX/DT
RC1/T1OSI/CCP2 RC6/TX/CK
RC2/CCP1 RC5/SDO/D+
RC3/SCK/SCL RC4/SDI/SDA/D-
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 16
PICmicro®® 40-Pin Families
MCLR/VPP RB7/PGD/KBI3
RA0/AN0 RB6/PGC/KBI2
RA1/AN1 RB5/PGM/KBI1
RA2/AN2/Vrl/Vref- RB4/KBI0
RA3/AN3/Vrh/Vref+ RB3/CCP2/CANRX
RA4/TOCKI RB2/INT2/CANTX
RA5/SS/ AN4/AVDD/Lvdin RB1/INT1
RE0/RD/AN5 RB0/INT0
RE1/WR/AN6 VDD
M
RE3/CS/AN7 VSS
AVDD RD7/PSP7/PD
AVSS RD6/PSP6/PC
OSC1/CLKI RD5/PSP5/PB
OSC2/CLKO/RA6 RD4/PSP4/ECC/PA
RC0/T1OSO/T1CKI RC7/RX/DT
RC1/T1OSI/CCP2 RC6/TX/CK
RC2/CCP1 RC5/SDO/D+
RC3/SCK/SCL RC4/SDI/SDA/D-
RD0/PSP0/C1IN+ RD3/SPS3/C2IN-
RD1/PSP1/C1IN- RD2/PSP2/C2IN+
PIC16CR65 PIC16C74B PIC16F74 PIC18F442 PIC18F448
PIC16C65B PIC16C77 PIC16F77 PIC18F452 PIC18F458
PIC16C67 PIC16C774 PIC16F871 PIC18F4450 PIC18C442
PIC16C662 PIC16C765 PIC16F874/A PIC18F4550 PIC18C452
PIC16F877/A PIC18F4220
PIC18F4320
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 17
Quad Flat No Lead (QFN)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 18
Cumulative PICmicro®® Shipment
(Millions of Units)
2000
1851
1800
2.0
1600
Billion 1472
1400 Shipped
May 22, 02
1200
1077
1000
800 771
600 542
400 365
241
200 141
76
5 12 23 40
0
CY CY CY CY CY CY CY CY CY CY CY CY CY
89 90 91 92 93 94 95 96 97 98 99 00 01
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 19
Thousands of Customers
Office
Consumer Automotive Automation Telecom Industrial
Black & Decker BMW Alps Codex Allen-Bradley
Coleman Ford Apple Computer Ericsson American Sensors
Genie Delphi Conner Kyocera Banner
Goldstar Honda Compaq Motorola Code Alarm
Hamilton Beach JCI DEC Nokia Foxboro
JVC Lear Dell Computer Northern General Electric
Mitsubishi Lexus Hewlett Packard Telecom Honeywell
Panasonic Mercedes/Benz IBM Pacific ILCO-Unican
Philips Monolithics
Nissan Logitech Invensys
Samsung Pulsecomm
Robert Bosch Microsoft Pitney Bowes
Sanyo Qualcomm
Sagem Mitsumi Tandy
Sega Rockwell
Siemens/VDO NCR United
Sony Sagem Technologies
Stribel Panasonic
Sunbeam Samsung Wayne Systems
Toyota Quantum
Toshiba Siemens Whirlpool
TRW Texas Instruments
Whirlpool UDS
Valeo
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 20
Process Technology
Advancements
PIC16C77 (0.9µ) PIC16C77 (0.7µ)* PIC16F77 (0.5µ)
S
S
P
* Equivalent device
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 21
Worldwide 8-bit Microcontroller
Market Share - Units
No.
No. 1990
1990 1991
1991 1992
1992 1993
1993 1994
1994 1995/96
1995/96 1997-00
1997-00
Rank Rank
Rank Rank Rank
Rank Rank
Rank Rank
Rank Rank
Rank Rank
Rank Rank
Rank
11 Motorola
Motorola Motorola
Motorola Motorola
Motorola Motorola
Motorola Motorola
Motorola Motorola
Motorola Motorola
Motorola
22 Mitsubishi
Mitsubishi Mitsubishi
Mitsubishi Mitsubishi
Mitsubishi Mitsubishi
Mitsubishi Mitsubishi
Mitsubishi Mitsubishi
Mitsubishi Microchip
Microchip
33 NEC
NEC NEC
NEC Intel
Intel NEC
NEC NEC
NEC SGS-Thomson
SGS-Thomson NEC
NEC
44 Intel
Intel Intel
Intel NEC
NEC Hitachi
Hitachi Philips
Philips NEC
NEC Hitachi
Hitachi
55 Hitachi
Hitachi Hitachi
Hitachi Philips
Philips Philips
Philips Intel
Intel Microchip
Microchip ST-Micro
ST-Micro
66 Philips
Philips Philips
Philips Hitachi
Hitachi Intel
Intel Microchip
Microchip Philips
Philips Infineon
Infineon
77 Matsushita
Matsushita Matsushita
Matsushita Matsushita
Matsushita SGS
SGS Zilog
Zilog Zilog
Zilog Mitsubishi
Mitsubishi
88 National
National SGS-Thomson
SGS-Thomson SGSSGS Microchip
Microchip SGS
SGS Hitachi
Hitachi Philips
Philips
99 Siemens
Siemens Siemens
Siemens National
National Matsushita
Matsushita Matsushita
Matsushita Fujitsu
Fujitsu Toshiba
Toshiba
10
10 TI
TI TI
TI TI
TI Toshiba
Toshiba Hitachi
Hitachi Intel
Intel Atmel
Atmel
11
11 Sharp
Sharp National
National Zilog
Zilog National
National Toshiba
Toshiba Siemens
Siemens Zilog
Zilog
12
12 Oki
Oki Toshiba
Toshiba Toshiba
Toshiba Zilog
Zilog National
National Toshiba
Toshiba Fujitsu
Fujitsu
13
13 Toshiba
Toshiba Sony
Sony Siemens
Siemens TI
TI TI
TI Matsushita
Matsushita Matsushita
Matsushita
14
14 SGS-Thomson Sharp
SGS-Thomson Sharp Microchip
Microchip Siemens
Siemens Ricoh
Ricoh TI
TI Realtek
Realtek
15
15 Zilog
Zilog Oki
Oki Sharp
Sharp Sharp
Sharp Fujitsu
Fujitsu National
National Samsung
Samsung
16
16 Matra MHS
Matra MHS Zilog
Zilog Sanyo
Sanyo Oki
Oki Siemens
Siemens Temic
Temic National
National
17
17 Sony
Sony Microchip
Microchip Matra
Matra MHS
MHS Sony
Sony Sharp
Sharp Sanyo
Sanyo Sanyo
Sanyo
18
18 Fujitsu
Fujitsu Matra
Matra MHS
MHS Sony
Sony Sanyo
Sanyo Oki
Oki Ricoh
Ricoh Elan
Elan
19
19 AMD
AMD Fujitsu
Fujitsu Oki
Oki Fujitsu
Fujitsu Sony
Sony Oki
Oki TI
TI
20
20 Microchip
Microchip Sanyo
Sanyo Fujitsu
Fujitsu AMD
AMD Temic
Temic Sharp
Sharp Sony
Sony
Based on unit shipment volume 1990-2000, Source: Dataquest, July 2001
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 22
PIC18 Architecture
And
Peripherals
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 23
PIC18 Architecture
Features
l High Performance 8-bit RISC CPU
l 40 MHz / 10 MIPs sustained operation
l 2.0V to 5.5V operation
l Linear Program Memory addressing to 2MB
l Linear Data Memory addressing to 4KB
l 3 Data Pointers with 5 addressing modes
l Relative conditional branch instructions
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 24
PIC18 Architecture
Features (Continued)
l Up to 10MIPS @ 10MHz with 4X PLL
l Enhanced Flash memory
l 2 Seconds Programming Time
l Low Cost MPLAB-ICD-II Support
l Flexible Program Memory Protection
l And Many More...
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 25
PIC18 Architecture
Harvard Architecture
l Separate memory spaces for instructions
and data
l Increased throughput
l Different program and data bus widths are possible
“rom”
keyword
accessed 16 PIC18 8 Data
Memory
Flash RISC
CPU (Up to 4KB)
Program
Memory
(Up to 2MB)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 26
PIC18 Block Diagram
MULWF POSTINC1
Table Pointer <2>
0000 001 1 11100110 5 8 8 Data RAM
21 (up to PRODH PRODL
21 Inc/dec logic 4K Bytes)
00001100 01001001
12
PCLATU PCLATH PORTS PERIPHERALS 8x8
Address<12>
Program Memory Address<12>
Multiply
8
(up to 2M Bytes) 4 12
PCU PCH PCL 11100110 00100101
Program Counter BSR FSR0 01010101
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 27
PIC18 Architecture
Oscillator
l Various oscillator modes
LP Low Power Crystal (200KHz max)
XT Crystal/Resonator (4MHz max)
HS High Speed Crystal/Resonator (40MHz max)
HS + PLL HS + 4X PLL (10MHz max)
RC External RC (4MHz max)
RCIO RC with OSC2 as I/O (4MHz max)
EC External Clock (40MHz max)
ECIO EC with OSC2 as I/O (40MHz max)
INTOSC Internal RC Oscillator (30/500 kHz, 1/4/8 MHz)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 28
PIC18 Architecture
Clocking Scheme
l Instruction cycle = 1/4 of clock input
frequency
l 100 ns Instruction cycle at 40 MHz clock
Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4
OSC1
Q1
Q2
Q3
Q4
OSC2
1 instruction cycle
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 29
PIC18 Architecture
Instruction Pipeline
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 30
PIC18 Architecture
ALU
Constant IR
OR
Bank 0 l Operates on WREG
Bank 1
Register
Bank 2 and a Register or
Bank 3
Bank 4
Constant
ALU
Bank 5 l Multi-Byte
Other Banks
calculation using
ADDWFC etc.
WREG Register
Special Function
Registers (SFR
(SFR))
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 31
PIC18 Architecture
8 x 8 Hardware Multiplier
l Single Cycle Hardware Multiplier
l Performs
l WREG X Register
l WREG X Constant
l 16-bit result stored in PRODH:PRODL
l Integer arithmetic operation
l Unsigned operation
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 32
PIC18 Architecture
Computation Performance
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 33
PIC18 Architecture
Indirect Access
l Indirect Addressing
l Three 12-bit FSRs
l FSRnH:FSRnL (0 ≤ n ≤ 2) GPR (Bank n-1)
l Linear access to 4KB
l Special Instruction to load GPR (Bank n)
12-bit FSR
FSRn in 2 cycles
l De-reference operations
GPR (Bank n+1)
l Unchanged
l Pre/Post Increment
l Post Decrement
l Indexed by WREG (signed)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 34
PIC18 Architecture
Stack Memory
l Hardware stack - 31 levels deep
l Separate memory, pointed by STKPTR
l Used by CALL, RCALL, INT, RETURN, RETFIE
20 0
Stack Level 1
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 35
PIC18 Architecture
Accessing HW Stack
l 5-bit Stack Ptr addresses 21-bit wide stack
l Top-Of-Stack = TOSU:TOSH:TOSL
l Readable & Writeable => RTOS Friendly
l PUSH puts current PC on Top-Of-Stack
l POP discards Top-Of-Stack
l When enabled, Stack OV resets the device
l Stack Underflow returns 00000h
TOSU TOSH TOSL
Top-Of-Stack
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 36
PIC18 Architecture
Program Memory
l Up to 2M x 8 in size* 000000h
Reset Vector
l Linear access
High Priority Interrupt Vector 000008h
l Two Interrupt Vectors
Low Priority Interrupt Vector 000018h
l Self programmable*
Rest Of
l Programmable over entire Program Memory
voltage range
Unimplemented
l Flexible Code Protection Read ‘0’
1FFFFFh
Modes* 200000h
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 37
PIC18 Architecture
Program Memory Organization
l Divided into blocks Boot Block
0
512
l 512 bytes of Boot block* Block 0
l 8KB on PIC18F452
...
l Blocks erased in bulk or 64*
bytes Read ‘0’
l Bulk erase in ICSP™ Or
External
programming mode (4.5 - 5.5V) Memory
2 M
l Code protection by block
l Internal Read/Write protection 8-bit Wide
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 38
PIC18 Architecture
Program Memory : Protection
Three types of Protection Scheme:
ICSP programming mode Reads from same block OK, Self Write to this block are disabled
Read and Write disabled reads from other blocks disabled
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 39
PIC18 Architecture
Program Memory Modes
Four Modes:
Extended Microprocessor
Microcontroller Microprocessor
Microcontroller With
Mode Mode
Mode Boot Block Mode
Internal 0 Internal 0 0 Internal 0
Internal Internal Internal
Boot
BootBlock
Block 512 Boot
BootBlock
Block 512 Boot
BootBlock
Block 512
Internal
Internal Internal
Internal
Program Program Program
Space Program Program
Flash
Flash Flash External
External
Flash
Program
Program External
External
Memory
Memory Program
Program
External
External Memory
Read Memory
Readas
as‘0’
‘0’ Program
Program
Memory
Memory
2M 2M 2M 2M
0 0 0
Data 0
Space Internal
Internal Internal
Internal Internal
Internal Internal
Internal
4K 4K 4K 4K
Note: Check your device datasheet
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 40
PIC18 Architecture
Accessing Program Memory
l 21-bit Divided into PCU:PCH:PCL
l PCL is readable/writeable
l PCU:PCH is readable/writeable via shadow
registers only
PCLU PCLH
16-bit Wide
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 41
PIC18 Architecture
Reading Program Memory
TBLRD Operation
TBLPTRU TBLPTRH TBLPTRL<7:1> <0> TABLAT
tblrd*+
MSB LSB
Program Memory
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 42
PIC18 Architecture
Writing to Program Memory
Table Pointer TBLPTRU TBLPTRH TBLPTRL
movff LOW(DATA),TABLAT
tblwt*+
movff HIGH(DATA),TABLAT
tblwt*
See Appendix C for more information
Holding LOW(DATA)
Latch
Internal Program Memory
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 43
PIC18 Architecture
Accessing Program Memory ((Cont.)
Cont.)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 44
Table Pointer Operations
l To enhance flexibility of table operations, the
TBLPTR automatically increment and decrement
during read/write operations
l PIC18 devices have 4 modify modes for TBLPTR
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 45
PIC18 Architecture
Data EEPROM
l Size ranges from 64 to 1024 bytes
l 1 M erase/write cycles (typical)
l > 40 years retention (typical)
l Read and Written at byte boundary
l Automatic Erase-Before-Write
l Protection against “run-away” code
l Code Protection And Internal Write Protection
l Accessed via EEADR, EEDATA and
EECONn registers
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 46
PIC18 Architecture
Configuration
l Configuration Registers at 300000h
l Bit(s) enable/define mode(s)
l Written one byte at a time
l Writeable in all modes
l Special “Configuration Write Protect” bit
l Most bits can be written to either ‘1’ or ‘0’
l Code, Read and Write Protection bits can be
written ‘1’ -> ‘0’ only
l Bulk Erase required to reset Code, Read and
Write Protection bits to a ‘1’
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 47
Specifying Configuration
Information in Source File
l Create “config.asm” file and include in project:
#include p18f452.inc
__CONFIG _CONFIG1L,0xFF
__CONFIG _CONFIG1H,_OSCS_OFF_1H&_HSPLL_OSC_1H
__CONFIG _CONFIG2L,_BOR_OFF_2L&_BORV_20_2L&_PWRT_OFF_2L
__CONFIG _CONFIG2H,_WDT_OFF_2H&_WDTPS_128_2H
__CONFIG _CONFIG3L,0xFF
__CONFIG _CONFIG3H,_CCP2MX_OFF_3H
__CONFIG _CONFIG4L,_STVR_ON_4L&_LVP_OFF_4L&_DEBUG_OFF_4L
__CONFIG _CONFIG4H,0xFF
__CONFIG _CONFIG5L,_CP0_OFF_5L&_CP1_OFF_5L&_CP2_OFF_5L&_CP3_OFF_5L
__CONFIG _CONFIG5H,_CPB_OFF_5H&_CPD_OFF_5H
__CONFIG _CONFIG6L,_WRT0_OFF_6L&_WRT1_OFF_6L&_WRT2_OFF_6L&_WRT3_OFF_6L
__CONFIG _CONFIG6H,_WRTC_OFF_6H&_WRTB_OFF_6H&_WRTD_OFF_6H
__CONFIG _CONFIG7L,_EBTR0_OFF_7L&_EBTR1_OFF_7L&_EBTR2_OFF_7L&_EBTR3_OFF_7L
__CONFIG _CONFIG7H,_EBTRB_OFF_7H
END
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 48
C Programmer’s
Interface
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 49
Accessing Peripheral Control
and Status Bits
l All peripheral control bits set up in
<processor>.h file as:
l Example:
l GIEH bit of INTCON can be accessed by:
INTCONbits.GIEH
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 50
Reset Vector
l Located at 0x00000, compiler automatically initializes
variables
l Calls main() after variable initialization
while(1){
// Place your main loop here
}
}
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 51
PIC18 Architecture
Interrupt Overview
l Interrupt Sources can individually
l Assigned to high or low priority vector
l High Priority Vector at 000008h (Default)
l Low Priority Vector at 000018h
l Polled or interrupt driven
l Automatic context save WREG, STATUS
and BSR on High Priority Interrupt
l Most interrupts wake processor from sleep
l Fixed interrupt latency is three instruction
cycles
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 52
PIC18 Architecture
Interrupt Logic (High Priority Level)
TMR0IF
TMR0IE Interrupt to CPU
TMR0IP Vector to location
RBIF 0008h
RBIE (High Priority
RBIP Interrupt
INT0IF Vector Address)
INT0IE
IPEN
From (a)
From (b)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 53
PIC18 Architecture
Interrupt Logic (Low Priority Level)
To (a) Wake-up
(if in SLEEP mode)
Peripheral Interrupt Enabled bit
Peripheral Interrupt Flag bit
Peripheral Interrupt Priority bit
Interrupt to CPU
Vector to Location
0018h (Low
Additional Peripheral Interrupts From (c)
Priority Interrupt
To (b) Vector Address)
TMR0IF
TMR0IE
TMR0IP
RBIF GIEH/GIE, GIEL/PEIE
RBIE
RBIP
INT1IF
INT1IE
INT1IP
INT2IF
INT2IE
INT2IP
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 54
Interrupt Priority Enable
l New bit added to the RCON register - IPEN
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 55
Peripheral Interrupt Control
Registers
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
PIR1 PSPIF ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IF
bit7 6 5 4 3 2 1 0
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
PIE1 PSPIE ADIE RCIE TXIE SSPIE CCP1IE TMR2IE TMR1IE
bit7 6 5 4 3 2 1 0
R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 R/W-1
IPR1 PSPIP ADIP RCIP TXIP SSPIP CCP1IP TMR2IP TMR1IP
bit7 6 5 4 3 2 1 0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 56
GIE PEIE In Compatibility
Mode
l When IPEN=0 Compatibility Mode
l INTCON<7> is GIE
l INTCON<6> is PEIE
l Note: definition exactly same as 16C INTCON
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 57
GIEH & GIEL In Priority Mode
l INTCON<7> is GIEH
l INTCON<6> is GIEL
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 58
High Priority Interrupts
l High Priority Vector uses shadow registers for
automatic context save / restore:
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 59
Low Priority Interrupts
l Low Priority Vector - compiler saves context and
restores it with “interruptlow” pragma
Example: ISR accesses a calculated array index and executes a division within the ISR:
#pragma interrupt sample_adc save=PROD, section("MATH_DATA")
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 62
Large Arrays and Structures
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 63
Large Arrays and Structures
((cont.)
cont.)
l Add #pragma to use new section in source.c
#pragma udata big_array // Select large section
unsigned char test[456];
#pragma udata // Return to normal section
l Access these large (>256 byte) arrays and
structures through pointers or a variable
based index (array[index] or *array)
l Avoid fixed element addressing on these large
arrays and structures (ex: array[2])
l Pointers are more code efficient than array
indexing
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 64
Peripherals
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 65
PIC18 Peripherals
l Digital I/O Ports
l Timer0, 1, 2, 3
l Compare/Capture/PWM (CCP)
l Analog-To-Digital Converter
l Analog Comparator
l Addressable USART (AUSART)
l Master Synchronous Serial Port (MSSP)
l External Memory Access (EMA)
l Controller Area Network (CAN)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 66
PIC18 Peripherals
Digital I/O Ports
l Up to 68 bi-directional I/O pins
l High sink/source capability (up to 25mA)
l Direct bit (pin) manipulation (single-cycle)
l Each port pin has:
l Individual direction control (TRISA~TRISJ)
l Data Latch (LATA~LATJ - read-modify-writes)
l Port Register (PORTA~PORTJ reads value
on pins)
l All I/O pins have ESD protection
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 67
Port Latch Block Diagram
Read LAT
I/O Pin
Data Bus
• D Q
• ¡ •
4CK Data Latch
Write PORT
or LAT TTL
• D Q
TRIS Latch
• Input
Buffer
4CK
Write TRIS Read TRIS
•
Q D
Read PORT EN Q1
PORT Input
Synchronizer Latch
I/O pins have ESD protection diodes
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 68
I/O Pin Direction
l Direction of I/O pins controlled by individual
TRIS bits
l 1 = Input (default power on reset state)
l 0 = Output
l Example
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 69
Reading / Writing I/O Ports
l Reading a I/O port or bit uses the PORT
register
l if (PORTCbits.RC2) // Execute if RC2 = 1
l if (PORTC == 0b11110000) // Check for F0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 70
PIC18 Peripherals
PORTB : Interrupt on Change
Data Bus
Internal D Q
Pull-up EN
I/O Pin
Interrupt/Wake-up
Q1
D Q
Port Read Q3 EN
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 71
PIC18 Peripherals
Timer0
l 8-bit/16-bit Timer/Counter
l 16-bit Read and Writes
l 8-bit Software Programmable Prescaler
l Internal or External clock select
l Interrupt on overflow from FFh/FFFFh to 00h
External 8-bit Data Bus
Fosc/4
Clock
Input Sync with
8-bit
internal TMR0H:TMR0L
clocks
T0SE Programmable
Prescaler (2 cycle delay)
T0CS
3 Set TMR0IF interrupt
PSA
T0PS2:T0PS0 flag on Overflow
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 72
Timer 0 Setup
bit 7 bit 0
T0CON TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 73
PIC18 Peripherals
Timer1 and Timer3
l 16-bit Timer / Counter
l Consists of two readable and writeable 8-bit
registers
l 16-bit Read / Write mode eliminates hazards
l ÷1, ÷2, ÷4, or ÷8 Prescaler
l Timer, Synchronous or Asynchronous
Counter
l Timer1 can also operate from an external
crystal with its built in oscillator feature.
l Interrupt on overflow from FFFFh to 0000h
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 74
PIC18 Peripherals
Timer1 and Timer3 (Continued)
Data Bus<7:0>
TMR1H 8
8
CCP Special Even Trigger
8
Synchronized
CLR 0
Clock Input
TMR1H
High Byte TMR1L 1
TMRON
T1SYNC
T1OSC on/off
T13CLI/ 1
T1OSO Prescaler Synchronize
Fosc/4
Fosc/4 1, 2, 4, 8 det
T1OSCEN Internal 0
Enable Clock 2
T1OSI SLEEP input
Oscillator TMR1CS
T1CKPS1:T1CKPS0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 75
PIC18FXXX MCU Peripherals
TMR1 as a Real Time Clock
+5V
PIC18FXXXX C
R T1OSI
OSC1
Y
T1OSO
C
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 76
Timer 1 Setup
bit 7 bit 0
T1CON RD16 - T1CKPS1 T1CKPS0 T1OSCEN T1SYNCH TMR1CS TMR1ON
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 77
Timer 3 Setup
bit 7 bit 0
T3CON RD16 T3CCP2 T3CKPS1 T3CKPS0 T3CCP1 T3SYNCH TMR3CS TMR3ON
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 78
PIC18 Peripherals
Timer2 and Timer4
Optional SSP
Instruction
Reset Baud Clock
Clock Prescaler
TMR2
1, 4, 16
T2CKPS<1:0>
Set TMR2IF
Postscaler
Comparator
1:1 to 1:16
PR2 TOUTPS<3:0>
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 80
Timer 2 Setup
T2CON Register Format
bit 7 bit 0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 81
PIC18 Peripherals
TMR4 Timer: Period Register
Instruction
Reset
Clock Prescaler
TMR4
1, 4, 16
T4CKPS<1:0>
Set TMR4IF
Postscaler
Comparator
1:1 to 1:16
PR4 T4OUTPS<3:0>
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 82
Timer 4 Setup
T4CON Register Format
bit 7 bit 0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 83
Timer 2 Interrupts
RCON Register
bit 7 bit 0
IPEN - - ~RI ~TO ~PD ~POR ~BOR
INTCON Register
bit 7 bit 0
GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF
bit 7
PIE1 (Peripheral Interrupt Enable) Register bit 0
PSPIE ADIE RCIE TXIE SSPIE CCP1IE TMR2IE TMR1IE
bit 7
IPR1 (Peripheral Interrupt Priority) Register bit 0
PSPIP ADIP RCIP TXIP SSPIP CCP1IP TMR2IP TMR1IP
TMR2=PR2
PR2 DC
TMR2=CCPR1L
TMR2=PR2
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 88
PIC18 Peripherals
CCP Module: Input Capture Mode
l Captures 16-bit TMR1 value when an event
occurs on CCPx pin:
l Every falling edge
l Every rising edge
l Every 4th rising edge
l Every 16th rising edge
l Capture generates an interrupt
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 89
PIC18 Peripherals
CCP Module: Input Capture Mode (continued)
(continued)
RCn//CCPx
RCn Prescaler
Pin ÷ 1, 4, 16 CCPRxH CCPRxL
edge detect
Q’s CCPxCON
CCPxCON<3:0>
<3:0> TMR1H TMR1L
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 90
PIC18 Peripherals
CCP Module: Output Compare Mode
l 16-bit CCPRx register value is compared to
TMR1, and on match the CCPx pin is
l Driven High/Low
l Toggled
l Unchanged
l Compare match generates interrupt
l Special event trigger clears TMR1 and can
start A/D conversion
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 91
PIC18 Peripherals
CCP Module: Output Compare Mode
(continued)
(continued)
8-bit Data Bus
Special Event
Trigger
Set CCPxIF
Flag Bit CCPRxH CCPRxL
RCn//CCPx
RCn Q S Output
Pin Logic Comparator
R
TRISC<n>
Output Enable
CCPxCON<3:0>
CCPxCON<3:0> TMR1H TMR1L
Mode Select
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 92
CCP1 Setup
bit 7 bit 0
CCP1CON - - DC1B1 DC1B0 CCP1M3 CCP1M2 CCP1M1 CCP1M0
ADC
0001
AN1 l Internal Or
AVDD 0000
AN0 External
VREF+
(Reference Reference
voltage) AVss
VREF- l Up to 25ksps
l 34 ksps without
PCFG2:PCFG0
channel change
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 95
A/D Setup ADCON0
bit 7 bit 0
ADCON0 ADCS1 ADCS0 CSH2 CHS1 CHS0 GO_DONE - ADON
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 96
A/D Setup ADCON1
bit 7 bit 0
ADCON1 ADFM ADCS2 - - PCFG3 PCFG2 PCFG1 PCFG0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 99
PICmicro MCU Peripherals
Analog Comparator Module (continued)
RA0/AN0
C1OUT
RA3/AN3
RA1/AN1
C2OUT
RA2/AN2
RA0/AN0
RA4 Open Drain
RA3/AN3 C1OUT
RA1/AN1
C2OUT
RA2/AN2
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 100
PIC18 Peripherals
Internal VREF: Block Diagram
16 stages
VREN
8R R R R R
• ••
8R
VRR
VR3
VREF 16:1 analog mux
VR0
l 24 or 32 step sizes
l Internal or External Voltage Reference
l Can be used as a D/A converter
l VREF can be directed to an output pin
Note: Check your device datasheet for availability
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 101
Comparator Setup
bit 7 bit 0
CMCON C2OUT C1OUT C2INV C1INV CIS CM2 CM1 CM0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 102
Comparator Reference Setup
bit 7 bit 0
CVRCON CVREN CVROE CVRR CVRSS CVR3 CVR2 CVR1 CVR0
When CVRR = 0
CVREF = (0.25 + (CVR<3:0>/32) )* CVRSRC
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 103
PIC18 Peripherals
Addressable USART (AUSART)
l Full-duplex Asynchronous Or Half-duplex
Synchronous
l 9-bit Addressable mode
l Double-buffered transmit and receive
buffers
l Separate transmit and receive interrupts
l Dedicated baud rate generator
l Max bit rates @ 40MHz
l Asynchronous: 625 kbps / 2.5 Mbps
l Synchronous: 10 Mbps
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 104
PIC18 Peripherals
USART Block Diagram
TX9D
TXEN SYNC
SREN CSRC
TXREG CREN
TXIE TX9 SPEN
Interrupt
TXDATA
TXIF TSR RC6/TX/CK
TXCLK
OERR FERR
RC7/RX/DT
RXDATA
RSR
RCCLK
ADDEN TO RC6, RC7
I/O Port Logic
RX9D
RX9
RCREG Baud Rate
Clock
RCIF
RCIE
SPBRG FOSC / 4
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 105
UART Tx Setup
bit 7 bit 0
TXSTA CVREN TX9 TXEN SYNC - BRGH TRMT TX9D
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 106
UART Rx Setup
bit 7 bit 0
RCSTA SPEN RXD SREN CREN ADDEN FERR OERR RX9D
64 * (SPBRG + 1)
16 * (SPBRG + 1)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 108
UART Buffers
l Load TXREG with byte to be transmitted
l Buffer empty ONLY when PIR1bits.TXIF is set
l Read received byte from RCREG
l Received data ONLY when PIR1bits.RCIF is
set
void putchar(value){
while (PIR1bits.TXIF == 0);// Wait for empty FIFO
TXREG = value;
}
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 109
PIC18 Peripherals
M aster S
Master ynchronous S
Synchronous erial P
Serial ort
Port
l Operates in either SPI™ or I2C™ mode
l SPI Mode
l Programmable baud rate
l Maximum baud rates (@ 40MHz)
l Master: 10 Mbps
l Slave: 2.5 Mbps Single Byte Tx
l All four SPI modes supported (0,0;0,1;1,0;1,1)
l I2C Mode
l Supports standard (100kHz), fast (400kHz), and Microchip’s
1MHz I2C standards
l Hardware Master/Slave implementation
SPI is a trademark of Motorola Semiconductor
I2C is a trademark of Philips Semiconductors
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 110
MSSP SPI Mode Setup
bit 7 bit 0
SSPSTAT SMP CKE D_A P S R_W UA BF
R_W Read / Write bit used ONLY in I2C mode, unused in SPI
UA Update Address bit used ONLY in I2C mode, unused in SPI mode
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 111
MSSP SPI Mode Setup Cont
Cont..
bit 7 bit 0
SSPCON1 WCOL SSPOV SSPEN CKP SSPM3 SSPM2 SSPM1 SSPM0
WCOL Write Collision Detection (Master Mode Only – Must be cleared in software)
1 = The SSPBUF register was written while still transmitting a previous word
0 = No write collision
SSPOV Receive Overflow Indicator (Slave Mode Only – Must be cleared in software)
1 = A new byte has been received from the master before the previous byte was read from
SSPBUF. In case of overflow, the data is lost and SSPBUF must be read to clear overflow
condition. Slave transmitter applications should also read SSBUF after each byte
0 = No Slave Receive Overflow
SSPEN Synchronous Serial Port Enable
1 = Enables serial port and configures SCK, SDO, SDI and SS as serial port pins
0 = Disables serial port; allows SCK, SDO SDI and SS to be used as general purpose I/Os
SCP Clock Polarity Selection
1 = Idle state for clock is a high level
0 = Idle state for clock is a low level
SSPM3:SSPM0 Synchronous Serial Port Mode Selection
0101 = SPI Slave Mode, Clock – SCLK, SS Control Disabled, SS is GPIO
0100 = SPI Slave Mode, Clock = SCLK, SS Control enabled
0011 = SPI Master Mode, Clock = Timer 2 Output / 2
0010 = SPI Master Mode, Clock = FOSC/64
0001 = SPI Master Mode, Clock = FOSC/16
0000 = SPI Master Mode, Clock = FOSC/4
NOTE: Other combinations used in I2C mode or reserved
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 112
PICmicro MCU Peripherals
Parallel Slave Port
l Provides an 8-bit interface such that the PICmicro
MCU may be used as a peripheral to a
microprocessor
l Three I/O on PORTE act as Chip Select, Read, and
Write lines
l PORTD is the data bus
l Separate read and write interrupts available
l Currently available on
PICmicro
most 40-pin, DSP RD, WR, CS
MCU
14-bit core devices or
8-bit Data Bus
MPU
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 113
PICmicro MCU Peripherals
Parallel Slave Port: MCU Interface
l Direct interface to 8-bit microprocessor
data bus
l Asynchronous operation (to external world)
l Interrupt generated on external read or write
operation on parallel port
l Uses Port D and Port E
l Port D: Data bus
l Port E: Control signals (read, write, and
chip select)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 114
PICmicro MCU Peripherals
Parallel Slave Port: Block Diagram
Data b us
D Q
WR RDx
POR T
CK pin
TTL
Q D
RD EN
EN
POR T
Read
TTL RD
Chip Select
TTL CS
Write
TTL WR
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 115
Parallel Slave Port Setup
bit 7 bit 0
TRISE IBF OBF IBOV PSPMODE - TRISE2 TRISE1 TRISE0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 116
Special Features
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 117
New Oscillator Modes
PIC18F452 Oscillator Block Diagram
To Fuse options select
Timer1/Timer3 oscillator modes
T13CKI/T1OSO input
32 kHz Oscillator
Fosc = 32 kHz
T1OSI
HS Osc
PLL Enable
MUX
Phase SYSCLK
OSCOUT Comparator Cvco
Ext RC FIN
and Loop VCO
Crystal Filter
Osc FOUT
Feedback Divider
OSCIN
3 2 1 0
OSCIN
Note: FOSC0, FOSC1, FOSC2, and OSCSEN bits are in CONFIG1H (300001h)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 118
PIC18 Special Features
Programmable Low Voltage Detect
l Provides “Early Warning”
l Programmable internal or external reference
l Up to 14 internal reference voltages (2 - 4.77V)
l Operates during SLEEP
l Low Voltage condition wakes-up/interrupts
MCU
l Software Controlled enable/disable
l Useful for low power applications
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 119
PIC18 Special Features
Programmable Brown-Out RESET
l Monitors operating voltage range
l Resets MCU when Vdd is below reference
voltage
l Deasserts RESET after Vdd is above reference
voltage
l Programmable internal reference
l Up to 4 voltages (2.0, 2.7, 4.2, 4.5)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 120
PIC18 Special Features
Watchdog Timer (WDT)
l Recovers from software malfunction
l Resets MCU if not attended on-time
l Software must clear it periodically (CLRWDT)
l Programmable period
l 18 ms to 3.0 s typical
l Configuration controlled postscaler
l Enabled via Configuration register or
Software
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 121
Watchdog Enhancements
Block Diagram
l The watchdog can be programmed on and off in software
l If Configuration bit WDTE = 1, the WDT cannot be
turned off in software
l If Configuration bit WDTE = 0, the software
watchdog bit SWDTEN, can be used to
enable/disable the WDOG timer
l This is useful for applications that want to
conserve power by turning off the WDT while in
sleep or executing non-critical application code
CLRWDT
WDT Timer Postscaler Reset
Instruction
WTD Time-out
WDTEN
Configuration Bit SWDTEN
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 122
PIC18 Special Features
In-Circuit Serial ProgrammingTM
TM
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 125
PICmicro 28/40 Pin Device
Compatibility
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 126
Future Products
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 127
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 128
PICmicro® Microcontroller
Development Tools
MPLAB
Integrated Development Environment
Built-in Source Level Project
Editor Debugger Manager
Emulators/
Languages Simulators Debuggers Programmers Other Tools
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 129
New MPLAB® V6.00
Native Windows / 32-Bit implementation
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 131
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 132
MPLAB-C18 C Compiler
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 133
Compiler, Assembler and
Linker Linker Script
LAB1.C DELAY.C LCD.C CONFIG.ASM P18F452.LKR
Compiler
or MPLAB-C18 MPLAB-C18 MPLAB-C18 MPASM
Assembler
LAB1.O DELAY.O LCD.O CONFIG.O
<libraries>.LIB
Linker MPLINK
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 135
MPLAB ICD 2 Options
l Programmer board enables use as a
universal PICmicro programmer
l Can replace your PICSTART PLUS programmer
l RS-232 interface and power supply for
legacy PCs without USB support
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 136
MPLAB-ICE 2000
In Circuit Emulator
l Unlimited Program
Breakpoints
l Trigger and Break on Data
Memory Read / Write
l (4) Individual Trigger
Events
l Programmable System
Oscillator 32Khz ~ 25 Mhz
l Code Coverage
l Pass Counter
l 32K Trace Buffer traces
program and data
memory
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 137
MPLAB ICE 2000 Connectivity
l Universal pod supports PIC12/16/18
l Processor module supports device family
l Device Adapter supports package type
l Transition Sockets support Surface
Mount
l Parallel Port PC interface
l ~$2,000 for complete system
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 138
MPLAB V6.0, MPLAB-ICD-II
PIC18FXXX Hands On Exercises
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 139
Hands On Exercises Agenda
l Lab 1 - Install MPLAB 6.0, MPLAB-ICD II,
MPLAB-C18, Connect Demo board
l Create Project, Compile, Download Code,
Get First Demo Up and Running, MPLAB
basics
l Lab 2 - Develop and Debug a traffic light
l Lab 3 - Develop and Debug A/D sampling ISR
l Lab 4 - Run DFT() algorithm on A/D Sampling
Buffer results and pass array to display routine
for graphing
l Lab 5 - Extra credit- Add Automatic Gain
Control using SPI controlled Digital POT
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 140
Audio Spectrum
Analyzer Board
North /
East / West A/D Channel
South Switch
Switch and Selection MIC,
and Gain
Adjust Gain Adjust RCA, POT
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 141
Schematics, Page 1
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 142
Schematics, Page 2
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 143
Schematics, Page 3
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 144
Install MPLAB V6.00, MPLAB-
C18, MPLAB-ICD-II
l Step 1: Connect USB cable of MPLAB ICD 2
l Step 2: Connect power supply to Workshop
target board
l Step 3: Install MPLAB IDE, ICD 2, C18
l • Run “MPLIDEV6.EXE” (MPLAB/32 IDE)
l • Run “MPICD2.EXE” (MPLAB ICD 2)
l • Re-boot after MPLAB IDE detect ICD 2
l • Run “MCC18V20.EXE” (MPLAB C18)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 145
Install Workshop Files
l Step 4: Install workshop files
l • Run Workshop.bat, installing workshop files
in default directory C:/workshop
l Step 5: Create your first Project
l Verify MPLAB C18 Installation and Paths
l Create Project, add source files
l Build Project
l Download HEX to target, Run
l LCD display should show a message...
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 146
Setting Up MPLAB V6.0
l Configure -> Select Device -> PIC18F452
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 147
Configuring MPLAB ICD 2
l Debugger -> Settings
l Status Tab - Check l Program Tab - Press “Full
“Automatically connect Range” button and end
at startup” address set to 0x7DBF
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 148
Verify Compiler Installation
l Project -> Set Language Tool Locations
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 149
Project Creation
l Project -> New
l Name project lab1 and place in c:\workshop
l Expect this to be
automated in future
release
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 151
Add Your Linker Script
l Project -> Insert Files -> Select Linker Script
C:\mcc18\lkr\p18F452i.lkr
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 152
Build Your Project
l Project->Build All
l MPLAB-C18 runs on all C files in output window
l MPASM assembler runs on config.asm file
l MPLINK linker links all files together and creates
HEX output
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 153
Download Code to Target and
Run…..
l Debugger -> Download to Target
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 154
Creating Watch Windows
l Debugger -> Halt
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 155
Configuration Bit Settings
Window
l Configure -> Configuration Bits
l Automatically Initialized by config.asm if
included in your project
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 156
Setting Breakpoints
l Double Click on lab1.c in Project Window
l Find lcd_putch(‘D’); select and click this statement with right
mouse button to set a breakpoint there
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 157
Modifying Watch Values
l Hit Debugger -> Run see debugger halt at
lcd_putdec
l “Count = <value>”on the LCD should be the
same as count in your watch window
l Place your cursor over the watch value, type
a new number and re-run
l See new value
on LCD display
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 158
Single Stepping
l Debugger->Step Into
l MPLAB automatically opens lcd.c and steps
into lcd_putch()….
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 159
Tool Bars
l Most common debugger functions are
available on the toolbar
Step Program
Halt Over Target
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 160
Lab 2 Traffic Light
l Traffic Light has (4) states:
State North / South East / West
EW_GREEN RED GREEN
EW_YELLOW RED YELLOW
NS_GREEN GREEN RED
NS_YELLOW YELLOW RED
N/S = North / South
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 161
Lab 2: Traffic Light Logic
EW_GREEN NS_GREEN
Wait 1.5 Seconds Wait 1.5 Seconds
No No
NS_SWITCH = 1? EW_SWITCH = 1?
Yes Yes
NS_SWITCH = 0 EW_SWITCH = 0
STATE = EW_YELLOW STATE = NS_YELLOW
update_state = 1 update_state = 1
EW_YELLOW NS_YELLOW
Wait 1.5 Seconds Wait 1.5 Seconds
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 162
Lab 2:Traffic Light
Implementation
l Project -> Open -> lab2.mcp
l Edit lab2.c and add the state transition code as follows:
switch(state){
case EW_GREEN: Delay100Ms(15); // Wait 1.5 Seconds
// Place your code for EW_GREEN here
break;
case EW_YELLOW: Delay100Ms(15); // Wait 1.5 Seconds
// Place your code for EW_YELLOW here
break;
case NS_GREEN: Delay100Ms(15); // Wait 1.5 Seconds
// Place your code for NS_GREEN here
break;
case NS_YELLOW: Delay100Ms(15); // Wait 1.5 Seconds
// Place your code for NS_YELLOW here
break;
}
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 163
Lab 3: Using Timer 2 for
Sampling Interval
l Timer 2 and PR2 are used to create an
automatic high priority periodic interrupt
l PICmicro running at 40 Mhz / 100 nS
instruction cycle
l Desire 5 Khz sampling rate = 200 uS
l 200 uS / (100 nS instruction cycle = 2000
instruction cycles
l Assign Timer 2 to the high priority vector and
enable high priority interrupts
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 164
Using PIC18FXXX Peripheral
Calculations Spreadsheet
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 165
Timer 2 and A/D Initialization
and Interrupt Assignment
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 166
Enter sample_adc() ISR
Lab 3: sample_adc
sample_adc
ISR Flowchart
Start ADC Conversion
ADCON0bits.GO_DONE = 1
Yes
ADCON0bits.GO_DONE = 1?
No
INBUFFER[buffer_index] = ADRESH
Yes
buffer_index = 32? start_dft = 1
No
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 167
Lab 3: Write code for Interrupt
Driven Sampling Routine
#pragma interrupt sample_adc // High priority interrupt
void sample_adc (void){ // TMR2 overflow every 2,000
// cycles, 200 uS / 5 Khz @ 40 Mhz
if (PIR1bits.TMR2IF){
// Start A/D conversion
// Clear TMR2 interrupt flag
// Spin lock + wait for A/D conversion to complete
// Store A/D result into next location in INBUFFER
// Increment buffer_index and use next buffer location
// Once you hit the end of the INBUFFER[32]:
// - Reset the pointer to zero
// - Set start_dft flag bit to run the DFT
}
}
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 168
Lab 3: Instructions
l Fill in source code for sample_adc() using:
l INBUFFER[32] stores results
l buffer_index accesses each element
l ADCON0bits.GO_DONE starts ADC conversion
l ADCON0bits.GO_DONE is 1 when ADC is busy
l ADRESH returns 8-bit ADC value
l start_dft = 1 when buffer is full, clear buffer_index
l Build/Compile code, program target
l Set breakpoint where you set start_dft
l Run and examine INBUFFER[] for results ->>>
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 169
INBUFFER[32]
Results
l View Input Sample Buffer
in Watch Window:
l INBUFFER[32] Shows
A/D sampling Results
l Values should be
centered around 0x80
l All 32 locations should be
captured and stored
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 170
Graphing INBUFFER[ ] Results
Add the following code…..
main()
start_dft = 1?
result_index = 0
Yes
result_index = 16? Plot Finished
No
Processor
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 172
Converting Time Domain
to Frequency Domain
l Convert Time 6
Domain 2
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Sampled Data
-2
-4
-6
to…
Discrete Fourier
l Frequency Transform
Domain Data 20
20
15
15
10
10
j
j
5
5
0
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 173
Discrete Fourier Transform
Discrete Fourier Transform Formula:
n=N
N = Number of Samples
F = Frequency Bin Number
Example: Fhz = F * (Sampling Frequency / Number of Samples)
Sampling Frequency = 5 Khz
(32) Samples
BINCOUNT = 16 N = 32
BINCOUNT = 16 N = 32
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 176
Frequency Table FTABLE[32]
Fsample = 5 Khz / 200 uS
300
(32) Samples -> F[1] = 5 Khz / 32
200
F[1] 156.25 Hz, BINCOUNT = 1
100
0
•32-sample signed Sine and
-100
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 Cosine Wave, F[1]
• Single table can be used by
-200
phase shifting sine by 90 degrees
-300
or (8) sample points
300
250
• Absolute Value of 32-sample
200
Sine and Cosine Wave, F[1]
150
• Increases Resolution by one bit
100
• Simplifies signed accumulation
50
math in DFT
0
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 177
F[N] Bin Generation
F[2] 312.5 Hz, BINCOUNT = 2 F[4] 625 Hz, BINCOUNT = 4
300 300
200 200
100 100
0 0
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
-100 -100
-200
-200
-300
-300
200
200
100
100
0
0
1 3 5 7 9 11 1 3 15 17 19 21 2 3 25 2 7 29 31
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
-100
-100
-200 -200
-300 -300
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 178
Lab 4: DFT Implementation
l Once 32 samples have been completed:
l start_dft is set by ISR, indicating
INBUFFER[32] is completed
l Call dft();
l dft() takes INBUFFER[32], convolves this
with FTABLE[32] calculating IBIN[16] and
QBIN[16]
l magnitude[F] = (unsigned long)(IBIN[F]>>8)2 +
(unsigned long)(QBIN[F]>>8)2
l Scale magnitude result for 0~16 bar display
l Use lcd_bargraph(magnitude,location) to plot
all 16 frequency magnitude bins
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 179
DFT Invocation Flowchart
main()
magnitude[result_index] =
start_dft = 1? (unsigned long)(IBIN[result_index]>>8) 2 +
(unsigned long)(QBIN[result_index]>>8)2
dft()
result_index = 0
max_result = 0
magnitude Yes
[result_index] >
No max_result?
result_index = 16?
max_result =
Yes magnitude
[result_index]
scale results
No
(see next slide)
result_index += 1
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 180
DFT Scaling + Plotting
scale results
Flowchart
Yes result_index = 0;
max_result > 16 ? magnitude_divisor = max_result/16
No
result_index = 0
Yes
result_index =
16?
Yes
No
result_index = 16? DFT Finished
magnitude
No [result_index] /=
magnitude_divisor
lcd_bargraph(magnitude[result_index],
result_index)
result_index += 1
result_index += 1
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 181
DFT Testing
l Build project, program target and run code
l LCD shows a real-time spectrum analyzer
bargraph:
18
16
14
12
10
8
6
4
2
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 182
Audio Test Frequency
Generator
l WinISD Audio Frequency Generator and
Speaker Design Tool created by Juha
Hartikainen www.linearteam.org
• Sweeping Audio Tones
• Fixed Audio Tones
• Attenuation control
• Audio Speaker Design Tool
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 183
Internal DFT results
l Set breakpoint after lcd_bargraph invocation and
View->Watch to look at the following values in
watch window:
l IBIN[16] - Real magnitude
l QBIN[16] - Imaginary magnitude
l magnitude[16] - Total magnitude
l max_result - Maximum magnitude value
l INBUFFER[32] - Input buffer samples
l FTABLE[32] - Sine / Cosine waveforms used in DFT
convolution.
l Select Decimal display format by right clicking on
each variable -> Properties, Format = Decimal
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 184
Lab 5: Automatic Gain Control
l Digital POT selects microphone gain
l Gain stored in pot_value. Default = 0xF2
l Use WritePOT(pot_value) to change
microphone gain
l Scan INBUFFER[32] for clipped values
l Centered around 128
l Clip when < 64 or > 192
l Increase pot_value gain by one until clipping
l Decrease pot_value gain by the number of
clipping events
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 185
Automatic Gain Control
DFT Finished
Flowchart
result_index = 0
agc_value = 0
No
result_index = 32 ? INBUFFER[result_index]
> 192 or
INBUFFER[result_index]
No < 64
agc_value = 0 Yes
&& pot_value < 255?
Yes agc_value += 1
No pot_value += 1
result_index += 1
pot_value -=1
agc_value -=1
agc_value > 0
WritePOT(pot_value) AGC Finished
Yes and pot_value > 0?
No
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 186
Congratulations, PIC18FXXX
Expert…….
l You now have the experience needed to
design and complete an embedded systems
application using the PIC18FXXX
l We hope you enjoyed this session!
l Please fill out the feedback forms
l Thanks for joining us!
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 187
Appendix A:
Improving Code Size With the
MPLAB C18 Compiler
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 188
Our Goal: To understand how to reduce C
application code size on PIC18 MCUs
through intelligent use of MPLAB C18 and
careful structuring of C code.
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 189
Suggestion #1
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 190
Code Size Comparison
Default Options
140000
120000
Code size (bytes)
Baseline
100000
-23%
80000 -26%*
(CQ1’02) -38%*
60000 (CQ3’02)
40000
20000
0
1.0 1.10 2.0 2.10
MPLAB C18 Version
* Projected
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 191
Suggestion #2
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 192
Code Size Comparison
Choosing Command-Line Options
140000
Baseline
120000
Code Size (bytes)
100000 -23%
-26%*
-38%*
80000 Default
60000 -45% Best
-48%*
(CQ1’02) -56%*
40000 (CQ3’02)
20000
0
1.0 1.10 2.0 2.10
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 193
Command-Line Options
LFSR Use
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 195
Command-Line Options
Memory Model
l MPLAB-C18 has two memory models:
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 196
Suggestion #3
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 197
Command-Line Options
Data Storage Class
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 198
Using auto Variables
movlw offset(a)
movff PLUSW2, tmp
movlw offset(b)
movf PLUSW2
addwf tmp
6 program words
(not counting prolog/epilog)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 199
Command-Line Options
Data Storage Class
l C also provides for static local variables
l MPLAB-C18 extends C with static parameters
(available in v1.10 and later)
l For example:
char add( static char a, static char b )
{
static char result;
result = a + b;
return result;
}
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 200
Using static Variables
movlb b*
movf b
addwf a
3 program words
(no prolog/epilog required)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 201
static Gotchas
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 202
static Gotchas
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 203
static Gotchas
l Example:
char add( char a, char b );
Will only work if the default storage class is
identical in both the declaring and defining
files.
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 204
static Gotchas
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 205
Command-Line Options
Data Storage Class
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 206
Suggestion #4
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 207
MPLAB-C18 Data Types
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 208
Using Appropriate Data Types
c=a+b
char: int:
MOVLB b MOVLB a
MOVF b,0,1 MOVF b,0,1
ADDWF a,0,1 ADDWF a,0,1
MOVWF c,1 MOVWF c,1
MOVF high(b),0,1
ADDWFC high(a),0,1
MOVWF high(c),1
(4 words) (7 words)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 209
Suggestion #5
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 210
Variable Allocation
Using Access RAM
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 211
Variable Allocation
Using Access RAM
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 212
Suggestion #6
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 213
Variable Allocation
Defining Variables
l MPLAB-C18 can be more aggressive
optimizing variables in the files where they
are defined.
Source code: Machine code:
char a, b, c;
MOVLB b
void foo( void ) MOVF b,0,1
ADDWF a,0,1
{
MOVWF c,1
c = a + b;
(4 words)
}
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 214
Variable Allocation
Defining Variables
l MPLAB-C18 must be more conservative with
externally-defined variables
Source code: Machine code:
extern char a, b, c; MOVLB b
MOVF b,0,1
void foo( void ) MOVLB a
{ ADDWF a,0,1
MOVLB c
c = a + b; MOVWF c,1
}
(6 words)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 215
Suggestion #7
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 216
Using ##pragma
pragma varlocate
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 217
Using ##pragma
pragma varlocate
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 218
Using ##pragma
pragma varlocate
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 219
Suggestion #8
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 220
Common Sub-Expression
Elimination
l Applies to all types of expressions
MY_STRUCT s[10];
for(i=0; i<10; i++) 10 words to calculate s[i]
2 words to assign i
{ 10 words to calculate s[i]
s[i].a = i; 3 words to assign 34
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 221
Common Sub-Expression
Elimination ((Contd.)
Contd.)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 222
Suggestion #9
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 223
Constant Evaluations
a = 2;
b = 17 + 52 * a; c = 121;
c = b;
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 224
Appendix B:
PIC18FXXXX
Instruction
Set and PIC16/17
Migration
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 225
PIC18 Architecture
ALU : Status Register
STATUS Register Format
bit 7 bit 0
- - - N OV Z DC C
Bit definitions
N Negative/Positive ALU result is negative
OV OVerflow 2’s Complement Overflow
occurred
Z Zero Result is zero
DC Digit Carry / !Borrow Carry/borrow from lower nibble
C Carry / !Borrow Carry/borrow from upper nibble
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 226
PIC18 Architecture
Data Memory
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 227
PIC18 Architecture
Accessing Data Memory
Points to
l Select a bank Bank
l BSR<3:0> contains bank
GPR (Bank n-1)
l Instruction with 8-bit
address as operand BSR<3:0>
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 228
PIC18 Architecture
Accessing Data Memory
4-bits from
BSR
Register 8-bits from Instruction Word
BSR<3:0> f f f f f f f f
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 229
PIC18 Architecture
Access Bank
Memory Map
l 256 bytes of non- Access RAM
000h
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 230
PIC18 Architecture
Accessing Access Bank
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 231
PIC18 Architecture
Accessing Access Bank
l Instruction Format Example:
16-bit Instruction
OP CODE a f f f f f f f f
f f f f f f f f
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 232
PIC18 Architecture
Program Memory Storage Scheme
l Little-Endian Format
Instruction Opcode Memory Address
… 00007h
MOVLW 55h 0E55h 55h 00008h
0Eh 00009h
GOTO 06h EF03h, F000h 03h 0000Ah
EFh 0000Bh
00h 0000Ch
F0h 0000Dh
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 233
PIC18 Instructions
Instruction Features
l Upward compatible with PIC16, PIC17,
16-bit Instruction width
l Instruction fetches are 16-bit wide
l Fetch and Execution is overlapped
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 235
PIC18 Instruction
Byte-Oriented Operations
Byte-Oriented
Operations
ADDWF f [,d [,a]] 16-bit Instruction for Byte Oriented Operations
ADDWFC f [,d [,a]]
ANDWF f [,d [,a]]
CLRF f [,a] OP CODE d a f f f f f f f f
COMF f [,d [,a]]
CPFSEQ f [,a] d = Destination Bit
CPFSGT f [,a] ‘W' for WREG (0)
CPFSLT f [,a] ‘F’ for f (1 - Default)
DECF f [,d [,a]]
DECFSZ f [,d [,a]]
DCFSNZ f [,d [,a]] a = Access Bit
INCF f [,d [,a]] ‘ACCESS’ (0)
INCFSZ f [,d [,a]] ‘BANKED’ (1 - Default)
INFSNZ f [,d [,a]] f = 8-bit Register Address
IORWF f [,d [,a]]
MOVF f [,d [,a]] Example:
MOVFF fs, fd ADDWF f [,d [,a]] MOVFF fs, fd
MOVWF f [,a] ADDWF Count MOVFF Source, Dest
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 236
PIC18 Instructions
Byte-Oriented Operations (Continued)
Byte-Oriented
Operations
MULWF f [,a] 16-bit Instruction for Byte Oriented Operations
NEGF f [,a]
RLCF f [,d [,a]]
RLNCF f [,d [,a]] OP CODE d a f f f f f f f f
RRCF f [,d [,a]]
RRNCF f [,d [,a]] d = Destination Bit
SETF f [,a] ‘W’ for WREG (0)
SUBFWB f [,d [,a]] ‘F’ for f (1 - Default)
SUBWF f [,d [,a]]
SUBWFB f [,d [,a]]
SWAPF f [,d [,a]] a = Access Bit
TSTFSZ f [,a] ‘ACCESS’ (0)
XORWF f [,d [,a]] ‘BANKED’ (1 - Default)
f = 8-bit Register Address
Example:
SUBWF f [,d [,a]]
SUBWF Value, W
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 237
PIC18 Instructions
Byte-Oriented Operations - Example
l Perform Multi-byte (4 byte) increment:
“Count32++”
...
movlw 01h
addwf Count32, F ; Inc LSB by ‘1’
clrf WREG ; Pass the carry
addwfc Count32+1, F ; to LOW MSB
addwfc Count32+2, F ; to HIGH LSB
addwfc Count32+3, F ; to HIGH MSB
...
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 238
PIC18 Instructions
Bit-Oriented Operations
Bit-Oriented
Operations 16-bit Instruction for Bit Oriented Operations
a = Access Bit
‘ACCESS’ (0)
‘BANKED’ (1 - Default)
f = 8-bit Register Address
Example:
BTFSC f, b [,a]
BTFSC STATUS, C
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 239
PIC18 Instructions
Control Operations
Control 16-bit Instruction for CALL and GOTO
Operations OP CODE s n n n n n n n n
BC n
BN n
OP CODE n n n n n n n n n n n n
BNC n
BNN n
s = 1-bit fast Save/Restore
BNOV n ‘FAST’ (1), (Default - 0)
BNZ n
BOV n k = 20-bit Immediate Value
BRA n
BZ n 16-bit Instruction for RCALL and BRA
CALL n [,s]
GOTO n OP CODE n n n n n n n n n n n
RCALL n
RETFIE [s]
k = 11-bit Immediate Value
RETURN [s]
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 240
PIC18 Instructions
Control Operations (Continued)
Control
Operations
BC n l (Un)Conditional branches
BN n spans -128 through +127
BNC n
BNN n
Instructions
BNOV n l CALL and GOTO contain full
BNZ n
BOV n 21-bit address
BRA n
BZ n
l Provides Linear access to
CALL n [,s] 2MB
GOTO n
RCALL n l RCALL spans -1024 through
RETFIE [s] 1023 Instructions
RETURN [s]
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 241
PIC18 Instructions
Control Operations (Continued)
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 242
PIC18 Instructions
Control Operations - Example #1
Utilize “Save Context”
Handling Interrupt
org 00008h
bra HighISR
...
HighISR:
...
retfie FAST
...
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 243
PIC18 Instructions
Control Operations - Example #2
Wait for an input trigger on PORTB RB6 pin
...
btfsc PORTB, RB6 ; Is RB6 low ?
bra $-2 ; No. Wait…
... ; Yes.
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 244
PIC18 Instructions
Literal Operations
Literal
Operations 16-bit Instruction for LFSR
ADDLW k OP CODE f f k k k k k k k k
ANDLW k
IORLW k f = 2-bit FSR Selector
LFSR f, k FSR0, FSR1 or FSR2
MOVLB k
MOVLW k k = 8-bit Immediate Value
MULLW k 16-bit Instruction for Other Literal Operations
RETLW k
SUBLW k OP CODE k k k k k k k k
XORLW k
k = 8-bit Immediate Value
Example:
MOVLW k LFSR f, k
MOVLW 5Ah LFSR FSR0, 400h
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 245
PIC18 Instructions
Literal Operations - Example
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 246
PIC18 Instructions
Data Ö Program Operations
Control
Operations
TBLRD*
TBLRD*+
TBLRD*- 16-bit Instruction for TBLRD*/TBLWT*
TBLRD+*
TBLWT* OP CODE
TBLWT*+
TBLWT*-
TBLWT+*
TBLRD and TBLWT operate on TABLAT
only
Example:
TBLRD*
TBLRD*+
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 247
PIC18 Instructions
Data Ö Program Operations - Example
Read a lookup table entry:
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 248
PIC16C/FXXX to
PIC18FXXXX
Source Code
Conversion Tips
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 249
PIC16F/CXXX to PIC18FXXXX
Assembly Compatibility
l C source code on most PIC16C/FXXX platforms will
directly port to PIC18FXXXX
l Compatible Assembly code source except:
l Absolute constants used for program memory
l Computed GOTO (addwf PCL,F)
l RAM requirements above 256 bytes are selected by BSR
not RP0 and RP1 bits
l FSR is 12 bits wide, also includes auto increment
l Double check immediate constants when initializing
peripherals
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 250
Code Conversion Tip
l Data Memory Access
bsf STATUS,RP0 bcf STATUS,RP0
l These instructions can be ignored because bits 7,6,5 in
STATUS register are unused
l For devices with less than 256 bytes of RAM, it is not
necessary to be concerned with RAM locations. Why is this
the case?
l Most memory accesses can be done in Access Bank
l Assembler will automatically select “a” bit when applicable
l Address locations now use 12-bit values.
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 251
Code Conversion Tip
l PCLATU, PCLATH
l CALL, GOTO instructions write directly to the program
counter.
l Operations to the PC latches before a CALL or GOTO
will be ignored.
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 253
Code Conversion Tip
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 254
Code Conversion Tip
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 255
Code Conversion Tip
16-bit Instruction for CALL and GOTO
OP CODE s n n n n n n n n
OP CODE n n n n n n n n n n n n
OP CODE n n n n n n n n n n n
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 256
Appendix C:
PIC18FXXXX
Flash
Programming
Tips
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 257
PIC18F FLASH
Program Memory Reads and Writes
l READs performed on bytes
l Can READ entire user Program Memory of up to
2M plus:
l User ID locations 200000h-200007h
l CONFIG registers 300000h-30000Dh
l Device ID registers 3FFFFEh,3FFFFFh
l To READ Program Memory:
l Load TBLPTRU,TBLPTRH,TBLPTRL
l Execute one of the TBLRDs
l TBLRD*, TBLRD*+, TBLRD+*, TBLRD*-
l result in TABLAT
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 258
PIC18F ARCHITECTURE
0h
l 18F Addressable Memory USER MEMORY
is divided into:
01FFFFh
l USER MEMORY:
l Up to 128 Kbytes
internal 200000h
l Up to 2 Mbytes external 200007h
User IDs
l USER IDs:
l 8 modifiable bytes
300000h
l CONFIGs:
30000Dh
CONFIGs
l Device settings, code
protects, etc
l DEVICE IDs: 3FFFFEh
3FFFFFh
Device IDs
l Part and rev. signature
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 259
PIC18F FLASH
Program Memory Reads and Writes
l ERASING User memory (USER MODE):
l Performed on 64 bytes (32 words)
l Load TBLPTRU,TBLPTRH,TBLPTRL
l TBLPTR 6 LSBs are don’t cares
l Configure EECON1
l Disable interrupts
l Perform programming sequence
l Start ERASE
l Internally timed, NO CODE EXECUTION
l Re-enable interrupts
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 260
PIC18F FLASH
Program Memory Reads and Writes
l WRITEs to User memory (USER MODE):
l Performed on 8 bytes (4 words)
l Load TBLPTRU,TBLPTRH,TBLPTRL
l Load 8 bytes into write buffers by 8 table write instructions
l TBLWT*,TBLWT *+,TBLWT*-,TBLWT+*
TBLWT*,TBLWT*+,TBLWT*-,TBLWT+*
l Configure EECON1
l Disable interrupts
l Perform programming sequence
l Start WRITE
l Internally timed, NO CODE EXECUTION
l Re-enable interrupts
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 261
PIC18F ARCHITECTURE
0h
l 18F Internal User Memory BOOT BLOCK
1FFh
is separated by: Panel 1
l PANELS: 1FFFh
l Define internal cell Panel 2
grouping boundaries 3FFFh
l Always 8 Kbytes
(4 Kwords) Panel 3
5FFFh
l BLOCKS:
l Define Code Protect Panel 4
boundaries 7FFFh
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 262
PIC18F ARCHITECTURE
207Fh
3F40h
3FFFh
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 263
PIC18F ARCHITECTURE
TABLAT
Holding Registers
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 264
PIC18F ARCHITECTURE
2000h
203Fh
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 265
PIC18F EECON1
EECON1 REGISTER
R/W-x R/W-x U-0 R/W-0 R/W-x R/W-0 R/S-0 R/S-0
EEPGD CFGS - FREE WRERR WREN WR RD
bit7 6 5 4 3 2 1 bit0
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 266
PIC18F EECON1
R/W-x R/W-x U-0 R/W-0 R/W-x R/W-0 R/S-0 R/S-0
EEPGD CFGS - FREE WRERR WREN WR RD
bit7 6 5 4 3 2 1 bit0
bit 3: WRERR: FLASH and EEPROM Error Flag Bit
1 = A write operation is prematurely terminated (RESET)
0 = The write operation completed
Note: When WRERR occurs, EEPGD and CFGS are not cleared.
bit 2: WREN: FLASH and EEPROM Write Enable Bit
1 = Allows write cycles
0 = Inhibits erases or writes to FLASH and EEPROM
bit 1: WR: Write Control Bit
1 = Initiates FLASH erase or write or EEPROM erase/write
0 = The write or erase operation is complete
bit 0: RD: Read Control Bit
1 = Initiates an EEPROM read
0 = Does not initiate an EEPROM read
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 267
PIC18F REQUIRED SEQUENCE
movlw 55h
movwf EECON2
movlw AAh
movwf EECON2
bsf EECON1,WR
nop
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 268
PIC18F READ
l READs performed on bytes
l Can READ entire user Program Memory of up to
2M plus:
l User ID locations 200000h-200007h
l CONFIG registers 300000h-30000Dh
l Device ID registers 3FFFFEh,3FFFFFh
l To READ Program Memory:
l Load TBLPTRU,TBLPTRH,TBLPTRL
l Execute one of the TBLRDs
l TBLRD*, TBLRD*+, TBLRD+*, TBLRD*-
l result in TABLAT next instruction cycle
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 269
PIC18F READ
l READ Code example:
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 270
PIC18F ERASE
l ERASING User memory:
l Performed on 64 bytes (32 words)
l Load TBLPTRU,TBLPTRH,TBLPTRL
l Configure EECON1
l Disable interrupts
l Perform programming sequence
l Start erase (Set WR bit)
l Internally timed 2 mS (typical)
l PROCESSOR ‘HALTS’, NO CODE EXECUTION
l TBLPTR 6 LSBs are don’t cares
TBLPTRU TBLPTRH TBLPTRL
xxxxxx
l Re-enable interrupts
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 271
PIC18F ERASE
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 272
PIC18F WRITE
l WRITEs to User memory:
l Performed on 8 bytes (4 words)
l Load TBLPTRU,TBLPTRH,TBLPTRL
l Load 8 bytes into write buffers by 8 table write instructions
l TBLWT*,TBLWT *+,TBLWT*-,TBLWT+*
TBLWT*,TBLWT*+,TBLWT*-,TBLWT+*
l Configure EECON1
l Disable interrupts
l Perform programming sequence
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 273
PIC18F WRITE
l WRITEs to User memory (cont):
l Start write (set WR bit)
l Internally timed 2 mS
l PROCESSOR ‘HALTS’, NO CODE EXECUTION
l TBLPTR 3 LSBs are don’t cares
l Re-enable interrupts
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 274
PIC18F WRITE
l What’s wrong with this?
; GIVEN:
; FSR0 -> points to first of 8 bytes of a buffer
; that will be written
; TBLPTR -> points to first byte of 8 byte block in
; internal user memory
; COUNTER = 8
; WRITEIT = Correct programming macro
WRITE_TO_HREGS
movff POSTINC0,TABLAT ; load Holding Regs
TBLWT*+ ;
decfsz COUNTER ;
bra WRITE_TO_HREGS ;
WRITEIT ; Write Holding Regs
; to user memory
PIC18FXXXX
Peripheral
Configuration
Spreadsheet
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 276
Spreadsheet Basics
PIC18Fxxx Peripheral Configuration.xls
Configuration.xls
l Spreadsheet based on Microsoft Excel
l Calculates period, baud rate, operating
frequency for the following peripherals:
l TMR0,TMR1,TMR2 and TMR3 period
l PWM / CCP0 through PWM / CCP4 frequency
l A/D conversion period
l UART Baud Rate
l Contains reference map for Special Function
Registers, Pinouts and Instruction Set
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 277
PWM Configuration Example
© 2002 Microchip Technology Incorporated. All Rights Reserved. 618 ICD PIC18FXXX DFT Hands On Workshop 278