Hi guys.

I am a C programmer for Microchip ICs and I need your help. I need to implement software bit-banging and my code is very slow. Can you help me make a faster version with assembler?

Software SPI program works as following
Firstly I have a byte value that I need to send. I look at the 7th bit and set a pin with its value: high or low.  Then I check another pin for incoming data. If that pin is high I set bit 7 of my incoming variable high.

Then I repeat this for bit 6, 5, etc to 0.

In effect I transmit and receive a bit simultaneously, forming a byte later.

During that I also send out clock, but it is not relevant.

The slowest parts of the program are:
accessing a variable to see if a BIT is high or low
and
storing a high or a low bit to another variable.

Each of those takes 50 (!) cycles in C compiler and I think someone here knows how to speed this up!

Can someone who knows assembler show me an example code that will read a bit and write a bit back?

Cheers.
Posted on 2009-05-25 14:22:56 by craze4you
You should try the PicList forums.  If you want to see if a particular bit is high or low then use bit operations. I'm just starting picbasic pro language but I'm sure you can figure out the inline assembly:
x var byte 'in declarations

'some where in code x gets assigned a value
x = $57 ' in binary this is %01010111

'here's what your looking for only in picbasic
y = x AND %00100000 'zero out all bits except for the 5th -- the one I want to see
y = y >> 5 'shift right 5 bits and y = 1
Posted on 2009-05-25 23:37:15 by GoldStar611
instructions to use:
RRF, BCF, BSF

An example, that uses the registerfile-versions of bcf/bsf :

InitCOM:
EnterBank1
bcf trisb,2 ; set RB2 as output
EnterBank0
TXB_Off
return

SendCOM:
char SendCOM_Data ; data to be stored
char SendCom_BitCntr; count of bits left
movwf SendCOM_Data
mov SendCom_BitCntr,8
TXB_On
call _x232BitDelay
;------[ send one bit ]----\
@@:
rrf SendCOM_Data,f
btfss status,C
TXB_On
btfsc status,C
TXB_Off
call _x232BitDelay
decfsz SendCom_BitCntr,f
goto @B
;--------------------------/
TXB_Off
call _x232BitDelay
return


_x232BitDelay:
char x232BD_Count
mov x232BD_Count,24
@@:
nop
decfsz x232BD_Count,f
goto @B
return
;========================================================================/

  endif

endm


TXB_On macro ; for COM.lib
bsf portb,2
endm
TXB_Off macro ; for COM.lib
bcf portb,2
endm

I'm sure there are faster ways, though.
Yet, all PICs have dedicated hardware to do SPI.
Posted on 2009-05-26 14:09:04 by Ultrano