Problem with pic16f84a code

Thread Starter

ecka333

Joined Oct 1, 2009
76
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
;BANK0:pORTA, 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:

Tahmid

Joined Jul 2, 2008
344
Hi,
The problem is in these two lines:
Rich (BB code):
MOVLW 00000000b
Rich (BB code):
MOVLW 11111111b
It should be:
Rich (BB code):
MOVLW b'00000000'
Rich (BB code):
MOVLW b'11111111'
With this your code works, but your code is not efficiently written and could be trimmed one. If time permits, I will try to help you to make your code more efficient.
Thanks.
 

Tahmid

Joined Jul 2, 2008
344
Hi,
It is better to use crystal oscillator instead of RC. Here is your trimmed code:
Rich (BB 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
; INCLUDING SHOULD BE IN CAPITAL LETTER AS HEADER FILE IS IN CAPITAL LETTER
; AND MPLAB IS CASE SENSITIVE.
; IF INCLUDE FILE IS THERE,SFR DECLARATION IS NO MORE REQUIRED.

COUNT1 EQU 0X0C  ; GENERAL PURPOSE REGISTER IS STARTS FROM  0X0C
COUNT2 EQU 0X0D  ; VARIABLE DECLARATION FOR DELAY

ORG  0X00  ; processor reset vector
GOTO  MAIN ; go to beginning of program

; SINCE INTR IS NOT USED IN THIS PROG, 0X04 IS NOT REQUIRED AT ALL.

MAIN  BSF    STATUS,5
      MOVLW  B'11111'   ; ALL PORTA PINS ARE INPUT
      MOVWF  TRISA   
      MOVLW  B'00000000' ; ALL PORTB PINS ARE OUTPUT
      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  B'00000000'
      MOVWF  PORTB

     ;Let,s make delay:

      MOVLW  0X08  ; USE OF SO MANY NOP IS NOT REQUIRED.
      MOVWF  COUNT1
AGAIN DECFSZ COUNT1,1
GOTO  AGAIN 

;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 B'11111111'
      MOVWF PORTB

      ;Let,s make delay:

      MOVLW  0X08    ; USE OF SO MANY NOPS ARE NOT REQUIRED.
      MOVWF  COUNT2
KHELA DECFSZ COUNT2,1
      GOTO   KHELA
      GOTO   START
      END
;-----------------------------------------------------------------------
 

Tahmid

Joined Jul 2, 2008
344
Hi,
I know that in ASM Code, binary is written in B'11111111' not the other way. I tried your Code with this and tested simulation and it is ok but with 11111111b, the code was assembled in MPLAB but in simulation did not give proper output. Why are you not trying with the correction? And do what blueroomelectronics has said for hardware.
Thanks.
 

Thread Starter

ecka333

Joined Oct 1, 2009
76
Ok, shematics is working fine fith Tahmid code! Thank you bouth very much! Additionally i want to ask about general purpose register: what size number i can write to it? Can I write for example0xFFF to it?
 

Tahmid

Joined Jul 2, 2008
344
Hi,
Since 16F84A is an 8-bit microcontroller, the maximum you can write to a single register, be it general purpose or special function, is 255, ie 0xFF.
Thanks.
 

Pooja Goel

Joined Apr 26, 2010
6
hi ,
while i was using mplab with pic16f84a this error occured.
plz reply how to correct it.
MPLINK 3.80.02, Linker
Copyright (c) 2004 Microchip Technology Inc.
Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00000000, length=0x00000036
Errors : 1
BUILD FAILED: Tue Apr 27 01:22:40 2010
 
Top