85
How to pick your PIC

How to pick your PIC

Embed Size (px)

DESCRIPTION

Overview of a PIC Project 1. Think carefully about what you really want to do. 2. Select which PIC you’re going to use. 3. Read the data sheet! 4. Use the editor in MPLAB to write the code. 5. Use the MPLAB assembler (MPASM) to assemble the code. 6. Fix the billions of assembly errors. 7. Simulate your code in the MPLAB PIC simulator (MPSIM). 8. Fix more errors (usually logical and initialization errors). 9. Load the program on your PIC with the programmer. 10. Test the PIC in circuit. Attempt to identify bugs. 11. Fix the errors (usually timing, interrupt and peripheral errors) and re-test. 12. Brag to your friends about how you’re on the cutting edge of embedded control. (Yellow Letters = what we’ll talk about today)

Citation preview

Page 1: How to pick your PIC

How to pick your PIC

Page 2: How to pick your PIC

Overview of a PIC Project 1. Think carefully about what you really want to do. 2. Select which PIC you’re going to use. 3. Read the data sheet! 4. Use the editor in MPLAB to write the code. 5. Use the MPLAB assembler (MPASM) to assemble the code. 6. Fix the billions of assembly errors. 7. Simulate your code in the MPLAB PIC simulator (MPSIM). 8. Fix more errors (usually logical and initialization errors). 9. Load the program on your PIC with the programmer.10. Test the PIC in circuit. Attempt to identify bugs.11. Fix the errors (usually timing, interrupt and peripheral errors) and re-test.12. Brag to your friends about how you’re on the cutting edge of embedded control.

(Yellow Letters = what we’ll talk about today)

Page 3: How to pick your PIC

PIC Architecture: BackgroundMicroprocessor: Requires ‘external’ support hardware E.g., External RAM, ROM, Peripherals.

Microcontroller: Very little external support hardware. Most RAM, ROM and peripherals on chip. “Computer on a chip”, or “System on chip” (SOC) E.g., PIC = Peripheral Interface Controller

Page 4: How to pick your PIC

PIC Architecture: BackgroundWe’re used to the Von-Neuman Architecture

Used in: 80X86 (PCs), 8051, 68HC11, etc.) Only one bus between CPU and memory RAM and program memory share the same bus and the same

memory, and so must have the same bit width Bottleneck: Getting instructions interferes with accessing RAM

CPU 8

Memory (Program

& Data)

Page 5: How to pick your PIC

PIC Architecture: BackgroundPICs use the Harvard Architecture

Used mostly in RISC CPUs (we’ll get there) Separate program bus and data bus: can be different widths! For example, PICs use:

– Data memory (RAM): a small number of 8bit registers– Program memory (ROM): 12bit, 14bit or 16bit wide

(in EPROM, FLASH, or ROM)

CPU 121416

Memory(Data) 8

Memory (Program)

Page 6: How to pick your PIC

PIC Architecture: BackgroundTraditionally, CPUs are “CISC” Complex Instruction Set Computer (CISC) Used in: 80X86, 8051, 68HC11, etc. Many instructions (usually > 100) Many, many addressing modes Usually takes more than 1 internal clock cycle (Tcyc) to execute Example:

MC68HC05: LDAA 0x55

01010101

1000 11002 bytes, 2 cycles

Page 7: How to pick your PIC

PIC Architecture: BackgroundPICs and most Harvard chips are “RISC” Reduced Instruction Set Computer (RISC) Used in: SPARC, ALPHA, Atmel AVR, etc. Few instructions (usually < 50) Only a few addressing modes Executes 1 instruction in 1 internal clock cycle (Tcyc) Example:

PIC16CXXX: MOVLW 0x551 word, 1 cycle

1100XX 01010101

Page 8: How to pick your PIC

PIC Architecture: ConvergenceMany Microcontrollers and DSP chips are “converging”

Heading towards some mean between RISC and CISC Large CPUs (DSPs) are adding microcontroller like options

(the 32bit, 100MHz StrongARM draws only 70mA) Small microcontrollers are getting more powerful, now able

to do some DSP General trend: Smaller packages, less power consumption,

faster Future possibility: “Sea of gates” reconfigurable processor

Page 9: How to pick your PIC

Exam

ple

PIC

: 12C

508

Blo

ck D

iagr

am

Page 10: How to pick your PIC

The PIC Family: CoresPICs come with 1 of 4 CPU ‘cores’: 12bit cores with 33 instructions: 12C50x, 16C5x

14bit cores with 35 instructions: 12C67x,16Cxxx

16bit cores with 58 instructions: 17C4x,17C7xx

‘Enhanced’ 16bit cores with 77 instructions: 18Cxxx

Page 11: How to pick your PIC

The PIC Family: PackagesPICs come in a huge variety of packages:

8 pin DIPs, SOICs: 12C50x (12bit) and 12C67x (14bit)18pin DIPs, SOICs: 16C5X (12bit), 16Cxxx (14bit)28pin DIPs, SOICs: 16C5X (12bit), 16Cxxx (14bit)40pin DIPs, SOICs: 16Cxxx (14bit), 17C4x (16bit)44 - 68pin PLCCs*: 16Cxxx (14bit), 17C4x / 17Cxxx (16bit)

* also TQFPs, etc.

Page 12: How to pick your PIC

The PIC Family: SpeedPICs require a clock to work. Can use crystals, clock oscillators, or even an RC circuit. Some PICs have a built in 4MHz RC clock

– Not very accurate, but requires no external components! Instruction speed = 1/4 clock speed (Tcyc = 4 * Tclk) All PICs can be run from DC to their maximum spec’d speed:

12C50x 4MHz12C67x 10MHz16Cxxx 20MHz17C4x / 17C7xxx 33MHz18Cxxx 40MHz

Page 13: How to pick your PIC

The PIC Family: Program MemoryPIC program space is different for each chip.

Some examples are:

12C508 512 12bit instructions16C71C 1024 (1k) 14bit instructions16F877 8192 (8k) 14bit instructions17C766 16384 (16k) 16bit instructions

Page 14: How to pick your PIC

The PIC Family: Program MemoryPICs have two different types of program storage:

1. EPROM (Erasable Programmable Read Only Memory) Needs high voltage from a programmer to program (~13V) Needs windowed chips and UV light to erase Note: One Time Programmable (OTP) chips are EPROM chips,

but with no window! PIC Examples: Any ‘C’ part: 12C50x, 17C7xx, etc.

Page 15: How to pick your PIC

The PIC Family: Program MemoryPICs have two different types of program storage:

2. FLASH Re-writable (even by chip itself) Much faster to develop on! Finite number of writes (~100k Writes) PIC Examples: Any ‘F’ part: 16F84, 16F87x, 18Fxxx (future)

Page 16: How to pick your PIC

The PIC Family: Data MemoryPICs use general purpose “file registers” for RAM

(each register is 8bits for all PICs)

Some examples are:

12C508 25 Bytes RAM16C71C 36 Bytes RAM16F877 368 Bytes (plus 256 Bytes of nonvolatile EEPROM)17C766 902 Bytes RAM

Don’t forget, programs are stored in program space (not in data space), so low RAM values are OK.

Page 17: How to pick your PIC

The PIC Family: Control RegistersPICs use a series of “special function registers” for

controlling peripherals and PIC behaviors.

Some examples are:

STATUS Bank select bits, ALU bits (zero, borrow, carry)INTCON Interrupt control: interrupt enables, flags, etc.TRIS Tristate control for digital I/O: which pins are ‘floating’TXREG UART transmit register: the next byte to transmit

Page 18: How to pick your PIC

The PIC Family: PeripheralsDifferent PICs have different on-board peripherals

Some common peripherals are:– Tri-state (“floatable”) digital I/O pins– Analog to Digital Converters (ADC) (8, 10 and 12bit, 50ksps)– Serial communications: UART (RS-232C), SPI, I2C, CAN– Pulse Width Modulation (PWM) (10bit)– Timers and counters (8 and 16bit)– Watchdog timers, Brown out detect, LCD drivers

