# SPI_Clock problem

I'm working on a project "SPI Communication" between a Master PIC18f2XK22 and a Slave pixart sensor (optical sensor).

I tried to write a data "01010101" on rising edge, it works but the level of SPI_CLK is not the same as the SPI_SDO.

Can someone help please?

Thanks for your answer, I'm running a clock at 2MHz and the SPI Clock is Fosc/64.

Here is my code:

void main (void)
{
unsigned char value;
// Master initialisation
OSCCON   =  0b01000000;                 // 2M       max 2MHz
TRISB    =  0b00000100;                 // SPI_SLK oupout, SPI_SDI input,
ANSELB   =  0b00000001;
SSP2STAT =  0b10000000;                 // SMP = 1 (at the end) : bit7 &
SSP2CON1 =  0b00100010;                 // Clock=Fosc/64 : bit<3:0> & CKP

IPR3bits.SSP2IP=1;                      // MSSP interrupt high priority
PIR3bits.SSP2IF=0;                      // MSSP interrupt flag cleared
PIE3bits.SSP2IE=0;                      // MSSP interrupt enable

// Led
TRISCbits.RC5 = 0;                      // set RC5 to output
SPI_CS = 1;                         // Set CS high

while(1)
{
// Write
SPI_Write(inv_Product_ID );         // send the address
delayms(10);                         // delay en ms
}
}

void SPI_Write(unsigned char data)
{
SPI_CS = 0;                         // Set CS low
// SSP2CON1 =  0b00100010;          // rising edge of clock
SSP2BUF = data;                     // put the data in the SSPBUF
while(!SSP2STATbits.BF);            // wait until the all bits sended
}


I changed the Clock (OSCCON = 0b00110000) to 1Mhz, the SPI Clock remain at (Fosc/64), here is the new Result. The Data is looking nice and square but the Clock is not looking Square.

Any Suggestions?

Problem solved, I Lowered The SPI Clock too using Clock=FOSC/(4 * (SSPxADD+1)

SSP2ADD = 0b11111111; // SSP2CON1 = 0b00101010; // Clock=FOSC/(4 * (SSPxADD+1)

And here is the Result:

Thank you all for your help :D

• Can we see a schematic? Your clock doesn't look very nice (not very square), how much capacitance is it driving? – Colin Mar 23 '17 at 14:18
• Are we supposed to magically know your circuit? – Bort Mar 23 '17 at 14:19
• How much capacitance do you have on your line? Not very good signal integrity, should be a better square wave. – 12Lappie Mar 23 '17 at 14:24
• It looks like you're running your SPI clock at about 2.5MHz. But your circuit isn't able to support that speed. Maybe you have long wires, lots of capacitance, whatever. Either address that or reduce your SPI clock speed well below 1MHz. 500kHz might be safe. – brhans Mar 23 '17 at 14:32
• Yes I'm runnig SPI Clock at 2Mhz, and I have long wire, but no cappacitance on the SPI Lines, I will try with 500 kHz and come back. Thanks – Walid Amehri Mar 24 '17 at 15:37

## 3 Answers

You have too much capacitance on the clock line, or conversely, the clock driver is too wimpy. Fix one or the other.

The clock looks so bad that there may be actually a capacitor on the line accidentally. Inspect the board.

If that fails, start disconnecting things from the clock line or break it in places and see what the signal looks right. Start at the clock pin with nothing connected. That should look nice and square. Keep adding more of the clock net to it until you find the culprit that is loading the clock line.

• I did not have a capacitance on the clock line, can you see please what my code and schematic – Walid Amehri Mar 24 '17 at 15:48
• @Walid: Yes, you do have capacitance on the clock line. Maybe you didn't put any there deliberately, but it's clearly there. Basically, there is too much capacitance for your clock line driver at that frequency. Lower the capacitance, get a better driver, or lower the frequency. – Olin Lathrop Mar 24 '17 at 15:52
• Ok, for the moment I used a clock frequency of 2 MHZ and a SPI frequency of Fosc/64. I will change my the clock frequency to 500 kHZ and see how it works, thanks – Walid Amehri Mar 24 '17 at 15:55

The slew rate of your clock signal is terrible. This indicates you are driving a load much greater than the driver is designed to handle. In different words, the clock signal is not reaching the power rails because it is either running too fast or does not have enough drive. Try slowing down the SPI clock rate to the point where the clock signal reaches the rail voltages.

Small scale / light duty buses such as SPI and I2C are meant to travel short distance between integrated circuits usually on the same printed circuit board. The objective is to reduce the pin count on integrated circuits by only using a few pins to communicate with.

• Thanks for your response, I will change the clock and see how it work. – Walid Amehri Mar 24 '17 at 16:00

In one of your comments above, you say:

I'm runnig SPI Clock at 2Mhz, and I have long wire, but no cappacitance on the SPI Lines.

Have you thought about cable capacitance?

Let's say you're using a cable with a length of two meters and capacitance of 100pF/m. This means, you're loading your clock and data lines with 200pF. Under 2MHz, the capacitor created by cable (yeah, it's a physical capacitor) turns into a resistor with a resistance of $X_C = (2\pi\cdot 2\cdot 10^6 \cdot 200\cdot 10^{-12})^{-1}= 400 \Omega$ which is quite low. Also, this capacitor, the resistor (created by the cable) plus the output impedance of clock driver forms a low-pass filter which can possibly chop-off some of your clock signal.

Results:

• Use shorter cable
• Decrease frequency

NOTE: SPI is well for short-range (i.e. on the same PCB) communications.

• Okay, I lowered the frequency to 1MHz and it works but the clock signal sstill a litte bit not square – Walid Amehri Mar 26 '17 at 10:18
• That's because the low pass filter formed by cable resistance (due to the length) and capacitance. This filter can smooth the sharp edges of clock and data signals. – Rohat Kılıç Mar 31 '17 at 3:46