help me im a newbie in assembly

Discussion in 'The Projects Forum' started by ernestclydeachua, Aug 5, 2012.

  1. ernestclydeachua

    Thread Starter New Member

    Aug 5, 2012
    16
    0
    Im currently working in this project and searched and modified the circuit,

    by using a spdt switch i can control a specific relay and LED
    where

    RB0 controls RB7 and RA0 ===>if RB0=0 then RB7 and RA0=0, else it will be 1 when RB0=1
    RB1 controls RB6 and RA1
    RB2 controls RB5 and RA2
    RB3 controls RB4 and RA3





    i want to know the codes in Assembly language



    i really want to know how to control each pins, i only know
    movlw b'00000000'
    movwf portb
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Which PIC are you using?
    It's unlikely that anyone will write all the code for you so it's probably best to try and make a start and then post what you have tried.
    There is a fairly good tutorial here:
    http://www.gooligum.com.au/tut_midrange.html

    The other recommended method of writing to the port is to make changes to a variable and then write that to the port, eg.
    bsf PORTB_buffer, 1 ;set bit 1
    movf PORTB_buffer, W ;copy to working register
    movwf PORTB ;copy working register to PORTB

    Setting or clearing bits directly in the port can cause problems.
     
  3. ernestclydeachua

    Thread Starter New Member

    Aug 5, 2012
    16
    0
    Code ( (Unknown Language)):
    1.  
    2. list      p=16F84A            ; list directive to define processor
    3.     #include <p16F84A.inc>        ; processor specific variable definitions
    4.     errorlevel -302, -207    
    5.  
    6.     __config _CP_OFF &_XT_OSC & _PWRTE_ON & _WDT_OFF
    7.  
    8.  
    9.  
    10. cntr1    equ    h'21'
    11. cntr2    equ    h'22'
    12.  
    13.  
    14.     ORG     0x000             ; processor reset vector
    15.       goto    main              ; go to beginning of program
    16.  
    17.     ORG     0x004             ; interrupt vector location
    18.     nop
    19.     retfie                    ; return from interrupt
    20.  
    21. main:
    22.     bsf    STATUS,RP0
    23.     movlw    b'00000'
    24.     movwf    TRISA
    25.     movlw    b'00001111'
    26.     movwf    TRISB
    27.     bcf    STATUS,RP0
    28.  
    29. loop:
    30.     btfss    PORTB,0
    31.     bsf    PORTB,7
    32.     bsf    PORTA,0
    33.  
    34.     btfss    PORTB,1
    35.     bsf    PORTB,6
    36.     bsf    PORTA,1
    37.  
    38.     btfss    PORTB,2
    39.     bsf    PORTB,5
    40.     bsf    PORTA,2
    41.  
    42.     btfss    PORTB,3
    43.     bsf    PORTB,4
    44.     bsf    PORTA,3
    45.     goto    loop
    46.  
    47. slow:
    48.     movlw    0XFE
    49.     movwf    cntr1
    50.     movlw    0XFE
    51.     movwf    cntr2
    52. del1:
    53.     movlw    0XFE
    54.     movwf    cntr2
    55. del2:
    56.     decfsz    cntr2,F
    57.     goto    del2
    58.     decfsz    cntr1,F
    59.     goto    del1
    60.     return
    61.  
    62.     end
    63.  
     
    Last edited by a moderator: Aug 5, 2012
  4. ernestclydeachua

    Thread Starter New Member

    Aug 5, 2012
    16
    0
    well im not sure if this is working because it wont run on the simulator, all leds are on
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I see the main problem, you are not clearing any of the bits in the ports.
    If you post more code it looks neater if you highlight it and click the button that looks like #
    I'd recommend something like this:
    I think it probably works but I haven't tested it.
    (Don't forget to add the two new variables at the start.)

    Code ( (Unknown Language)):
    1. loop:
    2. andlw PORTB_buffer, b'00001111'    ;clear bits 4-7
    3. andlw PORTA_buffer, b'11110000'    ;clear bits 0-3
    4.  
    5.     btfss    PORTB,0
    6.     bsf    PORTB_buffer,7
    7.     bsf    PORTA_buffer,0
    8.  
    9.     btfss    PORTB,1
    10.     bsf    PORTB_buffer,6
    11.     bsf    PORTA_buffer,1
    12.  
    13.     btfss    PORTB,2
    14.     bsf    PORTB_buffer,5
    15.     bsf    PORTA_buffer,2
    16.  
    17.     btfss    PORTB,3
    18.     bsf    PORTB_buffer,4
    19.     bsf    PORTA_buffer,3
    20.  
    21.    movf PORTB_buffer, W      ;copy to working register
    22.    movwf PORTB                  ;copy working register to PORTB
    23.  
    24.    movf PORTA_buffer, W      ;copy to working register
    25.    movwf PORTA                  ;copy working register to PORTA
    26.  
    27.     goto    loop
     
    Last edited: Aug 5, 2012
  6. ernestclydeachua

    Thread Starter New Member

    Aug 5, 2012
    16
    0
    how can i assign variables in PORTA_buffer and PORTB_buffer,
    im totally new to assembly

    PORTA_buffer equ h'21'
    i dont know if this is right.
    but i dont think the addlw will work done some reading in the internet
     
    Last edited: Aug 5, 2012
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You already have variables at locations h'21' and h'22' so to add the next ones you would use other locations like h'23' and h'24'

    <ed> Oops, I see what you mean, it should be:

    Code ( (Unknown Language)):
    1. movlw b'00001111'
    2. andwf PORTB_buffer, F    ;clear bits 4-7
    3. movlw b'11110000'
    4. andwf PORTA_buffer, F   ;clear bits 0-3
    </ed>
     
    Last edited: Aug 5, 2012
  8. ernestclydeachua

    Thread Starter New Member

    Aug 5, 2012
    16
    0
    cant compile using mplab
     
  9. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    OK, there should be a box which has errors, messages and warnings.
    If you are using MPLab 8 you should be able to double click on the error and it will take you to the offending line. I'm not sure what MPLab X does.
    If that's no help, let us know what the error message was.
     
  10. ernestclydeachua

    Thread Starter New Member

    Aug 5, 2012
    16
    0
    i found another way and solved my problem :D
    btw thanks for helping me.
     
Loading...