PDA

View Full Version : ASM cycles in PICBASIC



breeno
- 7th July 2006, 08:54
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!!!

paul borgmeier
- 8th July 2006, 07:15
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??



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

Bruce
- 9th July 2006, 18:25
Here's one version using a modified PBP library routine & macro;


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.

Darrel Taylor
- 9th July 2006, 20:02
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...
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).

breeno
- 10th July 2006, 08:59
Thank you guys!!!
I have tried a mix of your codes and it works....

Thank you again!

Breeno