• AVR Freaks

AnsweredHot!SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F1507

Author
jellyBean
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/11/19 16:33:50
  • Location: 0
  • Status: offline
2019/11/21 02:35:06 (permalink)
0

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?
 
SEND_ABC:
MOVLW 0x41                          ; load A,B,C into STRING_ADDR[0], [1] AND [2]
MOVWF STRING_ADDR
MOVLW 0x42
MOVWF STRING_ADDR+1
MOVLW 0x43
MOVWF STRING_ADDR+2
MOVLW 0x00
MOVWF STRING_ADDR+3
MOVLW d'4'                            ; move 4 into the byte counter
MOVWF 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

BYTE_SETUP:
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
SCK_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.
#1
ric
Super Member
  • Total Posts : 24581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/21 14:43:02 (permalink) ☼ Best Answerby jellyBean 2019/11/22 02:25:48
+2 (2)
jellyBean
I have sending and receiving single bytes working in IOC so understand the principles at least.

In this context, what does "IOC" mean?
(It's always dangerous to throw around acronyms if it's not crystal clear what they actually mean.)
 
I was going to ask "why not use the MSSP peripheral?" but you seem to have found one of the very few PICs that doesn't have one.
 
I don't know which of the four possible SPI modes your micro:bit use, but plainly you're sending the data too late.
Try swapping the data out and clock in sections of your code like this:
BIT_LOOP:
;output the next data bit
    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
;wait for the next clock cycle
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
SCK_LOW:
    BTFSS SCK                      ; (1/2) skip if HIGH
    GOTO SCK_LOW              ; (2) loop again till high
    DECFSZ bit_counter, 1     ; (1/2) decrement the bit counter for this byte till 0, then skip
    GOTO BIT_LOOP             ; (2) get the next bit

 
post edited by ric - 2019/11/21 14:44:05

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
markm23
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/11/21 12:51:59
  • Location: 0
  • Status: offline
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/21 15:59:15 (permalink) ☄ Helpfulby jellyBean 2019/11/23 05:20:14
0
Can you slow the SPI clock down (or run the PIC clock faster) and see if it helps? The PIC may be just fast enough overall, but fall behind when there's more work to do at the first or last bit. This may be the easiest thing to check.
 
Or it might be a mismatch in the clock phase and polarity. SPI gives 4 choices, and if the sender and receiver don't match, the first clock may act before the first bit is there, or vice-versa, shifting a zero bit in at one end and losing the data bit at the other end. Do you have a 2-channel scope and 2 probes? If you send 0x55 data, the clock transitions should occur where the bits are stable - and you need to know which clock transition captures the bit.  
 
 
#3
jellyBean
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/11/19 16:33:50
  • Location: 0
  • Status: offline
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/22 02:18:47 (permalink)
0
@ric: I agree entirely and thank-you for your solution!
 
Strangely, I had already tried what you suggested, as it was the logical thing to do - but when I did, the error was unchanging. Having retried it on your advice - the output is now as expected. My suspicion is that I had a second unknown error when I first tried (which I have since [unknowingly?] resolved) - and then made the very poor assumption that I had already tested your suggestion and there was no reason to try again!
 
Thank you also for your advice on the use of acronyms. In my case it was Interrupt On Change - which can also be used to implement this type of interface. I will try to be more precise and explicit in future!
 
 
#4
jellyBean
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/11/19 16:33:50
  • Location: 0
  • Status: offline
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/22 02:23:44 (permalink)
+1 (1)
@ric I won't excuse this laziness in not searching for the answer myself, but how did you add your code in the scrolling box please?
 
#5
ric
Super Member
  • Total Posts : 24581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/22 02:25:39 (permalink) ☄ Helpfulby jellyBean 2019/11/23 05:21:50
+2 (2)
put "code" tags around it.
[/code] after, and
[code] before.
 
I described them in reverse order so they would actually show in this post.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
jellyBean
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/11/19 16:33:50
  • Location: 0
  • Status: offline
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/22 02:28:16 (permalink)
+2 (2)
Excellent. Much obliged.
 
#7
jellyBean
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/11/19 16:33:50
  • Location: 0
  • Status: offline
Re: SPI polling in Assembly - Sending a string but string/bytes is right-shifted PIC16F150 2019/11/23 05:19:56 (permalink)
0
@markm23
Grateful for all the suggestions. Thanks. 
#8
Jump to:
© 2019 APG vNext Commercial Version 4.5