P18F4520 Assembly Exercise

Discussion in 'Homework Help' started by Sparky49, Feb 6, 2014.

  1. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Hi all,

    I thought I would post here, as this is related to uni stuff, rather than my own projects.

    This is an exercise I have spent several hours trying to figure out what to do. None of my classmates can figure it out either. Any guidance would be really appreciated.

    We were tasked to program a PIC 18F4520 in assembly on Proteus. Using just port D, we had to have two toggle switches which would light up a respective output LED. If both switches were closed, both LEDs would illuminate and a buzzer would also sound.

    Here is the schematic I drew up:

    [​IMG]



    I started by drawing out a logic table, from the schematic above, a closed switch would make a low and an open a high. From there I sketched out a rough flowchart, which is below:

    [​IMG]



    Here is my program. I assume it boils down to a misunderstanding about a function.

    Code ( (Unknown Language)):
    1. ;====================================================================
    2. ; Main.asm file generated by New Project wizard
    3. ;
    4. ; Created:   Thu Feb 6 2014
    5. ; Processor: PIC18F4520
    6. ; Compiler:  MPASM (MPLAB)
    7. ;====================================================================
    8.  
    9. ;====================================================================
    10. ; DEFINITIONS
    11. ;====================================================================
    12.         LIST P=18F4520
    13.         #include <P18f4520.INC>                ; Include register definition file
    14.  
    15. ;====================================================================
    16. ; VARIABLES
    17. ;====================================================================
    18.  
    19. ;====================================================================
    20. ; RESET and INTERRUPT VECTORS
    21. ;====================================================================
    22.  
    23.         ORG     0X0000      ;ORG
    24.        
    25.        
    26.         GOTO    MAIN
    27.        
    28. MAIN
    29.  
    30.         MOVLW   0x0F
    31.         MOVWF   TRISD
    32.    
    33.  
    34.    
    35. LOOP    BTFSC   PORTD,0
    36.         GOTO    DEC2
    37.         GOTO    DEC1
    38.    
    39. DEC1
    40.         BTFSC   PORTD,1
    41.         GOTO    LEDAO
    42.         GOTO    NOTO
    43.        
    44. DEC2    BTFSC   PORTD,1
    45.         GOTO    BUZO
    46.         GOTO    LEDBO
    47.        
    48.        
    49. LEDAO   BCF     PORTD,5
    50.         BCF     PORTD,7
    51.         BSF     PORTD,4
    52.         GOTO    LOOP
    53.        
    54. LEDBO   BCF     PORTD,4
    55.         BCF     PORTD,7
    56.         BSF     PORTD,5
    57.         GOTO    LOOP
    58.        
    59. BUZO    BSF     PORTD,4
    60.         BSF     PORTD,5
    61.         BSF     PORTD,7
    62.         GOTO    LOOP
    63.        
    64. NOTO    BCF     PORTD,4
    65.         BCF     PORTD,5
    66.         BCF     PORTD,7
    67.         GOTO    LOOP
    68.        
    69.        
    70. ;====================================================================
    71.       END
    72.  
    Any pointers, or helpful hints would be much appreciated. I've tried not to waffle, but may have missed out some information. If you need any clarification or extra information, just ask. :)

    Regards,

    Sparky
     
    Last edited by a moderator: Feb 7, 2014
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,028
    BUZ0 is entered when both switches are open (reading '1'), turning on the LEDs and buzzer. I think in DEC1 and DEC2, BTFSC should be BTFSS?

    That's all I looked at.
     
    Sparky49 likes this.
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    This work?
    Max.
    Code ( (Unknown Language)):
    1.  
    2.  
    3.     ORG 0X0000 ;ORG
    4.  
    5.  
    6.         goto MAIN
    7.  
    8. MAIN:
    9.  
    10.         movlw 0x0F
    11.         movwf TRISD
    12.  
    13.  
    14.  
    15. Loop:
    16.         btfsc     PORTD, 0
    17.         bsf        PORTD,    4    ;LED1
    18.        
    19.         btfsc    PORTD, 1            
    20.         bsf        PORTD, 5    ;LED2
    21.        
    22.         movlw    0x03
    23.         xorwf    PORTD
    24.         btfsc    STATUS, Z
    25.         bsf        PORTD, 7
    26.         btfss    PORTD, 0
    27.         bcf        PORTD, 4
    28.         btfss    PORTD, 1
    29.         bcf        PORTD, 5
    30.         xorwf    PORTD
    31.         btfsc    STATUS, Z
    32.         goto     Loop
    33.         bcf        PORTD, 7
    34.         goto     Loop
    35.         end
    36.  
     
    Sparky49 likes this.
  4. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Thanks for the replies guys.

    I will have to check tomorrow, as we are not given copies of Proteus ourselves.

    Whilst I appreciate the code Max, I would like to try to get an understanding of it for myself. :)

    Perhaps I could ask about the STATUS, Z?

    What does that mean/do?

    Regards,

    Sparky
     
  5. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    the XORWF does an exclusive OR with a register or other, and the result of the XOR is recorded by the ZERO (or NON-ZERO) result by the Z flag in the STATUS register.
    So a conditional jump can be done by looking at the flag, in this case, Are both inputs,0 & 1 = ON of the PORTD?
    I haven't tested the code, but it should be something to look at and maybe build on?
    Max.
     
    Sparky49 likes this.
  6. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Many thanks Max, very interesting to know.

    I'll give it a whizz and see what happens. :)

    Sparky
     
  7. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,028
    It looks like the BTFSC in LOOP should also be BTFSS - the switch logic looks inverted throughout.

    Also, your BSF/BCF should be on LATx, not PORTx to avoid read-modify-write issues.

    Consider formatting your code like Max shows, its much easier to read and find errors.

    A further way to make code easier to read is to use #defines to specify IO - for example:
    Code ( (Unknown Language)):
    1. #define SW1in_ PORTD,0 ; trailing underscore means its active low
    2. ..
    3. #define BUZZout LATD,7  ; no underscore; 1=buzz
    4. #define LED1out LATD,4  ; 1= LED on
    5.  
    6. then use:
    7.  btfss SW1in_
    8.  goto SW1true ; SW1 is pushed
    9.  goto SW1false ; SW1 is open
    10.  
    11. and:
    12.  bsf BUZZout ; buzzer ON
    13.  bsf LED1out  ; LED on
    Besides being easier to read, if you move something to another IO pin, you just have to change the #define, not pour through the code for all instances of it. It also ensures consistency.

    No comments? Comments in the code are most useful for others reading the code but especially to you while you are writing the code. I usually comment code blocks before coding them. If the block descriptions make sense, all I have to do is code that block to fit the description. I find that many, including myself, don't always go back to fully comment code once its working, making it tough to come back later for a revision or to re-use the code.

    Have fun!
     
    Last edited: Feb 6, 2014
    Sparky49 likes this.
  8. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,503
    380
    hi,

    PORTD.7 never turns ON in post #3 code.

    Also if I read the OP's text, the Input switch pins are active Low, not high as in #3

    E

    Code ( (Unknown Language)):
    1.  
    2. main:
    3.     MOVF PORTD,W
    4.     ANDLW 0x03
    5.     BTFSC STATUS,Z
    6.     GOTO buzz    
    7.     BCF PORTD,7
    8.    
    9.     BTFSC PORTD,0
    10.     BCF PORTD,4
    11.     BTFSS PORTD,0
    12.     BSF PORTD,4
    13.  
    14.     BTFSC PORTD,1
    15.     BCF PORTD,5
    16.     BTFSS PORTD,1
    17.     BSF PORTD,5
    18.    
    19.      GOTO main
    20. buzz:
    21.     BSF PORTD,7
    22.        
    23.         GOTO main
    24.  
     
    Sparky49 likes this.
  9. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    Then the btfsc needs to be changed to btfss.
    Or if the switch logic is changed, it should work.
    Max.
     
    Last edited: Feb 7, 2014
    Sparky49 likes this.
  10. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Thanks guys.

    Yep, I was kicking myself after not putting comments into it. Up to now, the programs have been simple enough, so I added comments after I got a working program. I see now hat it certainly more beneficial to add them whilst programming.

    A couple of questions, why does Eric's program not need to define I/O's?

    And Max, are you saying:

    BTFSC STATUS,Z

    Should be

    BTFSS STATUS, Z

    Regards,

    Sparky

    P.S. Thanks for the sample programs. It is most interesting to see the different ways a program can be set out.
     
  11. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    I made a bit of a slip by not noticing that the switches were active low instead of active high, if the reverse were the case the program should work, I did not re work it for active high.
    You can either predefine a port etc or use the explicit name and bit address, the define makes it a more readable code and easier to modify, if required.


    Give a task to ten different programmers and you will get ten different results, if the program runs, none of them wrong just different methods used.
    Did you get the program to run, if so what was the code result?
    Max.
     
    Sparky49 likes this.
  12. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Unfortunately, no, I haven't managed to test the code.

    Went in on Friday, put my USB stick (with the program in a word document) into the computer. Then I realised it wasn't switched on, so I booted it up, and it tried to boot an OS I had on the USB stick.... -_-

    We're not allowed in on weekends, so I'll try it tomorrow.

    Sparky
     
Loading...