PIC16F877 Program & MPLAB

Discussion in 'Embedded Systems and Microcontrollers' started by Mazaag, Mar 19, 2006.

  1. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    Hey guys,
    okay here is my first PIC program

    Code ( (Unknown Language)):
    1.  
    2.  
    3.     list   p=16f877      ; list directive to define processor
    4.     #include <p16f877.inc>    ; processor specific variable definitions
    5.    
    6.     __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF
    7.  
    8. ; '__CONFIG' directive is used to embed configuration data within .asm file.
    9. ; The lables following the directive are located in the respective .inc file.
    10. ; See respective data sheet for additional information on configuration word
    11.  
    12.  
    13.  
    14. ;**********************************************************************
    15.  ORG   0      ; processor reset vector
    16.  
    17.  
    18. bcf STATUS,RP1   ;
    19. bsf STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1
    20.  
    21. movlw b'111100' ; Set RA0 and RA1 to output
    22. movwf TRISA
    23.  
    24. bcf STATUS,RP1
    25. bcf STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0
    26.  
    27. movlw b'000011'
    28. movwf PORTA
    29.  
    30.  
    31. end
    32.  
    33.  

    It basically sets RA1 and RA0 to output, and sends a logicla 1 to those outputs, very very simple.

    when I click on Assemble, i get the following warnings and messages

    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F877 "f877temp.asm" /l"f877temp.lst" /e"f877temp.err" /o"f877temp.o"
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 17 : Found opcode in column 1. (bcf)
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 18 : Found opcode in column 1. (bsf)
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 20 : Found opcode in column 1. (movlw)
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Found opcode in column 1. (movwf)
    Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Register in operand not in bank 0. Ensure that bank bits are correct.
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 23 : Found opcode in column 1. (bcf)
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 24 : Found opcode in column 1. (bcf)
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 26 : Found opcode in column 1. (movlw)
    Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 27 : Found opcode in column 1. (movwf)
    Warning[205] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 30 : Found directive in column 1. (end)
    BUILD SUCCEEDED: Sun Mar 19 10:44:16 2006



    what is wrong with my code?

    Secondly , how can I go through each line of code, step by step, and monitor the values of the STATUS register and the TRISA and PORTA registers ?

    thanks guys
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Most assembly language compilers expect a label at the start of the new line. If a label is not used, it expects a space or spaces to precede the start of a opcode.

    You should be able to elimnate the warning(203) by starting each new line with a space.

    hgmjr
     
  3. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    What about Message 302 ?

    "Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Register in operand not in bank 0. Ensure that bank bits are correct.
    "

    Whats going on with that?
     
  4. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    MPLAB does that for every instructions that access registers in bank1 even when you have switched to bank1. It is just a annoying reminderin case you forget to switch the bank. There's an option to turn off that particular message or you can just ignore it.
     
  5. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    Okay cool , i'll try to figure out how to disable that message.

    So I plugged in my programmer, and set the com port adn everything. When I click on ENABLE PROGRAMMER , I get a message about the firmware.

    "This device requires firmware
    version 2.1.0 or higher"

    How do I upgrade the firmware ? There are a few on Microchip website. However, I have no clue which one to download, and how to install it.
     
  6. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    OKay guys this is driving me nuts.
    I'm trying to debug my program and track register values.

    I have debugged and traced these instructions, and it is working fine

    Code ( (Unknown Language)):
    1.  
    2.  
    3. main
    4.     BCF STATUS,RP1  ;
    5.     BSF STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1
    6.  
    7.     MOVLW b'111100' ; Set RA0 and RA1 to output
    8.     MOVWF TRISA
    9.  
    10. ;   BCF STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0
    11.  
    12. ;   MOVLW b'000011'
    13. ;   movwf PORTA
    14.  
    15.  
    16.     end
    17.  

    Now, in order for me to step the instructions , i have to save and build the project, which is fine, until I UNCOMMENT the next instructoin ( namely MOVLW b'000011' . when I try to build the project, I get a building failed error, with the following message :

    "MPLINK 4.01, Linker
    Copyright © 2005 Microchip Technology Inc.
    Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00000000, length=0x0000000c
    Errors : 1
    "

    I have no idea why its doing that, everything seemed to be working fine on the debugging with the first few instructions..

    Need serious help guys.

    Thanks
     
  7. Hurdy

    Senior Member

    Feb 27, 2006
    137
    0
    You should take a look at www.microchip.com and go to their discussion forums. I use their forums for PIC help, the people there are pretty awesome at PIC.

    Link to the forum is http://forum.microchip.com/

    Microchip are the creators of MPLAB.
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,144
    1,790
    The linker allowed you to put four(4) instructions starting at location zero. The next instruction is going where the processor wants to go when there is an interrupt, which is to address 4. You can ignore any interrupts if they happen with the following code fragment

    The solution:
    Code ( (Unknown Language)):
    1.  
    2. org 0
    3. goto start
    4. ; Locations 1 through 3 are empty
    5. org 4
    6. CLRF INTCON  ; clear interrupt enable bits
    7. RETFIE     ; GIE=1, but the rest are 0
    8.  
    9. start:
    10. MOVLW b'00011'
    11.  
    12.  
     
  9. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    Papa
    I don't quite understand what you mean.

    So the PIC will start at position 0, and start executing instructions up to and including the 4th instruction, why doesn't it keep going? is the 4th instruction resereved for interrupts? so we have to sorta like bypass them ? couldn't we start the code from org 5 striaght away? i'm not really sure I know what you mean.

    anyway, i altered the code using the code fragment you posted, but it still isn't working, here's how the code looks like now:

    Code ( (Unknown Language)):
    1.  
    2.     list   p=16f877      ; list directive to define processor
    3.     #include <p16f877.inc>    ; processor specific variable definitions
    4.    
    5.     __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF
    6.  
    7.  
    8.  ORG 0      ; processor reset vector
    9.  goto start
    10.    
    11.  ORG 4
    12.  
    13.  CLRF INTCON  ; clear interrupt enable bits
    14.  RETFIE    ; GIE=1, but the rest are 0
    15.  
    16.  
    17.  BCF STATUS,RP1  ;
    18.  BSF STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1
    19.  
    20.          MOVLW b'111100' ; Set RA0 and RA1 to output
    21.  MOVWF TRISA
    22.  
    23. start             BCF STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0
    24.  MOVLW b'000011'
    25.  MOVWF PORTA
    26.  
    27.  
    28.     end
    29.  
     
  10. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    okay guys i seemed to have fixed the problem myself

    I let my program start at org 00005h, so now my code looks like this


    Code ( (Unknown Language)):
    1.  
    2.                 org 00000h; Reset Vector
    3.  goto   start
    4.  
    5.  org    00005h; Beginning of program EPROM
    6.  
    7. start         BCF STATUS,RP1  ;
    8.  BSF STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1
    9.  
    10.  MOVLW b'111100' ; Set RA0 and RA1 to output
    11.  MOVWF TRISA
    12.  
    13.  BCF STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0
    14.  
    15. MOVLW b'000011'
    16.  MOVWF PORTA
    17.  
    18.  
    19.  end
    20.  
     
  11. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    Okay,
    Now i'm debugging again and monitoring the registers, everythign works fine, but when i come to move the binary value to PORTA, the value at PORTA doesn't change !!! it worked with TRISA but not with PORTA!!! i'm oging insane guys
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,144
    1,790
    One thing at a time. There are two fixed addresses in the PIC architecture which are special. The first is address 0 and the other is address four. There are many possible ways to deal with this situation. The most common of which you have already discovered is to place a goto instruction at location zero to jump around location four.

    When you execute the instruction to write to PORTA do any of the registers in the register file display turn RED? Is the number of the register that tunrs RED the same as the number of the register that corresponds to the PORTA data register?
     
  13. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    Thats what was happening with the previous instructions, but not with this one. The PORTA data register doesn't turn red, thats why its driving me nuts.
     
  14. Papabravo

    Expert

    Feb 24, 2006
    10,144
    1,790
    Look at the bits in the status register to confirm that RP1 and RP0 are both equal to zero.

    If nothing new is turning RED then perhaps the data destined for PORTA is being written somewhere else, Like TRISA which has the same address but in a different bank.

    PORTA is at address 05h in register bank 0
    TRISA is at address 05h in register bank 1

    Recheck the definition of the symbol PORTA

    it should be something like

    PORTA equ H'05'


    As a last resort you could look at the listing file to see if you agree with the assembler.

    MOVWF PORTA should look like 0805 in hexadecimal.
     
  15. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    Papa,
    I even tried simply coding a program to write the binary value out to PORTA, and it still iddn't work, PORTA stays 000000.. I have no idea what to do
     
  16. Papabravo

    Expert

    Feb 24, 2006
    10,144
    1,790
    I have finally figured out what is going on. You really need to develop your skills at reading data sheets. In addition to being I/O pins, there are other functions which are multiplexed with PORTA. In the case of the PIC16F877 it is the A/D Converter. When you write to PORTA the data goes to the PORTA data latch. When you read from PORTA you are reading the state of the pins, which is not the same as the content of the PORTA data latch.

    Enclosed is a zip file which modifies your program to correctly configure PORTA as a general purpose I/O. I copied this piece of code right out of the data sheet which describes the behavior of the PORTA pins. In a version of the PIC without an A/D converter your code would have worked. It also has a loop which writes the contents of W to PORTA, then PORTB, then VAR1 which is located in the register file at address H'20' and behaves like read write memory. Then W is incremented and the same three locations are written again.

    When you step the simulator you should see PORTA change to the value of W, PORTB will remain unchanged because it is not configured as general purpose I/O, and finally location 20 and PORTA should have the same value.

    RTFDS!
    [attachmentid=1297]
     
  17. kuttan

    New Member

    Dec 11, 2008
    1
    0
    sir please send some programs mentioned below

    addition of 2 16bit nos
    32 bit addition
    binary to bcd conversion programs
    integration
    and differentiation
     
Loading...