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.

spi programs for 18f4550

Status
Not open for further replies.

hr_anup

New Member
i am using 18f4550 pic.i am not easily getting about the spi program concepts.i am using c18 compiler.so can anyone help me with the program for spi in 18f4550.i used several methods to crack it up but they never worked properly.the main prolem is that should i use openSWSPI if yes how ,i am unable to what parameters are to be written in openSWSPI.how can i make the spi ports software spi ports is it by using following ..
#define SW_DIN_PIN PORTCbits.RC4 // Data in
#define TRIS_SW_DIN_PIN TRISCbits.TRISC4
#define SW_DOUT_PIN PORTCbits.RC6 // Data out
#define TRIS_SW_DOUT_PIN TRISCbits.TRISC6
by using these kind of sentances can i make RA0 and RD0 ,DIN and DOUT ports.
please post me the program for spi
 
the main prolem is that should i use openSWSPI if yes how ,i am unable to what parameters are to be written in openSWSPI.how can i make the spi ports software spi ports is it by using following ..
#define SW_DIN_PIN PORTCbits.RC4 // Data in
#define TRIS_SW_DIN_PIN TRISCbits.TRISC4
#define SW_DOUT_PIN PORTCbits.RC6 // Data out
#define TRIS_SW_DOUT_PIN TRISCbits.TRISC6
by using these kind of sentances can i make RA0 and RD0 ,DIN and DOUT ports.
please post me the program for spi

Code:
//RA0 as DIN
//RD0 as DOUT
#define SW_DIN_PIN        PORTAbits.RA0     // Data in
#define TRIS_SW_DIN_PIN   TRISAbits.TRIA0
#define SW_DOUT_PIN       PORTDbits.RD0    // Data out
#define TRIS_SW_DOUT_PIN  TRISDbits.TRISD0
 
openSWSPI.

Thats the software implementation you are using, the 18F4550 has hardware SPI.

Whats even better is that the C18 hardware SPI libraries have routines which configures the IO of the SPI bus for you. The only pin you need to work out youself is the cable select.


Good luck.

Mark
 
thanx can u give me the full program..i think there is a need to write 2 separate prog for master and slave ..help me
 
In short no.

I don't have any code that demonstrates PIC to PIC data transfer via SPI.

But by the sounds of it, I believe you need to find an easier project that will let you get a grasp on SPI communications first, RTC or EEPROM perhaps?

Good luck.

Mark
 
Last edited:
Yes it's possible, just read the SPI section of your PIC data sheet, it's all in there.
 
About the hardware SPI you have:

#include <spi.h>

SPI exchange routine

unsigned char xchg_spi (unsigned char dat){
SSPBUF = dat;
while (!SSPSTATbits.BF);
return (BYTE)SSPBUF;
}

to use that routine use this commands

#define xmit_spi(dat) xchg_spi(dat)
#define rcvr_spi() xchg_spi(0xFF)

And finally I use this routines to set the speed of the SPI

static
void FCLK_SLOW(void){// Set slow clock (100k-400k)
SSPSTATbits.SMP = 0;
SSPCON1 = 0b00100010;// SSPEN (bit5) = 1, CKP (bit4) = 0, <3:0>0010 //0010 0010
SSPCON1bits.CKP = 0;
SSPSTATbits.CKE = 1; //Mode 0
SSPCON1bits.SSPEN = 1;
}

static
void FCLK_FAST(void){// Set fast clock (depends on the CSD)
SSPSTATbits.SMP = 0;
SSPCON1 = 0b00100000;// SSPEN (bit5) = 1, CKP (bit4) = 0, <3:0>0010 //0010 0010
SSPSTATbits.CKE = 1; //Mode 0
SSPCON1bits.CKP = 0;
SSPCON1bits.SSPEN = 1;
}

The pin settings for the SPI are in the datasheet. also remember to pull up and down your CS. In this configuration I gave you I seted up my SPI as master, in the datasheet you can check on how to set it to work as slave to use multiple pics.

About the software SPI you have:

#include <sw_spi.h>

OpenSWSPI(); //Set SPI Mode 0,CS pin B2,
//DIN pin B3, DOUT pin B7, SCK pin B6

you can change the pins but it would mean to edit and recompile the sw_spi.h library.

about the use of it, you can check the C18_libraries.pdf document and it goes like this.

3.5.2 Example of Use
#include <p18C452.h>
#include <sw_spi.h>
#include <delays.h>
void main( void )
{
char address;
// configure software SPI
OpenSWSPI();
for( address=0; address<0x10; address++ )
{
ClearCSSWSPI(); //clear CS pin
WriteSWSPI( 0x02 ); //send write cmd
WriteSWSPI( address ); //send address hi
WriteSWSPI( address ); //send address low
SetCSSWSPI(); //set CS pin
Delay10KTCYx( 50 ); //wait 5000,000TCY
}
}

In that document doesn't say anything about the software SPI speed so if you have any clue about it let me know, otherwise I'll run some test and let you know as soon as posible.
 
hr, If you're familiar with C and using libraries such as your stdio.h library and the components there in, I don't think you will hve any problem using. I hope I'm not out in left field with my answer because you indicate the .I file rather than .h file.
Also yes! change, for example, SW_DIN_PIN PORTCbits.RC4 to .R(anything you want) as long as it is a valid port. Then when you call that function, that port will be operated on. Don't forget to change your TRIS bit as well. As a side note, You may want to make a copy of the original library into your project directory, refer your project to that file rather than the original and make your changes there. Good luck. Hope I was able to help. Now I'll keep looking for the solution to my problem.
 
Hi Ghostman.

The OP was written 3 years ago and the member hasn't been online since then. Have no fear it will help someone, though.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top