9 years, 10 months ago.

how to use the serial interrupts ?

Hello !

I would use the operation of serial interrupts power to stop my program when I press a key on the keyboard pc.getc. The PC is connected by RS232.du microcontroller LPC1768 link. Can you help me with my program ? After several attempts I don't quite understandI the serial Interrupts. thank you very much here is my current program by sending a value SPI connection every 0.5 seconds :

  1. include "mbed.h"
  2. include "TextLCD.h"

Ticker flipper; DigitalOut led1(LED1); DigitalOut led2(LED2); Serial pc(USBTX, USBRX); tx, rx SPI spi(p11, p12, p13); Pins liaison SPI : mosi, miso, sclk TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); Pins écran LCD : rs, e, d4-d7 DigitalOut cs1(p21), cs2(p22), cs3(p23), cs4(p25); Pins des CS des 4 DAC

void flip(void);

int val=0;

int main() { led2 = 0; spi.format(16,1); Setup the spi for 16 bit data, high steady state clock, spi.frequency(1000000); second edge capture, with a 1MHz clock rate

cs1=1;

flipper.attach(&flip, 0.5); setup flipper to call flip after 2 seconds

}

void flip() { cs1=0; spi.write(val); cs1=1;

lcd.printf("val=%d \n\n", val); led2 = !led2; val=val+1000; }

Question relating to:

1 Answer

9 years, 10 months ago.

void onSerialInput() {
  int keyIn = pc.getc()  // find out what key was pressed.
  if (keyIn=='S')           // if S for stop
    flipper.detach();
  else if (keyIn =='G')  // if G for go
    flipper.attach(&flip,0.5);
}

void main () { 
...
pc.attach(&onSerialInput);
...
}

If all you want to do is stop no matter what was pressed then you can simplify a bit.

Also if you put <<code>> on a line all on it's own before your code and <</code>> afterwards it will format the code for you and make it a lot more readable.

Accepted Answer

is there a way i can run the flip function without interval? like run it in one time and done.

posted by Boon Boon 06 Jul 2018