Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

dsPic30f3014 SPI interfacing

Status
Not open for further replies.

dspic

New Member
I am using MPLAB IDE C30 compiler and I am using a dsPic30f3014 mcu. I am trying to interface with the spi to send data to a peripheral (audio attenuator) using C coding. I think I have the initialization correct, but I am stumped on how to actually send data. I was able to compile and got the correct 'printf' output.

Can someone please tell me how to actually send data via the SPI?

Code:
// EE 400/401
// Group #27: Audio normalizer
// SPI interfacing
//************************************************************//

#include <p30F3014.h>
#include <stdio.h>
_FOSC(CSW_FSCM_OFF & XT_PLL8);    						// (clock switching and fail safe monitor are disabled &
														//    crystal connected to OSC1 is the clock source (8x PLL applied, primary oscillator)
_FWDT(WDT_OFF);                    						//    Watchdog timer disabled
_FBORPOR(PBOR_OFF & BORV_27 & PWRT_16 & MCLR_EN);    	// Brown-out reset disabled &_____&____& MCLR reset enabled


#define FCY 8000000                						// Instruction cycle rate (Osc(4Mhz) x PLL(8) / 4) = 8 MIPS
#define T1Period 800            						// T1Period = FCY*100 us = 800

void setup_spi();

int main()
{

setup_spi();

return 0;
}


//***********************
void setup_spi()
{

SPI1CON = 0x063E;                						// 0000 | 0110 | 0011 | 1110
SPI1STAT = 0x8000;                						// enable SPI port

printf("To C, or not to C: that is the question.\n");

return;
}
 
I have spent at least 10 hours searching all over the web and in text books. Every example of code I find is compiled in different languages or uses special compilers with different libraries. So I am not able to decipher whats going on. Any help would be greatly appreciated! All I need is an idea of the commands I need to use to send data to the SPI buffer for transfer.

Thanks
 
Have you tried bit bashing it? You have 4 lines: CS (chip select), DIN (data in) DOUT(data out) and SCLK (Serial clock).

Taking CS low starts the whole sequence. a rising edge on the SCLK edge clocks data in and a falling edge on the SCLK line clocks out data.

Present data on the DIN, take SCLK high to clock data in. take SCLK low and look at data on DOUT to clock data out. At the end of the clocking sequence, take the CS line high

One gotcha: make sure your DOUT feeds into the DIN pin of the receiving chip and vice versa. Easy to miss and you will spend hours of time sorting it out when you have better things to do.

If it is not working with your dedicated SPI engine, toggling port pins is a good cross check
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top