Les 2 - onderwerpen

36
2PROJ5 – PIC assembler Hogeschool Utrecht / Institute for Computer, Communication and Media Technology 1 Les 2 - onderwerpen Herhaling instructieset Assembler ‘truukjes’ Uitwerking opgaven vorig les Allokeren van variabelen Gebruik van shadow registers Subroutines en macro’s Gebruik van MPLAB Simuleren met MPLAB Opgaven: delay W ms, kwadraat

description

Les 2 - onderwerpen. Herhaling instructieset Assembler ‘truukjes’ Uitwerking opgaven vorig les Allokeren van variabelen Gebruik van shadow registers Subroutines en macro’s Gebruik van MPLAB Simuleren met MPLAB Opgaven: delay W ms, kwadraat. file + w => (same) file, of w. - PowerPoint PPT Presentation

Transcript of Les 2 - onderwerpen

Page 1: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

1

Les 2 - onderwerpen• Herhaling instructieset• Assembler ‘truukjes’ • Uitwerking opgaven vorig les• Allokeren van variabelen• Gebruik van shadow registers• Subroutines en macro’s• Gebruik van MPLAB• Simuleren met MPLAB• Opgaven: delay W ms, kwadraat

Page 2: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

2

file + w => (same) file, of w

Page 3: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

3

Instructies: bit set/clear, bit test

Page 4: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

4

- file ‘op’ literal => (same) file, or => w- Control- Miscalaneous

Page 5: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

5

Assembler “truukjes”• Ingebouwd:

SKPZ, SKPNZ, SKPC, SKPNCSETC, CLRC, SETZ, CLRZMOVFW

• Macro’s:#define W 0#define F 1Let op mogelijke fouten, wat doet:

RRC W, F

Page 6: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

6

oefening 1 : optellen; tel de variabelen H'20' en H'21' op,; stop de som in H'22'

movf H'20', waddwf H'21', wmovwf H'22'

sleep ; zet dit na je codeend ; zet dit aan het einde van je file

Page 7: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

7

oefening 2 : maximum ( C )// versie 1if( a > b ){ max = a;} else { max = b;}

// versie 2max = a;if( b > a ){ max = b;}

// versie 3max = ( a > b ) ? a : b;

Page 8: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

8

SUBWF instruction (1)

Page 9: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

9

SUBWF instruction (2)

Page 10: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

10

oefening 2 : maximum (versie 1a); bepaal het maximum van de variabelen H'20' en H'21'; stop dit maximum in H'22‘(10 instructies)

; vergelijkmovfw H'20'subwf H'21', w ; H’21’ – H’20’skpnc ; C resultaat is positief H’20’ is kleiner goto kleiner ; C neem H’21’goto groter

; als we hier komen was H'21' groterkleiner

movfw H'21'movwf H'22'goto klaar

groter; als we hier komen was H'20' dus grotermovfw H'20'movwf H'22'

klaar

if( a > b ){ max = a;} else { max = b;}

Page 11: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

11

oefening 2 : maximum (versie 1b); bepaal het maximum van de variabelen H'20' en H'21'; stop dit maximum in H'22‘(9 instructies)

; vergelijkmovfw H'20'subwf H'21', w ; H’21’ – H’20’skpnc ; C resultaat is positief H’20’ is kleiner goto kleiner ; C neem H’21’

; als we hier komen was H'20' dus grotermovfw H'20'movwf H'22'goto klaar

; als we hier komen was H'21' groterkleiner

movfw H'21'movwf H'22'

klaar

max = a;if( b > a ){ max = b;}

Page 12: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

12

oefening 2 : maximum (versie 2); dat kan ook wat korter (6 instructies)

; neem aan dat H'20' het maximum ismovf H'20', wmovwf H'22'

; vergelijk met H'21'; movfw H'20' is niet nodig, dat zit al in Wsubwf H'21', w

; dit beinvloedt de C flag niet!!!movf H'21', wskpnc movfw H'22'

max = ( a > b ) ? a : b;

Page 13: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

13

oefening 2 : maximum (versie 3); of zo (ook 6 instructies)

; vergelijkmovfw H'20'subwf H'21', w

; dit beinvloed de flags niet!!!movf H'20', wskpnc movf H'21', wmovwf H'22'

max = ( a > b ) ? a : b;

Page 14: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

14

PIC16F887 memory map

Page 15: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

15

Assembler : variabelen – doe het zelf

Absolute adressen:

Of met #define of EQU:

movfw H’20’movwf H’21

#define A H’20’B EQU H’21’

movfw Amovwf B

Page 16: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

16

Assembler : variabelen – laat ‘cblock’ het doen

cblock 0x20name_1, name_2 name_3, name_4

endc ...cblock

name_5 name_6 : 2

endc

Page 17: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

17

Assembler template (zie website)

list p=16f887, f=inhx32#include <P16F887.INC>

org 0cblock H’20’ endc

; hier komt uw code

sleepEND

Page 18: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

18

Een stukje van PIC16F887.INC ;----- Register Files-----------------------------

INDF EQU H'0000'TMR0 EQU H'0001'PCL EQU H'0002'STATUS EQU H'0003'FSR EQU H'0004'PORTA EQU H'0005'

PORTC EQU H'0007'

PCLATH EQU H'000A'INTCON EQU H'000B'PIR1 EQU H'000C'

Staat op C:/Program Files/MPLAB IDE/MChIP_Tools

Page 19: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

19

read-modify-write : IO pin

Page 20: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

20

read-modify-write (fout)clrf PORTAbsf PORTA, 0bsf PORTA, 1bsf PORTA, 2bsf PORTA, 3bsf PORTA, 4bsf PORTA, 5bsf PORTA, 6bsf PORTA, 7

Page 21: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

21

subroutine voorbeeld

waitaddlw 0skpz

returnaddlw 1goto wait......movlw D’200’call wait

Page 22: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

22

subroutine

• lijkt op een C/Java/C# functie maar veel primitiever• label waar je met een call instructie heen springt• daar een reeks instructies• een return (of retlw) instructie brengt je terug• er is een stack voor de return adressen• die stack is maar 8 niveau’s diep• volgorde van subroutines en main is niet belangrijk, maar• let wel op als je subroutines vooraan staan!

Page 23: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

23

wat is een macro

• naam voor een aantal regels text• wordt letterlijk ingevoegd

RRFW macromovwf temprrf temp, wendm

Page 24: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

24

wat is een macro

• Kan (textuele) parameters parameters hebben

double macro REGISTERbcf STATUS, Crlf REGISTER, fendm

Page 25: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

25

macro voorbeeld en gebruikFLUSH_ MACRO MACRO Shadow, Port

CBLOCKShadow

ENDCMOVFW ShadowMOVWF PortRETURNENDM

PORTA_FLUSH FLUSH_MACRO PORTA_SHADOW, PORTAPORTB_FLUSH FLUSH_MACRO PORTB_SHADOW, PORTBPORTC_FLUSH FLUSH_MACRO PORTC_SHADOW, PORTCPORTD_FLUSH FLUSH_MACRO PORTD_SHADOW, PORTDPORTE_FLUSH FLUSH_MACRO PORTE_SHADOW, PORTE

Page 26: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

26

Macro – listing (.lst)

Page 27: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

27

read-modify-write (goed)clrf PORTA_SHADOWcall PORTA_FLUSHbsf PORTA_SHADOW, 0call PORTA_FLUSHbsf PORTA_SHADOW, 1call PORTA_FLUSHbsf PORTA_SHADOW, 2call PORTA_FLUSH...

PORTA_FLUSHmovfw PORTA_SHADOWmovwf PORTAreturn

Page 28: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

28

Macro Subroutine

• marco heeft geen call/return (gebruikt de stack niet)

• subroutine aanroep is altijd 1 statement• macro ‘aanroep’ voegt de complete macro in (je

programma wordt al snel erg groot)• een macro kan assembly-time argumenten hebben

Page 29: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

29

MPLAB IDE

IDE : Integrated Development Environment• Project management• Editor• Assembler• Programmer/debugger interface(s)• Integration of third-party tools (compilers)

Page 30: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

30

Gebruik van MPLAB(Zie ook de MPLAB quick start guide op de Microchip website )• Start MPLAB• Controleer: Configure Select Device 16F887• Start een project: Project New kies een project naam, zet

project directory naar keuze lokaal, op je USB stick, of op (in directory in) je network drive (heel erg lange pad-namen kunnen problemen geven)

• Of open een bestaand project: Project Open kies een bestaand project

• Een nieuwe file creeren: File New; File Save As mag zelfde naam als project (als het de hoofdfile is, of als je maar 1 file gebruikt)

• Een assembler file toevoegen aan een project: Project Add Files to Project double click to add the file as source file

Page 31: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

31

Gebruik van de assembler• Check: edit properties editor tab zet

“line numbers’ aan• Edit je file

(saven is niet nodig maar wel verstandig)• Assembleren en linken: Project Build All• Herhalen tot de fouten en warnings eruit zijn!

Page 32: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

32

Gebruik van de simulator• Debugger select tool MPLAB SIM• Debugger reset processor reset (F6)• Debugger Clear Memory GPRs (let op!)• Debugger step into (F7)• View 4 File Registers• View 5 Special Function Registers

(Waarden die in de vorige stap zijn veranderd worden rood weergegeven.)

Page 33: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

33

Gebruik van de simulator• Stap nu een aantal keren tot je denkt dat je

programma-lus goed werkt (F6)• Double-click op de regel na een loop om een

breakpoint te zetten• Debugger Run (F9)• Controleer of het resultaat klopt

Page 34: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

34

Doen

test je ‘vermenigvuldig’ programma in de simulator, laat het aftekenen als het werkt

Page 35: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

35

Programmeer opgave 1: Een delay subroutine

• Een instructie duurt 0.2 us (20 MHz, 5 MIPS)• Een ‘geskipte’ instructie ook!• Behalve GOTO, CALL, RETURN: 0.4 us Maak een subroutine die W ms wacht

(naukeurigheid 1% of beter)

• Test dmv de stopwatch/instructie counter

Page 36: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

36

Programmeer opgave 2: een kwadraat functie

Schrijf een macro if_return met twee parameters X en Y. Als W de waarde X bevat dan voert de macro een ‘RETLW Y’ instructie uit.

Schrijf met behulp van deze macro een subroutine die de waarde in W kwadrateert, mits W kleiner is dan of gelijk aan 6. Zo niet dan wordt de waarde 0 teruggegeven.

• Test je routine in de simulator.