Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MSSP port in PIC (SPI interface)

Status
Not open for further replies.

pepillo

Advanced Member level 4
Full Member level 1
Joined
Jan 23, 2006
Messages
114
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Location
Spain
Activity points
2,382
mssp pic

How do I use the MSSP port in SPI mode? I don´t want to make "bit-banging" SPI, I want to load the SSPBUF and let it do the job for me...
Do I need to generate any SCK signals?
Have you got any example code for CCS?
Thanks
 

mssp spi

That page doesn´t say how to use SPI internal module, I don´t want to do bit-banging SPI, but hardware controlled SPI.

The CCS built-in functions use hardware or software SPI?

Any example code?
 

ccs spi code

You need to configure only a few thing in the SSPCON1 register. From this register, you can select operation as Master (lowest 4 bit) and corresponding clock (also with these bits). The fastest clock is FOSC/4. SO if your PIC is running at 40 MHz (internal PLL enabled), maximum clock speed for SPI is 10 MHz. Other speed includes FOSC/16 and FOSC/64. Timer 2 can also be used as the clock source. Other setting like clock polarity bit (CKP bit in SSPCON1) must also be configured. And don't forget to enabled the SSPEN bit in SSPCON1 to allow SPI mode instead of setting these pins as normal I/O. Also set SMP and CKE bit in SSPSTAT register accordingly. Set the TRIS accordingly for all the SPI pins as only SDI pin is configured automatically it's direction if SPI mode is enabled.

All the best.

Added after 17 minutes:

Hi Pepillo,

Here's are some sample code. Wrote it some time ago.

// Description:
// This routine initialises the Serial Peripheral Interface (SPI).
void SPI_Init(void)
{
// Transmit occurs on transition from active to idle clock state.
SSPSTAT = 0x40;
// Initialise SPI - synchronous serial port enabled, master clock = Fosc/4,
// and idle state is low level for clock
SSPCON1 = 0x20;
}

// Macro to send 1 byte of data on SPI bus.
#define SPI_PUT_DATA(data) {SSPBUF = data;}
// Macro to deselect the slave device.
#define SLAVE_DESELECT {output_high(P_CS);}
// Macro to select the slave device.
#define SLAVE_SELECT {output_low(P_CS);}

In order to receive, if software polling is used, poll the BF bit in SSPSTAT register. If the bit is set, the data is received and the data is located in SSPBUF. 2nd way is using the interrupt method.

All the best.
 
#use spi ccs

Thank you very much, your code is so helpful and the explanations as well. I will try that and will.
By the way, have you used Analog Devices AD7708? It´s the converter i'm driving from the pic.
I am having problems with splitting the C files in two (main.c and spi_hw.c, in spi_hw.c I implement the functions, in spi_hw.h I define the prototypes and in main.c I use them), but CCS doesn´t let me compile it. Any idea about how to do it? Thanks!!!!
 

ccs spi

Hi Pepillo,

You should include your spi_hw.h in your main.c and I guess you should be able to compile after that. Good to see that you split your functions into files based on functionality, a good practice for code modularization.

I have yet to try the AD7708 IC. But currently I'm going to start to redesign a 24-bit ADC board using IC from AD also. Previously working on internal ADC like those in PIC and PSoC.
 

ccs spi example

Thanks again. Finally I could compile my code splitting in multiple files, the error was due to something I don´t understand yet: a function I called "adc_reset()" gave me a weird error, after trying many things I changed its name for adc_init and it worked fine!!! It drove me crazy to fix that error, I never expected and error caused by a function name.
I hope you don´t have this problem, perhaps my experience could help others with this same error.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top