PIC16F877A Assembly Program

Discussion in 'Programmer's Corner' started by simo_x, Mar 7, 2011.

  1. simo_x

    Thread Starter Member

    Dec 23, 2010
    200
    6
    Hi all, I need a help with one of my first programs, wich function is to visualize in a 7 segment display connected to PORTD, the number of bits set to 1 in input on the PORTC.

    The program is not complete, but I started to compile and try it as I always do when I write software.

    I post the code and than I explain which is my problem.
    Code ( (Unknown Language)):
    1.  
    2. ;    Name: EX5
    3. ;    Author: Simo
    4.  
    5. LIST p=16f877a, r=HEX
    6. INCLUDE p16F877A.INC
    7.  
    8. __CONFIG _CP_OFF&_DEBUG_OFF&_WRT_OFF&_CPD_OFF&_LVP_OFF&_BODEN_ON&_PWRTE_ON&_WDT_OFF&_HS_OSC
    9.  
    10.         ORG 0x00
    11.                                 ;step tp bank #1
    12.         bsf        STATUS, RP0        ;set RP0
    13.         bcf        STATUS, RP1        ;clear RP1
    14.  
    15.         movlw     0xff            ;set PORTC as input
    16.         movwf    TRISC        
    17.        
    18.         movlw     0x00            ;set PORTD as output
    19.         movwf    TRISD    
    20.    
    21.                                 ;step to bank #0
    22.         bcf        STATUS,    RP0        ;clear RP0
    23.         bcf        STATUS, RP1        ;clear RP1
    24.  
    25. CBLOCK    0x20
    26.     i
    27.     nbits
    28. ENDC
    29.  
    30.  
    31. main:
    32.  
    33.         call count                
    34.         movwf    PORTD            ;copy data to PORTD
    35.  
    36. count
    37.         clrf    i                ;clear i to make sure it starts from 0
    38.  
    39.         btfsc    PORTC, 0        ;if bit0 is not set, look up to bit1
    40.         incf    i,    1            ;if set increse i by one
    41.  
    42.         btfsc    PORTC, 1        ;if bit1 is not set, look up to bit2
    43.         incf    i,    1            ;if set increse i by one
    44.  
    45.         btfsc    PORTC, 2        ;if bit2 is not set, look up to bit3
    46.         incf    i,    1            ;if set increse i by one
    47.  
    48.         btfsc    PORTC, 3        ;if bit3 is not set, look up to bit4
    49.         incf    i,    1            ;if set increse i by one
    50.  
    51.         btfsc    PORTC, 4        ;if bit4 is not set, look up to bit5
    52.         incf    i,    1            ;if set increse i by one
    53.  
    54.         btfsc    PORTC, 5        ;if bit5 is not set, look up to bit6
    55.         incf    i,    1            ;if set increse i by one
    56.  
    57.         btfsc    PORTC, 6        ;if bit6 is not set, look up to bit7
    58.         incf    i,    1            ;if set increse i by one
    59.  
    60.         btfsc    PORTC, 7        ;if bit7 is not set leave function
    61.         incf    i,    1            ;if set increse i by one
    62.        
    63.         movlw    i
    64.         movwf    nbits
    65.         return
    66.  
    67.         end
    68.  
    At the moment I am just trying to put in output the binary value of the bits set to 1 in PORTC to PORTD (copied from i to nbits), just to understand if I doing it correctly, but seems that in PORTD I have the binary value of the address of nbits, wich is 0x21.

    Can you help me please to understand and solve this problem?
    Thank you.
    Simo
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    movf i, W
    does what you intend.
     
  3. simo_x

    Thread Starter Member

    Dec 23, 2010
    200
    6
    Yes it works now.. I had already used that instruction, but I don't know due to what kind or error didn't work before..
    Thank you.
    Simo
     
Loading...