SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F1507
PIC16F1507 to micro:bit communication, SPI, Assembly
Hi all! First time on any forum anywhere so go really hard on me so I learn how to do this asap :) Thanks.
I have sending and receiving single bytes working in IOC so understand the principles at least.
Trying to write a subroutine which uses polling to sense the SCK state and sends bits on MISO accordingly.
I can send bytes - but they are right shifted by 1 bit - so H would become 00100100 from 01001000. As this appears to be true of all bytes in the stream, it seems to indicate that a 0 is being sent as my most significant bit, prior to the rest of the stream - right shifting every byte - OR a 0 is prepended to every byte and I am only sending 7 bits of my own ... Is there an obvious error in my subroutine that might cause such an error?
MOVLW 0x41 ; load A,B,C into STRING_ADDR,  AND 
MOVLW d'4' ; move 4 into the byte counter
MOVLW STRING_ADDR ; (1) this is the base address for the string array (0x30 - GPR BANK0 on PIC16F1507)
MOVWF FSR0 ; (1) give the pointer the array base address
MOVLW d'8' ; (1) load 8 (for the bits of the byte)
MOVWF bit_counter ; (1) into bit counter
MOVIW FSR0++ ; (1) move the contents of the indirect address to w and increment the pointer afterwards
MOVWF bits_out ; (1) put W in bits_out for TX
SCK_HIGH: ; SPI reads on the positive edge of SCK wave so load when HIGH
BTFSC SCK ; (1/2) skip if LOW - SCK is PORTB,4
GOTO SCK_HIGH ; (2) loop again till low
BTFSS SCK ; (1/2) skip if HIGH
GOTO SCK_LOW ; (2) loop again till high
; drive the data line before the next rising edge of SCK
BCF MISO ; (1) set MISO LOW (master in slave out - PORTB, 5)
BTFSC bits_out, 7 ; (1/2) check sig bit of output reg
BSF MISO ; (1) set MISO HIGH
LSLF bits_out ; (1) left shift output reg
DECFSZ bit_counter, 1 ; (1/2) decrement the bit counter for this byte till 0, then skip
GOTO SCK_HIGH ; (2) goto SCK loops and get the next bit
DECFSZ byte_counter, 1 ; (1/2) decrement the byte counter for the string till 0, then skip
GOTO BYTE_SETUP ; (2) get the next byte
BUG:GOTO BUG ; (infinite!) all the bytes have been sent -- get stuck here
Incidentally, I've tried switching the SCK_LOW and HIGH loops to see if it was an issue with timing, but get the same result - probably as there is space at 16MHz for 16 instructions between each positive edge of the SCK which is driven at 125kHz. Could it be because there are 18 instructions to retrieve a new byte? - if this was true wouldn't the error be compounded? which I do not see.
Thanks in advance for any suggestions on where to check/correct.