What is wrong with my code PIC16F84A?

Thread Starter

vladtess

Joined Jan 5, 2011
43
Hi there. I'm trying to make a simple testing circuit which would scan PORTB, 0 for changes (from 0 -> 1) and upon doing so, it will send 0x1 to PORTA. Meaning when button is pressed, PORTA,0 will turn to 1. Here's my code:

Rich (BB code):
    title "Program"
    include "p16f84a.inc"
    __CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC

;===== variables ===========
count1 equ 0xc
count2 equ 0xd
;===== end variables =======
    ORG     0x000
;===== set ports ======
    bsf STATUS,RP0 ;set PORTB, 0 to input, others to output
    movlw 0x1       
    movwf TRISB
    movlw 0x0
    movwf TRISA
    bcf STATUS, RP0
; ====== end setting ports ======

start:
    BTFSC PORTB, 0
    goto change
    goto start

change:
    movlw 0x1
    movwf PORTA
    return
    end
But in the simulator, nor when testing on chip, this does not work! Please help. what is wrong with my code??! I read the manual for PIC16F84A and it seems that I am doing everything right. For some reason, the 'goto change' line is always skipped even when I put PORTB,0 high. I use "PIC Simulator IDE" to test my program.

Thanks much!!!
 

thatoneguy

Joined Feb 19, 2009
6,359
When you say "PIC Simulator IDE", do you mean within Microchip's MPLAB, or a different program?

Have you tried using the skeleton .asm code for starting the program?

0x04 is the address of the interrupt routine. PortB.0 is also the Interrupt pin on the 16F84, so this could be causing issues as well.

Usually 0x00 is only a line of "goto main", while 0x04 is the interrupt service routine.

If just getting started on programming for PICs, I'd suggest starting out with a C compiler such as Boost C or Mikro C (Not much of a fan of Hi-Tech, personally). MikroBASIC or PIC BASIC are two other options if you do not know C.
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
See if the attached Zipped .hex file does what you want (button on portb.0, LED on porta.0)

It works in simulation, wrote it quick in BoostC with minimal code, ignored interrupts, etc. I don't have a 16F84 to test it on though.


View attachment 16F84button.zip

Rich (BB code):
;// Code Generator: BoostC Compiler - http://www.sourceboost.com
ORG 0x00000000
    GOTO    _startup
    ORG 0x00000004
main
; { main ; function begin
    BSF STATUS, RP0
    CLRF gbl_trisa
    MOVLW 0xFF
    MOVWF gbl_trisb
label1
    BCF STATUS, RP0
    CLRF gbl_porta
    BTFSS gbl_portb,0
    GOTO    label1
    MOVLW 0x01
    MOVWF gbl_porta
    GOTO    label1
; } main function end

    ORG 0x0000000F
_startup
    BCF PCLATH,3
    BCF PCLATH,4
    GOTO    main
    ORG 0x00002007
    DW 0x3FFB
    END
 

Markd77

Joined Sep 7, 2009
2,806
Works fine for me.
I'd recommend setting WDT to off in the config.
Also you use "goto change" but use return which will cause problems. Use call if you are going to use return.

 

Attachments

Top