Les 2 - onderwerpen

36
2PROJ5 – PIC assembler Hogeschool Utrecht / Institute for Computer, Communication and Media Technology 1 Les 2 - onderwerpen Herhaling instructieset en Assembler ‘truukjes’ Bespreking van de opgaven van de vorig les Allokeren van variabelen Gebruik van MPLAB, simuleren DB037 bordje Gebruik van MPLAB, bordje, en pickit2 Opgaven: tellen op de LEDs

description

Les 2 - onderwerpen. Herhaling instructieset en Assembler ‘truukjes’ Bespreking van de opgaven van de vorig les Allokeren van variabelen Gebruik van MPLAB, simuleren DB037 bordje Gebruik van MPLAB, bordje, en pickit2 Opgaven: tellen op de LEDs. 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 en Assembler ‘truukjes’ • Bespreking van de opgaven van de vorig les• Allokeren van variabelen• Gebruik van MPLAB, simuleren• DB037 bordje• Gebruik van MPLAB, bordje, en pickit2• Opgaven: tellen op de LEDs

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', w

addwf H'21', w

movwf H'22'

sleep ; zet dit na je code

end ; 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 – 16F887 datasheet

Page 9: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

9

SUBWF instruction – MRM (1)

Page 10: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

10

SUBWF instruction (2)

Page 11: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

11

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 12: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

12

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

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

Page 13: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

13

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;if( b > a ){ max = b;}

Page 14: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

14

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 15: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

15

MPLAB IDE

IDE : Integrated Development Environment

• Project management

• Editor

• Assembler

• Programmer/debugger interface(s)

• Integration of third-party tools (compilers)

Page 16: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

16

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 17: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

17

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 18: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

18

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 19: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

19

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 20: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

20

Opgave 1

test je ‘vermenigvuldig’ programma in de simulator, als het goed werkt: demonstreren en laten het aftekenen

Page 21: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

21

PIC16F887 memory map

Page 22: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

22

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 A

movwf B

Page 23: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

23

Assembler : variabelen – laat ‘cblock’ het doen

cblock 0x20name_1, name_2 name_3, name_4

endc ...cblock

name_5 name_6 : 2

endc

Page 24: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

24

Assembler template (zie website)

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

org 0cblock H’20’ endc

; hier komt uw code

sleepEND

Page 25: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

25

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 26: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

26

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 27: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

27

subroutine voorbeeld

wait

addlw 0

skpz

return

addlw 1

goto wait

...

...

movlw D’200’

call wait

Page 28: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

28

DB038

Contains:• Target chip: PIC16F887• Programmer: pickit2 clone• Power: from USB (2x), Wall-Wart / NiCad• Peripherals: LSP, LEDs (and much more)

Page 29: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

29

DB038Programming connector

Power source (zet de jumper rechts)

8 LEDs

reset

Programming

activity LED

Power LED

Page 30: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

30

Using DB038

• Get les2-count.zip (from my website)• Unzip to new directory (let op path!)• Double-click count.mcp• Edit count.asm• Assemble• Correct errors and repeat ....

Page 31: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

31

an (empty) DB038 program

;================================================================; ; count.asm;;================================================================

; initialisation etc for DB038; also beeps and activated the LEDs#include <DB038-01.INC>

;================================================================; main ;================================================================

; put your code here

;================================================================; end of assembler source;================================================================

SLEEPEND

Page 32: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

32

DB038-01.INC

1. Includes the Microchip16F887 include file (register definitions)

2. Sets the configuration word(s) (oa. 20 MHz crystal, external reset)

3. ORG 0, CBLOCK H’20’

4. M10WAIT subroutine – die mogen jullie vandaag gebruiken (wacht 10 ms)

5. Initialises TRIS (direction) registers

6. Beeps

7. Activates LEDS, pattern 0x55

Page 33: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

33

PICkit 2 V1.20

• Gebruik V1.20 !!!

• Device Family > Midrange (14 bit core)

Page 34: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

34

PICkit 2 V1.20

• Selecteer de .hex file die je in MPLAB hebt aangemaakt: <project name>.HEX

Page 35: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

35

PICkit 2 V1.20

• Zet target 5.0V aan

• Zet programmeren van de Data EEPROM (voorlopig) uit

Page 36: Les 2 - onderwerpen

2PROJ5 – PIC assembler

Hogeschool Utrecht / Institute for Computer, Communication and Media Technology

36

opgave 2 – tel op 8 LEDs (DB038 bord) Main loop:

– Tel in een variabele– Copieer die naar PORTD– Wacht even (gebruik de M10WAIT subroutine,

die wacht 10 ms)

Allokeer je variabelen nu en voortaan altijd op de nette manier (cblock).

Hoe snel zal de meest linker LED ongeveer gaan knipperen?