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 and I²C

Status
Not open for further replies.

SneaKSz

Member
Hello ,

I want to use SPI & I²C with the same µC ( pic 18f14K50), I've successfully implemented I²C signals on the output . Now I want to use create SPI signals, but I can't see to get how to set the register for SPI. They use the same register ( SSPSTAT) , can someone help me with setting these registers.

Thanks in advance.
 
Hello ,

I want to use SPI & I²C with the same µC ( pic 18f14K50), I've successfully implemented I²C signals on the output . Now I want to use create SPI signals, but I can't see to get how to set the register for SPI. They use the same register ( SSPSTAT) , can someone help me with setting these registers.

Thanks in advance.

hi,
You could write your own SPI control using free PIC I/O pins.
 
SSPSTAT I think contains the CKE and CKP (I think those are the names) bits. If I remember correctly, most stuff works with those bits as 0. I think there is another SSP register that let's you select either SPI or I2C, and then wheter you are in master or slave mode on SPI.

Is there a particular part you are lost on?

You'll have to look at the datasheet of the part you are trying to comm with and see what it says in terms of expected clock polarity and when it samples/puts out signals on the MOSI and MISO lines.
 
SSPSTAT I think contains the CKE and CKP (I think those are the names) bits. If I remember correctly, most stuff works with those bits as 0. I think there is another SSP register that let's you select either SPI or I2C, and then wheter you are in master or slave mode on SPI.

Is there a particular part you are lost on?

You'll have to look at the datasheet of the part you are trying to comm with and see what it says in terms of expected clock polarity and when it samples/puts out signals on the MOSI and MISO lines.

Hello,

Yes you're correct , SPI uses for SSPSTAT SMP & CKE. But I²C also uses this bits . Thats what made me so confused. I want to apply settings to these registers for the I²C and the SPI mode and then use those modes. But I think that when I want to use I²C I have to initialize the registers and then use I²C .

When I want to use SPI i also have to initialize the registers and then use SPI.

I thought it was maybe possible to initialize bot I²C and SPI and then use the protocol that I want to use. I think that I have to initialise SPI or I²C and then use it and when I want to use the other protocol , initialise that protocol and use it.

Yes maybe I can make it with standard I/0 ports , but I have pay a lot of attention to the time periods of the signals.

Datasheet : https://www.electro-tech-online.com/custompdfs/2011/01/41350b.pdf

Page 138 : SSPSTAT
Thanks in advance
 
Last edited:
Yes maybe I can make it with standard I/0 ports , but I have pay a lot of attention to the time periods of the signals.

Both I2C and SPI have separate clock signals. You don't need to keep track of signal time like with UART serial. SPI is incredibly easy to do in software. I can give you examples in C, but I think you are using PIC Asm. I'm sure there's some examples out there for that. SPI reads and writes at the same time, so everything is all in one function.
 
Both I2C and SPI have separate clock signals. You don't need to keep track of signal time like with UART serial. SPI is incredibly easy to do in software. I can give you examples in C, but I think you are using PIC Asm. I'm sure there's some examples out there for that. SPI reads and writes at the same time, so everything is all in one function.

Hello , I"m using C18, well if you look into my PIC's datasheet ( pic18F14k50 ) . THe link : RB6/SCL/SCK , so I think the clocks arent separated ( Page 4)
 
Last edited:
Although some of the newer chips allow you to map pins to different functions; most chips have only one MSSP module. Such is the case for the 18F14k50.
From the manual:
"The MSSP module can operate in one of two modes:
• Serial Peripheral Interface (SPI)
• Inter-Integrated Circuit (I2C™)"
So you can only use one or the other at a time. However, as was mentioned previously, you can pick one (I2C or SPI) to be used for with the MSSP (i.e. built-in hw), and emulate the other one in software.
 
Hello , I"m using C18, well if you look into my PIC's datasheet ( pic18F14k50 ) . THe link : RB6/SCL/SCK , so I think the clocks arent separated ( Page 4)

