Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MPASM CODE FOR PUSH BUTTON

Status
Not open for further replies.

prinsloo

Member level 5
Joined
Sep 19, 2004
Messages
93
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Bethlehem RSA
Activity points
697
mpasm save w register

Dear Members

How do I write a code (not in C pls) for this :



The LED must be off when I switch on the PIC. If I press the button once it must switch the LED on.

Regards
 

here is code to turn on and off LED using interrupt, i did it before:

Code:
;=========================
; setup and configuration
;=========================
processor 16f84A
include <p16f84A.inc>
__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;=====================================================
; variables in PIC RAM
;=====================================================

cblock 0x0d 				; Start of block
count2 						; ISR counter
old_w 						; Context saving
old_STATUS 					; Idem
Delay1			; Define two file registers for the
count1


endc


;========================================================
; ********** PROGRAM START **********
;========================================================
org 0 						; Program start at address 0
goto main

;=============================
; INTERRUPT HANDLER
;=============================
org 0x04
goto IntServ

;=============================
; MAIN PROGRAM
;=============================
main:
bsf		STATUS,RP0			; Select Bank1
bcf		OPTION_REG,INTEDG	; Set interrupt on falling edge	
							; by clearing INTEDG bit of OPTION_REG	

movlw 	b'00000001'			; PORTB bit 0
movwf	TRISB				; is Input, all other bits are Output


bcf		STATUS,RP0 			; Go back to Bank0	


clrf 	PORTB 				; Clear PORTB


;bsf 	PORTB,0 			; set PORTB bit 0

							; SETUP INTERRUPTS
bcf INTCON,INTF 			; Clear external interrupt flag (INTF = bit 1)
bsf INTCON,GIE 				; Enable global interrupts (GIE = bit 7)
bsf INTCON,INTE 			; Enable RB0 interrupt (INTE = bit 4)

;============================
; flash led
;============================
Loop:

nop
nop
nop

	

goto	Loop
;clrf	PORTB


;=======================================================
; Interrupt Service Routine
;=======================================================

IntServ:					; First test if source is an RB0 interrupt
btfss INTCON,INTF 			; INTF flag is RB0 interrupt
goto notRB0 				; Go if not RB0 origin

							; Save context
movwf old_w 				; Save w register
swapf STATUS,w				; STATUS to w
movwf old_STATUS 			; Save STATUS
							; Make sure that interrupt occurred on the falling edge
							; of the signal. If not, abort handler
btfsc PORTB,0 				; Is bit set?
goto exitISR 				; Go if clear

;=========================
; interrupt action
;=========================
;clrf	PORTB
movlw 10 					; Number of repetitions
movwf count2 				; To counter
wait:						; Check to see that port-B bit 0 is still 0
							; If not, wait until it changes
btfsc PORTB,0 				; Is bit set?
goto exitISR 				; Go if bit not 0
							; At this point RB0 bit is clear
decfsz count2,f 			; Count this iteration
goto wait 					; Continue if not zero
							; Interrupt action consists of toggling bit 2 of
							; port-B to turn LED on and off
; ACTION!!!!

movlw b'10000000' 			; Xoring with a 1-bit produces
xorwf	PORTB,1 				; Complement bit 2, port-B



;=========================
; exit ISR
;=========================
exitISR:					; Restore context

swapf old_STATUS,w 			; Saved STATUS to w
swapf old_w,f 				; Swap file register in itself
swapf old_w,w 				; re-swap back to w

notRB0:						; Reset,interrupt
bcf INTCON,INTF 			; Clear INTCON bit 1
retfie

delay:
movlw 4 
movwf count1 ; Store value in counter
repeat:
decfsz count1,f ; Decrement counter
goto repeat ; Continue if not 0
return


end
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top