The first piece of code simply sets all the PORTB pins as OUTPUTS and sequentially turns on each of the PORTB LED's in a simple loop. i.e. 00000001; 00000011; 00000111; 00001111; 00011111; 00111111; 01111111; 11111111, then clears all outputs and starts again.
| ; TUTMM1.ASM ; making all Port B pins as OUTOUTS and sequentially turn on each portB LED |
|||
| LIST | p=16F84 | ||
| ; ****** PROGRAM EQUATES ****** ; |
|||
| STATUS | EQU | 0x03 | |
| PORTB | EQU | 0x06 | |
| ; ; ****** MAIN PROGRAM ****** |
|||
| ORG | 0 | ; Reset Vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 4 | ; Interrupt vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 5 | ; Start of program memory | |
| ; | |||
| CLRF | PORTB | ; Clear PORTB register - makes all Port B pins to logic 0 | |
| BSF | STATUS,5 | ; set the RP0 bit of the status register (access to bank 1) | |
| CLRF | PORTB | ; clear TRISB making all Port B pins outputs (PB0 to PB7) | |
| BCF | STATUS,5 | ; clears RP0 bit of the status register (returns to bank 0) | |
| ; | |||
| LOOPIT | BSF | PORTB,0 | ; set Port B pin 0 to logic 1 (PB0 = high) |
| BSF | PORTB,1 | ; set Port B pin 1 to logic 1 (PB1 = high) | |
| BSF | PORTB,2 | ; set Port B pin 2 to logic 1 (PB2 = high) | |
| BSF | PORTB,3 | ; set Port B pin 3 to logic 1 (PB3 = high) | |
| BSF | PORTB,4 | ; set Port B pin 4 to logic 1 (PB4 = high) | |
| BSF | PORTB,5 | ; set Port B pin 5 to logic 1 (PB5 = high) | |
| BSF | PORTB,6 | ; set Port B pin 6 to logic 1 (PB6 = high) | |
| BSF | PORTB,7 | ; set Port B pin 7 to logic 1 (PB7 = high) | |
| CLRF | PORTB | ; Clear portB (make all portB pins LOW) | |
| GOTO | LOOPIT | ; Loop back to the 'LOOPIT' label and repeat | |
| END | ; final statement | ||
This code should be first simulated to step through to get a feel for what is happening with the registers.
Then it can be downloaded to the MatrixMultiMedia Development Board, where it can run SLOWLY in RC mode to demonstrate the incrementing LED's on PORTB. (If the downloaded code simply lights up all the PortB LED's, then you may find that the 'configuration' is probably set to crystal mode and it is running too fast for the eye to discern anything. For this code make sure the configuration is set to RC operation and the switches S2 and S4 (on the target board) will need setting accordingly. S2 ought to be in RC mode (to the right!) and S4 ought to be in slow mode for this code (to the left!) to step through slow enough for one to see).
The second piece of simple assembler code produces a single lit LED that starts at the right hand side of PORTB LED's (LB0) and moves left until it reaches the eighth and final LED of PORTB(LB7), then the loop repeats. The code uses aliases, bit-names and 'conditional loops' which demonstrate how the 'carry bit' rotates into the register. (i.e. When register rolls over from 1111 1111 to 0000 0000, the CARRY bit is set.)
| ; TUTMM2.ASM ; example of assembler code using aliases, bit-names and 'conditional loops' ; showing how the carry bit rotates into the register. |
|||
| LIST | p=16F84 | ||
| ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; Configuration data for running the code on the MMM development board. ; PICmicro MCU type: 16F84 ; Oscillator: RC mode, slow, VR1 fully clockwise (max.rate) ; LCD display: off ; 7-segment display: off ; Version 2 board settings: J14 links: Digital ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; ; The following line embeds configuration data into the PICmicro __CONFIG H'3FFB' ; RC mode ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |
|||
| ; | |||
| #DEFINE PAGE0 BCF STATUS,5 | |||
| #DEFINE PAGE1 BSF STATUS,5 | |||
| ; | |||
| STATUS | EQU | H'03' | ; STATUS register (STATUS at address 0x03) |
| TRISB | EQU | H'86' | ; Port B direction register (TRISB at address 0x86) |
| PORTB | EQU | H'06' | ; Port B data register (PORTB at address 0x06) |
| W | EQU | 0 | ; Working register (when we use 'W', pc reads 0) |
| F | EQU | 1 | ; File register (when we use 'F', pc reads 1) |
| C | EQU | 0 | ; Carry flag (when we use 'C', pc reads 0) |
| ; ; ****** MAIN PROGRAM ****** |
|||
| ORG | 0 | ; Reset Vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 4 | ; Interrupt vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 5 | ; Start of program memory | |
| ; | |||
| CLRF | PORTB | ; Clear PORTB register - makes all Port B pins to logic 0 | |
| PAGE1 | ; access page1 of memory | ||
| CLRF | TRISB | ; clear TRISB making all Port B pins outputs (PB0 to PB7) | |
| PAGE0 | ; back to page0 of memory | ||
| ; | |||
| LOOP1 | MOVLW | 1 | ; load the literal value of 1 into Working register |
| MOVWF | PORTB | ; load this value (1) as data into Port B | |
| BCF | STATUS,C | ; clear the Carry flag (bit 0 of the Status register) | |
| ; | |||
| LOOP2 | RLF | PORTB,F | ; rotate value of PORTB left by 1 logical place |
| BTFSS | STATUS,C | ; If Carry bit of STATUS is set, then skip next line. | |
| GOTO | LOOP2 | ; this line is only actioned when Carry bit is zero (0) | |
| GOTO | LOOP1 | ; this line is only actioned when Carry bit is set(1)) | |
| END | ; final statement | ||
This code can again be simulated within the MPLAB environment to step through the lines of code and watch the registers change and observe the code loop-pattern. Once the machine code has been downloaded to the MMM development board, it will run (when operated in slow RC-mode) and can be seen operating through its loops.
Development Board Settings for RC operation - switches S2 set to RC mode (to the right!) and S4 set to SLOW mode (to the left!)
The CARRY FLAG is set when Port B register rotates left from 1000 0000 and overflows to 0000 0000 (i.e. bit 0 of the STATUS register is set).
The third piece of simple assembler code produces a decrementing single illuminated LED moving from left to right on the PORTB LED's (from LB7 to LB0). It utilises the Rotate Right Instruction and checks the condition of the ZERO bit to determine when PORTB goes from 0000 0001 to 0000 0000. (when the RESULT of an (arithmetic or logic) operation is ZERO, Z bit (bit2) of the STATUS register is set.) This code achieves the opposite direction of rotation for the PORTB LED's, compared to TUTMM2.ASM code.
| ; TUTMM3.ASM ; example of assembler code using the RRF instruction and Z (bit2 of 'STATUS') ; Z=1 :- The RESULT of an arithmetic/logic operation is zero ; When the RESULT of an operation is NOT ZERO, then Z = 0 |
|||
| LIST | p=16F84 | ||
| ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; Configuration data for running the code on the MMM development board. ; PICmicro MCU type: 16F84 ; Oscillator: RC mode, slow, VR1 fully clockwise (max.rate) ; LCD display: off ; 7-segment display: off ; Version 2 board settings: J14 links: Digital ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; ; The following line embeds configuration data into the PICmicro __CONFIG H'3FFB' ; RC mode ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |
|||
| ; | |||
| #DEFINE PAGE0 BCF STATUS,5 | |||
| #DEFINE PAGE1 BSF STATUS,5 | |||
| ; | |||
| STATUS | EQU | H'03' | ; STATUS register (STATUS at address 0x03) |
| TRISB | EQU | H'86' | ; Port B direction register (TRISB at address 0x86) |
| PORTB | EQU | H'06' | ; Port B data register (PORTB at address 0x06) |
| W | EQU | 0 | ; Working register flag (when we use 'W', pc reads 0) |
| F | EQU | 1 | ; File register flag (when we use 'F', pc reads 1) |
| C | EQU | 0 | ; Carry flag (when we use 'C', pc reads 0) |
| Z | EQU | 2 | ; Zero flag (when we use 'Z', pc reads 2) |
| ; ; ****** MAIN PROGRAM ****** |
|||
| ORG | 0 | ; Reset Vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 4 | ; Interrupt vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 5 | ; Start of program memory | |
| ; | |||
| CLRF | PORTB | ; Clear PORTB register - makes all Port B pins to logic 0 | |
| PAGE1 | ; access page1 of memory | ||
| CLRF | TRISB | ; clear TRISB making all Port B pins outputs (PB0 to PB7) | |
| PAGE0 | ; back to page0 of memory | ||
| ; | |||
| LOOP1 | MOVLW | D'128' | ; load the literal value of 128 into W |
| MOVWF | PORTB | ; load this value (128) as data into Port B | |
| BCF | STATUS,C | ; clear the Carry flag (bit 0 of the Status register) | |
| ; | |||
| LOOP2 | RRF | PORTB,F | ; rotate value of PORTB right by 1 logical place |
| MOVF | PORTB,F | ; move PORTB to itself (used to check when PORTB becomes zero) | |
| BTFSS | STATUS,Z | ; If ZERO bit of STATUS is set (when PORTB goes to 0), then skip next line. | |
| GOTO | LOOP2 | ; this line is actioned when Zero bit is zero (0) i.e. when PORTB is NOT 0 | |
| GOTO | LOOP1 | ; this line is only actioned when Zero bit is set(1)) i.e. when PORTB IS 0 | |
| END | ; final statement | ||
This code can again be simulated within the MPLAB environment to step through the lines of code and watch the registers change and observe the code loop-pattern. Once the machine code has been downloaded to the MMM development board, it will run (when operated in slow RC-mode) and can be seen operating through its loops.
Development Board Settings for RC operation - switches S2 set to RC mode (to the right!) and S4 set to SLOW mode (to the left!)
When PORTB rotates from 0000 0001 to 0000 0000, we need to loop back around to the start position again (via loop1). To achieve this if we then move the contents of PORTB onto itself ('MOVF PORTB, F'), it will generate a ZERO Flag (bit 2 of the Status register is set), as the result of this operation is ZERO.
The fourth piece of simple assembler code displays both incrementing (followed by decrementing) decimal value representation on the PORTB LED's. The code uses loops while checking the ZERO status bit. It illustrates both UP and DOWN counts. This code can be sped up by rotating the 'RV1' pot (to the left of the S2 & S4 switches on the development board). This code can also be used to experiment with running up to a set BREAK POINT (see Practical Session 4), to allow the code to run to a specifc set point in the code before stepping through the code-area of interest.
| ; TUTMM4.ASM ; example of assembler code using loops while checking the ZERO status ; illustrating both down and up counts |
|||
| LIST | p=16F84 | ||
| ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; Configuration data for running the code on the MMM development board. ; PICmicro MCU type: 16F84 ; Oscillator: RC mode, slow, VR1 fully clockwise (max.rate) ; LCD display: off ; 7-segment display: off ; Version 2 board settings: J14 links: Digital ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; ; The following line embeds configuration data into the PICmicro __CONFIG H'3FFB' ; RC mode ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |
|||
| ; | |||
| #DEFINE PAGE0 BCF STATUS,5 | |||
| #DEFINE PAGE1 BSF STATUS,5 | |||
| ; | |||
| STATUS | EQU | H'03' | ; Status register at address 0x03 |
| TRISB | EQU | H'86' | ; Port B direction register (TRISB at address 0x86) |
| PORTB | EQU | H'06' | ; Port B data register (PORTB at address 0x06) |
| W | EQU | 0 | ; Working register flag (when we use 'W', pc reads 0) |
| F | EQU | 1 | ; File register flag (when we use 'F', pc reads 1) |
| Z | EQU | 2 | ; Zero flag (when we use 'Z', pc reads 2) |
| ; | |||
| COUNT | EQU | H'20' | ; user created variable 'COUNT' (@ address 0x20) |
| ; ; ****** MAIN PROGRAM ****** |
|||
| ORG | 0 | ; Reset Vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 4 | ; Interrupt vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 5 | ; Start of program memory | |
| ; | |||
| CLRF | PORTB | ; Clear PORTB register - makes all Port B pins to logic 0 | |
| PAGE1 | ; access page1 of memory | ||
| CLRF | TRISB | ; clear TRISB making all Port B pins outputs (PB0 to PB7) | |
| PAGE0 | ; back to page0 of memory | ||
| CLRF | COUNT | ; give COUNT an initial value of zero | |
| ; incrementing_section | |||
| LOOP1 | MOVF | COUNT,W | ; load the value of COUNT into Working register |
| MOVWF | PORTB | ; load count from working register to Port B | |
| INCFSZ | COUNT,F | ; increment COUNT and skip next line IF count is zero | |
| ; i.e. checks for when it rolls over from 255 to 0 | |||
| GOTO | LOOP1 | ; keeps looping as long as COUNT is not yet 0 | |
| ; when count goes to zero, go to the decrementing loop | |||
| ; decrementing_section | |||
| LOOP2 | MOVF | COUNT,W | ; load the value of COUNT into Working register |
| MOVWF | PORTB | ; load count from working register to Port B | |
| DECFSZ | COUNT,F | ; decrement COUNT and skip next line IF count is zero | |
| ; i.e. check when count goes from one to zero | |||
| GOTO | LOOP2 | ; keeps looping as long as COUNT is not yet 0 | |
| GOTO | LOOP1 | ; when count goes to zero, go back to the incrementing loop | |
| END | ; final statement | ||
Like the previous examples this code can again be simulated within the MPLAB environment then the machine code can be downloaded to the MMM development board.
Development Board Settings for RC operation - switches S2 set to RC mode (to the right!) and S4 set to SLOW mode (to the left!)
The fifth piece of simple assembler code uses a single switch on Port A to increment the Port B LED count
| ; TUTMM5.ASM ; using single switch on Port A to increment Port B LED count ; |
|||
| LIST | p=16F84 | ||
| ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; Configuration data for running the code on the MMM development board. ; PICmicro MCU type: 16F84 ; Oscillator: RC mode, slow, VR1 fully clockwise (max.rate) ; LCD display: off ; 7-segment display: off ; Version 2 board settings: J14 links: Digital ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; ; The following line embeds configuration data into the PICmicro __CONFIG H'3FFB' ; RC mode ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |
|||
| ; | |||
| #DEFINE PAGE0 BCF STATUS,5 | |||
| #DEFINE PAGE1 BSF STATUS,5 | |||
| ; | |||
| STATUS | EQU | H'03' | ; Status register at address 0x03 |
| TRISB | EQU | H'86' | ; Data Direction Register for PORTB @ 086 address |
| PORTB | EQU | H'06' | ; Port B data register (PORTB at address 0x06) |
| TRISA | EQU | H'85' | ; Data Direction Register for PORTA @ 085 address |
| PORTA | EQU | H'05' | ; Port A data register (PORTB at address 0x05) |
| W | EQU | 0 | ; Working register flag (when we use 'W', pc reads 0) |
| F | EQU | 1 | ; File register flag (when we use 'F', pc reads 1) |
| ; | |||
| COUNT | EQU | H'20' | ; user created variable 'COUNT' (@ address 0x20) |
| ; ; ****** MAIN PROGRAM ****** |
|||
| ORG | 0 | ; Reset Vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 4 | ; Interrupt vector | |
| GOTO | 5 | ; Goto start of program | |
| ORG | 5 | ; Start of program memory | |
| ; | |||
| CLRF | PORTA | ; Clear PORTA register - makes all Port A pins to logic 0 | |
| CLRF | PORTB | ; Clear PORTB register - makes all Port B pins to logic 0 | |
| PAGE1 | ; access page1 of memory | ||
| MOVLW | B'00000001' | ; load literal value of '1' into W | |
| MOVWF | TRISA | ; set Port A pin RA0 as input (rest as outputs) | |
| CLRF | TRISB | ; clear TRISB making all Port B pins outputs (PB0 to PB7) | |
| PAGE0 | ; back to page0 of memory | ||
| BEGIN | CLRF | COUNT | ; clear 'count' |
| LOOP | MOVF | PORTA,W | ; copy the value of PORTA into W |
| ANDLW | B'00000001' | ; 'AND' literal value binary 1 with W | |
| ADDWF | COUNT,F | ; the value of RA0 is ADD-ed to 'count' | |
| ; if switch is pressed RA0 = 1 ; if switch is unpressed RA0 = 0 |
|||
| MOVF | COUNT,W | ; load the value of COUNT into Working register | |
| MOVWF | PORTB | ; load count from working register to Port B | |
| GOTO | LOOP | ; endless loop | |
| END | ; final statement | ||
Like the previous examples, this code can, again, be simulated within the MPLAB environment then the machine code can be downloaded to the MMM development board. For simulation on this example try using the following STIMULUS FILE:-
| ! Stimulus file for pulse on RA0 | ||
|---|---|---|
| STEP | RA0 | |
| 11 | 1 | !switch RA0 pressed 1st loop |
| 18 | 0 | !switch RA0 NOT pressed 2nd loop |
| 25 | 1 | !switch RA0 pressed 3rd loop |
| 32 | 1 | !switch RA0 pressed 4th loop |
| 39 | 1 | !switch RA0 pressed 5th loop |
| 46 | 0 | !switch RA0 NOT pressed 6th loop |
| 53 | 1 | !switch RA0 pressed 7th loop |
| 60 | 0 | !switch RA0 NOT pressed 8th loop |
| 67 | 1 | !switch RA0 pressed 9th loop ...... |
Development Board Settings for RC operation - switches S2 set to RC mode (to the right!) and S4 set to SLOW mode (to the left!). If this code is run in XT mode (with the clock crytal) it will run too fast for the eye to see!
Updated 06.07.07 ML
Powered by Google
Site Map