Page 19: How to pick your PIC

PIC Peripherals: Ports (Digital I/O) All PICs have digital I/O pins, called ‘Ports’

– the 8pin 12C508 has 1 Port with 4 digital I/O pins– the 68pin 17C766 has 9 Ports with 66 digital I/O pins

Ports have 2 control registers– TRISx sets whether each pin is an input or output– PORTx sets their output bit levels

Most pins have 25mA source/sink (directly drives LEDs) WARNING: Other peripherals SHARE pins!

Page 20: How to pick your PIC

PIC Peripherals: ADCs Only available in 14bit and 16bit cores Fs (sample rate) < 54KHz Most 8bits, newer PICs have 10 or 12bits All are +/- 1LSB and are monotonic Theoretically higher accuracy when PIC is in sleep mode

(less digital noise) Can generate an interrupt on ADC conversion done Multiplexed 3 (12C671) - 12 (17C7xxx) channel input

– Must wait Tacq to charge up sampling capacitor(see datasheets)

Page 21: How to pick your PIC

PIC Peripherals: USART: UART Serial Communications Peripheral: Universal

Synchronous/Asynchronous Receiver/Transmitter Only available in 14bit and 16bit cores Interrupt on TX buffer empty and RX buffer full

Asynchronous communication: UART (RS-232C serial)– Can do 300bps - 115kbps– 8 or 9 bits, parity, start and stop bits, etc. – Outputs 5V so you need a RS232 level converter (e.g., MAX232)

Page 22: How to pick your PIC

PIC Peripherals: USART: USRT Synchronous communication: i.e., with clock signal

SPI = Serial Peripheral Interface– 3 wire: Data in, Data out, Clock– Master/Slave (can have multiple masters)– Very high speed (1.6Mbps)– Full speed simultaneous send and receive (Full duplex)

I2C = Inter IC– 2 wire: Data and Clock– Master/Slave (Single master only; multiple masters clumsy)– Lots of cheap I2C chips available; typically < 100kbps

(For example, 8pin EEPROM chips, ADC, DACs, etc.)

Page 23: How to pick your PIC

PIC Peripherals: Timers Available in all PICs. 14+bit cores may generate interrupts on timer overflow. Some 8bits, some 16bits, some have prescalers Can use external pins as clock in/clock out

(ie, for counting events or using a different Fosc) Warning: some peripherals share Timer resources

Page 24: How to pick your PIC

PIC Peripherals: CCP Modules Capture/Compare/PWM (CCP) 10bit PWM width within 8bit PWM period (frequency)

– Enhanced 16bit cores have better bit widths Frequency/Duty cycle resolution tradeoff

– 19.5KHz has 10bit resolution– 40KHz has 8bit resolution– 1MHz has 1bit resolution (makes a 1MHz clock!)

Can use PWM to do DAC - See AN655 Capture counts external pin changes Compare will interrupt on when the timer equals the value in a

compare register

Page 25: How to pick your PIC

PIC Peripherals: Misc. Sleep Mode: PIC shuts down until external interrupt (or internal timer)

wakes it up. Interrupt on pin change: Generate an interrupt when a digital input pin

changes state (for example, interrupt on keypress). Watchdog timer: Resets chip if not cleared before overflow Brown out detect: Resets chip at a known voltage level LCD drivers: Drives simple LCD displays Future: CAN bus, 12bit ADC, better analog functions

VIRTUAL PERIPHERALS:– Peripherals programmed in software. UARTS, timers, and more can be

done in software (but it takes most of the resources of the machine)

Page 26: How to pick your PIC

Selecting your PIC See Microchip Line card for the entire list of PICs :

http://www.microchip.com/10/Lit/rLit/00148d1/index.htm

See the Digikey catalog for pricing information.http://www.digikey.com

Page 27: How to pick your PIC

