Electronic – How to get a PIC32 SPI port working for transmitting data

cpicpickitspi

I am looking to get a SPI serial port running on a PIC32 (PIC32MX360F). can anyone point me to some good resources on how to do this so that a newbie can figure it out? I am using the PIC32 starter kit and have a PIC32 breakout board from Digi-Key.

This is the code that I have been using… I found it in the PIC32 family manual.

#include <p32xxxx.h>
   int main(void){
        int rData;
        IEC0CLR=0x03800000; // disable all interrupts
        SPI1CON = 0; // Stops and resets the SPI1.
        rData=SPI1BUF; // clears the receive buffer
        IFS0CLR=0x03800000; // clear any existing event
        IPC5CLR=0x1f000000; // clear the priority
        IPC5SET=0x0d000000; // Set IPL=3, Subpriority 1
        IEC0SET=0x03800000; // Enable RX, TX and Error interrupts
        SPI1BRG=207; // use FPB/4 clock frequency
        SPI1STATCLR=0x40; // clear the Overflow
        SPI1CON=0x8220; // SPI ON, 8 bits transfer, SMP=1, Master mode
        // from now on, the device is ready to transmit and receive data
        SPI1BUF='A'; // transmit an A character

        while(1){
            SPI1BUF= 'A'; // transmit an A character
            while(i < 10000){i++;}
            i = 0;
        }
}

How do I set the baudrate? I am guessing it got something to do with SPI1BRG

EDIT:

Thanks to tcrosley I feel that I am getting closer to being able to communicate with my pic32. I changed SPI1BRG to 207. By my calculations this will set the baudrate to around 9600.

Fsck = Fpb/(2(SPIxBRG +1)) = 4MHZ/(2*(207+1))= 9615

I am trying to view the output on the picchip with HyperTerminal. The settings I am using are:

Baud: 9600
Data bits: 8
Parity: None
Stop bits: 1
Flow Control: None

The pins I am using are:

93 - connected to db9 3
95 - connected to db9 2

and db9 5 is connected to the boards ground…

I am now getting an output, which is nice, but unfortunately it is garbage. my output on HyperTerminal comes out as a bunch of hearts :S now I just feel like my pic32 is mocking me

Best Answer

This application note AN1277 describes how to interface a serial SRAM to a PIC32 microcontroller using the C32 Compiler. The page has a link to a zip file containing source code, which should have low level routines for reading and writing data via the SPI interface.

EDIT:

How do I set the baudrate? I am guessing it got something to do with SPI1BRG

I see that you have downloaded the "datasheet" for your processor (datasheet is a bit of a misnomer, it is 632 pages long), so I will be using page references from that.

The equation (on page 397) for the SPI clock frequency is

Fsck = Fpb / (2 * (SPI1BRG+1))

I believe the PIC32MX360F starts out with a internal FRC oscillator of 8 MHz, divided by 2 (due to the FRCDIV default value of 1 in OSCCON register, page 56), and since you are not making a changes in your code to OSCCON, the SYSCLK value will be 4 MHz.

Fpb is the Peripheral Bus Clock frequency. It is set up in PBDIV field of the OSCCON register, which in turn is initialized to the value of the FPBDIV field in the DEVCFG1 (boot configuration register), depicted on page 63. Assuming you haven't changed this, the default POR (power-on reset) value is binary 11, which means the Fpb is SYSCLK divided by 8.

Therefore Fpb = 4 MHz / 8 or 500 KHz.

If SPI1BRG = 1, as in your example, Fsck = Fpb / (2 * (1+1)) = Fpb / 4 = 500 KHz / 4 or 125 KHz.

If your SYSCLK differs from 4 MHz, then scale the Fpb accordingly.

Aren't embedded processors fun?