By separate clock signals I mean that they have clock signals, unlike asynchronous serial (UART), so you don't care about exact timing each bit in our out. SPI is also a master/slave relationship as well, so you don't care about having to handle data randomly coming in, you have to request it. If you are doing SPI in software it doesn't matter what pins you use, it uses regular GPIO's.

They have an example of a software SPI in the RFM01 programming guide. It uses a bunch of NOP's to slow everything down, but in my experience they aren't needed.
Quick googling only got me PIC hardware peripheral code, but I remembered there was software SPI code in here.
**broken link removed**

This is the function that I used with the RFM12B with MSP430 for 16bit SPI. RFM_IN and RFM_OUT are port definitions and the other RFM_ definitions are the individual pin indicators.
Code:
unsigned int spi_shift16(unsigned int data)
{
    unsigned int result = 0;
    unsigned char width;
    RFM_OUT &= ~(RFM_SCL_PIN);             //init clock low
    RFM_OUT &= ~(RFM_CS_PIN);             //begin transmit - cs low
    for (width=0; width<16; width++)
    {
        if (data & 0x8000)
        {   //check bit and set data line accordingly
            RFM_OUT |= RFM_MOSI_PIN;
        }
        else
        {
            RFM_OUT &= ~(RFM_MOSI_PIN);
        }
        RFM_OUT |= RFM_SCL_PIN;          //end bit - clock high
        result <<= 1;
        if((RFM_IN & RFM_MISO_PIN) == RFM_MISO_PIN)
        	result |= 0x00001;
		RFM_OUT &= ~(RFM_SCL_PIN);        //begin bit - clock low
        data <<= 1;
    }
    RFM_OUT |= RFM_CS_PIN;               //end transmit
    return result;
}
 
Last edited:
ahn no start and stop bit , I understand.

I dont understand the C code. Whats that :d.
RFM_MOSI_PIN??
you use init scl low en cs low. then you want to shift all the bits of the 2 bytes out.

0x8000 is that the address ?

I dont get the code , I'm sorry. Its to abstract , I think I need a time diagram.

Well thanks .
 
The 0x8000 is referring to the top bit of the word (2byte) variable

I dont understand the C code. Whats that :d.

DirtyLude's code in pseudocode, if it helps:
Code:
Function SpiTxRx(word data)

	word variable returnValue = 0

	SCK low
	CS low

	repeat 16 times
		if topBitSet(data) then MOSI = high else MOSI = low

		SCK = high

		shift left returnValue
		if MISO = high then returnValue = returnValue + 1

		SCK = low
	end repeat loop

	CS high

	return returnValue

end function
 
Thanks Dougy83.

It's C code. It can't actually be abstract. You should probably look up SPI and get familiar with the signals, MOSI, MISO, SCK, and Slave Select; Wikipedia has a good page on it. The signals go under other names as well. Basically you step through each bit in your data, send it out the output pin, toggle the clock pin, and read what's coming in on the input pin. Repeat that for the length of your data, usually 8 or 16bits.
 
Thanks Dougy83.

It's C code. It can't actually be abstract. You should probably look up SPI and get familiar with the signals, MOSI, MISO, SCK, and Slave Select; Wikipedia has a good page on it. The signals go under other names as well. Basically you step through each bit in your data, send it out the output pin, toggle the clock pin, and read what's coming in on the input pin. Repeat that for the length of your data, usually 8 or 16bits.

Thanks for the code & pseudocode , I understand it . But at witch speed does this run? at core speed ( fosc/4)?
 
Thanks for the code & pseudocode , I understand it . But at witch speed does this run? at core speed ( fosc/4)?

It doesn't matter. Again, SPI and I2C both have a separate clock lines. The protocol runs as fast or as slow as you want it to go. Both have some guidelines for a maximum speed of the interface, but I've found most peripheral chips can generally communicate much faster than the interface specs.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top