Low End: 12C508 8pin package (DIP, SO) 12bit core - 33 instructions 1us instruction time (Tclk = 4MHz) 512 12bit program memory 25 8bit data memory or registers (“File registers”) 2 level hardware stack (no interrupts) 5 GPIO pins, 1 input only (25mA source/sink) Features: Internal pullups, wake up on pin change, internal oscillator Peripherals: Timer, Watch Dog Timer $1.88(1), $1.25(100), $9.65(W)

Page 28: How to pick your PIC

Mid Range: 16F876 28pin package (DIP, SO) 14bit core - 35 instructions 200ns instruction time (Tclk = 20MHz) 8,092 14bit FLASH program memory 368 8bit data memory or registers (“File registers”) 256 8bit EEPROM (nonvolatile) data registers 8 level hardware stack (interrupts enabled) 22 GPIO (20mA source / 25mA 7sink) Peripherals: 5ch 10bit ADC, USART/I2C/SPI, 16bit & 8bit timers Features: Brown out detect, In-Circuit Debugger (ICD) $11.00(1), $5.89(100)

Page 29: How to pick your PIC

High End: 17C766 84pin PLCC package 16bit core - 58 instructions 121ns instruction time (Tclk = 33MHz) 16,384 16bit program memory 902 8bit data memory or registers 16 level hardware stack (priority interrupts) 66 GPIO (20mA source / 35mA sink) Features: 8x8 multiply, BOD, microprocessor mode Peripherals:

– 2x 16bit + 2x 8bit timer, WDT, 2x USART, 4x CCP, – 12ch 10bit ADC,

$20.25(1), $10.53(100), $18.38(W)

Page 30: How to pick your PIC

12C508, 16F876, 17C766 Uses 12C508

– Inexpensive controllers, glue logic, simple tasks– E.g., quadrature decoding, digital interfacing

16F876– Multitasking programs, serial communication– E.g., Cheap data acquisition system and digital I/O system for PC

off COM ports, data logging 17C766

– RTOS, low end DSP, communications, big moosey applications– E.g., FEC converter, Rocket Flight Computer, cheap FFT chip

Page 31: How to pick your PIC

For Comparison: The 68HC11 48pin DIP, 52pin PLCC packages 145 instructions & 6 addressing modes 333ns instruction time (Tclk = 12MHz) 12k 8bit ROM or EPROM program memory 512 8bit general RAM memory 512 8bit EEPROM Software stack (uses general RAM) 16 I/O, 11 inputs, 11 outputs Peripherals:

– USART, SPI, 8ch 8bit ADC, 4x 16bit capture timers, 4x PWM, Pulse accumulator, WDT, 16bit GP clock

~$7.00(1), ?(100), ?(W)

Page 32: How to pick your PIC

Getting ready to code! ALWAYS have the data sheet for your PIC:

http://www.microchip.com/

There are just too many details you have to know!

Example: See PIC12C508 data sheet

Page 33: How to pick your PIC

PART II

Writing PIC Code

Page 34: How to pick your PIC

Software: Introduction In this course, we’ll only talk about PIC assembly language as used in

the MPLAB assembler.– MPLAB is FREE (score): see http://www.microchip.com/– Assembly is a bit harder to code, but faster and more compact.

BASIC and C compilers exist but are spendy.– Easier to use for mathematically intensive programs

(E.g., floating point, really complicated logic)– Small, simple programs may be better done in assembly.

Excursion: Brief introduction to MPLAB

Page 35: How to pick your PIC

Software: Instruction Set Let’s see what makes PICs tick!

See handout:

“PIC 16CXX Instruction Set Summary”

Program counter increments once each Tcyc to “grab” the next instruction

Remember, instructions are stored in program memory and are completely separate from RAM (“File registers”).

Page 36: How to pick your PIC

Software: Programmers Model

Program Memory

“Burned” in by programmer (can’t

change during execution). Stored

instructions, addresses and “literals” (numbers).

<- 12/14/16 bits ->

Hardware StackStores addresses for subroutines

Program Counter-PCL(PCH)

Status

Special PurposeRegisters

I/O pin states, peripheral

registers, etc.

General PurposeRegisters

RAM or “data memory”. Variables are stored here.

W “Register”

<- 8 bits ->

Page 37: How to pick your PIC

Software: Instruction Examples

movlw 0xFF

Move (“mov”) the number (“l” for “literal”) 0xFF - that’s 256 in decimal- into the working register (“w”).

In other words, load W with the value 0xFF.

Page 38: How to pick your PIC

Software: Programmers Model

Program Memory

<- 12/14/16 ->

Hardware Stack

Program Counter-PCL(PCH)

Status

Special PurposeRegisters

General PurposeRegisters

W “Register”

<- 8 bits ->

0xFF

Page 39: How to pick your PIC

Software: Instruction Examplesmovwf PORTA

Move (“mov”) the working register (“w”) into the file register(“f”) named PORTA.

In other words, load the register called PORTA with whatevernumber is in the W register.

Page 40: How to pick your PIC

Software: Programmers Model

Program Memory

<- 12/14/16 ->

Hardware Stack

Program Counter-PCL(PCH)

Status

Special PurposeRegisters

PORTA

General PurposeRegisters

W “Register”

<- 8 bits ->

Value in W

Page 41: How to pick your PIC

Software: Instruction Examples

movf PORTA, W

Move (“mov”) the the value of the file register (“f”) named PORTA into the working register (“w”) .

In other words, load W with the whatever number is in PORTA.

Page 42: How to pick your PIC

Software: Programmers Model

Program Memory

<- 12/14/16 ->

Hardware Stack

Program Counter-PCL(PCH)

Status

Special PurposeRegisters

PORTA

General PurposeRegisters

W “Register”

<- 8 bits ->

Value in PORTA

Page 43: How to pick your PIC

Software: Assembly Format First column: Labels Second column: opcodes and assembler directives Third Columns & more: operands

; This is a comments since it starts with a “;”; This program puts out a square wave on PORTA Pin 0

clrf PORTA ; Clear PORTA registerclrf TRISA ; Make PORTA all outputs

Loop bsf PORTA,0 ; Turn on PORTA Pin 0nop ; Match ‘goto’ delay nop ; “ “ “ bcf PORTA,0 ; Turn off PORTA Pin 0goto Loop ; If not zero, loop back

Page 44: How to pick your PIC

Software: Branches All branches are “Bit Tests” All branches only skip one instruction

; Set EqualFlag if PORTA = PORTB

bcf EqualFlag, 7 ; First, clear the flagmovf PORTA, W ; Move PORTA -> Wsubwf PORTB, W ; W - PORTB -> Wbtfsc STATUS, Z ; Check Z bit (see

STATUS)bsf EqualFlag, 7 ; Ports equal; set flag

Page 45: How to pick your PIC

STA

TUS

Reg

iste

r

Page 46: How to pick your PIC

Software: Direct Addressing All file registers (RAM) are accessed by an address. This is

called direct addressing.

For example,

movlw 0xFFmovwf 0x06

loads W with FF, and then loads W into GPIO (address 0x06).

Thankfully, we can use labels instead of addresses:

GPIO equ 0x06movwf GPIO

Page 47: How to pick your PIC

Software: Relative Addressing PCL = Low byte of the Program Counter Can be read and written. Writing to it sets the address of the next instruction to be executed.

12bit core 14bit core

Page 48: How to pick your PIC

Software: Relative AddressingExample of Relative Addressing (using a table):

; Here’s a simple lookup table which is called as a; subroutine. Expects the table offset to be loaded in W.; An example call looks like this:; movlw 0x04 ; Load W with 4; call Table ; Call the table subroutine; movwf Result ; Store the result from the table

Table addwf PCL, W ; Jump to (current PCL) + Wretlw 0x00 ; Return with 0x00 in Wretlw 0x23 ; Return with 0x23 in Wretlw 0x33 ; etc.retlw 0x88

