360 likes | 458 Views
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.
E N D
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 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
file + w => (same) file, of w Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Instructies: bit set/clear, bit test Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
- file ‘op’ literal => (same) file, or => w- Control- Miscalaneous Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Assembler “truukjes” • Ingebouwd: SKPZ, SKPNZ, SKPC, SKPNC SETC, CLRC, SETZ, CLRZ MOVFW • Macro’s: #define W 0 #define F 1 Let op mogelijke fouten, wat doet: RRC W, F Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
oefening 2 : maximum ( C ) // versie 1 if( a > b ){ max = a; } else { max = b; } // versie 2 max = a; if( b > a ){ max = b; } // versie 3 max = ( a > b ) ? a : b; Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
SUBWF instruction – 16F887 datasheet Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
SUBWF instruction – MRM (1) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
SUBWF instruction (2) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
oefening 2 : maximum (versie 1a) ; bepaal het maximum van de variabelen H'20' en H'21' ; stop dit maximum in H'22‘(10 instructies) ; vergelijk movfw 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' groter kleiner movfw H'21' movwf H'22' goto klaar groter ; als we hier komen was H'20' dus groter movfw H'20' movwf H'22' klaar if( a > b ){ max = a; } else { max = b; } Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
oefening 2 : maximum (versie 1b) ; bepaal het maximum van de variabelen H'20' en H'21' ; stop dit maximum in H'22‘(9 instructies) ; vergelijk movfw 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 groter movfw H'20' movwf H'22' goto klaar ; als we hier komen was H'21' groter kleiner movfw H'21' movwf H'22' klaar if( a > b ){ max = a; } else { max = b; } Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
oefening 2 : maximum (versie 2) ; dat kan ook wat korter (6 instructies) ; neem aan dat H'20' het maximum is movf H'20', w movwf H'22' ; vergelijk met H'21' ; movfw H'20' is niet nodig, dat zit al in W subwf H'21', w ; dit beinvloedt de C flag niet!!! movf H'21', w skpnc movfw H'22' max = a; if( b > a ){ max = b; } Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
oefening 2 : maximum (versie 3) ; of zo (ook 6 instructies) ; vergelijk movfw H'20' subwf H'21', w ; dit beinvloed de flags niet!!! movf H'20', w skpnc movf H'21', w movwf H'22' max = ( a > b ) ? a : b; Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
MPLAB IDE IDE : Integrated Development Environment • Project management • Editor • Assembler • Programmer/debugger interface(s) • Integration of third-party tools (compilers) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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! Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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.) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Opgave 1 test je ‘vermenigvuldig’ programma in de simulator, als het goed werkt: demonstreren en laten het aftekenen Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
21 PIC16F887 memory map Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Assembler : variabelen – doe het zelf Absolute adressen: Of met #define of EQU: 22 movfw H’20’ movwf H’21 #define A H’20’ B EQU H’21’ movfw A movwf B Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Assembler : variabelen – laat ‘cblock’ het doen 23 cblock 0x20 name_1, name_2 name_3, name_4 endc ... cblock name_5 name_6 : 2 endc Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Assembler template (zie website) list p=16f887, f=inhx32 #include <P16F887.INC> org 0 cblock H’20’ endc ; hier komt uw code sleep END 24 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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' 25 Staat op C:/Program Files/MPLAB IDE/MChIP_Tools Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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! 26 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
subroutine voorbeeld wait addlw 0 skpz return addlw 1 goto wait ... ... movlw D’200’ call wait 27 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038 28 Contains: • Target chip: PIC16F887 • Programmer: pickit2 clone • Power: from USB (2x), Wall-Wart / NiCad • Peripherals: LSP, LEDs (and much more) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038 29 8 LEDs Programming connector Programming activity LED Power LED reset Power source (zet de jumper rechts) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Using DB038 30 • 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 .... Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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 ;================================================================ SLEEP END 31 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC Includes the Microchip16F887 include file (register definitions) Sets the configuration word(s) (oa. 20 MHz crystal, external reset) ORG 0, CBLOCK H’20’ M10WAIT subroutine – die mogen jullie vandaag gebruiken (wacht 10 ms) Initialises TRIS (direction) registers Beeps Activates LEDS, pattern 0x55 32 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PICkit 2 V1.20 Gebruik V1.20 !!! Device Family > Midrange (14 bit core) 33 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PICkit 2 V1.20 Selecteer de .hex file die je in MPLAB hebt aangemaakt: <project name>.HEX 34 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PICkit 2 V1.20 Zet target 5.0V aan Zet programmeren van de Data EEPROM (voorlopig) uit 35 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
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? 36 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology