PSU IEEE Workshop On PIC Microcontrollers: Andrew and Tim January 16, 2000
PSU IEEE Workshop On PIC Microcontrollers: Andrew and Tim January 16, 2000
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
Memory
CPU 8
(Program
& Data)
PIC Architecture: Background
PICs use the Harvard Architecture
Memory Memory
(Data) 8
CPU 12 (Program)
14
16
PIC Architecture: Background
Traditionally, 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:
1100XX 01010101
PIC16CXXX: MOVLW 0x55
1 word, 1 cycle
PIC Architecture: Convergence
Many Microcontrollers and DSP chips are “converging”
12C50x 4MHz
12C67x 10MHz
16Cxxx 20MHz
17C4x / 17C7xxx 33MHz
18Cxxx 40MHz
The PIC Family: Program Memory
PIC program space is different for each chip.
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)
The PIC Family: Data Memory
PICs use general purpose “file registers” for RAM
(each register is 8bits for all PICs)
◆ 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
controlling peripherals and PIC behaviors.
◆ 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.)
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
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
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)
Selecting your PIC
◆ See Microchip Line card for the entire list of PICs :
http://www.microchip.com/10/Lit/rLit/00148d1/index.htm
http://www.microchip.com/
See handout:
RAM or “data
memory”. Variables
are stored here.
Software: Instruction Examples
movlw 0xFF
Move (“mov”) the number (“l” for “literal”) 0xFF - that’s 256
in decimal- into the working register (“w”).
Special Purpose
Registers
0xFF
W “Register”
General Purpose
Registers
Software: Instruction Examples
movwf PORTA
Move (“mov”) the working register (“w”) into the file register
(“f”) named PORTA.
Special Purpose
Registers
Value in W PORTA
W “Register”
General Purpose
Registers
Software: Instruction Examples
movf PORTA, W
Move (“mov”) the the value of the file register (“f”) named
PORTA into the working register (“w”) .
Special Purpose
Registers
Value in PORTA PORTA
W “Register”
General Purpose
Registers
Software: Assembly Format
◆ First column: Labels
◆ Second column: opcodes and assembler directives
◆ Third Columns & more: operands
◆ For example,
movlw 0xFF
movwf 0x06
movlw 0x20
movwf FSR
Either:
Banksel <registername>
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!)
Example: A + B -> A
3 instructions 2 instructions
Software Tips
WASTE MACRO NUMBER
ENDM
Software Tips
; Define variable names (without bothering with
; absolute addresses)
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
Software: Pitfalls!
◆ Bit tests will screw you up! Be careful!
◆ For example:
movf Register, W
btfsc STATUS, Z
goto NZero
Zero .
.
Nzero .
(WRONG!)
Software: Pitfalls!
◆ For all 12 and 14bit cores, the working register, “W”, can
NOT be addressed. So:
swapf W, W
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.
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.
Software: Pitfalls!
◆ Read-Modify-Write problems CAN BE SERIOUS! (Uplink)
clrf TRISA
clrf PORTA
Software: Conclusion
◆ Choose: Project then Build All. MPASM
will attempt to compile your code.
– 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
◆ 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.
0 0 0 0 0 0 0 0 0
0 1 Z 1 0 0 0 0 0
A I/O 1 0 Z 0 1 0 0 0 0
PICmicro Z 0 1 0 0 1 0 0 0
5 Z 1 0 0 0 0 1 0 0
1 2 0 Z 1 0 0 0 0 1 0
B I/O 6
1 Z 0 0 0 0 0 0 1
0 0 1 0 0 1 0 1 0
0 1 0 1 0 1 0 0 0
C I/O 3 4 0 1 1 1 0 0 0 1 0
1 0 0 0 1 0 0 0 1
1 0 1 0 1 1 0 0 0
1 1 0 0 0 0 1 0 1
1 1 1 0 0 0 0 0 0
Cool Ideas
The following macro swaps the contents of W and REG
without using a second register
◆ PIC Books
– PIC’n (?)
– Introduction to PICs (Predko)
Cool Things
◆ Some examples of PIC applications