Hello, i try to make simple schematics with pic16f84a chip. The LED must turn off, when switch is connected. Else it must be turned on. In my case the led turns on, when supply is turned on and does not react to switch. The code is written with assembly, hex code generated with MPLAB and program written to ucontroller with Ponyprog programmer. And sorry for my bad English language, i am from Lithuania. There is the code:
list p=16F84A ; list directive to define processor
#include <p16F84A.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
w_temp EQU 0x0C ; variable used for context saving
status_temp EQU 0x0D ; variable used for context saving
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
; isr code can go here or be located as a call subroutine elsewhere
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
main
STATUS equ 03h
PORTA equ 05h
PORTB equ 06h
TRISA equ 85h
TRISB equ 86h
;BANK0ORTA, PORTB
;BANK1:TRISA, TRISB
;Jungiklis prijungtas tarp RA1 ir minusines synos
;Sviesdiodis prijungtas tarp RB7 ir pliusines synos
;Make RA1 as input:
BSF STATUS,5
MOVLW 11111b
MOVWF TRISA
; Make RB7 as output:
MOVLW 00000000b
MOVWF TRISB
;Check whether switch is connected or not:
Start BTFSC PORTA,1
goto LIGHT; turn on LED, if switch disconnected
goto DARK;turn off LED, if switch connected
; Turn on LED say make RB7 output low:
LIGHT BCF STATUS,5
MOVLW 00000000b
MOVWF PORTB
;Let,s make delay:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
;Check whether switch is connected or not:
BTFSC PORTA,1
goto LIGHT; turn on LED, if switch disconnected
goto DARK;turn off LED, if switch connected
; Turn off LED, say make RB7 output high:
DARK BCF STATUS,5
MOVLW 11111111b
MOVWF PORTB
;Let,s make delay:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
goto Start
end
list p=16F84A ; list directive to define processor
#include <p16F84A.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
w_temp EQU 0x0C ; variable used for context saving
status_temp EQU 0x0D ; variable used for context saving
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
; isr code can go here or be located as a call subroutine elsewhere
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
main
STATUS equ 03h
PORTA equ 05h
PORTB equ 06h
TRISA equ 85h
TRISB equ 86h
;BANK0ORTA, PORTB
;BANK1:TRISA, TRISB
;Jungiklis prijungtas tarp RA1 ir minusines synos
;Sviesdiodis prijungtas tarp RB7 ir pliusines synos
;Make RA1 as input:
BSF STATUS,5
MOVLW 11111b
MOVWF TRISA
; Make RB7 as output:
MOVLW 00000000b
MOVWF TRISB
;Check whether switch is connected or not:
Start BTFSC PORTA,1
goto LIGHT; turn on LED, if switch disconnected
goto DARK;turn off LED, if switch connected
; Turn on LED say make RB7 output low:
LIGHT BCF STATUS,5
MOVLW 00000000b
MOVWF PORTB
;Let,s make delay:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
;Check whether switch is connected or not:
BTFSC PORTA,1
goto LIGHT; turn on LED, if switch disconnected
goto DARK;turn off LED, if switch connected
; Turn off LED, say make RB7 output high:
DARK BCF STATUS,5
MOVLW 11111111b
MOVWF PORTB
;Let,s make delay:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
goto Start
end
Last edited: