Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Is the two codes the same?pls have a look

Status
Not open for further replies.

ryan_ryan

New Member
Code:
         clrf PORTB           ; start with zero
loop:
         btfsc PORTB, 4       ; switch closed, (gives 0)?
         goto loop            ; not yet
                              ; switch has been detected closed
         incf PORTB, f        ; add 1 to port B
                              ; wait a while to make sure switch has 
                              ; settled  (debounce closure)
         movlw D'10'          ; wait about 10 msec
         call nmsec
                              ; now wait for release 
         btfss PORTB, 4       ; will be high (1) when released
         goto $ -1            ; still low
                              ; now must wait a make sure bouncing stopped
         movlw D'10'          ; 10 milliseconds
         call nmsec
                              ; and check again
         btfss PORTB, 4       ; if set, still released
         goto $ -5            ; still low start release wait all over

         goto loop            ; loop forever

Instead of using the goto $-2 and goto $-5 , i used labels instead, so just want to know if the -2 is to step back in number of cycles? like -2 means go back 2 lines?? anyway i tried it in another approach, just wanna know if they are doing the same thing still.thanks

Code:
         clrf PORTB           ; start with zero
loop:
         btfsc PORTB, 4       ; switch closed, (gives 0)?
         goto loop            ; not yet
                              ; switch has been detected closed
         incf PORTB, f        ; add 1 to port B
                              ; wait a while to make sure switch has 
                              ; settled  (debounce closure)
         movlw D'10'          ; wait about 10 msec
         call nmsec
                              ; now wait for release 
 chk     btfss PORTB, 4       ; will be high (1) when released
         goto  chk            ; still low
                              ; now must wait a make sure bouncing stopped
         movlw D'10'          ; 10 milliseconds
         call nmsec
                              ; and check again
         btfss PORTB, 4       ; if set, still released
         goto  chk                ; still low start release wait all over

         goto loop            ; loop forever


if my code is wrong, what shd i do to make the 2 codes the same.Thanks..
 
Yes, they are the same! - the advantage of the $ approach is that you don't need a label, so it makes the routine more reusable - as there's no external jump to those addresses anyway, they don't actually 'need' a label.

Don't forget, the PIC knows nothing about labels at all, the actual code assembles down to a simple address jump - jump to location xxxx. The labels and $ are simply assembler instructions, nothing PIC at all really?.

If you ever want to check the code does actually compile the same?, simply assemble both versions and compare the HEX files, they should be identical. You could always blow one into a PIC, and verify against the other HEX file as well (saves straining to check the files against each other).
 
Status
Not open for further replies.

Latest threads

Back
Top