SPI - SPI1_Write() infinite loop.

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
amulder17
Posts: 2
Joined: 30 Jun 2022 18:35

SPI - SPI1_Write() infinite loop.

#1 Post by amulder17 » 30 Jun 2022 18:48

Hi, I am just starting with mikroc using a P18F47J53 and I am running into an issue trying to operate the spi library. I am trying to just send a 1 over the spi port; however when I enter into SPI1_Write() I end up in an infinite loop
I went through debugging and confirmed data is being written to SSP1BUF but the buffer full bit in SSP1STAT is not being set. Any insight to why this is happening would be much appreciated
Attachments
spi debug2.PNG
spi debug2.PNG (15.56 KiB) Viewed 575 times
spi debug.PNG
spi debug.PNG (8.63 KiB) Viewed 575 times

Bill Legge
Posts: 235
Joined: 28 Oct 2007 03:16
Location: West Australia

Re: SPI - SPI1_Write() infinite loop.

#2 Post by Bill Legge » 02 Jul 2022 11:27

I've just spent a few days brushing up on SPI comms - (see my latest in the 'USER PROJECTS' forum at the bottom of the page.
I don't quite undersand your problem but some working code follows

Code: Select all

////////////////////////////////////////////////////////////////////////////////
#define  HEARTBEAT      LATA0_bit                                             //
#define  HEARTBEAT_DIR  TRISA0_bit                                            //
#define  SLAVE_DRIVE    LATC0_bit                                             //
#define  SLAVE_DIR      TRISC0_bit                                            //
////////////////////////////////////////////////////////////////////////////////

void main() {
    // Local variables
    char                 x = 0;
    char                 y = 0;
    signed int signed_data = 0;
    int               loop = 0;
    signed int     payload = 5;
    char    my_string[] = "               ";
    // Init PORTS and TRIS
    HEARTBEAT_DIR = 0;
    SLAVE_DIR     = 0;
    TRISC5_bit    = 0;           // output for SDO1
    TRISC4_bit    = 1;           // input for SDI1
    TRISC3_bit    = 0;           // output for SCK1
    TRISF7_bit    = 1;           // input for SS1 active low
    // Init SPI1 with SDI1/RC4 SDO1/RC5 SCK1/RC3 SS1/RF7
    // Clock speed = 40/64 = 625kHz measure at 520kHz
    // Data sampled in the middle of the clock period
    // Clock idles low and goes high
    // Data is transmitted when clock goes from low to high
    // Settling time for peripheral
    SPI1_Init_Advanced(\
    _SPI_MASTER_OSC_DIV64,\
    _SPI_DATA_SAMPLE_MIDDLE,\
    _SPI_CLK_IDLE_LOW,\
    _SPI_LOW_2_HIGH);
    delay_ms(1);
   
    while (1) {
        // Send SPI message: payload as signed int then two bytes per signed  word         
        Send_Word(payload);    // payload = 5 so 2 + 2*5 = 12 bytes
        for(x=0;x<payload;x++){
            Send_Word(signed_data);
            signed_data++;
        }
        // Housekeeping
        HEARTBEAT = ~HEARTBEAT;
        delay_ms(400);
     }
}
////////////////////////////////////////////////////////////////////////////////
// Function - Use SPI 8 bit to transmit a signed 16 bit int. Hi byte first    //
void Send_Word(signed int in_word){                                           //
     unsigned short lo_byte = 0;                                              //
     unsigned short hi_byte = 0;                                              //
     SLAVE_DRIVE = 0;                                                         //
     delay_us(10);           // vital guard band                              //
     lo_byte = in_word & 0xFF;                                                //
     hi_byte = (in_word & 0xFF00)>>8;                                         //
     SPI1_write(hi_byte);                                                     //
     SPI1_write(lo_byte);                                                     //
     delay_us(10);           // vital guard band                              //
     SLAVE_DRIVE = 1;                                                         //
     return;                                                                  //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////
It's for a different MCU but may help - you do need to make the appropriate SPI pins inputs or ourputs. Good luck
The Mikro SPI seems to work OK on all the PIC18 and PIC32 that I've used.

Bill Legge

amulder17
Posts: 2
Joined: 30 Jun 2022 18:35

Re: SPI - SPI1_Write() infinite loop.

#3 Post by amulder17 » 06 Jul 2022 13:14

I figured out my issue. I was not aware Mikro had a software only debugger so when I was debugging I was assuming it was doing so on the hardware I had attached. Since SPI uses a hardware flag to determine when data is sent the software debugger was getting stuck because there was no hardware to set the flag to indicate a successful write.

Post Reply

Return to “mikroC PRO for PIC General”