Page 201 - ARM Based Microcontroller Projects Using MBED
P. 201
8.17 PROJECT 14—SERIAL INTERRUPT 187
/*************************************************************************
SERIAL INTERRUPT
================
This program attaches to the serial communication link so that when a
character is typed on the keyboard the program generates an interrupt
and jumps to the interrupt service routine. In this program the User
LED normally flashes every 100 milliseconds. Every time the + key is
pressed on the keyboard the flashing rate increases by 100ms. When
the flashing rate is one second it is reset back to 100ms.
Author: Dogan Ibrahim
Date : August 2018
File : SerISR
**************************************************************************/
#include "mbed.h"
Serial MyPC(USBTX, USBRX);
DigitalOut MyLED(LED1); // LED1 is output
#define ON 1
#define OFF 0
double dly;
//
// This is the interrupt service routine. The program jumps here whenever
// a key is pressed on the keyboard. Here, the delay (flashing rate) of the
// LED is increased by 100ms. When the delay is one second it is reset
// back to 100ms
//
void ISR()
{
char c;
c == MyPC.scanf("%c",&c); // Read the character
if(c == '+') // Is it + ?
{
dly = dly + 0.1; // Increment dly by 100ms
if(dly > 1.0)dly = 0.1; // If one sec, reset
}
}
int main()
{
MyPC.attach(&ISR, Serial::RxIrq); // Attach to serial link
dly = 0.1; // Initial value of dly
while(1) // Do forever
{
MyLED = ON; // LED ON
wait(dly); // Wait dly
MyLED = OFF; // LED OFF
wait(dly); // Wait dly
}
}
FIG. 8.50 Program listing.