Problem with assembly code and PIC

Discussion in 'General Electronics Chat' started by hunterage2000, Jan 31, 2013.

  1. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Hello,

    I have used the following assembly code in Microchip MPASM to turn on RA0 of a PIC16F877A. When I Clean and build there is no errors with it and the hex file shows something there but when upload it to the PIC RA0 doesnt output HIGH.

    Can someone quickly look through the code and see if they can see the problem?

    Code ( (Unknown Language)):
    1.  
    2. ; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
    3.  
    4. PROCESSOR 16F877A
    5. list p=16f877a
    6.  
    7. #INCLUDE "P16F877A.INC"
    8.  
    9. __config _HS_OSC & _WDT_OFF & _CP_OFF
    10.                
    11. MAIN_PROG CODE                    
    12.  
    13. ;************
    14. ;Declarations
    15. ;************
    16.  
    17. START
    18. porta equ 05
    19. org 1FF
    20. goto START
    21. org 0
    22.  
    23. ;***********
    24. ;Subroutines
    25. ;***********
    26.  
    27. Init clrf porta
    28. movlw 0
    29. tris porta
    30. retlw 0
    31.  
    32. ;************
    33. ;Program Start
    34. ;************
    35.  
    36. Start call Init
    37. Main bsf porta, 0
    38. goto Main
    39. END
    40.  
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You need to set the pins as digital I/O. This is done through the ADCON1 register, page 41 of the datasheet describes this...
     
  3. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    or right, I took it straight from a book. I read that the 16 series family code is the same for all devices. I assumed it work because it didnt return any errors.
     
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I did not mention the 16f5x without reason.

    It is a good chip to learn PIC programming.

    I am still having numerous PIC 16f5x here, all of them: 16f54, 16f57, and 16f59.

    Also the program in the book is not written for the 16f877.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    No worries, took me about a week to figure that one out! The first PIC I used had digital I/O defaulted, so, when I migrated to others, I had a heck of a time trying to understand what went wrong!
     
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    There are some major differences between baseline (like the 16F54) and midrange (like 16F877).
    I'd suggest getting the template file from this folder:
    C:\Program Files\Microchip\MPASM Suite\Template\Code
    You don't want the org 0x1FF part, in fact everything above the subroutines label can just be replaced by the template file.
    <ed> (Change the config line so the parts of it mentioned in your code are correct) </ed>
     
    Last edited: Jan 31, 2013
  7. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Yeah looked at the pins of the PIC16F54 and PORTA is just digital while my PIC16F877A PORTA has both analog and digital. So why is ADCON1 used? I recognise ADC0804 pins on PORTE (rd,wr AND cs).
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    It because after every reset/power on. Pins with analog functions will ALWAYS be configured as analog input pins not digital I/O. In order to use such pins as digital I/O. They must in your program be programmed for such function. This may putting gray hair in the beginner until to learn it ;). And you just have to live with it
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ADCON1 is used to determine whether or not the pins should be treated as digital or analog, basically, it determines whether the digital drive circuitry is enabled or not.

    It gives the user the option to use a single PIC to read analog values, and/or digital ones...
     
  10. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    I have tried to initialize PORTA with the code from the datasheet but it still doesnt work. I tried to use the 16F877A template in the code folder of MPASM but it wouldnt let me save it. Any ideas why it doesnt work?

    Code ( (Unknown Language)):
    1.  
    2. ; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
    3.  
    4. __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF
    5.  
    6. p =16f877a
    7.  
    8. #INCLUDE <P16F877A.INC>
    9.  
    10. RES_VECT  CODE    0x0000            ; processor reset vector
    11.     GOTO    START                   ; go to beginning of program
    12.  
    13. ; TODO ADD INTERRUPTS HERE IF USED
    14.  
    15. MAIN_PROG CODE                      ; let linker place main program
    16.  
    17. START
    18.  
    19. BCF STATUS, RP0 ;
    20. BCF STATUS, RP1 ; Bank0
    21. CLRF PORTA                          ; Initialize PORTA by
    22.                                     ; clearing output
    23.                                     ; data latches
    24. BSF STATUS, RP0                     ; Select Bank 1
    25. MOVLW 0x06                          ; Configure all pins
    26. MOVWF ADCON1                    ; as digital inputs
    27. MOVLW 0xCF                          ; Value used to
    28.                                     ; initialize data
    29.                                     ; direction
    30. MOVWF TRISA                         ; Set RA<3:0> as inputs
    31.                                     ; RA<5:4> as outputs
    32.                                     ; TRISA<7:6>are
    33.  
    34.  
    35. END
    36.  
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You should not overwrite the template file. I guess it is read only. Use save as instead.
     
  12. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    I have saved a copy of the template and added the iniatialization code from the datasheet and still aint working.

    Code ( (Unknown Language)):
    1.  
    2. ;**********************************************************************
    3. ;   This file is a basic code template for assembly code generation   *
    4. ;   on the PIC16F877A. This file contains the basic code              *
    5. ;   building blocks to build upon.                                    *  
    6. ;                                                                     *
    7. ;   Refer to the MPASM User's Guide for additional information on     *
    8. ;   features of the assembler (Document DS33014).                     *
    9. ;                                                                     *
    10. ;   Refer to the respective PIC data sheet for additional             *
    11. ;   information on the instruction set.                               *
    12. ;                                                                     *
    13. ;**********************************************************************
    14. ;                                                                     *
    15. ;    Filename:        xxx.asm                                           *
    16. ;    Date:                                                            *
    17. ;    File Version:                                                    *
    18. ;                                                                     *
    19. ;    Author:                                                          *
    20. ;    Company:                                                         *
    21. ;                                                                     *
    22. ;                                                                     *
    23. ;**********************************************************************
    24. ;                                                                     *
    25. ;    Files Required: P16F877A.INC                                     *
    26. ;                                                                     *
    27. ;**********************************************************************
    28. ;                                                                     *
    29. ;    Notes:                                                           *
    30. ;                                                                     *
    31. ;**********************************************************************
    32.  
    33.  
    34.     list        p=16f877A    ; list directive to define processor
    35.     #include    <p16f877A.inc>    ; processor specific variable definitions
    36.    
    37.     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF
    38.  
    39. ; '__CONFIG' directive is used to embed configuration data within .asm file.
    40. ; The lables following the directive are located in the respective .inc file.
    41. ; See respective data sheet for additional information on configuration word.
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48. ;***** VARIABLE DEFINITIONS
    49. w_temp        EQU    0x7D        ; variable used for context saving
    50. status_temp    EQU    0x7E        ; variable used for context saving
    51. pclath_temp    EQU    0x7F        ; variable used for context saving            
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58. ;**********************************************************************
    59.     ORG     0x000             ; processor reset vector
    60.  
    61.     nop              ; nop required for icd
    62.       goto    main              ; go to beginning of program
    63.  
    64.  
    65.     ORG     0x004             ; interrupt vector location
    66.  
    67.     movwf   w_temp            ; save off current W register contents
    68.     movf    STATUS,w          ; move status register into W register
    69.     movwf    status_temp       ; save off contents of STATUS register
    70.     movf    PCLATH,w      ; move pclath register into w register
    71.     movwf    pclath_temp      ; save off contents of PCLATH register
    72.  
    73. ; isr code can go here or be located as a call subroutine elsewhere
    74.  
    75.     movf    pclath_temp,w      ; retrieve copy of PCLATH register
    76.     movwf    PCLATH          ; restore pre-isr PCLATH register contents
    77.     movf    status_temp,w     ; retrieve copy of STATUS register
    78.     movwf    STATUS            ; restore pre-isr STATUS register contents
    79.     swapf   w_temp,f
    80.     swapf   w_temp,w          ; restore pre-isr W register contents
    81.     retfie                    ; return from interrupt
    82.  
    83.  
    84.  
    85. main
    86.  
    87. ; remaining code goes here
    88.  
    89. START
    90.  
    91. BCF STATUS, RP0 ;
    92. BCF STATUS, RP1 ; Bank0
    93. CLRF PORTA ; Initialize PORTA by
    94. ; clearing output
    95. ; data latches
    96. BSF STATUS, RP0 ; Select Bank 1
    97. MOVLW 0x06 ; Configure all pins
    98. MOVWF ADCON1 ; as digital inputs
    99. MOVLW 0xCF ; Value used to
    100. ; initialize data
    101. ; direction
    102. MOVWF TRISA
    103.  
    104. END                       ; directive 'end of program'
    105.  
    106.  
    107.  
     
  13. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    You probably want to turn the LVP (low voltage programming) configuration bit 'off'...

    You need some code after your init sequence even if it's only a loop...

    Code ( (Unknown Language)):
    1.  
    2. loop    goto    loop            ;
    3.  
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    But anyway your code do not do much on PORTA besides clearing it. Also you say not working what does what mean ;)
     
  15. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Try replacing everything from the line "MOVLW 0xCF" to the end with this:

    Code ( (Unknown Language)):
    1. MOVLW b'11000000' ; Value used to
    2. ; initialize data
    3. ; direction
    4. MOVWF TRISA
    5. BCF STATUS, RP0 ; Bank0
    6. movlw b'11010101'    ;some pins high, some low
    7. movwf PORTA
    8. loop
    9. goto loop
    10.  
    11. END
    Hopefully you should then be able to measure alternating high and low voltages on pins RA0-5.
     
  16. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    The code is supposed to make RA0-3 inputs and RA4/5 outputs but there is no HIGH voltage at the outputs.
     
  17. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Yeah they work except RA5. I will have a play around and try other examples.
     
Loading...