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