Tutorial 04_Assembler port programming and button press detection (LO3 & LO4)
Tutorial 04_Assembler port programming and button press detection (LO3 & LO4)
Figure 1 shows a PIC/MSP430 Microcontroller interfaced with buttons and LEDs. A client has
requested to program this microcontroller to toggle LED 1 when SW 1 and SW 2 is pressed one
after the other in a sequential order and for any other combination LED 1 should be in off state.
In addition, for all SW 1 even number (for any condition of SW 2) of presses, LED 2 should be
switched ON. Furthermore, LED 2 should be switched OFF for all odd number of SW 1 presses.
Write the assembly code with comments to simulate the above request. Please note that, you are
not allowed to distract the operation of the other pins and ports of the microcontroller.
Figure 01
; 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
MOVLW 0XFF
MOVWF TRISB ; Set PORTB as input
MOVLW 0X00
MOVWF TRISC ; Set PORTB as output
MAIN:
BTFSC PORTB, 0
GOTO MAIN1
BCF PORTC,0
GOTO MAIN
MAIN1:
INCF button_press
BTFSS button_press, 0
GOTO MAIN3
BCF PORTC,1
TEMP:
BTFSC PORTB, 1
GOTO MAIN2
BCF PORTC,0
GOTO MAIN
MAIN2:
BSF PORTC,0
CLRF PORTB
GOTO MAIN
MAIN3:
BSF PORTC,1
GOTO TEMP
// END start