Está en la página 1de 19

Electronics Design Lab

TUTORIAL

PIC Microcontrollers
Francesco Tenore
2/10/2006
The Microchip ® PIC ucontrollers
• http://www.microchip.com
• Characteristics
– types; speeds; I/O pins; Analog to Digital
Converters; Capture/Compare modules
• Programming (MPLab)
• Instruction set
• Implementations and Examples
Characteristics
• RISC CPUs
– 8-bit
– 16-bit
• Number of I/O pins: 4-70
• Memory types and sizes:
– Flash; OTP; ROM
– 0.5k – 256k
Speeds
• All PICs require oscillators to execute
instructions:
– Internal* (low speeds, up to 8 MHz)
– External (high speeds, up to 40 MHz)
• Instructions are executed at least at ¼
oscillator speed (4 clocks/instruction)

(*Note: not all PICs have internal oscillators)


A/D converters and C/C modules
• All PICs have between 0 and 16 A/D
converters with 8/10-bit resolution
• 8-16 bit Timers/Counters
• Comparator Modules (0-2)
Example: PIC16F877A
5/6 Programming pins
8 A/D channels
2 Oscillator Inputs
2 RS-232 inputs
33 I/O ports
Programming – MPLab and
Assembly
MPLab
• Download @ http://microchip.com
• Assembly compiler for programming
PICs
• Based on specific PIC instruction set
• To upload the program:
1. Compile: ProjectBuild All (Ctrl+F10)
2. Erase the device: Programmer Erase
Flash Device
3. Program: Programmer Program
Instruction Set
• 35 single word instructions
– Byte oriented file register operations
– Bit oriented file register operations
– Literal and control operations
Example 1

Using the PIC12F683 as a 2-state


switch
Giving memory to a pushbutton
Giving memory to a pushbutton
• In this example we:
– Store the state of the LED and
• Turn off if on and
• Turn on if off
Example 2

Using the PICs A/D converters for


Pulse Width Modulation
Pulse Width Modulation (PWM)
• 1-2 ms pulse used to control servomotors
• In this example, we:
– ACQUIRE an analog 0-5V signal
– CONVERT it into a 1-2 ms pulse that depends
on the analog voltage and output the result on
an output pin
– REPEAT
Steps for Analog-to-Digital
Conversion
1. Configure the A/D module: 4. Start conversion:
• Configure analog pins/voltage • Set GO/DONE bit (ADCON0)
reference and digital I/O 5. Wait for A/D conversion to complete, by
(ADCON1) either:
• Select A/D input channel (ADCON0) • Polling for the GO/DONE bit to be cleared
• Select A/D conversion clock (with interrupts enabled); OR
(ADCON0) • Waiting for the A/D interrupt
• Turn on A/D module (ADCON0) 6. Read A/D result register pair
2. Configure A/D interrupt (if desired): (ADRESH:ADRESL), clear bit ADIF if
• Clear ADIF bit required.
• Set ADIE bit 7. For the next conversion, go to step 1 or
• Set PEIE bit step 2, as required. The A/D conversion
• Set GIE bit time per bit is defined as TAD. A minimum
3. Wait the required acquisition time. wait of 2TAD is required before the next
acquisition starts.
ADC on PIC 16F877
• Configuration:
– ADCON0, ADCON1
• Result
– ADRESH, ADRESL

(See handout)
Samples
• The microchip website:
www.microchip.com
offers samples of PICs (maximum 5)
that are sent to you for free.

Please e-mail me at fra@jhu.edu if you have


any questions.
; EDL_test1.asm: blinks an LED when pushbutton is pressed.

#INCLUDE "P12F683.INC" LOOP:


BTFSS GPIO,5 ; skip if button NOT pressed
GOTO CLEAR_ROUTINE ; otherwise
ORG 0x000000 GOTO LOOP
bsf STATUS,RP0 ;Bank 1 CLEAR_ROUTINE:
movlw b'01110010' ; 0x00 INCF COUNTER,1 ; increment the counter
movwf OSCCON ; 0x01 MOVLW 0x01 ; w=1
goto START ; 0x02 ANDWF COUNTER,0 ; counter AND w =>
ORG 0x000020 ; w = 0x01
MOVWF TEMP ; TEMP = 0x01
START: BTFSC TEMP,0 ; skip if TEMP is 0
BCF GPIO,0 ; otherwise, clear GPIO<0>
COUNTER EQU 0x21 BTFSS TEMP,0
TEMP EQU 0x22 BSF GPIO,0 ; if it's 1, then set it.

BCF STATUS,RP0 ;Bank 0 LOOP2:


CLRF GPIO ;Init GPIO BTFSC GPIO,5
MOVLW 07h ;Set GP<2:0> to GOTO LOOP
MOVWF CMCON0 ;digital I/O GOTO LOOP2
BSF STATUS,RP0 ;Bank 1
CLRF ANSEL ;digital I/O END
MOVLW 28h ;Set GP<5> and GP<3> as inputs
MOVWF TRISIO ;and set GP<4,2:0> as outputs
BCF STATUS,RP0 ;Bank 0
BSF GPIO,0

También podría gustarte