PICkit basics

Download Report

Transcript PICkit basics

16.317
Microprocessor Systems Design I
Instructor: Dr. Michael Geiger
Spring 2014
Lecture 29:
PICkit introduction
Lecture outline

Announcements/reminders




Review


HW 5 due today
HW 6 to be posted; due date TBD
Working with PICkits—groups of 2 or 3
Sample programming sequences: delay, state machine
Today’s lecture: working with PICkit


Assembler directives
MPLAB IDE




4/13/2015
Working with existing projects
Simulator
In-circuit debugging
Sample programs in assembly and C
Microprocessors I: Lecture 29
2
Review: A Delay Subroutine
; ***********************************************************************************
; TenMs subroutine and its call inserts a delay of exactly ten milliseconds
; into the execution of code.
; It assumes a 4 MHz crystal clock. One instruction cycle = 4 * Tosc.
; TenMsH equ 13
; Initial value of TenMs Subroutine's counter
; TenMsL equ 250
; COUNTH and COUNTL are two variables
TenMs
nop
movlw
movwf
movlw
movwf
Ten_1
decfsz
goto
decfsz
goto
return
4/13/2015
TenMsH
COUNTH
TenMsL
COUNTL
COUNTL,F
Ten_1
COUNTH,F
Ten_1
; one cycle
; Initialize COUNT
COUNTH = TenMsH
COUNTL = TenMsL
COUNTL = COUNTL
-1
Yes
No
COUNTL == 0 ?
Yes
COUNTH = COUNTH - 1
Yes
; Inner loop
No
COUNTH == 0 ?
; Outer loop
Yes
return
Microprocessors I: Lecture 29
3
Review: Strategy to “Blink”


The LEDs are
toggled in sequence
- green, yellow, red,
green, yellow, red…
Let’s look at the
lower three bits of
PORTD
001=green,
010=yellow, 100=red
Read current PORTD
state
Green (001)?
Yes
Toggle Red LED
(010->100)
No
Yellow (010)?
No
Toggle Green LED
(100->001)
The next LED to be
toggled is determined
by the current LED.
001->010->100->001->…

4/13/2015
Yes
Toggle Yellow LED
(001->010)
return
Microprocessors I: Lecture 29
4
Coding “Blink” with Table Use
BlinkTable
movf
andlw
addwf
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
PORTD, W
B'00000111'
PCL,F
B'00000001'
B'00000011'
B'00000110'
B'00000010'
B'00000101'
B'00000100'
B'00000111'
B'00000110'
; Copy present state of LEDs into W
; and keep only LED bits
; Change PC with PCLATH and offset in W
; (000 -> 001) reinitialize to green
; (001 -> 010) green to yellow
; (010 -> 100) yellow to red
; (011 -> 001) reinitialize to green
; (100 -> 001) red to green
; (101 -> 001) reinitialize to green
; (110 -> 001) reinitialize to green
; (111 -> 001) reinitialize to green
In calling program
call
xorwf
4/13/2015
BlinkTable
PORTD, F
; get bits to change into W
; toggle them into PORTD
Microprocessors I: Lecture 29
5
PIC microcontroller programming





Done through MPLAB IDE
Allows for generation of projects in assembly
or in C
Options to generate initialization code
Simulator to allow code testing before
programming hardware
In-circuit debugger to view device state while
hardware is actually running
4/13/2015
Microprocessors I: Lecture 29
6
PIC assembler directives

banksel label



cblock/endc


Used to define a block of variables
org address



Changes BSR to bank containing label
Example: banksel TRISC
Indicates starting address for block of code that
follows
Address 0 in simple programs (initial PC value)
#include file

4/13/2015
Typically used to include processor-specific
definitions, macros
Microprocessors I: Lecture 29
7
Example 1: Light single LED (.asm)
Start:
banksel
bcf
banksel
clrf
bsf
goto
TRISC
TRISC,0
LATC
LATC
LATC,0
$
;select bank1
;make C0 an output
;select bank2
;initialize the
; LATCH by
; turning off
; everything
;turn on LED C0 (DS1)
;sit here forever!
end
4/13/2015
Microprocessors I: Lecture 29
8
Example 1 notes (.asm)

TRISC: controls state of Port C pins



LATC: used for writing data to Port C



TRISC bits = 1  corresponding pins are inputs
TRISC bits = 0  corresponding pins are outputs
Equivalent to writing to PORTC register
Convention: for input, read PORTC; for output,
write LATC
Infinite loop at end

4/13/2015
$ symbol—special label for current instruction
Microprocessors I: Lecture 29
9
Example 1: Light single LED (C)
void main(void) {
TRISCbits.TRISC0 = 0;
// Pin 0 =
// output
LATC = 0; //clear all pins to 0
LATCbits.LATC0 = 1; // turn ON LED
while(1) continue;
}
4/13/2015
Microprocessors I: Lecture 29
10
Example 1 notes (C)


Can access entire registers by name
To access individual bits, use form:
regnamebits.regname#

4/13/2015
Example: TRISCbits.TRISC0 = 0;
Microprocessors I: Lecture 29
11
Running program

Choose target device under
File  Project Properties


Either use PICkit3 or Simulator
Compilation


Clean and Build
Make and Program Device (PICkit3 only)



Separate options required for debug configuration
Click arrow on right of button to build/make for debug
Window  PIC Memory Views

4/13/2015
Allows you to view file registers and/or SFRs
Microprocessors I: Lecture 29
12
Example 2: Blink LED (.asm) (1 of 2)
cblock 0x70
Delay1
Delay2
endc
ORG 0
Start:
banksel
movlw
movwf
bcf
banksel
clrf
4/13/2015
;shared memory accessible from all banks
;Two registers for delay loop in shared mem
OSCCON
b'00111000'
OSCCON
TRISC,0
LATC
LATC
;bank1
;set cpu speed of 500KHz
;OSCCON configures
; internal clock
;Pin C0 = output for DS1
;bank2
;Turn off all of the LEDs
Microprocessors I: Lecture 29
13
Example 2: Blink LED (.asm) (2 of 2)
MainLoop:
bsf
LATC, 0
OndelayLoop:
decfsz
bra
Delay1,f
OndelayLoop
;turn on DS1
decfsz
Delay2,f
bra
OndelayLoop
bcf
OffDelayLoop:
decfsz
bra
decfsz
bra
bra
LATC,0
Delay1,f
OffDelayLoop
Delay2,f
OffDelayLoop
MainLoop
;Waste time.
;Inner loop takes 3 instructions
; per loop * 256 loops = 768 instructions
;The outer loop takes an additional 3
; instructions per loopp * 256 loops
;(768+3) * 256 = 197376 instructions /
; 125K instructions per second =
;
1.579 sec.
;Turn off LED C0
;same delay as above
;Do it again...
end
4/13/2015
Microprocessors I: Lecture 29
14
Example 2: Blink LED (C)
void main(void) {
unsigned int delay;
// 16 bit variable
OSCCON = 0b00111000;
//500KHz clock speed
TRISCbits.TRISC0 = 0;
//using pin as output
delay = 11250;
while (1) {
//each instruction is 8us (1/(500KHz/4))
while(delay-- != 0)continue;
LATCbits.LATC0 ^= 1;
delay = 11250;
//toggle LED
//reset delay counter
}
}
4/13/2015
Microprocessors I: Lecture 29
15
Final notes

Next time:


Continue PIC programming
Reminders:



4/13/2015
HW 5 due today
HW 6 to be posted; due date TBD
Working with PICkits—groups of 2 or 3
Microprocessors I: Lecture 29
16