Useful Tools

contact us contact tutor/group email to a friend accessibility options report a fault
Microcontrollers

Microcontrollers

Further Assembler Tutorials for both MPLAB Simulation and Downloading to the MMM development Board.


Code TUTMM6.ASM

; TUTMM6.ASM
; using single switch on Port A to increment Port B LED count
; showing how bit testing can be used to test switch status
  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
TRISA EQU H'85' ; Data Direction Register for PORTB @ 086 address
PORTA EQU H'05' ; Port B data register (PORTB at address 0x06)
TRISB EQU H'86' ; Data Direction Register for PORTA @ 085 address
PORTB EQU H'06' ; 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
;
LOOP BTFSS PORTA,0 ; test bit 0 of Port A, skip next line if it is set (= 1)
  GOTO LOOP ; if switch RA0 is NOT pressed then keep LOOP-ing
  INCF COUNT,F ; if switch RA0 IS pressed then increment count
  MOVF COUNT,W ; load COUNT value into W
  MOVWF PORTB ; output 'COUNT' to PORTB
  GOTO LOOP ; go back to loop and repeat
  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.

                                 

Fig 6 Flowchart for TUTMM6 code

TUTMM6 Flowchart

Fig 6a Pseudo-code for TUTMM6 code

TUTMM6 pseudo code

Code TUTMM7.ASM

; TUTMM7.ASM
; using single switch on Port A to increment Port B LED count
; showing how bit testing can be used to test switch status
; and to cause a count increment only at the moment the switch is pressed
  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
TRISA EQU H'85' ; Data Direction Register for PORTA @ 085 address
PORTA EQU H'05' ; Port A data register (PORTA at address 0x05)
TRISB EQU H'86' ; Data Direction Register for PORTB @ 086 address
PORTB EQU H'06' ; Port B data register (PORTB at address 0x06)
W EQU 0 ;
F EQU 1 ;
;
COUNT EQU H'20' ; user created variable 'COUNT' (@ address 0x20)
SWITCH EQU H'21' ; user created variable 'SWITCH' (@ address 0x21)
;
; ****** 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 1 ; 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 ; zero the COUNT variable
  CLRF SWITCH ; zero the SWITCH variable
;
TESTIT BTFSC PORTA,0 ; test RA0, skip next line if switch is NOT pressed
  GOTO TSTPRV ; when switched pressed jump to TSTPRV
  BCF SWITCH,0 ; when switch ISN'T pressed, clear bit 0 of 'SWITCH'
  GOTO TESTIT ; jump back to TESTIT
;
TSTPRV BTFSC SWITCH,0 ; (switch pressed) test bit0 of SWITCH, skip if clear
  GOTO TESTIT ; when switch bit0 = 1, then go to TESTIT
  INCF COUNT,F ; when switch bit0 = 0, then increment count
  MOVF COUNT,W ; load COUNT into W
  MOVWF PORTB ; output COUNT to PORTB
  BSF SWITCH,0 ; set bit 0 of SWITCH
  GOTO TESTIT ; repeat the whole 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.

                                 

Fig 7 Flowchart for TUTMM7 code

TUTMM7 Flowchart

Fig 7a Pseudo-code for TUTMM7 code

TUTMM7 pseudo code

 


Code TUTMM8.ASM

; TUTMM8.ASM
; using two switches on Port A, SA0 to increment PORTB LED count
; and SA2 to decrement the PORTB LED count. SA0 takes priority.
  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
TRISA EQU H'85' ; Data Direction Register for PORTA @ 085 address
PORTA EQU H'05' ; Port A data register (PORTA at address 0x05)
TRISB EQU H'86' ; Data Direction Register for PORTB @ 086 address
PORTB EQU H'06' ; Port B data register (PORTB at address 0x06)
W EQU 0 ;
F EQU 1 ;
;
COUNT EQU H'20' ; user created variable 'COUNT' (@ address 0x20)
SWITCH EQU H'21' ; user created variable 'SWITCH' (@ address 0x21)
;
; ****** 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'00000101' ; load literal value of '5' into W
  MOVWF TRISA ; set Port A pin RA2 and 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 ; zero the COUNT variable
  CLRF SWITCH ; zero the SWITCH variable
;
TEST1 BTFSC PORTA,0 ; test Switch RA0, skip next line if switch is NOT pressed
  GOTO TSTPR1 ; when switched pressed jump to TSTPR1
  BCF SWITCH,0 ; when switch ISN'T pressed, clear bit 0 of 'SWITCH' (clear switch0 flag)
  GOTO TEST2 ; go and check if Switch RA2 is pressed
;
TSTPR1 BTFSC SWITCH,0 ; (switchRA0 pressed previously?) test bit0 of 'SWITCH', skip next line if clear
  GOTO TEST2 ; when switch RA0 flag = 1, go to check if Switch RA2 is pressed (TEST2)
  BSF SWITCH,0 ; when switch RA0 flag = 0, set bit0 of 'SWITCH' ('switch0' pressed flag)
  INCF COUNT,F ; increment value of 'COUNT' by 1
  GOTO OUTPUT ; by-pass the TEST2 loop
