Problem with pic16f84a code

Discussion in 'Embedded Systems and Microcontrollers' started by ecka333, Oct 2, 2009.

  1. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    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

    [​IMG]
     
    Last edited: Oct 2, 2009
  2. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    The problem is in these two lines:
    Code ( (Unknown Language)):
    1. MOVLW 00000000b
    Code ( (Unknown Language)):
    1. MOVLW 11111111b
    It should be:
    Code ( (Unknown Language)):
    1. MOVLW b'00000000'
    Code ( (Unknown Language)):
    1. 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.
     
  3. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Try 330 ohms on the LED instead of 3.6K

    Also you need a 0.1uF decoupling cap on VDD & VSS
     
  4. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    It is better to use crystal oscillator instead of RC. Here is your trimmed code:
    Code ( (Unknown Language)):
    1. list P=16F84A ; list directive to define processor
    2. #INCLUDE <P16F84A.INC> ; processor specific variable definitions
    3. __CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
    4. ; INCLUDING SHOULD BE IN CAPITAL LETTER AS HEADER FILE IS IN CAPITAL LETTER
    5. ; AND MPLAB IS CASE SENSITIVE.
    6. ; IF INCLUDE FILE IS THERE,SFR DECLARATION IS NO MORE REQUIRED.
    7.  
    8. COUNT1 EQU 0X0C  ; GENERAL PURPOSE REGISTER IS STARTS FROM  0X0C
    9. COUNT2 EQU 0X0D  ; VARIABLE DECLARATION FOR DELAY
    10.  
    11. ORG  0X00  ; processor reset vector
    12. GOTO  MAIN ; go to beginning of program
    13.  
    14. ; SINCE INTR IS NOT USED IN THIS PROG, 0X04 IS NOT REQUIRED AT ALL.
    15.  
    16. MAIN  BSF    STATUS,5
    17.       MOVLW  B'11111'   ; ALL PORTA PINS ARE INPUT
    18.       MOVWF  TRISA  
    19.       MOVLW  B'00000000' ; ALL PORTB PINS ARE OUTPUT
    20.       MOVWF  TRISB
    21.  
    22. ;Check whether switch is connected or not:
    23.  
    24. START BTFSC PORTA,1
    25.       GOTO   LIGHT; turn on LED, if switch disconnected
    26.       GOTO   DARK;turn off LED, if switch connected
    27.  
    28. ; Turn on LED say make RB7 output low:
    29.  
    30. LIGHT  BCF STATUS,5
    31.       MOVLW  B'00000000'
    32.       MOVWF  PORTB
    33.  
    34.      ;Let,s make delay:
    35.  
    36.       MOVLW  0X08  ; USE OF SO MANY NOP IS NOT REQUIRED.
    37.       MOVWF  COUNT1
    38. AGAIN DECFSZ COUNT1,1
    39. GOTO  AGAIN
    40.  
    41. ;Check whether switch is connected or not:
    42.  
    43.       BTFSC PORTA,1
    44.       GOTO  LIGHT ; turn on LED, if switch disconnected
    45.       GOTO  DARK  ;turn off LED, if switch connected
    46.                   ; Turn off LED, say make RB7 output high:
    47. DARK  BCF STATUS,5
    48.       MOVLW B'11111111'
    49.       MOVWF PORTB
    50.  
    51.       ;Let,s make delay:
    52.  
    53.       MOVLW  0X08    ; USE OF SO MANY NOPS ARE NOT REQUIRED.
    54.       MOVWF  COUNT2
    55. KHELA DECFSZ COUNT2,1
    56.       GOTO   KHELA
    57.       GOTO   START
    58.       END
    59. ;-----------------------------------------------------------------------
    60.  
     
  5. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    OK, but what the difference between 11111111b and b'11111111'?
     
  6. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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.
     
  7. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    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?
     
  8. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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.
     
  9. Pooja Goel

    New Member

    Apr 26, 2010
    6
    0
    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
     
Loading...