Page 49: How to pick your PIC

Software: Indirect Addressing

Load indirect address into FSR Reading/Writing to INDF acts on

address stored in FSR

Example code to clear 0x20 - 7F:

movlw 0x20movwf FSR

loopclrf INDFincf FSR,Fbtfss FSR,7goto loop

INDF00h

04h

7FhRegister File

FSR

Page 50: How to pick your PIC

Software: BankingRAM in the PICs is banked, especially special function registers. Use the bank select commands to choose the bank.

Either:

bsf STATUS, RP0bcf STATUS, RPO

Or use the assembler directive:

Banksel <registername>

Page 51: How to pick your PIC

Software: Real Code! Note: Each PIC has a predefined “.h” file which

contains labels for each special file register(so you don’t have to!)

A working program requires initialization code and option codes set in the program. See .ASM examples for initialization code

Please see Example.asm

Page 52: How to pick your PIC

Software Tips

Destination bit determines W or F for result Look at data movement and re-structure

Example: A + B -> A

MOVF B,WADDWF A,F

2 instructions

MOVF A,WADDWF B,WMOVWF A

3 instructions

Page 53: How to pick your PIC

Software TipsWASTE MACRO NUMBER

NOLIST ; Doesn’t expand this into listingLOCAL i ; Use local variable in Macroi set NUMBER ; Initialize the local counter

WHILE i ;Count of cycles to waste IF i > 1 goto $+1 ;twice the waste, half the space i -= 2 ELSE nop ;Waste 1 Cycle

i -= 1 ENDIF ENDW LIST

ENDM

Page 54: How to pick your PIC

Software Tips; Define variable names (without bothering with; absolute addresses)

CBLOCK 0x20 ;Start of data space. Var1: 1

Var2: 1Var16:2

ACCL: 1 ACCH: 1

ENDC

; You can always call one thing many names, Grasshopper.

ACCA equ ACCL ;alias ACCLACCB equ ACCH ;alias ACCH

Page 55: How to pick your PIC

Software Tips; This routine multiplies W by tmp (8x8). Uses; temporary register CntDwn and stores 16bit result; in ACCH:ACCL.

Mult clrf ACCL clrf ACCH clrf CntDown bsf CntDown,3 ;CntDown -> 8 bcf STATUS,CMSum rrf tmp,F btfsc STATUS,C addwf ACCH,F rrf ACCH,F rrf ACCL,F decfsz CntDown,F goto MSum return

Page 56: How to pick your PIC

Software Tips;Save the current state on interrupt;(NOTE: _W must map in both Banks - e.g. 7F/FF)

Interrupt movwf _W swapf STATUS,W ;Move STATUS w/o changing it bcf STATUS,RP0 ;Switch to page 0 movwf _STATUS ;Save old status (swapped)

.

.

. swapf _STATUS,W ;Load old STATUS (& unswap)movwf STATUS ;also restores old page#

swapf _W,F swapf _W,W retfie

Page 57: How to pick your PIC

Software: Pitfalls! Bit tests will screw you up! Be careful! For example:

movf Register, Wbtfsc STATUS, Zgoto NZero

Zero ..

Nzero .

(WRONG!)

Page 58: How to pick your PIC

Software: Pitfalls! For all 12 and 14bit cores, the working register, “W”, can

NOT be addressed. So:

swapf W, W

will not work!

However, in the 17CXXX series you CAN address the working register (called WREG).

Page 59: How to pick your PIC

Software: Pitfalls! Peripheral Pin sharing

Many times pins share functions. E.g., a GPIO will share a pin with a UART module (say the TX line). You CAN’T use one pin for two functions! You must choose between them.

Peripheral Resource Sharing

Some resources require using the same resource. For example, some of the

PWM modules use TMR2, which may also be used in the USART module.

Page 60: How to pick your PIC

Software: Pitfalls! Read-Modify-Write problems CAN BE SERIOUS! (Uplink)

