8 years, 6 months ago.

First program with the SPI

Hello,

i am a beginner in the microcontroller world. At the moment i want to use the SPI Library to communicate with a nother component.

The Spi library works very fine but in the next step i wanna toggle a LED on PIN d9 if the communications starts. My Problem is that thee pin D9 is to slow to turn the pin on a low level or High level. On the oscilooscope there you can see a small high-level peak but this peak isn't during the whole time of the data communication. In additional to that i don't wanna use any wait functions. there should be a better solution

WHat i want to do is :

Led on -> start with the communication -> Communication is over -> LeD off; > Later it should be in a loop.

here is my programm

<<code>>

  1. include "mbed.h"
  2. include "FastPWM.h"

DigitalOut Enable(D9); int f = 50000; PwmOut Enable(D9);

SPI device(SPI_MOSI, SPI_MISO, SPI_SCK); SPI device(D11, D12, D13); int main() {

f=1000000;

Enable=1; device.frequency(f); device.format(16,1); wait_us(20);

while(1) { Enable = 0;

device.write(25);

Enable = 1;

} } <</code>>

2 Answers

8 years, 6 months ago.

Hi M Bre,

If by "communication" you refer to the line

M Bre's code

...
device.write(25);
...

then you should change the way you activate pin d9 with Enable variable in while loop.

M Bre's code

include "mbed.h"
include "FastPWM.h" 

DigitalOut Enable(D9);
int f = 50000;
PwmOut Enable(D9);

SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
SPI device(D11, D12, D13);

int main()
{
   f=1000000;
   Enable=1;
   device.frequency(f);
   device.format(16,1);
   wait_us(20);

   while(1)
   {
      Enable = 1;
      device.write(25);
      Enable = 0;
   }
}

Also, go through the page: https://developer.mbed.org/handbook/PwmOut on usage of PWM.

Question: Is there a reason that you have Enable(D9) as DigitalOut at the beginning? It is later defined as PwmOut, so no need for DigitalOut. Also, you could use only DigitalOut to control LED if you don't want to adjust light intensity.

Cheers Miloje

At first thank you for your comment.

I know that i have to change the way how i am activating D9. But i don'T know how it can be solve. The led should reveal the status of the communication. If the communication starts the LEd should turn on and afterwards, at the end of the conversation the led should turn off.

posted by M Bre 15 Oct 2015
8 years, 6 months ago.

The typical SPI slave device needs an enable signal that is active Low. Your original code was right as far as that was concerned. However, you need to get rid of the redeclaration of the same digitalout pin as PWMOut. You can connect an LED to the same enable pin to get some indication of the active communication: connect the LED cathode to pin D9, connect the anode to a series resistor to 3V3. The LED will light up when enable goes Low. However, a single message will only take a very short time so the LED will hardly light up. A repeated SPI communication in a while loop that runs as fast as possible will probably be visible, but you can not detect a single communication with the naked eye. You need to use a scope or logic analyser. The short high level signal that you noticed on the scope is NOT the active part of the SPI communication, but rather the inactive part (ie enable is high again) that last only very briefly because your code immediately repeats the SPI communication in a lightning fast while loop.

include "mbed.h"
 
DigitalOut Enable(D9);
int f = 50000;
 

SPI device(D11, D12, D13);  // SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
 
int main()
{
   f=1000000;
   Enable=1;
   device.frequency(f);
   device.format(16,1);
   wait_us(20);
 
   while(1)
   {
      Enable = 0;   // enable SPI slave device & activate the LED connected to D9
      device.write(25); // datatransmission
      Enable = 1;  // SPI slave will accept received data and ignore any further bits on MOSI & LED will be switched off
                          // note that LED will turn off only briefly since loop is repeated immediately and Enable becomes low again
   }
}

Thank you very much. But i already tried this program and it doesn't work. During one data impuls D9 is only for less than 10 µs high.

I also include my measurement results which i made with the oscilloscope https://www.dropbox.com/s/oz9l7ezdxz6q1ow/Unbenannt.png?dl=0

posted by M Bre 16 Oct 2015

I noticed you asked the same question again here. That doesnt help to keep track of the answers.. The answer you got there is the same as what I gave here in the text above: the short positive pulse is due to the while loop where at the end of the loop you pull the line high, then the loop restarts and immediately you pull the line low again to prepare for the next device.write(). This is exactly the expected behaviour. The actual communication is taking place during the time that line is LOW, not during the time that it is high. There is no exact match between the low time and the communication due to the fact that is takes some overhead to startup the SPI transmission after you call the device.write() method.

posted by Wim Huiskamp 17 Oct 2015