simonbramble
Active Member
I am having a problem with a new PIC micro. In the past I have always coded my own SPI routines. Now I thought I would use the peripheral. The part is the 16F15355:
https://ww1.microchip.com/downloads/en/DeviceDoc/PIC16(L)F15354_55 Data Sheet 40001853C.pdf
I am using PortB as the SPI bus with
RB0 as CS
RB1 as SCLK
RB2 as DIN
RB3 as DOUT
I am sending data fine (it is appearing on the scope), and I can see data coming back on the DI pin. However, the SSP1BUF is empty.
Here is how I am configuring Port B:
The following code sets up the rest of the SPI bus:
I am controlling the CS line just by toggling the port pin manually, so I can do higher than 8 bit transactions in one go.
Here is the SPI function call:
And here is the main code:
As I said, data is appearing on RB2, but the buffer is empty. (I have another port pin that toggles high or low for 1 second as it reads through 'data', MSB first
What am I doing wrong??
If anyone can help, I am all ears.
Thanks
Simon
https://ww1.microchip.com/downloads/en/DeviceDoc/PIC16(L)F15354_55 Data Sheet 40001853C.pdf
I am using PortB as the SPI bus with
RB0 as CS
RB1 as SCLK
RB2 as DIN
RB3 as DOUT
I am sending data fine (it is appearing on the scope), and I can see data coming back on the DI pin. However, the SSP1BUF is empty.
Here is how I am configuring Port B:
Code:
/* PORT B */
TRISB = 0b00000100; /* RB2 = SPI DIN */
PORTB = 0b00000001; /* set CS high */
WPUB = 0b00000100; /* this speed up SPI DI line */
ANSELB = 0x00;
INLVLB = 0x00; /* TTL input logic levels */
SLRCONB = 0b11111111; /* slew rate is ON */
The following code sets up the rest of the SPI bus:
Code:
/* set up SPI 1 */
SSP1STAT = 0b01000000; /* SMP = 0, CKE = 1 */
SSP1CON1 = 0b00101010; /* CKP = 0 */
SSP1ADD = 0x4F; /* baud rate = 100kHz */
//SSP1ADD = 0x13; /* baud rate = 400kHz */
RB1PPS = 0x15; /* RB1 = SPI SCLK */
SSP1DATPPS = 0x0A; /* RB2 = SPI DIN */
RB3PPS = 0x16; /* RB3 = SPI DOUT */
I am controlling the CS line just by toggling the port pin manually, so I can do higher than 8 bit transactions in one go.
Here is the SPI function call:
Code:
void write_spi(unsigned char data) /* write one byte via SPI */
{
SSP1BUF = data;
while(!(checkbit(PIR3, 0)))
{
}
clearbit(PIR3, 0);
}
And here is the main code:
Code:
clearbit(PORTB, CS);
write_spi(0x00); /* write null to SPI */
setbit(PORTB, CS);
data = SSP1BUF;
As I said, data is appearing on RB2, but the buffer is empty. (I have another port pin that toggles high or low for 1 second as it reads through 'data', MSB first
What am I doing wrong??
If anyone can help, I am all ears.
Thanks
Simon