PowerPoint-presentatie

advertisement
2TPRJ5 – PIC assembler
Wouter van Ooijen
Mail: [email protected]
Sheets en verdere info:
www.voti.nl/hvu/2TPRJ5
Onderwerp:
Materiaal:
PIC assembler programmeren
- PIC USB target board (€ 60 )
- handouts etc (worden uitgedeeld)
- (thuis) PC met XP of Vista
- Memory stick
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1-1
2TPRJ5 – PIC assembler
1-2
Wat er van u verwacht wordt
• U bent (op tijd!) aanwezig
•
•
•
•
•
•
•
Theorie in B02.12 (vanaf 9;50), praktijk in C01.14
De eerste weken iedere week een of meer opgave(n)
De opgaven moeten voor de volgende les klaar zijn
U maak de opgave(n) zelf
De opgaven worden beoordeeld op werking, code en begrip
De laatste weken een individueel projectje
Het resultaat van het projectje is uw cijfer, mits alle voorgaande
opgaven voldoende, en op tijd (anders minpunten!)
• Er is dus geen tentamen, geen projectverslag, etc.
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
Onderwerpen voor vandaag
• wat is een microcontroller
(embedded, real-time)
• wat is assembler
• hoe werk je met een microcontroller
• wat is een PIC
• de PIC16F887
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1-3
2TPRJ5 – PIC assembler
1-4
Wat is een microcontroller?
meestal:
 ‘computer-achtig’ ding (computertje)
 combinatie van processor, ROM, RAM, I/O, ...
 compact
 goedkoop
 zuinig
 geschikt voor real-time
 geschikt voor embedded gebruik
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
ultra-goedkoop
chinees, inclusief de documentatie 
lastig verkrijgbaar (in kleinere aantallen)
general-purpose
PIC, Atmel, 8051, 68HC, ...
high-end
ARM, PowerPC
special purpose:
DSP, ethernet, USB, analoog, LCD, ...
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1-5
2TPRJ5 – PIC assembler
 ergens in,
deel van een geheel,
(diep) verstopt,
dienstbaar aan waar ‘t in zit
 vaste functie,
dus niet vrij programmeerbaar
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1-6
2TPRJ5 – PIC assembler
1-7
Wat is assembler?
• De meeste directe manier om een CPU te programmeren
• Veel werk voor de programmeur
• Een stuk beter dan binair, hex of met schakelaars
programmeren ...
• Regel-georienteerd
• (meestal) 1 regel  1 CPU instructie
• maar ook: het programma dat een in assembler geschreven
programma vertaalt (de ‘compiler’)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1-8
een assembler fragment
; delay
movlw
H'00'
delay_loop:
addlw H'01'
skpz
goto delay_loop
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
bijna altijd cross-development:
 jij werkt op een PC
 daar draait je editor, compiler,