BCF/BSF PORTn Does the following:– Reads in the PORTn byte – Clears/sets the bit– Write the whole byte back.– BUT! If something external pulls a different output pin low or high during the READ,

the read in value will not be what you expect - WORSE, the WRITE will permanently change it that way.

Solution: Use Shadowed I/O (Example: set PORTA Bit 0)bsf _PORTA, 0movf _PORTA, Wmovwf PORTA

Page 61: How to pick your PIC

Software: Pitfalls!Make sure you always set the correct BANK bits!

bsf STATUS, RP0clrf TRISAbcf STATUS, RPO

is correct; but if you just do

clrf TRISA

you’ll actually execute:

clrf PORTA

Page 62: How to pick your PIC

Software: Conclusion Choose: Project then Build All. MPASM

will attempt to compile your code.

Get ready to fix a billion errors!

These errors are usually syntactical in nature… you won’t catch the logical errors until you simulate!

Page 63: How to pick your PIC

PART III

Simulating your code

Page 64: How to pick your PIC

Simulation Introduction Ok, my code assembles! Let’s go!

– Not so fast. Sure it assembles, but does it actually do what it’s suppose to do?

– Simulating your code - stepping through it line by line and watching register and port values - will let you catch all sorts of logical and program flow errors.

MPLAB has a free simulator called MPSIM– Warning: Many of the peripherals are not simulated correctly.– Coolness: You can step through your code, use watch windows, time

routines, set breakpoints, etc.

See MPSIM Example on Example.asm

Page 65: How to pick your PIC

Simulation Issues Once your simulation seems to work,

you’ve probably caught most of the major logical errors in your program.

Now you’re ready to program your PIC and find out how it behaves in the Real World™.

Page 66: How to pick your PIC

PART IV

Programming PICs

Page 67: How to pick your PIC

Programming PICs: PICSTART+ Low cost ($149.00) programmer with ZIF socket

Programs all PICs except > 40pin packages(which require an adapter)

Cheaper available but only for 16F84 series

Con: Must take chip out of circuit to program

Page 68: How to pick your PIC

Programming PICs: ICSP

In-Circuit Serial Programmability– Good for commercial design– Available for most PICS– Assemble boards, then program them

Vpp = +13V Vdd = Operating Vdd Cons: Requires dual use on ICSP pins

PIC16CXXPIC14CXXXPIC12CXXX

MCLR/VPP

VDD VDD

VPP

VSS VSS

I/O 1 Clock

I/O 2 Data I/O

Page 69: How to pick your PIC

Programming PICs: ICD Low cost ($159.00)

Programs only 16F87xx PICs in ICP mode

Is also an in-circuit debugger (!)

Page 70: How to pick your PIC

Programming PICs: Erasing You must have a UV Eraser to do PIC EPROM

programming– Good one from Digikey is about $50.00– Takes anywhere between 3 - 10 minutes to erase.– You can check if it’s erased using the “Blank Check”

option in MPLAB.– Not blank? Erase it again. Light won’t ever damage the

silicon. We think.

Page 71: How to pick your PIC

Programming PICs: Tricks NOP reprogramming Trick

– Erased EPROM is all ‘1’s - ie, 0x3FF (12bit core)– Programming changes instructions by adding ‘0’s– You can ‘0’ out your code and use the remaining unprogrammed

memory to do in the field “reprogramming– I.E., change all of the existing code to NOPs so startup will

simply pass right through it– Consider adding some 0x3FF “spaces” to add GOTOs if you think

you’ll be reprogramming (which you always will!)

Page 72: How to pick your PIC

Programming PICs: Pitfalls THERE IS ONLY ONE DOWNLOAD BUFFER.

– If you assemble your program,– then Read another PIC,– You will program that PIC’s Hex dump and not your

program!– ALWAYS reassemble your code after reading anything

you don’t want to write!

Page 73: How to pick your PIC

PART V

The Real World™ and your PIC:Building and debugging

Page 74: How to pick your PIC

The Minimum PIC System You need some kind of a clock:

– Internal 4MHz clock on some PICs, external RC, external crystal, or external clock oscillator.

And you need some power:– Most PICs can operate from 2.5V - 5.5V (see Data Sheets)– New 16HVXX can directly use up to 14V

(Great for direct 9V or 12V battery hook ups)

And that’s it!

Page 75: How to pick your PIC

Infiltrating the Epoxy Curtain Biggest drawback to PICs is the lack of easy in-circuit

debugging.– You’ve programmed your PIC, stuck it in your circuit, turned it

on and it just sits there and does nothing. Now what do you do?– Your PIC is now a black (epoxy) box– You can check the code by hand, but that takes a lot of time,

and doesn’t always work.

You need debugging tools!

Page 76: How to pick your PIC

Debugging Tools: ICD The 16F87x ICD lets you debug your PIC in situ.

– Allows real hardware single stepping, break points, register modifications, all very cool stuff

– Step through the program while the PIC actually executes the code.

– But; this only works on the 16F87x

Page 77: How to pick your PIC

Debugging Tools: Emulators Emulators: Cool as heck and expensive as it too

– Allows real hardware single stepping, break points, register modifications, all very cool stuff

– Works with all PICs (requires a module for each PIC)– But we can’t afford it: > $1000

Page 78: How to pick your PIC

Debugging Tools: Wetware Solution: Wetware debugging tools

– Status Pins» LED or digital outputs to show program status/Location

– Debugging Modes» Controlled by digital inputs; ie, wait if pin high.

– Exotics» Measuring timing, current draw, output spikes, temperature

Page 79: How to pick your PIC

Debugging Tools: Do This Write your own monitor program that takes a minimum of

resources on the PIC.– Allow the PIC, even EPROM versions, to do some forms of

breakpointing based on RAM data.– Communicate serially to dump RAM values into a PC.

Post on the web! Become famous overnight!

Page 80: How to pick your PIC

PART VI

Things that make you go “cool!”

Page 81: How to pick your PIC

Cool Ideas

I/O LEDsA B C 1 2 3 4 5 6

0 0 0 0 0 0 0 0 00 1 Z 1 0 0 0 0 01 0 Z 0 1 0 0 0 0Z 0 1 0 0 1 0 0 0Z 1 0 0 0 0 1 0 00 Z 1 0 0 0 0 1 01 Z 0 0 0 0 0 0 10 0 1 0 0 1 0 1 00 1 0 1 0 1 0 0 00 1 1 1 0 0 0 1 01 0 0 0 1 0 0 0 11 0 1 0 1 1 0 0 01 1 0 0 0 0 1 0 11 1 1 0 0 0 0 0 0

A I/O

B I/O

C I/O

PICmicro1 2

3 4

5 6

6 LEDs off only three pins!

Page 82: How to pick your PIC

Cool Ideas

The following macro swaps the contents ofThe following macro swaps the contents of W W and and REGREG without using a second registerwithout using a second register

XCHGWFXCHGWF MACROMACRO REGREGXORWF REG,FXORWF REG,FXORWF REG,WXORWF REG,WXORWF REGXORWF REG,F,F

ENDMENDM

Needs:Needs: 0 TEMP Registers0 TEMP Registers3 Instructions3 Instructions3 Tcy3 Tcy

Page 83: How to pick your PIC

Cool Things Application Notes - www.microchip.com

– Almost everything you could imagine» RTOS for the 17CXXX family» DSP for the 16CXX family - Inc. FFTs and IIR filters» Micropower applications» All sorts of tricks and tips and in depth explanations

– Code listed in the notes is available as well!

PIC Books– PIC’n (?)– Introduction to PICs (Predko)

Page 84: How to pick your PIC

Cool Things Some examples of PIC applications

– 12C508 web server– 17C756 audio FFT, direct display on VGA monitor– Data acquisition board– Going over board: Replacing a 555 timer

(we don’t condone this, but it has been done)

Page 85: How to pick your PIC

That’s it! Go PIC the world.

Thanks. Questions/Comments?Improvements on this Seminar?