PSU IEEE Workshop on PIC Microcontrollers Andrew and

  • Slides: 87
Download presentation
PSU IEEE Workshop on PIC Microcontrollers Andrew and Tim January 16, 2000 You can

PSU IEEE Workshop on PIC Microcontrollers Andrew and Tim January 16, 2000 You can reach us at: drew@uad. com

Introduction u u We have no idea what we’re talking about, except we’ve used

Introduction u u We have no idea what we’re talking about, except we’ve used PICs. You are here at your own risk. We will assimilate you into the Microchip, Inc. PIC family of RISC Microcontrollers. But there are other good microcontrollers. You will enjoy yourselves.

Overview of a PIC Project 1. Think carefully about what you really want to

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)

PART I How to pick your PIC

PART I How to pick your PIC

PIC Architecture: Background Microprocessor: Õ Requires ‘external’ support hardware Õ E. g. , External

PIC Architecture: Background Microprocessor: Õ 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

PIC Architecture: Background We’re used to the Von-Neuman Architecture Õ Õ Used in: 80

PIC Architecture: Background We’re used to the Von-Neuman Architecture Õ Õ Used in: 80 X 86 (PCs), 8051, 68 HC 11, 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 Memory CPU 8 (Program & Data)

PIC Architecture: Background PICs use the Harvard Architecture ÕUsed mostly in RISC CPUs (we’ll

PIC Architecture: Background PICs 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 8 bit registers – Program memory (ROM): 12 bit, 14 bit or 16 bit wide (in EPROM, FLASH, or ROM) Memory (Data) 8 CPU Memory 12 14 16 (Program)

PIC Architecture: Background Traditionally, CPUs are “CISC” Complex Instruction Set Computer (CISC) Õ Used

PIC Architecture: Background Traditionally, CPUs are “CISC” Complex Instruction Set Computer (CISC) Õ Used in: 80 X 86, 8051, 68 HC 11, etc. Õ Many instructions (usually > 100) Õ Many, many addressing modes Õ Usually takes more than 1 internal clock cycle (Tcyc) to execute Õ Example: Õ MC 68 HC 05: LDAA 0 x 55 1000 1100 0101 2 bytes, 2 cycles

PIC Architecture: Background PICs and most Harvard chips are “RISC” Reduced Instruction Set Computer

PIC Architecture: Background PICs 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: Õ PIC 16 CXXX: MOVLW 0 x 55 1100 XX 0101 1 word, 1 cycle

PIC Architecture: Convergence Many Microcontrollers and DSP chips are “converging” Heading towards some mean

PIC Architecture: Convergence Many Microcontrollers and DSP chips are “converging” Heading towards some mean between RISC and CISC Õ Large CPUs (DSPs) are adding microcontroller like options (the 32 bit, 100 MHz Strong. ARM draws only 70 m. A) Õ 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 Õ

Example PIC: 12 C 508 Block Diagram

Example PIC: 12 C 508 Block Diagram

The PIC Family: Cores PICs come with 1 of 4 CPU ‘cores’: Õ 12

The PIC Family: Cores PICs come with 1 of 4 CPU ‘cores’: Õ 12 bit cores with 33 instructions: 12 C 50 x, 16 C 5 x Õ 14 bit cores with 35 instructions: 12 C 67 x, 16 Cxxx Õ 16 bit cores with 58 instructions: 17 C 4 x, 17 C 7 xx Õ‘Enhanced’ 16 bit cores with 77 instructions: 18 Cxxx

The PIC Family: Packages PICs come in a huge variety of packages: 8 pin

The PIC Family: Packages PICs come in a huge variety of packages: 8 pin DIPs, SOICs: 18 pin DIPs, SOICs: 28 pin DIPs, SOICs: 40 pin DIPs, SOICs: 44 - 68 pin PLCCs*: * also TQFPs, etc. 12 C 50 x (12 bit) and 12 C 67 x (14 bit) 16 C 5 X (12 bit), 16 Cxxx (14 bit), 17 C 4 x (16 bit) 16 Cxxx (14 bit), 17 C 4 x / 17 Cxxx (16 bit)

The PIC Family: Speed PICs require a clock to work. Õ Can use crystals,

The PIC Family: Speed PICs require a clock to work. Õ Can use crystals, clock oscillators, or even an RC circuit. Õ Some PICs have a built in 4 MHz 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: 12 C 50 x 12 C 67 x 16 Cxxx 17 C 4 x / 17 C 7 xxx 18 Cxxx 4 MHz 10 MHz 20 MHz 33 MHz 40 MHz

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

The PIC Family: Program Memory PIC program space is different for each chip. Some examples are: 12 C 508 16 C 71 C 16 F 877 17 C 766 512 12 bit instructions 1024 (1 k) 14 bit instructions 8192 (8 k) 14 bit instructions 16384 (16 k) 16 bit instructions

The PIC Family: Program Memory PICs have two different types of program storage: 1.

The PIC Family: Program Memory PICs have two different types of program storage: 1. EPROM (Erasable Programmable Read Only Memory) Õ Needs high voltage from a programmer to program (~13 V) Õ 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: 12 C 50 x, 17 C 7 xx, etc.

The PIC Family: Program Memory PICs have two different types of program storage: 2.

The PIC Family: Program Memory PICs have two different types of program storage: 2. FLASH Õ Re-writable (even by chip itself) Õ Much faster to develop on! Õ Finite number of writes (~100 k Writes) Õ PIC Examples: Any ‘F’ part: 16 F 84, 16 F 87 x, 18 Fxxx (future)

The PIC Family: Data Memory PICs use general purpose “file registers” for RAM (each

The PIC Family: Data Memory PICs use general purpose “file registers” for RAM (each register is 8 bits for all PICs) Some examples are: 12 C 508 16 C 71 C 16 F 877 17 C 766 u 25 Bytes RAM 368 Bytes (plus 256 Bytes of nonvolatile EEPROM) 902 Bytes RAM Don’t forget, programs are stored in program space (not in data space), so low RAM values are OK.

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

The PIC Family: Control Registers PICs use a series of “special function registers” for controlling peripherals and PIC behaviors. Some examples are: STATUS INTCON TRIS TXREG Bank select bits, ALU bits (zero, borrow, carry) Interrupt control: interrupt enables, flags, etc. Tristate control for digital I/O: which pins are ‘floating’ UART transmit register: the next byte to transmit

The PIC Family: Peripherals Different PICs have different on-board peripherals Some common peripherals are:

The PIC Family: Peripherals Different 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 12 bit, 50 ksps) Serial communications: UART (RS-232 C), SPI, I 2 C, CAN Pulse Width Modulation (PWM) (10 bit) Timers and counters (8 and 16 bit) Watchdog timers, Brown out detect, LCD drivers

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

PIC Peripherals: Ports (Digital I/O) u All PICs have digital I/O pins, called ‘Ports’ – the 8 pin 12 C 508 has 1 Port with 4 digital I/O pins – the 68 pin 17 C 766 has 9 Ports with 66 digital I/O pins u Ports have 2 control registers – TRISx sets whether each pin is an input or output – PORTx sets their output bit levels u u Most pins have 25 m. A source/sink (directly drives LEDs) WARNING: Other peripherals SHARE pins!

PIC Peripherals: ADCs u u u u Only available in 14 bit and 16

PIC Peripherals: ADCs u u u u Only available in 14 bit and 16 bit cores Fs (sample rate) < 54 KHz Most 8 bits, newer PICs have 10 or 12 bits All are +/- 1 LSB 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 (12 C 671) - 12 (17 C 7 xxx) channel input – Must wait Tacq to charge up sampling capacitor (see datasheets)

PIC Peripherals: USART: UART u Serial Communications Peripheral: Universal Synchronous/Asynchronous Receiver/Transmitter Only available in

PIC Peripherals: USART: UART u Serial Communications Peripheral: Universal Synchronous/Asynchronous Receiver/Transmitter Only available in 14 bit and 16 bit cores Interrupt on TX buffer empty and RX buffer full u Asynchronous communication: UART (RS-232 C serial) u u – Can do 300 bps - 115 kbps – 8 or 9 bits, parity, start and stop bits, etc. – Outputs 5 V so you need a RS 232 level converter (e. g. , MAX 232)

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

PIC Peripherals: USART: USRT u Synchronous communication: i. e. , with clock signal u SPI = Serial Peripheral Interface – – u 3 wire: Data in, Data out, Clock Master/Slave (can have multiple masters) Very high speed (1. 6 Mbps) Full speed simultaneous send and receive (Full duplex) I 2 C = Inter IC – 2 wire: Data and Clock – Master/Slave (Single master only; multiple masters clumsy) – Lots of cheap I 2 C chips available; typically < 100 kbps (For example, 8 pin EEPROM chips, ADC, DACs, etc. )

PIC Peripherals: Timers u u u Available in all PICs. 14+bit cores may generate

PIC Peripherals: Timers u u u Available in all PICs. 14+bit cores may generate interrupts on timer overflow. Some 8 bits, some 16 bits, 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

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

PIC Peripherals: CCP Modules u u Capture/Compare/PWM (CCP) 10 bit PWM width within 8 bit PWM period (frequency) – Enhanced 16 bit cores have better bit widths u Frequency/Duty cycle resolution tradeoff – 19. 5 KHz has 10 bit resolution – 40 KHz has 8 bit resolution – 1 MHz has 1 bit resolution (makes a 1 MHz clock!) u u u Can use PWM to do DAC - See AN 655 Capture counts external pin changes Compare will interrupt on when the timer equals the value in a compare register

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

PIC Peripherals: Misc. u 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, 12 bit ADC, better analog functions u VIRTUAL PERIPHERALS: u u u – Peripherals programmed in software. UARTS, timers, and more can be done in software (but it takes most of the resources of the machine)

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

Selecting your PIC u See Microchip Line card for the entire list of PICs : http: //www. microchip. com/10/Lit/r. Lit/00148 d 1/index. htm u See the Digikey catalog for pricing information. http: //www. digikey. com

Low End: 12 C 508 u u u u u 8 pin package (DIP,

Low End: 12 C 508 u u u u u 8 pin package (DIP, SO) 12 bit core - 33 instructions 1 us instruction time (Tclk = 4 MHz) 512 12 bit program memory 25 8 bit data memory or registers (“File registers”) 2 level hardware stack (no interrupts) 5 GPIO pins, 1 input only (25 m. A 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)

Mid Range: 16 F 876 u u u 28 pin package (DIP, SO) 14

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

High End: 17 C 766 u u u u u 84 pin PLCC package

High End: 17 C 766 u u u u u 84 pin PLCC package 16 bit core - 58 instructions 121 ns instruction time (Tclk = 33 MHz) 16, 384 16 bit program memory 902 8 bit data memory or registers 16 level hardware stack (priority interrupts) 66 GPIO (20 m. A source / 35 m. A sink) Features: 8 x 8 multiply, BOD, microprocessor mode Peripherals: – 2 x 16 bit + 2 x 8 bit timer, WDT, 2 x USART, 4 x CCP, – 12 ch 10 bit ADC, $20. 25(1), $10. 53(100), $18. 38(W)

12 C 508, 16 F 876, 17 C 766 Uses u 12 C 508

12 C 508, 16 F 876, 17 C 766 Uses u 12 C 508 – Inexpensive controllers, glue logic, simple tasks – E. g. , quadrature decoding, digital interfacing u 16 F 876 – Multitasking programs, serial communication – E. g. , Cheap data acquisition system and digital I/O system for PC off COM ports, data logging u 17 C 766 – RTOS, low end DSP, communications, big moosey applications – E. g. , FEC converter, Rocket Flight Computer, cheap FFT chip

For Comparison: The 68 HC 11 u u u u u 48 pin DIP,

For Comparison: The 68 HC 11 u u u u u 48 pin DIP, 52 pin PLCC packages 145 instructions & 6 addressing modes 333 ns instruction time (Tclk = 12 MHz) 12 k 8 bit ROM or EPROM program memory 512 8 bit general RAM memory 512 8 bit EEPROM Software stack (uses general RAM) 16 I/O, 11 inputs, 11 outputs Peripherals: – USART, SPI, 8 ch 8 bit ADC, 4 x 16 bit capture timers, 4 x PWM, Pulse accumulator, WDT, 16 bit GP clock ~$7. 00(1), ? (100), ? (W)

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

Getting ready to code! u ALWAYS have the data sheet for your PIC: http: //www. microchip. com/ There are just too many details you have to know! u Example: See PIC 12 C 508 data sheet

PART II Writing PIC Code

PART II Writing PIC Code

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

Software: Introduction u 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. u 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. u Excursion: Brief introduction to MPLAB

Software: Instruction Set u Let’s see what makes PICs tick! See handout: “PIC 16

Software: Instruction Set u Let’s see what makes PICs tick! See handout: “PIC 16 CXX Instruction Set Summary” u u 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”).

Software: Programmers Model Hardware Stack <- 12/14/16 bits -> Stores addresses for subroutines Program

Software: Programmers Model Hardware Stack <- 12/14/16 bits -> Stores addresses for subroutines Program Memory “Burned” in by programmer (can’t change during execution). Stored instructions, addresses and “literals” (numbers). W “Register” (PCH) Program Counter-PCL <- 8 bits -> Status Special Purpose Registers I/O pin states, peripheral registers, etc. General Purpose Registers RAM or “data memory”. Variables are stored here.

Software: Instruction Examples movlw 0 x. FF Move (“mov”) the number (“l” for “literal”)

Software: Instruction Examples movlw 0 x. FF Move (“mov”) the number (“l” for “literal”) 0 x. FF - that’s 256 in decimal- into the working register (“w”). In other words, load W with the value 0 x. FF.

Software: Programmers Model Hardware Stack <- 12/14/16 -> Program Memory (PCH) Program Counter-PCL <-

Software: Programmers Model Hardware Stack <- 12/14/16 -> Program Memory (PCH) Program Counter-PCL <- 8 bits Status Special Purpose Registers 0 x. FF W “Register” General Purpose Registers ->

Software: Instruction Examples movwf PORTA Move (“mov”) the working register (“w”) into the file

Software: Instruction Examples movwf PORTA Move (“mov”) the working register (“w”) into the file register (“f”) named PORTA. In other words, load the register called PORTA with whatever number is in the W register.

Software: Programmers Model Hardware Stack <- 12/14/16 -> Program Memory (PCH) Program Counter-PCL <-

Software: Programmers Model Hardware Stack <- 12/14/16 -> Program Memory (PCH) Program Counter-PCL <- 8 bits Status Special Purpose Registers Value in W W “Register” PORTA General Purpose Registers ->

Software: Instruction Examples movf PORTA, W Move (“mov”) the value of the file register

Software: Instruction Examples movf PORTA, W Move (“mov”) 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.

Software: Programmers Model Hardware Stack <- 12/14/16 -> Program Memory (PCH) Program Counter-PCL <-

Software: Programmers Model Hardware Stack <- 12/14/16 -> Program Memory (PCH) Program Counter-PCL <- 8 bits Status Special Purpose Registers Value in PORTA W “Register” PORTA General Purpose Registers ->

Software: Assembly Format u u u First column: Labels Second column: opcodes and assembler

Software: Assembly Format u u u 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 Loop clrf bsf nop bcf goto PORTA TRISA PORTA, 0 Loop ; ; ; ; Clear PORTA register Make PORTA all outputs Turn on PORTA Pin 0 Match ‘goto’ delay “ “ “ Turn off PORTA Pin 0 If not zero, loop back

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

Software: Branches u u All branches are “Bit Tests” All branches only skip one instruction ; Set Equal. Flag if PORTA = PORTB bcf movf subwf btfsc bsf Equal. Flag, 7 ; First, clear the flag PORTA, W ; Move PORTA -> W PORTB, W ; W - PORTB -> W STATUS, Z ; Check Z bit (see STATUS) Equal. Flag, 7 ; Ports equal; set flag

STATUS Register

STATUS Register

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

Software: Direct Addressing u All file registers (RAM) are accessed by an address. This is called direct addressing. u For example, movlw movwf 0 x. FF 0 x 06 loads W with FF, and then loads W into GPIO (address 0 x 06). u Thankfully, we can use labels instead of addresses: GPIO movwf equ GPIO 0 x 06

Software: Relative Addressing u u u PCL = Low byte of the Program Counter

Software: Relative Addressing u u u 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. 12 bit core 14 bit core

Software: Relative Addressing u. Example of Relative Addressing (using a table): ; Here’s a

Software: Relative Addressing u. Example 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 0 x 04 ; Load W with 4 ; call Table ; Call the table subroutine ; movwf Result ; Store the result from the table Table addwf retlw PCL, W 0 x 00 0 x 23 0 x 33 0 x 88 ; ; Jump to (current PCL) + W Return with 0 x 00 in W Return with 0 x 23 in W etc.

Software: Indirect Addressing 00 h 04 h INDF u Load indirect address into FSR

Software: Indirect Addressing 00 h 04 h INDF u Load indirect address into FSR Reading/Writing to INDF acts on address stored in FSR u Example code to clear 0 x 20 - 7 F: u FSR loop 7 Fh Register File movlw movwf 0 x 20 FSR clrf incf btfss goto INDF FSR, 7 loop

Software: Banking RAM in the PICs is banked, especially special function registers. Use the

Software: Banking RAM in the PICs is banked, especially special function registers. Use the bank select commands to choose the bank. Either: bsf bcf STATUS, RP 0 STATUS, RPO Or use the assembler directive: Banksel <registername>

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

Software: Real Code! u Note: Each PIC has a predefined “. h” file which contains labels for each special file register (so you don’t have to!) u. A working program requires initialization code and option codes set in the program. See. ASM examples for initialization code u Please see Example. asm

Software Tips u Destination bit determines W or F for result u Look at

Software Tips u Destination bit determines W or F for result u Look at data movement and re-structure Example: A + B -> A MOVF ADDWF MOVWF A, W B, W A 3 instructions MOVF ADDWF B, W A, F 2 instructions

Software Tips WASTE MACRO NUMBER NOLIST ; Doesn’t expand this into listing LOCAL i

Software Tips WASTE MACRO NUMBER NOLIST ; Doesn’t expand this into listing LOCAL i ; Use local variable in Macro i set NUMBER ; Initialize the local counter WHILE i IF i > 1 goto $+1 i -= 2 ELSE nop i -= 1 ENDIF ENDW LIST ENDM ; Count of cycles to waste ; twice the waste, half the space ; Waste 1 Cycle

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

Software Tips ; Define variable names (without bothering with ; absolute addresses) CBLOCK 0 x 20 ; Start of data space. Var 1: 1 Var 2: 1 Var 16: 2 ACCL: 1 ACCH: 1 ENDC ; You can always call one thing many names, Grasshopper. ACCA equ ACCL ACCB equ ACCH ; alias ACCL ; alias ACCH

Software Tips ; This routine multiplies W by tmp (8 x 8). Uses ;

Software Tips ; This routine multiplies W by tmp (8 x 8). Uses ; temporary register Cnt. Dwn and stores 16 bit result ; in ACCH: ACCL. Mult MSum clrf bsf bcf rrf btfsc addwf rrf decfsz goto return ACCL ACCH Cnt. Down, 3 STATUS, C tmp, F STATUS, C ACCH, F ACCL, F Cnt. Down, F MSum ; Cnt. Down -> 8

Software Tips ; Save the current state on interrupt ; (NOTE: _W must map

Software Tips ; Save the current state on interrupt ; (NOTE: _W must map in both Banks - e. g. 7 F/FF) Interrupt movwf swapf bcf movwf. . . swapf movwf swapf retfie _W STATUS, W ; Move STATUS w/o changing it STATUS, RP 0 ; Switch to page 0 _STATUS ; Save old status (swapped) _STATUS, W ; Load old STATUS (& unswap) STATUS ; also restores old page# _W, F _W, W

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

Software: Pitfalls! u Bit tests will screw you up! Be careful! u For example: movf Register, W btfsc STATUS, Z goto NZero. . Nzero. (WRONG!)

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

Software: Pitfalls! u For all 12 and 14 bit cores, the working register, “W”, can NOT be addressed. So: swapf W, W will not work! u However, in the 17 CXXX series you CAN address the working register (called WREG).

Software: Pitfalls! u Peripheral Pin sharing Many times pins share functions. E. g. ,

Software: Pitfalls! u 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. u Peripheral Resource Sharing Some resources require using the same resource. For example, some of the PWM modules use TMR 2, which may also be used in the USART module.

Software: Pitfalls! u Read-Modify-Write problems CAN BE SERIOUS! (Uplink) u BCF/BSF PORTn Does the

Software: Pitfalls! u Read-Modify-Write problems CAN BE SERIOUS! (Uplink) u BCF/BSF PORTn Does the following: – – u 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, 0 movf _PORTA, W movwf PORTA

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

Software: Pitfalls! Make sure you always set the correct BANK bits! bsf clrf bcf STATUS, RP 0 TRISA STATUS, RPO is correct; but if you just do clrf TRISA you’ll actually execute: clrf PORTA

Software: Conclusion u Choose: Project then Build All. MPASM will attempt to compile your

Software: Conclusion u Choose: Project then Build All. MPASM will attempt to compile your code. u Get ready to fix a billion errors! These errors are usually syntactical in nature… you won’t catch the logical errors until you simulate!

PART III Simulating your code

PART III Simulating your code

Simulation Introduction u Ok, my code assembles! Let’s go! – Not so fast. Sure

Simulation Introduction u 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. u 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. u See MPSIM Example on Example. asm

Simulation Issues u Once your simulation seems to work, you’ve probably caught most of

Simulation Issues u Once your simulation seems to work, you’ve probably caught most of the major logical errors in your program. u Now you’re ready to program your PIC and find out how it behaves in the Real World™.

PART IV Programming PICs

PART IV Programming PICs

Programming PICs: PICSTART+ u Low cost ($149. 00) programmer with ZIF socket u Programs

Programming PICs: PICSTART+ u Low cost ($149. 00) programmer with ZIF socket u Programs all PICs except > 40 pin packages (which require an adapter) u Cheaper u Con: available but only for 16 F 84 series Must take chip out of circuit to program

Programming PICs: ICSP u In-Circuit Serial Programmability – Good for commercial design – Available

Programming PICs: ICSP u In-Circuit Serial Programmability – Good for commercial design – Available for most PICS – Assemble boards, then program them MCLR/VPP VDD VSS I/O 1 Clock I/O 2 u u u Vpp = +13 V Vdd = Operating Vdd Cons: Requires dual use on ICSP pins VPP PIC 16 CXX PIC 14 CXXX PIC 12 CXXX Data I/O

Programming PICs: ICD u Low cost ($159. 00) u Programs u Is only 16

Programming PICs: ICD u Low cost ($159. 00) u Programs u Is only 16 F 87 xx PICs in ICP mode also an in-circuit debugger (!)

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

Programming PICs: Erasing u 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.

Programming PICs: Tricks u NOP reprogramming Trick – Erased EPROM is all ‘ 1’s

Programming PICs: Tricks u NOP reprogramming Trick – Erased EPROM is all ‘ 1’s - ie, 0 x 3 FF (12 bit 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 0 x 3 FF “spaces” to add GOTOs if you think you’ll be reprogramming (which you always will!)

Programming PICs: Pitfalls u THERE IS ONLY ONE DOWNLOAD BUFFER. – If you assemble

Programming PICs: Pitfalls u 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!

PART V The Real World™ and your PIC: Building and debugging

PART V The Real World™ and your PIC: Building and debugging

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

The Minimum PIC System u You need some kind of a clock: – Internal 4 MHz clock on some PICs, external RC, external crystal, or external clock oscillator. u And you need some power: – Most PICs can operate from 2. 5 V - 5. 5 V (see Data Sheets) – New 16 HVXX can directly use up to 14 V (Great for direct 9 V or 12 V battery hook ups) u And that’s it!

Infiltrating the Epoxy Curtain u Biggest drawback to PICs is the lack of easy

Infiltrating the Epoxy Curtain u Biggest drawback to PICs is the lack of easy incircuit 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. u You need debugging tools!

Debugging Tools: ICD u The 16 F 87 x ICD lets you debug your

Debugging Tools: ICD u The 16 F 87 x 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 16 F 87 x

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

Debugging Tools: Emulators u 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

Debugging Tools: Wetware u Solution: Wetware debugging tools – Status Pins » LED or

Debugging Tools: Wetware u 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

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

Debugging Tools: Do This u 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. u Post on the web! Become famous overnight!

PART VI Things that make you go “cool!”

PART VI Things that make you go “cool!”

Cool Ideas 6 LEDs off only three pins! A I/O PICmicro B I/O C

Cool Ideas 6 LEDs off only three pins! A I/O PICmicro B I/O C I/O 1 5 6 2 3 4 I/O A B C LEDs 1 2 3 4 5 6 0 0 1 Z Z 0 1 0 0 0 1 1 0 0 0 1 Z Z 0 1 1 0 Z Z 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

Cool Ideas The following macro swaps the contents of W and REG without using

Cool Ideas The following macro swaps the contents of W and REG without using a second register XCHGWF MACRO XORWF REG, F XORWF REG, W XORWF REG, F ENDM Needs: 0 TEMP Registers 3 Instructions 3 Tcy REG

Cool Things u Application Notes - www. microchip. com – Almost everything you could

Cool Things u Application Notes - www. microchip. com – Almost everything you could imagine » RTOS for the 17 CXXX family » DSP for the 16 CXX 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! u PIC Books – PIC’n (? ) – Introduction to PICs (Predko)

Cool Things u Some examples of PIC applications – – 12 C 508 web

Cool Things u Some examples of PIC applications – – 12 C 508 web server 17 C 756 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)

That’s it! Go PIC the world. Thanks. Questions/Comments? Improvements on this Seminar?

That’s it! Go PIC the world. Thanks. Questions/Comments? Improvements on this Seminar?