What is wrong with my code PIC16F84A?

Discussion in 'Embedded Systems and Microcontrollers' started by vladtess, Jan 5, 2011.

  1. vladtess

    Thread Starter Member

    Jan 5, 2011
    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:

    Code ( (Unknown Language)):
    1.     title "Program"
    2.     include "p16f84a.inc"
    3.     __CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC
    5. ;===== variables ===========
    6. count1 equ 0xc
    7. count2 equ 0xd
    8. ;===== end variables =======
    9.     ORG     0x000
    10. ;===== set ports ======
    11.     bsf STATUS,RP0 ;set PORTB, 0 to input, others to output
    12.     movlw 0x1      
    13.     movwf TRISB
    14.     movlw 0x0
    15.     movwf TRISA
    16.     bcf STATUS, RP0
    17. ; ====== end setting ports ======
    19. start:
    20.     BTFSC PORTB, 0
    21.     goto change
    22.     goto start
    24. change:
    25.     movlw 0x1
    26.     movwf PORTA
    27.     return
    28.     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!!!
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    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: Jan 5, 2011
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    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

    Code ( (Unknown Language)):
    2. ;// Code Generator: BoostC Compiler - http://www.sourceboost.com
    3. ORG 0x00000000
    4.     GOTO    _startup
    5.     ORG 0x00000004
    6. main
    7. ; { main ; function begin
    8.     BSF STATUS, RP0
    9.     CLRF gbl_trisa
    10.     MOVLW 0xFF
    11.     MOVWF gbl_trisb
    12. label1
    13.     BCF STATUS, RP0
    14.     CLRF gbl_porta
    15.     BTFSS gbl_portb,0
    16.     GOTO    label1
    17.     MOVLW 0x01
    18.     MOVWF gbl_porta
    19.     GOTO    label1
    20. ; } main function end
    22.     ORG 0x0000000F
    23. _startup
    24.     BCF PCLATH,3
    25.     BCF PCLATH,4
    26.     GOTO    main
    27.     ORG 0x00002007
    28.     DW 0x3FFB
    29.     END
  4. Markd77

    Senior Member

    Sep 7, 2009
    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.