300 likes | 394 Views
Learn about creating delay functions and utilizing register file banks on the DB038 board at Hogeschool Utrecht. The tutorial covers different methods for introducing delays and optimizing performance. Enhance your programming skills with these practical tips.
E N D
Les 3 - onderwerpen • Hints voor ‘delay’ • Register file banks • Gebruik van het DB038 bord • Aftekenen vorige opgaves • Twee nieuwe opgaves Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een W * 1 ms delay int i = W; while( i > 0 ){ i--; Delay_1ms(); } Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een Delay_1ms • 5 MIPS, dus 1 ms is 5000 instructies* • In 8 bit kan je tot 256 tellen, neem 250 • 1 keer door de lus moet dan 5000 / 250 = 20 instructies zijn Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Delay_1ms : eerste poging cblock teller endc movlw D’250’ movwf teller loop: decfsz teller, f goto loop Hoeveel vertraging moeten we nog toevoegen, en waar precies? Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een paar instructies vertraging (1) loop: nop ; 17 NOPs ... nop decfsz teller, f goto loop Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een paar instructies vertraging (2) nop4: macro nop nop nop nop enmd loop: nop nop4 nop4 nop4 nop4 decfsz teller, f goto loop Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een paar instructies vertraging (3) cblock teller endc movlw D’250’ movwf teller loop: nop call delay_4 call delay_4 call delay_4 call delay_4 decfsz teller, f goto loop delay_4: return Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een paar instructies vertraging (4) cblock teller endc movlw D’250’ movwf teller loop: nop call delay_16 decfsz teller, f goto loop delay_16: call delay_4 delay_12: call delay_4 delay_8: call delay_4 delay_4: return Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Een paar instructies vertraging (5) delay_2: macro goto $+1 endm delay_4: macro delay_2 delay_2 endm delay_8: macro delay_4 delay_4 endm delay_16: macro delay_8 delay_8 endm 9 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PIC16F887 memory map Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PIC –register bank selection Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038 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 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 • Get 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 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’ • PORTx_SHADOW, PORTx_FLUSH (x = A,B,C,D) • WWAIT subroutine – die mogen jullie niet gebruiken • Initialises TRIS (direction) registers • Beeps • Activates LEDS, pattern 0x55 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (1) ;================================================================ ; ; include file for DB038 for a first program ; ; beeps and activates the LEDs with a 0x55 pattern ; ;================================================================ ; select target chip and hex file format LIST p=16f887, f=inhx32 ; include target chip stuff #include <P16F887.INC> Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (2) ; configuration settings __config _CONFIG1, 0x20E2 ; -debug, -LVP, -fcmen, -ieso, -boren, ; -cpd, -cp, mclre, pwtre, -wdte, HSosc __config _CONFIG2, 0x3FFF ; nothing special Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (3) ; start code at 0 ORG 0 ; start variables at 0x20 CBLOCK H'20' ENDC ; skip subroutines GOTO WContinue Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (4) ; =========================================================== ; shadow registers and flush subroutines FLUSH_MACRO MACRO Shadow, Port CBLOCK Shadow ENDC MOVFW Shadow MOVWF Port RETURN ENDM PORTA_FLUSH FLUSH_MACRO PORTA_SHADOW, PORTA PORTB_FLUSH FLUSH_MACRO PORTB_SHADOW, PORTB PORTC_FLUSH FLUSH_MACRO PORTC_SHADOW, PORTC PORTD_FLUSH FLUSH_MACRO PORTD_SHADOW, PORTD PORTE_FLUSH FLUSH_MACRO PORTE_SHADOW, PORTE Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (5) ; =========================================================== ; WWAIT WWAIT CBLOCK WWaitCounter ENDC MOVLW 0x00 MOVWF WWaitCounter WWaitLoop CALL WWaitReturn DECFSZ WWaitCounter, f GOTO WWaitLoop WWaitReturn RETURN Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (6) WContinue ; =========================================================== ; A0..A2 and D and E0..E2 are outputs BSF STATUS, RP0 MOVLW 0xD8 MOVWF ( 0x80 ^ TRISA ) MOVLW 0x00 MOVWF ( 0x80 ^ TRISD ) MOVLW 0xF8 MOVWF ( 0x80 ^ TRISE ) BCF STATUS, RP0 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (7) ; =========================================================== ; beep CBLOCK WBeepCounter ENDC CLRF WBeepCounter MOVLW H'02' MOVWF PORTE_SHADOW CALL PORTE_FLUSH WBeepLoop BSF PORTA_SHADOW, 1 CALL PORTA_FLUSH CALL WWAIT BCF PORTA_SHADOW, 1 CALL PORTA_FLUSH CALL WWAIT DECFSZ WBeepCounter, f GOTO WBeepLoop Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
DB038-01.INC (8) ; =========================================================== ; activate the LEDs MOVLW H'04' MOVWF PORTE_SHADOW CALL PORTE_FLUSH MOVLW H'55' ^ H'FF' MOVWF PORTD_SHADOW CALL PORTD_FLUSH ; the students' application follows ; (in the file that includes this file) Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PICkit 2 V1.20 • Gebruik V1.20 !!! • Device Family > Midrange (14 bit core) 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 Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PICkit 2 V1.20 • Zet target 5.0V aan Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
PICkit 2 V1.20 • Zet programmeren van de Data EEPROM (voorlopig) uit Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
opgave 1 – tel op 8 LEDs (DB038 bord) Main loop: • Tel in een variabele • Copieer die naar PORTD • Wacht 4 ms (gebruik je wacht subroutine) Allokeer je variabelen nu en voortaan altijd op de nette manier (cblock). Hoe snel zal de meest linker LED ongeveer gaan knipperen? Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
Opgave 2 – Kitt Display (DB038 bord) Maakt een ‘Kitt’ display op de 8 LEDs. (Kitt patroon is: steeds 1 LED aan, de LED die aan is ‘beweegt’ heen-en-weer). NB: 4 ms voor een stap is nu een beetje te snel! Hogeschool Utrecht / Institute for Computer, Communication and Media Technology