assembler, linker
 loader of programmer om te laden
 burn and crash
 luuxer: cross-debugger, ICE
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1-9
2TPRJ5 – PIC assembler
Wat is een PIC?
• ooit: Peripheral Interface Controller
voor de CP1600 minicomputer
• nu: merknaam voor een zeer brede reeks 8-bit
microcontrollers van fabrikant Microchip
• Harvard-architectuur
• concurrenten: AVR (Atmel), 68HC, MSP430
(Motorola), 8051, ARM (diverse fabrikanten),
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 10
2TPRJ5 – PIC assembler
Soorten PICs
Processor (core)
– 12-bit (max 2k code, 2-level stack, geen interrupts)
– 14-bit  (max 8k code, 8-level stack)
– 16-bit
– 24-bit
volkomen andere processors
– 32-bit
Behuizing
– 6 .. 80 pinnen  40
– DIP, SMD, PLCC  DIP
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 11
2TPRJ5 – PIC assembler
Soorten PICs
Soort geheugen
– Mask ROM
– OTP EPROM
– Window EPROM
– Flash 
– Reduced Flash
Prijzen (@ 1)
– $1.00 .. $30
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 12
2TPRJ5 – PIC assembler
1 - 13
PIC extra’s
•
•
•
•
•
•
•
•
•
•
A/D (8, 10 of 12 bits)
UART
Data EEPROM
Timers
Interne oscillator
PWM uitgangen
USB slave interface
Ethernet interface
comparator
etc
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
Hoe schrijf je een PIC programma
•
•
•
•
•
•
•
•
•
HEX
Assembler 
C 
C++ (beperkt)
Jal
Pascal
Ladder
Basic
…
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 14
2TPRJ5 – PIC assembler
Hoe krijg je het programma in de PIC
•
•
•
•
•
In de fabriek, eenmalig (mask ROM)
Eraser / ex-circuit programmer (EPROM)
Ex-circuit programmer (FLASH)
In-circuit programmer 
(In-circuit debugger)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 15
2TPRJ5 – PIC assembler
1 - 16
PIC16F887
•
•
•
•
•
•
40-pins chip, max. 35 beschikbaar als I/O
met kristal maximaal 20 MHz (interne osc 8 MHz)
8k code (14-bit instructies)
368 general purpose registers (RAM)
256 bytes EEPROM
Diverse peripherals (bv A/D, timers, UART, PWM)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
De PIC16F887
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 17
2TPRJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 18
2TPRJ5 – PIC assembler
PIC programming model
• W (accumulator) – het enige echte ‘register’
• Geheugen, adressen 0 .. 0x1FF (512), waarin oa:
– File registers (memory, RAM)
– Flags – die zitten dus in het ‘geheugen’!
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 19
2TPRJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 20
2TPRJ5 – PIC assembler
File Registers (SFR, GPR)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 21
2TPRJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 22
2TPRJ5 – PIC assembler
Literal to W, W to file, file to W
a represents an address, [ a ] the (byte) content of that address
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 23
2TPRJ5 – PIC assembler
1 - 24
Wat doet dit code fragment?
MOVF
MOVWF
MOVLW
MOVWF
H’20’, W
H’21’
H’30’
H’31’
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
W
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 25
2TPRJ5 – PIC assembler
1 - 26
Arithmetic instructions - diadic
a represents an address, [ a ] the (byte) content of that address, n a (byte) literal
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
Arithmetic
instructions
- diadic
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 27
2TPRJ5 – PIC assembler
1 - 28
Wat doet dit code fragment?
MOVFW
ADDWF
MOVWF
H’20’
H’21’, w
H’21’
1. MOVFW is een synoniem voor MOVF …,W
2. Hoe kan dit korter?
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
Arithmetic instructions - monadic
a represents an address, [ a ] the (byte) content of that address
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 29
2TPRJ5 – PIC assembler
Arithmetic
instructions
- monadic
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 30
2TPRJ5 – PIC assembler
1 - 31
Wat doet dit code fragment?
RLF
MOVFW
RRF
H’30’, w
H’31’
H’32’, f
Het maakt niet uit of je hoofdletters of kleine letters
gebruikt, maar CaMelCaSe werkt niet.
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 32
Bit set and clear
a represents an address, [ a ] the (byte) content of that address, n a (byte) literal,
b a 3-bit literal (a bit number, 0..7), X : b represents bit b of X
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
Bit set and clear
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 33
2TPRJ5 – PIC assembler
1 - 34
Wat doet dit code fragment?
BSF
RRF
H’03’, 0
H’32’, f
Zoek in de INSTRUCTION SET SUMMARY op hoe
de RRF instrcutie precies werkt.
Wat gebeurt er als je de BSF instructie weg laat?
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
goto
call
return
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 35
2TPRJ5 – PIC assembler
In welke volgorde worden de MOVLW
instructies uitgevoerd?
Spagetti:
MOVLW 1
CALL spinazie
GOTO spinazie
MOVLW 2
Spinazie:
MOVLW 3
RETURN
MOVLW 4
Begin_Hier:
MOVLW 5
CALL spagetti
MOVLW 6
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 36
2TPRJ5 – PIC assembler
Conditional skips
a represents an address, [ a ] the (byte) content of that address
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 37
2TPRJ5 – PIC assembler
Conditional
skips
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 38
2TPRJ5 – PIC assembler
diversen
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1 - 39
2TPRJ5 – PIC assembler
1 - 40
Assembler build-in macro’s
SKPZ
SKPNZ
SKPC
SKPNC
= btfss 3, 2
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 41
In welke MOVLW instructies worden uitgevoerd?
movlw 5
movwf h’30’
btfsc h’30’, 0
movlw 12
btfss h’30’, 1
movlw 13
addlw 252
skpz
movlw 14
skpnc
movlw 15
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 42
wat doet dit stukje assembler code?
; delay
movlw H'00'
delay_loop
addlw H'01'
skpz
goto delay_loop
nb: skpz == btfss 3, 2
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 43
oefening 1
Op geheugenplaatsen H’20’ en H’21’
staan 2 getallen. Schrijf een programma
dat deze getallen optelt en het resultaat
in H’22’ plaatst
(3 instructies)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 44
oefening 2
Op geheugenplaatsen H’20’ en H’21’
staan 2 getallen. Zet het grootste van
deze twee getallen in geheugenplaats
H’22’.
(diverse oplossingen mogelijk, bv 8
instructies)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 45
Oefening 3
Op geheugenplaatsen H’20’ en H’21’ staan 2
getallen. Vermenigvuldig deze getallen en
plaats het resultaat in H’22’. Na afloop mogen
de getallen in H’20’ en H’21’ veranderd zijn.
(diverse oplossingen mogelijk, bv 10
instructies. Tip: vermenigvuldigen is herhaald
optellen, evt eerst uitschrijven in C of Java)
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2TPRJ5 – PIC assembler
1 - 46
Doen
neem de slides nog eens door
 noteer wat je niet duidelijk is !
 neem € 60 mee voor het bordje (gepast!)
 schrijf de programma’s voor de oefeningen
(mag op papier, of in notepad, etc )
dit moet voor volgende week af zijn!!
 let goed op het verschil tussen MOVLW en
MOVWF/MOVF/MOVFW.
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Download