Documente Academic
Documente Profesional
Documente Cultură
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
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
Memory
CPU
Memory (Data)
8
Memory
CPU
12 14 16
(Program)
MC68HC05:
LDAA 0x55
1000
1100
2 bytes, 2 cycles
01010101
1100XX 01010101
PIC16CXXX:
MOVLW 0x55
1 word, 1 cycle
12C50x (12bit) and 12C67x (14bit) 16C5X (12bit), 16Cxxx (14bit) 16C5X (12bit), 16Cxxx (14bit) 16Cxxx (14bit), 17C4x (16bit) 16Cxxx (14bit), 17C4x / 17Cxxx (16bit)
25 Bytes RAM 36 Bytes RAM 368 Bytes (plus 256 Bytes of nonvolatile EEPROM) 902 Bytes RAM
Dont forget, programs are stored in program space (not in data space), so low RAM values are OK.
Most pins have 25mA source/sink (directly drives LEDs) WARNING: Other peripherals SHARE pins!
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)
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)
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.)
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
Capture/Compare/PWM (CCP) 10bit PWM width within 8bit PWM period (frequency)
Enhanced 16bit cores have better bit widths
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
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)
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)
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)
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)
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
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)
ALWAYS have the data sheet for your PIC: http://www.microchip.com/ There are just too many details you have to know!
PART II
Writing PIC Code
Software: Introduction
In this course, well 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.
Lets 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).
W Register
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.
W Register
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 clrf bsf nop nop bcf goto PORTA TRISA PORTA,0 ; ; ; ; ; ; ; 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
Loop
PORTA,0 Loop
Software: Branches
All branches are Bit Tests All branches only skip one instruction
EqualFlag, 7 ; First, clear the flag PORTA, W ; Move PORTA -> W PORTB, W ; W - PORTB -> W STATUS, Z ; Check Z bit (see STATUS) EqualFlag, 7 ; Ports equal; set flag
STATUS Register
All file registers (RAM) are accessed by an address. This is called direct addressing. For example, movlw movwf 0xFF 0x06
loads W with FF, and then loads W into GPIO (address 0x06).
Thankfully, we can use labels instead of addresses: GPIO movwf equ GPIO 0x06
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
; Heres 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 retlw retlw retlw retlw PCL, W 0x00 0x23 0x33 0x88 ; ; ; ; Jump to (current PCL) + W Return with 0x00 in W Return with 0x23 in W etc.
04h
FSR
Load indirect address into FSR Reading/Writing to INDF acts on address stored in FSR Example code to clear 0x20 - 7F:
movlw movwf
loop clrf incf btfss goto
0x20 FSR
INDF FSR,F FSR,7 loop
7Fh
Register File
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, RP0 STATUS, RPO
Each PIC has a predefined .h file which contains labels for each special file register (so you dont have to!)
working program requires initialization code and option codes set in the program. See .ASM examples for initialization code
see Example.asm
Please
Software Tips
Destination
3 instructions
2 instructions
Software Tips
WASTE MACRO NUMBER
NOLIST ; Doesnt 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 0x20 ;Start of data space. Var1: 1 Var2: 1 Var16: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 (8x8). Uses ; temporary register CntDwn and stores 16bit result ; in ACCH:ACCL. Mult clrf clrf clrf bsf bcf rrf btfsc addwf rrf rrf decfsz goto return ACCL ACCH CntDown CntDown,3 STATUS,C tmp,F STATUS,C ACCH,F ACCH,F ACCL,F CntDown,F MSum
;CntDown -> 8
MSum
Software Tips
;Save the current state on interrupt ;(NOTE: _W must map in both Banks - e.g. 7F/FF) Interrupt movwf swapf bcf movwf . . . swapf movwf swapf swapf retfie _W STATUS,W ;Move STATUS w/o changing it STATUS,RP0 ;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!
Bit
(WRONG!)
Software: Pitfalls!
For all 12 and 14bit cores, the working register, W, can NOT be addressed. So:
swapf W, W
However, in the 17CXXX series you CAN address the working register (called WREG).
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 CANT 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.
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, 0 movf _PORTA, W movwf PORTA
Software: Pitfalls!
Make sure you always set the correct BANK bits!
bsf clrf bcf STATUS, RP0 TRISA STATUS, RPO
Software: Conclusion
Choose:
Project then Build All. MPASM will attempt to compile your code. ready to fix a billion errors!
Get
These errors are usually syntactical in nature you wont catch the logical errors until you simulate!
PART III
Simulating your code
Simulation Introduction
Ok, my code assembles! Lets go! Not so fast. Sure it assembles, but does it actually do what its 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
Simulation Issues
Once
your simulation seems to work, youve probably caught most of the major logical errors in your program.
youre ready to program your PIC and find out how it behaves in the Real World.
Now
PART IV
Programming PICs
Programs
all PICs except > 40pin packages (which require an adapter) available but only for 16F84 series
Cheaper
Con:
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
VPP
VDD
VSS Clock Data I/O
cost ($159.00)
only 16F87xx PICs in ICP mode
Programs Is
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 its erased using the Blank Check option in MPLAB. Not blank? Erase it again. Light wont ever damage the silicon. We think.
THERE IS ONLY ONE DOWNLOAD BUFFER. If you assemble your program, then Read another PIC, You will program that PICs Hex dump and not your program! ALWAYS reassemble your code after reading anything you dont want to write!
PART V
The Real World and your PIC: Building and debugging
You
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
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 cant afford it: > $1000
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
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.
PART VI
Things that make you go cool!
Cool Ideas
6 LEDs off only three pins!
I/O A B C 0 0 1 Z Z 0 1 0 0 0 1 1 1 1 0 1 0 0 1 Z Z 0 1 1 0 0 1 1 0 Z Z 1 0 1 0 1 0 1 0 1 0 1 LEDs 1 2 3 4 5 6 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0
A I/O
PICmicro 1 2 5 6
B I/O
C I/O
Cool Ideas
The following macro swaps the contents of W and REG without using a second register XCHGWF MACROREG XORWF REG,F XORWF REG,W XORWF REG,F ENDM Needs: 0 TEMP Registers 3 Instructions 3 Tcy
Cool Things
PIC Books
PICn (?) Introduction to PICs (Predko)
Cool Things