2
\$\begingroup\$

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?

enter image description here


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

enter image description here

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?

enter image description here

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:

enter image description here

Thank you all for your help :D

\$\endgroup\$
  • 4
    \$\begingroup\$ Can we see a schematic? Your clock doesn't look very nice (not very square), how much capacitance is it driving? \$\endgroup\$ – Colin Mar 23 '17 at 14:18
  • 1
    \$\begingroup\$ Are we supposed to magically know your circuit? \$\endgroup\$ – Bort Mar 23 '17 at 14:19
  • \$\begingroup\$ How much capacitance do you have on your line? Not very good signal integrity, should be a better square wave. \$\endgroup\$ – 12Lappie Mar 23 '17 at 14:24
  • \$\begingroup\$ 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. \$\endgroup\$ – brhans Mar 23 '17 at 14:32
  • \$\begingroup\$ 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 \$\endgroup\$ – Walid Amehri Mar 24 '17 at 15:37
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I did not have a capacitance on the clock line, can you see please what my code and schematic \$\endgroup\$ – Walid Amehri Mar 24 '17 at 15:48
  • \$\begingroup\$ @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. \$\endgroup\$ – Olin Lathrop Mar 24 '17 at 15:52
  • \$\begingroup\$ 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 \$\endgroup\$ – Walid Amehri Mar 24 '17 at 15:55
5
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • \$\begingroup\$ Okay, I lowered the frequency to 1MHz and it works but the clock signal sstill a litte bit not square \$\endgroup\$ – Walid Amehri Mar 26 '17 at 10:18
  • \$\begingroup\$ 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. \$\endgroup\$ – Rohat Kılıç Mar 31 '17 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.