trouble with pic assembler codes

Discussion in 'Embedded Systems and Microcontrollers' started by mantsali, Oct 21, 2009.

  1. mantsali

    Thread Starter New Member

    Oct 20, 2009
    7
    0
    im programming a pic 16f627 to recieve input from an ultrasonic reciever and to count the number of times it changes its state from high to low. depending on how high or low the counter is, i can determine how far from an obstacle it is. this is all done in a short delay of 0.5s to determine how many times a change occured every o.5s. but the problem arises when i take the final value of my third counter and subtract it from, what i believe should be, a higher value even though i have tried smaller value so that a carry occurs and the status bit c changes to 1. the change in the status bit should set my portb 3 but that never occurs could anyone tell where i went wrong, is it my thinking or my code



    Code ( (Unknown Language)):
    1.  
    2.  
    3. ;Sekoli, 3 september 2009
    4.  
    5.     cnt1    equ h'20'
    6.     cnt2    equ h'21'
    7.     valx    equ h'22'
    8.     valy    equ h'23'
    9.     valz    equ h'24'
    10.     cnt3    equ h'25'
    11.  
    12.     org h'00'
    13.     goto    init
    14.  
    15.     org h'04'
    16.     retfie
    17.  
    18. init:
    19.     movlw   h'07'
    20.     movwf   cmcon
    21.     bsf status,5
    22.     ;1=input 2=output
    23.     movlw   b'11000000' ;set porta 6,7 as input and the rest as output
    24.     movwf   trisa
    25.     movlw   b'00000000' ;set portb to output
    26.     movwf   trisb
    27.     bcf status,5
    28.  
    29. main:  
    30.    
    31.            
    32.     clrf    cnt1        ;clear counters
    33.     clrf    cnt2
    34.     clrf    cnt3
    35.  
    36.     call    delay2;
    37.  
    38.    
    39.     bcf status,c
    40.     movlw   d'100'      ;test value higher than cnt3
    41.     sublw   cnt3        ;at a certain distance
    42.     btfsc   status,c    ;check for carry
    43.     goto    set     ;if carry true set portb,3
    44.  
    45.     movlw   d'10'       ;test value lower than cnt3
    46.     sublw   cnt3        ;at a certain distance
    47.     btfsc   status,c    ;check for carry
    48.     goto    set     ;if carry true set portb,3
    49.  
    50.     goto    clear       ;else clear portb,3
    51.  
    52. ;well all i keep geting is a low bit from portb,3  
    53.  
    54. ;   movlw   cnt3        ;tests final values from rx
    55. ;   movwf   portb       ;and displays them in binary on portb
    56.  
    57.     goto    main
    58.  
    59. set:
    60.     bsf portb,3
    61.     nop
    62.     nop
    63.     nop
    64.     nop
    65.     nop
    66.     nop
    67.     nop
    68.     nop
    69.     nop
    70.     goto    main
    71. clear:
    72.     bcf portb,3
    73.     goto    main
    74.  
    75. theCount:
    76.         incf    cnt1,1     
    77.         movlw   d'255'      ;sub max value from cnt1
    78.         subwf   cnt1,0      ;if cnt1 reached max val
    79.         btfsc   status,z   
    80.         incf    cnt2,1      ;inc cnt2  
    81.         movlw   d'255'      ;do the same as for cnt1 with cnt2
    82.         subwf   cnt2,0
    83.         btfsc   status,z
    84.         incf    cnt3,1      ;if cnt2 max val reached inc cnt3
    85.         return
    86.  
    87. ;0.5s
    88. delay2:
    89.  
    90.     movlw   d'3'
    91.     movwf   valx
    92.  
    93. loopx:
    94.     movlw   d'216'
    95.     movwf   valy
    96.  
    97. loopy:
    98.     movlw   d'255'
    99.     movwf   valz
    100.  
    101. loopz:
    102.     btfss   porta,7     ;is rx high
    103.     call    theCount    ;no, inc counters
    104.    
    105.     decfsz  valz,1
    106.     goto    loopz
    107.     decfsz  valy,1
    108.     goto    loopy
    109.     decfsz  valx,1
    110.     goto    loopx
    111.     return
    112.  
    113.  
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I haven't looked at all the code but I spotted a couple of things, there is no need to ever do bcf status, c because the bit always gives the result for the previous instruction.
    It doesen't affect the operation but is unnecessary.
    The other thing is that the carry bit in status is clear if there is a carry and set if there is not a carry, the opposite of what happens in an add. I've no idea why that is and it got me too.
     
    Last edited: Oct 21, 2009
  3. mantsali

    Thread Starter New Member

    Oct 20, 2009
    7
    0
    thanks for tip. going to implement it today
     
  4. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,645
    759
    In PICs from the 16F and 18F family, the C (carry) bit in the STATUS register will be set (=1) if the last operation generated a carry.
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Code ( (Unknown Language)):
    1.  
    2. Example 1: SUBLW 0x02
    3. Before Instruction
    4. W = 1
    5. C = ?
    6. After Instruction
    7. W = 1
    8. C = 1; result is positive
    9. Example 2: Before Instruction
    10. W = 2
    11. C = ?
    12. After Instruction
    13. W = 0
    14. C = 1; result is zero
    15. Example 3: Before Instruction
    16. W = 3
    17. C = ?
    18. After Instruction
    19. W = 0xFF
    20. C = 0; result is negative
    21.  
    My .02
     
Loading...