Assembly language help for PIC16F887

Discussion in 'Programmer's Corner' started by zhengkoon8, Nov 28, 2011.

  1. zhengkoon8

    Thread Starter New Member

    Nov 28, 2011
    10
    0
    Below is the language for my code. but i unable to on the led. so anyone can help me to solve this problem? thanks





    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4. #include "prologue.inc"
    5.  
    6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    7. ; Declaring Variable used
    8.      cblock     0x20
    9. Value                                    ; Assign an address 0x20 to label Value
    10. Read                                     ; Assign an address 0x21 to label Read
    11. Value1                                   ; Assign an address 0x22 to label Value1
    12. Read1                                    ; Assign an address 0x23 to label Read1
    13. Counter                                  ; Assign an address 0x27 to label Counter
    14. Time                               ; Assign an address 0x27 to label TimerValue
    15.      endc
    16.  
    17. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    18. ; Initialisation
    19. Start:
    20.      bsf       STATUS,RP0                ; select Register Bank 1
    21.      clrf      TRISD                     ; Make PortD all output
    22.      movlw     0x01                      ; Bit one to work register
    23.      movlw     0xFF                      ; b'11111111'
    24.      movwf     TRISA                     ; Make PortA all input
    25.      movwf     TRISB                     ; Make RBO pin input (switch)
    26.      bsf       STATUS,RP1                ; select Register Bank 3
    27.      movlw     0x00                      ; move b'00000000' to work register
    28.      movwf     ANSELH                    ; PortB pins are digitial (important as RB0 is switch)
    29.      bcf       STATUS,RP0                ; select Register Bank 2
    30.      bcf       STATUS,RP1                ; select Register Bank 0
    31.      clrf      PORTD                     ; clear value in PORTD
    32.      clrf      Counter                   ; clear value in Counter
    33.  
    34. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    35. MainLoop:
    36. FirstValue:
    37.      incf      Counter                   ; increase counter value
    38.      btfsc     PORTB,0                   ; test whether switch is press
    39.      goto      FirstValue                ; switch not press go back to the beginning of FirstValue
    40.      movf      Counter,w                 ; move value in counter to work register
    41.      movwf     Value                     ; move value in work register to Value register
    42.      movf      Value,w                   ; move value in Value to work register
    43.      movwf     PORTD                     ; move value in work register to PORTD register
    44.      movf      PORTD,w                   ; move value in PORTD to work register
    45.      movwf     Value                     ; move value in work register to Value register
    46.      call      DelayOneSecond            ; delay 1 second
    47.      movlw     0x00                      ; move b'00000000' into work register
    48.      movwf     PORTD                     ; move value in work register to PORTD register
    49. SecondValue:
    50.      incf      Counter                   ; increase counter value
    51.      btfsc     PORTB,0                   ; test whether switch is press
    52.      goto      SecondValue               ; switch not press go back to the beginning of SecondValue
    53.      movf      Counter,w                 ; move value in counter to work register
    54.      movwf     Read                      ; move value in work register to Read register
    55.      movf      Read,w                    ; move value in Read to work register
    56.      movwf     PORTD                     ; move value in work register to PORTD register
    57.      movf      PORTD,w                   ; move value in PORTD to work register
    58.      movwf     Read                      ; move value in work register to Read register
    59.      call      DelayOneSecond            ; delay 1 second
    60.      movlw     0x000                     ; move b'00000000' into work register
    61.      movwf     PORTD                     ; move value in work register to PORTD register
    62. Sum:
    63.      btfsc     PORTB,0                   ; test whether switch is press
    64.      goto      Sum                       ; switch not press go back to the beginning of Sum
    65.      movf      Value,w                   ; move value in Value register to work register
    66.      addwf     Read,0                    ; add value in work register with value in Read register
    67.      movwf     PORTD                     ; move value in work register to PORTD register
    68.      call      DelayOneSecond            ; delay 1 second
    69.      movlw     0x000                     ; move b'00000000' into work register
    70.      movwf     PORTD                     ; move value in work register to PORTD register
    71. ThirdValue:
    72.      incf      Counter                   ; increase counter value
    73.      btfsc     PORTB,0                   ; test whether switch is press
    74.      goto      ThirdValue                ; switch not press go back to the beginning of ThirdValue
    75.      movf      Counter,w                 ; move value in counter to work register
    76.      movwf     Value1                    ; move value in work register to Value1 register
    77.      movf      Value1,w                  ; move value in Value1 to work register
    78.      movwf     PORTD                     ; move value in work register to PORTD register
    79.      movf      PORTD,w                   ; move value in PORTD to work register
    80.      movwf     Value1                    ; move value in work register to Value1 register
    81.      call      DelayOneSecond            ; delay 1 second
    82.      movlw     0x000                     ; move b'00000000' into work register
    83.      movwf     PORTD                     ; move value in work register to PORTD register
    84. FourthValue:
    85.      incf      Counter                   ; increase counter value
    86.      btfsc     PORTB,0                   ; test whether switch is press
    87.      goto      FourthValue               ; switch not press go back to the beginning of FourthValue
    88.      movf      Counter,w                 ; move value in counter to work register
    89.      movwf     Read1                     ; move value in work register to Read1 register
    90.      movf      Read1,w                   ; move value in Read1 to work register
    91.      movwf     PORTD                     ; move value in work register to PORTD register
    92.      movf      PORTD,w                   ; move value in PORTD to work register
    93.      movwf     Read1                     ; move value in work register to Read1 register
    94.      call      DelayOneSecond            ; delay 1 second
    95.      movlw     0x000                     ; move b'00000000' into work register
    96.      movwf     PORTD                     ; move value in work register to PORTD register
    97. Substract:
    98.      btfsc     PORTB,0                   ; test whether switch is press
    99.      goto      Substract                 ; switch not press go back to the beginning of Substract
    100.      movf      Read1,w                   ; move value in Read register to work register
    101.      subwf     Value1,0                  ; subtract value in Value1 register with value in work register
    102.      movwf     PORTD                     ; move value in work register to PORTD register
    103.      call      DelayOneSecond            ; delay 1 second
    104.      movlw     0x000                     ; move b'00000000' into work register
    105.      movwf     PORTD                     ; move value in work register to PORTD register
    106.      goto      MainLoop                  ; go back to main loop
    107.  
    108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    109. ; Function Delays for One Second
    110. DelayOneSecond:
    111.      clrf      Time                ; clear value in TimerValue register
    112. WaitOneSecond:
    113.      clrf      TMR0                      ; clear Timer0
    114. WaitTMR0:
    115.      movf      TMR0,w                    ; move value in TMR0 register to work register
    116.      xorlw     .250                      ; wait for 4ms (250x16us)
    117.      btfss     STATUS,Z                  ; check whether zero present in STATUS
    118.      goto      WaitTMR0                  ; loop back to WaitTMR0
    119.      incf      Time,f                    ; increase the value of Counter register by 1 and store in TimerValue register
    120.      movlw     .250                      ; wait for 1s (250x4ms)
    121.      xorwf     Time,w                    ; move value in TimerValue register to work register
    122.      btfss     STATUS,Z                  ; check whether zero present in STATUS
    123.      goto      WaitOneSecond             ; loop back to WaitOneSecond
    124.      return                              ; return back to the next instruction after function call
    125. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    126.      end
    127.  
    128.  
     
    Last edited by a moderator: Nov 28, 2011
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I can't see anything obvious.
    What is the configuration word set to?
    What are the chip connections?
    If you add the code:
    Code ( (Unknown Language)):
    1. movlw b'10101010'
    2. movwf PORTD
    3. goto MainLoop
    directly after mainloop, can you measure the correct voltages on PORTD?
     
  3. zhengkoon8

    Thread Starter New Member

    Nov 28, 2011
    10
    0
    Actually this is my task

    Description: Design a simple calculator that performs addition and subtraction.

    Just that I just simply copy the value from counter to be my first random value and do the same things for the second random value. Then add both of them together. Then I do the same thing to the subtract part. and lastly loop back to the beginning to do back the main loop again.

    the configuration is

    Code ( (Unknown Language)):
    1.  
    2. #include <P16F887.INC>
    3.     __CONFIG     _CONFIG1, _INTOSCIO & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF & _LVP_OFF & _DEBUG_ON
    4.  
    5. what do u mean by Chip connection?
    6.  
    7. and i have added the code to try out already. it only does not work after
    8.  
    9. WaitTMR0:      
    10. movf      TMR0,w                    ; move value in TMR0 register to work register      
    11. xorlw     .250                      ; wait for 4ms (250x16us)      
    12. btfss     STATUS,Z                  ; check whether zero present in STATUS      
    13. goto      WaitTMR0                  ; loop back to WaitTMR0
    14. [COLOR=Red]incf      Time,f                    ; increase the value of Counter register by 1 and store in TimerValue register[/COLOR]      
    15. movlw     .250                      ; wait for 1s (250x4ms)      
    16. xorwf     Time,w                    ; move value in TimerValue register to work register      
    17. btfss     STATUS,Z                  ; check whether zero present in STATUS
    18. goto      WaitOneSecond             ; loop back to WaitOneSecond      
    19. return                              ; return back to the next instruction after function call
    20.  
    21.  


    And thanks for the reply. I am very appreciate it.
     
    Last edited by a moderator: Nov 29, 2011
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    OK, I can't see OPTION_REG being set up anywhere. TMR0 won't be running.
    Also if you are going to check TMR0 in a loop, you either need to set the prescaler so that it increments less often than the loop takes, or use a subtract operation and check for a carry, or preload TMR0 with a value then check the TMR0 interrupt flag.
    _MCLRE_ON is defined, have you got the MCLR pin pulled up to +V with a resistor?
     
  5. zhengkoon8

    Thread Starter New Member

    Nov 28, 2011
    10
    0
    Thank you very much. It works already after I added the OPTION_REG. Thanks for it. There is not hardware to added. It is purely software based programming. :)
     
Loading...