Tutorial 05 – Assembler Programming, OddEven button presses (LO3 & LO4)
Tutorial 05 – Assembler Programming, OddEven button presses (LO3 & LO4)
LO4)
A microcontroller based system comes with a push button for mode selection. For all the odd
presses of the button, PORTB bit 0 should be logic HIGH and bit 1 should be logic LOW. For all
even presses, PORTB bit 0 should be logic LOW and bit 1 should be logic HIGH. Write a code
segment in assembler to accommodate these requirements.
Solution
; PIC16F877A Configuration Bit Settings
; CONFIG
CONFIG FOSC = EXTRC ; Oscillator Selection bits (RC oscillator)
CONFIG WDTE = OFF ; Watchdog Timer Enable bit (WDT disabled)
CONFIG PWRTE = OFF ; Power-up Timer Enable bit (PWRT disabled)
CONFIG BOREN = OFF ; Brown-out Reset Enable bit (BOR disabled)
CONFIG LVP = OFF ; Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is
digital I/O, HV on MCLR must be used for programming)
CONFIG CPD = OFF ; Data EEPROM Memory Code Protection bit (Data EEPROM code protection
off)
CONFIG WRT = OFF ; Flash Program Memory Write Enable bits (Write protection off; all program
memory may be written to by EECON control)
CONFIG CP = OFF ; Flash Program Memory Code Protection bit (Code protection off)
;--------initialising-------------
PSECT start, CLASS = CODE, DELTA=2
start:
PAGESEL MAIN
GOTO MAIN
;---------end initialising-------------
BCF STATUS, 6 ;
BSF STATUS, 5 ; Select Bank 1
MAIN:
BSF PORTB, 1
BCF PORTB, 0
call delay_start
BCF PORTB, 1
BSF PORTB, 0
call delay_start
GOTO MAIN
NEXT:
INCF count
NEXT1:
BTFSC count, 0
goto notzero ; ODD press
BCF PORTB, 0 ; EVENpress
BSF PORTB, 1
GOTO TESTBUTTON
goto NEXT1
notzero:
BSF PORTB, 0
BCF PORTB, 1
GOTO TESTBUTTON
goto NEXT1
delay_start:
DECFSZ delay1
goto delay_start
MOVLW 0XFF
MOVWF delay1
DECFSZ delay2
goto delay_start
MOVLW 0XFF
MOVWF delay2
return
TESTBUTTON:
BTFSC PORTB, 2
GOTO INCRIMENT
GOTO TESTBUTTON
INCRIMENT:
INCF count
GOTO NEXT1
END start