title: PIC32 SPI not working
body: <p>I'm trying to get the SPI working on a PIC32MX250F128D without much luck.</p>
  
  <p>I tried in 8 bit mode and 32 bit mode, but I'm not getting all the data or no data at all.</p>
  
  <p>I'm trying to use a 4MHz SPI to drive a WS2812 ledstrip.</p>
  
  <p>This is the code I have:</p>
  
  <pre><code>#include &lt;xc.h&gt;
  #include &lt;peripheral/system.h&gt;
  #include "config.h"
  
  void settings( void );
  char Send_SPI(unsigned char);
  
  int main( void ) {
      int i, j;
      unsigned char scrapdata;
      unsigned char buffer[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0x01};
  
      settings();
  
      while(1) {
          Send_SPI(buffer[j]);
          j = (j &gt; 4)? 0 : j++;
          for(i = 0; i &lt; 300; i++);
      }
  
      return 0;
  }
  
  void settings( void ) {
      SYSTEMConfigPerformance(SYS_FREQ);
      TRISB &amp;= 0xFFFB;
      RPB2R = 0x3;
  
      SPI1STAT = 0;
      SPI1CON = 0;
      unsigned int rData = SPI1BUF;
  
      // =&gt; 4MHz = 48MHz / (2 * (5 + 1))
      SPI1BRG = 5;
      SPI1STATCLR = 0x40; // clear the Overflow
      SPI1CON = 0x00008230;
  }
  
  char Send_SPI(unsigned char buffer) {
      //while(!(SPI1STAT &amp; 0x2));
      SPI1BUF = buffer;
      while(!(SPI1STAT &amp; 0x1)); // wait transfer complete 
      char scrapdata = SPI1BUF; //read to clear SPI2BUF before reload
      return scrapdata;
  }
  </code></pre>
  
  <p>At the moment there is nothing coming out of the controller.
  Does anyone of you know what's wrong?</p>
  
  <p>Laurens</p>