my first SPI Program

14 Oct 2015

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 test>>

  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>>

16 Oct 2015

You are seeing exactly what I would expect.

while (1) {
set D9 low
do something
set D9 high
}

So D9 will be high for the time it takes to loop back to the start of the while loop, the rest of the time it will be low.

What you could do is switch your output from being a PwmOut to being a DigitalOut (if you are only turning it on or off why use a PWM?)

You could then do

enable = 1;
while (1) {
enable = !enable;
device.write(25);
}

Which would change the state of the LED each time you write, you should see a 50% duty cycle square wave on the LED pin.