Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Rodolfo Rodriguez
Kevin Zhang
MJ Gellada
1
Overview
Microcontroller and Architecture Basics
Variants among families
ISA
Programming overview
Hardware interface/Developmental boards
2
Description
PIC Peripheral Interface Controller
Made by Microchip Technology
Most popular by industry developers and
hobbyists
Low cost (cents to dollars)
Availability
Extensive application notes
Serial programming
3
v
http://www.microchip.com/stellent/images/mchpsiteimages/en537986.jpg
4
8-bit Core Architecture
Harvard (separate code
and data space)
RISC
One accumulator
W Register
Small addressable data
space (256 bytes)
Banking
http://www.microchip.com/_images/BaselineArch_large.jpg
RAM
PC, special purpose registers
5
Advantages & Limitations to 8-bit Architecture
Small ISA to learn
Built in oscillator with selectable speeds
Development made easy
Inexpensive
Device variants
Wide range of interfaces (I2C, SPI, etc.)
One accumulator
Bank switching
Hardware call stack unaddressable (no multi-tasking)
6
16-bit and 32-bit Architecture
More working registers
No bank switching
Assignable interrupt vector table
More flash memory
Cache (32-bit architecture)
7
Application Notes
Very extensive
16758 items
Description of
documentation
PDF documentation
Source code
http://www.microchip.com/TechDoc.aspx?type=appnotes
http://www.microchip.com/TechDoc.aspx?type=appnotes
8
Baseline Mid-Range Enhanced Mid-Range PIC18 Architecture
Architecture Architecture Architecture
8-bit architecture
INSTRUCTION DESCRIPTION OPERATION
PIC16 ISA:
Data Transfer Instructions
35 Instructions, 14-bit
SWAPF f,d Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4)
Arithmetic-logic Instructions
ANDWF f,d
Logical AND with W with f
W AND f -> d
SA
IORLW k Logical OR with W with constant W OR k -> W
INSTRUCTION DESCRIPTION OPERATION XORWF f,d Logical exclusive OR with W with constant W XOR k -> W
6I
INCF f,d Increment f by 1 f+1 -> f
C1
RRF f,d Rotate right f through CARRY bit
PI
BTFSC f,b Skip if f(b) = 0
clear.
(7:4)
INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0
W: Working register(Accumulator)
GOTO k Go to address k -> PC
Arithmetic-logic Instructions
Add W and
ADDLW k Add W and constant W+k -> W
ADDWF f,d Add W and f W+f -> d ANDLW k Logical AND with W with constant W AND k -> W
INCF f,d Increment f by 1 f+1 -> f ANDWF f,d Logical AND with W with f W AND f -> d
SA
IORLW k Logical OR with W with constant W OR k -> W
DECF f,d Decrement f by 1 f-1 -> f XORWF f,d Logical exclusive OR with W with constant W XOR k -> W
6I
Rotate left f through INCF f,d Increment f by 1 f+1 -> f
RLF f,d
CARRY bit
DECF f,d Decrement f by 1 f-1 -> f
C1
RRF f,d Rotate right f through CARRY bit
RRF f,d
through CARRY bit
Bit-oriented Instructions
BCF f,b Clear bit b in f 0 -> f(b)
BSF f,b Clear bit b in f 1 -> f(b)
Program Control Instructions
PI
BTFSC f,b Skip if f(b) = 0
SUBLW, SUBWF INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0
CALL k
Go to address
Call subroutine
k -> PC
RETLW k
Return from subroutine
Other instructions
NOP
Return from interrupt
No operation
TOS -> PC, 1 -> GIE
MOVWF f
MOVF f,d
Move constant to W
Move W to f
Move f to d
k -> w
W -> f
f -> d
CLRW Clear W 0 -> W
CLRF f Clear f 0 -> f
Arithmetic-logic Instructions
Test bit b of f. Skip ANDWF f,d Logical AND with W with f W AND f -> d
BTFSS f,b the following Skip if f(b) = 1 ANDWF f,d Logical AND with W with f W AND f -> d
SA
instruction if set.
IORLW k Logical OR with W with constant W OR k -> W
Decrement f. Skip XORWF f,d Logical exclusive OR with W with constant W XOR k -> W
DECFSZ f,d the following f-1 -> d skip if Z = 1 XORLW k Logical exclusive OR with W with f W XOR f -> d
6I
instruction if clear. INCF f,d Increment f by 1 f+1 -> f
C1
RRF f,d Rotate right f through CARRY bit
Bit-oriented Instructions
Complement f f -> d
PI
BTFSC f,b Skip if f(b) = 0
clear.
Return from
set.
Return with
RETLW k k -> W, TOS -> PC INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0
constant in W
GOTO k Go to address k -> PC
RETLW k
Return from subroutine
Other instructions
Arithmetic-logic Instructions
Bit-oriented Instructions
SUBWF f,d Subtract W from f f-W -> d
SA
BSF f,b Set bit b in f 1 -> f(b) IORLW k Logical OR with W with constant W OR k -> W
6I
INCF f,d Increment f by 1 f+1 -> f
Other instructions
C1
RRF f,d Rotate right f through CARRY bit
Clear watchdog 0 -> WDT, 1 -> Test bit b of f. Skip the following instruction if
PI
BTFSC f,b Skip if f(b) = 0
clear.
CLRWDT
timer TO, 1 -> PD BTFSS f,b
Test bit b of f. Skip the following instruction if
Skip if f(b) = 1
set.
INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0
Other instructions
I2C
UART
http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-worl
d-of-microcontrollers/
14
Programming a PIC
Microchip provides the free MPLAB:
Assembler and linker
Application development
Hardware emulation
Debugging
C or assembly compatible
http://www.microchip.com/pagehandler/e
Compiler n-us/family/mplabx/
15
Programming Tools
Basic based environments
are available, but dont
offer the functionality of C
Third party IDEs might be
preferred due to an
enhanced software library
or debugging tools
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&d
DocName=en019469&part=SW007002
16
Device Programmer
Need device to store machine code
into PICs memory (EEPROM or Flash)
Can be external device, but ICSP is
easier:
Dont have to remove chip from its circuit
Provides interface between computer
(USB) and PIC
Specific to circuit (due to interconnect
scheme and surrounding circuit)
Communication protocol requires 5
signals
http://en.wikipedia.org/wiki/PIC_microcontroller
17
Device Programmer
Five Signals:
Vpp (programming voltage)
Vdd (power)
Vss (ground)
IC SPCLK (clock)
IC SPDAT (data)
http://en.wikipedia.org/wiki/PIC_microcontroller
18
Necessary Connections (PIC16F877A)
PIC can be bread-boarded, with the following
important connections:
Power
Ground
Reset signal
Crystal (oscillator)
http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-world-of-microcontr
ollers/
19
LED example: code
//LED example program written for
//PIC programming tutorial. From //(http://
seniord.ece.iastate.edu/dec0604/index_files/tutorialDec0604.pdf)
//standard include files
#include <stdlib.h>
#include <pic.h>
#include delay.h
//main function
void main()
{
PORTA = 0x00; //set RA0-RA5 low
TRISA = 0x00; //set PORTA to output
//superloop
while(1)
{
PORTA = ~PORTA;
DelayMs(250);
}
}
20
Development Boards
Prepackaged boards come with a multitude of
peripherals for development and debugging:
Programmer
User I/O: buttons, port pinouts, LEDs
Displays: LCD and seven segment
Power
Serial connection interface
21
Easy PIC v7 from MikroElectronika
USB Port
Programmer
PIC
Power
Supply
LEDs and
Port Pinouts buttons
http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-worl
d-of-microcontrollers/
22
Additional Peripherals
Touch Screen
ADC converter
23
Questions?
24
References
http://www.microchip.com/
http://en.wikipedia.org/wiki/PIC_microcontroller
http://www.mikroe.com/eng/chapters/view/10/chapter-9-instruct
ion-set/
http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-cha
pter-1-world-of-microcontrollers/
http://www.slideshare.net/element14/microchips-16bit-and-32b
it-pic-mcus-7267006
http://www.ladyada.net/library/picvsavr.html
http
://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter
-1-world-of-microcontrollers/
http://www.slideshare.net/element14/microchips-16bit-and-32b
it-pic-mcus-7267006 25