Electronic – SPI on PIC24H not progressing

picspi

I am trying to setup the SPI1 port on my PIC24HJ206GP256 micro. I have set the SPI1CON1 control register with 0x0020, the SPI1STAT control register with 0x8000 (i.e. turn on the SPIEN), and SPI1CON2 = 0.

At the moment a simple write to the SPI1BUF is implemented like this:

 void writeSPI1(char data){
     _SPI1IF = 0;             //set interrupt flag to zero
     SPI1BUF = data;          //write data to SPI1BUF
     while( _SPI1IF == 0);    //wait to flag to go high
 }

When the data is written, the SPI1IF should go high, but it never reaches this point so the program simply remains in the loop forever. So it seems no data will ever be clocked out of the SPI port through MOSI.

I have tried to make it work by setting TRISF = 0x0000, although I have read that the SPI port should take care of SPI pin configuration.

I am using a watch window to view the contents of the SPI1BUF.

I have tried with different settings within the control registers but have had no luck. Also have tried enabling the SPI1IF (IEC0: Interrupt Enable Control Register 0) and still the problem persists.

Are there any extra configurations I have forgotten, like some odd functionality of PORTF which takes precedence over the SPI port?

Best Answer

Use the SPI peripheral library, it'll make things much easier.

Check the data sheet for the alternative functions of the pins you are using. If they are shared with the ADC that will be the default function and you will need to enable them for digital I/O.

You should use the Microchip forums for this type of question, many more people there will be using the 16-bit PICs.