;
TEST2 BTFSC PORTA,2 ; test Switch RA2, skip next line if switch is NOT pressed
  GOTO TSTPR2 ; when switched pressed, jump to TSTPR2
  BCF SWITCH,2 ; when switch ISN'T pressed, clear bit 2 of 'SWITCH' (clear switch2 flag)
  GOTO TEST1 ; go back and check if Switch RA0 is pressed
;
TSTPR2 BTFSC SWITCH,2 ; (switchRA2 pressed previously?) test bit2 of 'SWITCH', skip next line if clear
  GOTO TEST1 ; when switch RA2 flag = 1, go back to check if Switch RA0 is pressed (TEST1)
  BSF SWITCH,2 ; when switch RA2 flag = 0, set bit2 of 'SWITCH' ('switch2' pressed flag)
  DECF COUNT,F ; decrement value of 'COUNT' by 1
;
OUTPUT MOVF COUNT,W ; Load Count into W
  MOVWF PORTB ; Put Count value output onto PORTB LEDS
  GOTO TEST1 ; Loop around to testing for SWITCH RA0 pressed again
  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.

             

Fig 8 Pseudo-code for TUTMM8 code

TUTMM8 pseudo code

Code TUTMM9.ASM

; TUTMM9.ASM
; simply reading the 5 switches on Port A and showing their status on Port B
;
  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
TRISA EQU H'85' ; Data Direction Register for PORTA @ 085 address
PORTA EQU H'05' ; Port A data register (PORTA at address 0x05)
TRISB EQU H'86' ; Data Direction Register for PORTB @ 086 address
PORTB EQU H'06' ; Port B data register (PORTB at address 0x06)
W EQU 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
  MOVLW B'00011111' ; load literal value of '31' into W
  MOVWF TRISA ; set all Port A as inputs
  CLRF TRISB ; clear TRISB making all Port B pins outputs (PB0 to PB7)
  PAGE0   ; back to page0 of memory
;
LOOP MOVF PORTA,W ; get the value of Port A (which switches pressed)
  ANDLW B'00011111' ; ANDED with binary 00011111, to keep switch status in bits 7,6,5,4,3,2,1,0.
  MOVWF PORTB ; Put value of PORT A switches onto PORTB LEDS
  GOTO LOOP ; Loop around endlessly
  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.

             

Fig 9 Pseudo-code for TUTMM9 code

TUTMM9 pseudo code

Code TUTMM10.ASM

; TUTMM10.ASM - same as TUTMM9.ASM but utilising the SWAPF Instruction.
; simply reading the 5 switches on Port A, swapping upper & lower nibble values
; and showing their swapped nibble status on Port B
  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
TRISA EQU H'85' ; Data Direction Register for PORTA @ 085 address
PORTA EQU H'05' ; Port A data register (PORTA at address 0x05)
TRISB EQU H'86' ; Data Direction Register for PORTB @ 086 address
PORTB EQU H'06' ; Port B data register (PORTB at address 0x06)
W EQU 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
  MOVLW B'00011111' ; load literal value of '31' into W
  MOVWF TRISA ; set all Port A as inputs
  CLRF TRISB ; clear TRISB making all Port B pins outputs (PB0 to PB7)
  PAGE0   ; back to page0 of memory
;
LOOP SWAPF PORTA,W ; read Port A, swap the lefthand and righthand nibbles (4 bits), & store in W.
  ANDLW B'11110001' ; ANDED with binary 11110001, to keep switch status in bits 3,2,1,0,7,6,5,4.
  MOVWF PORTB ; Put value of (swapped nibble) PORT A switches onto PORTB LEDS
  GOTO LOOP ; Loop around endlessly
  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.

             

Fig 10 Pseudo-code for TUTMM10 code

TUTMM10 pseudo code

Code TUTMM11.ASM

; TUTMM11.ASM - same as TUTMM9.ASM but utilising the COMF Instruction.
; simply reading the 5 switches on Port A, inverting the 8 bits (complement function)
; and showing the rearranged (inverted) value on Port B
  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' ; user-created variable called COUNT stored in location 20 hex
TRISA EQU H'85' ; Data Direction Register for PORTA @ 085 address
PORTA EQU H'05' ; Port A data register (PORTA at address 0x05)
TRISB EQU H'86' ; Data Direction Register for PORTB @ 086 address
PORTB EQU H'06' ; Port B data register (PORTB at address 0x06)
W EQU 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
  MOVLW B'00011111' ; load literal value of '31' into W
  MOVWF TRISA ; set all Port A as inputs
  CLRF TRISB ; clear TRISB making all Port B pins outputs (PB0 to PB7)
  PAGE0   ; back to page0 of memory
;
LOOP COMF PORTA,W ; read Port A, invert each bit, & store in W. (Complement Function)
  ANDLW B'00011111' ; ANDED with binary 00011111, to just keep the 5 switch bits.
  MOVWF PORTB ; Put value of (inverted bits) PORT A switches onto PORTB LEDS
  GOTO LOOP ; Loop around endlessly
  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.

             

Fig 11 Pseudo-code for TUTMM11 code

TUTMM11 pseudo code

[Link to Even More Assembler examples (tutorials 12 to 16)]

 

[back to top]


Updated 09.07.07 ML

Site Search

Powered by Google
Site Map