pic24 spi module - Page 1
Forum section: electronics / general

author: Rendon
author_rank: Newbie
timestamp: October 14, 2014, 05:59:25 pm
content: I am using a pic24FG128GA006 and can't seem to transmit data on the spi module. I have set the registers up as per the datasheet, but no joy. The module is enabled. It is set to master. The clock and data out lines are configured as outputs. I load data in the SPIBUF register and nothing happens. I am happy to post my code if need be but I wondered if there is anything I should know? Any insight would greatly be appreciated. I am new to PIC24 (and to SPI). Thanks :-)

author: dannyf
author_rank: Super Contributor
timestamp: October 14, 2014, 07:46:33 pm
content: QuoteYeah: help others help you.You are looking for help so make it as easy as possible for others to help you.

author: hans
author_rank: Super Contributor
timestamp: October 14, 2014, 08:13:28 pm
content: In theory the SPI module should be rather easy and straight forward if you're not going to use any advanced features.Make sure (SPIxCON1):You're not using both primary & secondary prescale 1:1. For example, set one to 1:2 or 1:4, and the other to 1:1 (if you really need the high clock speed).Enable master mode (bit 5 = 1)Select your clock settings (bit 6 and 8 ). You should see some clocks I think, but if they are set wrong you may lose your first or last bit.I would set data sampling at middle if your clock speed allows itMake sure you enable the SDO and SCK.SPIxCON2 is all for FIFO and SPI framing. Set it to 0 if you don't use it Make sure to read the errata if you do want to use that, some early PIC24 parts had some horrible FIFO bugs.When transmitting just poll the SPIxSTAT.SPIRBF bit until it goes high, then the transfer should be done.SPIxSTAT.SPIEN (bit 15) should be set to 1 to enable the module. Make sure to reset the module before configuring it, you can't first enable the module and then change registers. In my projects I reset all registers to 0 (CON1, CON2, STAT), write my settings, and then enable the module.That's about it as far as I can see. It seems like the PIC24FJ128GA006 does not have PPS, so that's one less module to worry about.If you do have all of this, then feel free to post your code. Otherwise we can only guess (like this post is all about).

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: October 14, 2014, 08:41:25 pm
content: Something I think the datasheet mentions that I've not checked to see if it matters is to set up all the registers with the ON bit in SPICON1 clear, then set the ON bit as a final action.And of course map the pins on parts with remappable IOs

author: mazurov
author_rank: Frequent Contributor
timestamp: October 15, 2014, 01:03:04 am
content: Where did you get this part? Microchip isn't aware of it. If you meant FJ then how did you test? If you don't have a scope connect MISO to MOSI and you should be able to read back SPIBUF :-). If you're testing with another device make sure SPI modes are compatible on both.

author: Rendon
author_rank: Newbie
timestamp: October 16, 2014, 07:56:06 pm
content: Thank you Hans! Problem solved :-)The solution was to set the enable bit last. Lesson learned. Many thanks.mazurov, it's a PIC24FJ, not FG. Typo on my part.