ASM cycles in PICBASIC


Closed Thread
Results 1 to 5 of 5
  1. #1
    breeno's Avatar
    breeno Guest

    Default ASM cycles in PICBASIC

    Hello guys...i am a novice in picbasic
    Just a little question...
    For increasing speed i want to write a simple cyle in ASM:
    i want to read a pin (example PORTB.4) for x times and put the read state in the bits of a variabile (example CODE).
    This is the PICbasic code:

    for i=0 to 7
    x=0
    while x<10
    if in then
    CODE.0[i]=1
    else
    code.0[i]=0
    endif
    x=x+1
    wend
    next i

    This is the ASM idea:

    for i=0 to 7
    ASM
    movlw 0 ;
    movwf _x ;x=0
    movlw 10
    movwf _y ;y=10
    TEST
    btfss PORTB, 4
    goto ZERO
    goto UNO
    ZERO
    bcf _CODE,_i
    goto INC
    UNO
    bsf _CODE,_i
    INC
    incf _x,1 ;x=x+1
    movf _x,W ;x->w
    sublw 10 ;w=10-w
    subwf _y,1 ;y=y-w
    decfsz _y,0 ;w=y-1
    goto TEST
    endasm
    next i

    I know the problem is that the "bsf _CODE,_i" and "bcf _CODE, _i" are wrong (8 bit _i variabile respect of 3 bit code).

    The question is: how can i increment the value of the CODE bit with a variabile?

    thank you for help!!!

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by breeno
    I know the problem is that the "bsf _CODE,_i" and "bcf _CODE, _i" are wrong
    I have no idea why anyone would want to do what you are doing. Further, I have not tested your ASM code. See my additions to your ASM code to address the bsf and bcf issues you noted above. Someone else might have a more elegant way to solve this??

    Code:
    BitUpdate = $80	;b10000000	(Added line and variable)
    for i=0 to 7
    ASM 
    	movlw 0 ; 
    	movwf _x ;x=0 
    	movlw 10 
    	movwf _y ;y=10
    	rlf	_BitUpdate, W	;(Added line)
    	rlf	_BitUpdate, F 	;(Added line)
    TEST 
    	btfss PORTB, 4 
    	goto ZERO 
    	goto UNO 
    ZERO 
    	;bcf _CODE,_i 		;(commented out)
    	comf	_BitUpdate,W	;(Added line)
    	andwf	_CODE,F		;(Added line)
    	goto INC 
    UNO 
    	;bsf _CODE,_i 		;(commented out)
    	movf	_BitUpdate,W	;(Added line)
    	iorwf	_CODE,F		;(Added line)
    INC 
    	incf _x,1 ;x=x+1 
    	movf _x,W ;x->w 
    	sublw 10 ;w=10-w 
    	subwf _y,1 ;y=y-w 
    	decfsz _y,0 ;w=y-1 
    	goto TEST 
    	
    ENDASM
    next i
    Good Luck,
    Paul Borgmeier
    Salt Lake City, Utah
    USA

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Here's one version using a modified PBP library routine & macro;
    Code:
    Result VAR BYTE BANK0 SYSTEM
    I      VAR BYTE BANK0 SYSTEM
    Temp   VAR BYTE BANK0 SYSTEM
    X      VAR BYTE
    
        TRISB.4 = 1
        Result = 0
        
    Main:
        for I = 0 to 7       ' loop x 8
          X = 0
          while X < 10       ' Test pin x 10
            if PORTB.4 = 1 then
             call BitSet     ; Result.0[I] = 1
            else            
             call BitClear   ; Result.0[I] = 0
            endif
            X = X + 1 
          wend
        next I
    
    Done:
        goto   Done
    	
    BitClear:
    ASM
        call   ConvertBit  ; Bitmask -> W
        movwf  Temp        ; W -> Temp
        comf   Temp,F      ; Invert Temp
        movf   Temp,W      ; Temp -> W
        andwf  Result,F    ; AND W with Result
        return
    ENDASM
    
    BitSet:
    ASM
        call   ConvertBit  ; Bitmask -> W
        iorwf  Result,F    ; OR W with Result
        return
        
    ;******************************************************
    ;* Input      : I = bit (0 - 7)                       *
    ;* Output     : W = bit mask                          *
    ;* Notes      : Modified example from PBP library     *
    ;******************************************************
    ConvertBit              ; See pbppic14.lib CONVBIT & TABLE?C macro
        movlw   btable >> 8 ; Get high address of jump table
        movwf   PCLATH      ; Set program counter latch high
        movf    I,W         ; Get bit number from I into W reg
        andlw   07h         ; Isolate lower 3 bits (only works with 0-7)
        TABLE?C 08h         ; PBP library macro for table alignment
    btable                  ; Returns from table with bit mask in W reg
        retlw   01h         ; %00000001
        retlw   02h         ; %00000010
        retlw   04h         ; %00000100
        retlw   08h         ; %00001000
        retlw   10h         ; %00010000
        retlw   20h         ; %00100000
        retlw   40h         ; %01000000
        retlw   80h         ; %10000000
    ENDASM
    
        END
    You can learn a good deal about assembly language by reading through the
    PBP library.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    breeno,

    In the original example (basic version), the while loops 10 times, reading the pin and overwriting the same bit in the CODE variable. It's no different than just reading it once (other than time used).

    So, with that in mind, here's another possibility...
    Code:
    ASM
       MOVE?CB  8, _i
    IN_Loop
       MOVE?TT  _in, STATUS, C
       CHK?RP   _CODE
       rrf      _CODE, F
       CHK?RP   _i
       decfsz   _i, F
       goto     IN_Loop
    ENDASM
    It just reads the pin, put's the value in the Carry flag, then rotates it into the CODE variable (8 times).
    DT

  5. #5
    breeno's Avatar
    breeno Guest


    Did you find this post helpful? Yes | No

    Talking

    Thank you guys!!!
    I have tried a mix of your codes and it works....

    Thank you again!

    Breeno

Similar Threads

  1. Problem using ASM instruction
    By ewandeur in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd April 2008, 15:26
  2. asm e picbasic
    By chip15 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 5th April 2006, 16:03
  3. Replies: 22
    Last Post: - 12th July 2005, 17:39
  4. Can an asm interrupt contain gosub to a picbasic routine?
    By sougata in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 21st April 2005, 19:49
  5. asm to picbasic
    By wilfried in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 12th March 2003, 01:18

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts