SERIAL PORT RECEIVE ATTACH DEMO PROGRAM Simple demo program showing how the serial port \"attach()\" method is used to interrupt when a character has been received into the USART. The main() program sets up the serial port baudrate, attaches the function pointer to the function to be called when a character is received on this serial port, and then loops and flashed some LEDs. Routine \"void SerialRecvInterrupt(void)\" is invoked anytime a character has been received by the by the USART.

Dependencies:   mbed

Committer:
YouTahDoug
Date:
Thu May 26 19:50:24 2011 +0000
Revision:
0:444ff736d35e
v1.0 Initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YouTahDoug 0:444ff736d35e 1 //*****************************************************************************
YouTahDoug 0:444ff736d35e 2 //* SERIAL PORT RECEIVE ATTACH DEMO PROGRAM
YouTahDoug 0:444ff736d35e 3 //* Simple demo program showing how the serial port "attach()" method is used
YouTahDoug 0:444ff736d35e 4 //* to interrupt when a character has been received into the USART.
YouTahDoug 0:444ff736d35e 5 //*
YouTahDoug 0:444ff736d35e 6 //* The main() program sets up the serial port baudrate, attaches the
YouTahDoug 0:444ff736d35e 7 //* function pointer to the function to be called when a character is received
YouTahDoug 0:444ff736d35e 8 //* on this serial port, and then loops and flashed some LEDs.
YouTahDoug 0:444ff736d35e 9 //*
YouTahDoug 0:444ff736d35e 10 //* Routine "void SerialRecvInterrupt(void)" is invoked anytime a character
YouTahDoug 0:444ff736d35e 11 //* has been received by the by the USART.
YouTahDoug 0:444ff736d35e 12 //*
YouTahDoug 0:444ff736d35e 13 //*****************************************************************************
YouTahDoug 0:444ff736d35e 14 #include "mbed.h"
YouTahDoug 0:444ff736d35e 15 #include <ctype.h>
YouTahDoug 0:444ff736d35e 16
YouTahDoug 0:444ff736d35e 17 #define BFR_SIZE 32 // Needs to be a power of two.
YouTahDoug 0:444ff736d35e 18 #define BFR_WRAP_MASK 0x1F // Modulo 32
YouTahDoug 0:444ff736d35e 19
YouTahDoug 0:444ff736d35e 20 unsigned char rcvBuffer[BFR_SIZE]; // Receive buffer.
YouTahDoug 0:444ff736d35e 21 unsigned char inIdx; // Read by background, written by Int Handler.
YouTahDoug 0:444ff736d35e 22 unsigned char outIdx; // Read by Int Handler, written by Background.
YouTahDoug 0:444ff736d35e 23
YouTahDoug 0:444ff736d35e 24 DigitalOut myled(LED1);
YouTahDoug 0:444ff736d35e 25 Serial pc (USBTX,USBRX);
YouTahDoug 0:444ff736d35e 26
YouTahDoug 0:444ff736d35e 27 //*****************************************************************************
YouTahDoug 0:444ff736d35e 28 // mc serial receive interrupt handler
YouTahDoug 0:444ff736d35e 29 //*****************************************************************************
YouTahDoug 0:444ff736d35e 30
YouTahDoug 0:444ff736d35e 31 void SerialRecvInterrupt (void)
YouTahDoug 0:444ff736d35e 32 {
YouTahDoug 0:444ff736d35e 33 unsigned char c;
YouTahDoug 0:444ff736d35e 34
YouTahDoug 0:444ff736d35e 35 c = pc.getc(); // On receive interrupt, get the character.
YouTahDoug 0:444ff736d35e 36
YouTahDoug 0:444ff736d35e 37 while (!pc.writeable()){}; // Wait until xmit buffer is empty.
YouTahDoug 0:444ff736d35e 38 pc.putc(c); // Echo char back out the serial port.
YouTahDoug 0:444ff736d35e 39
YouTahDoug 0:444ff736d35e 40 while (!pc.writeable()){}; // Make sure transmit buffer is empty.
YouTahDoug 0:444ff736d35e 41 pc.putc(toupper(c)); // Echo the uppercase value of input char.
YouTahDoug 0:444ff736d35e 42 }
YouTahDoug 0:444ff736d35e 43
YouTahDoug 0:444ff736d35e 44 //*****************************************************************************
YouTahDoug 0:444ff736d35e 45 // Main Program
YouTahDoug 0:444ff736d35e 46 //*****************************************************************************
YouTahDoug 0:444ff736d35e 47
YouTahDoug 0:444ff736d35e 48 int main() {
YouTahDoug 0:444ff736d35e 49 int i;
YouTahDoug 0:444ff736d35e 50
YouTahDoug 0:444ff736d35e 51 for (i=0; i<BFR_SIZE; i++) { // Clear buffer if you have OCD.
YouTahDoug 0:444ff736d35e 52 rcvBuffer[i] = 0;
YouTahDoug 0:444ff736d35e 53 }
YouTahDoug 0:444ff736d35e 54
YouTahDoug 0:444ff736d35e 55 inIdx = 0; // Initialize bfr input and output pointers.
YouTahDoug 0:444ff736d35e 56 outIdx = 0;
YouTahDoug 0:444ff736d35e 57
YouTahDoug 0:444ff736d35e 58 pc.baud(38400); // USART to PC USB USART.
YouTahDoug 0:444ff736d35e 59 pc.attach (&SerialRecvInterrupt, pc.RxIrq); // Recv interrupt handler
YouTahDoug 0:444ff736d35e 60 pc.printf ("Serial interrupt demo\n");
YouTahDoug 0:444ff736d35e 61
YouTahDoug 0:444ff736d35e 62 while(1) {
YouTahDoug 0:444ff736d35e 63 myled = 1;
YouTahDoug 0:444ff736d35e 64 wait(0.2);
YouTahDoug 0:444ff736d35e 65 myled = 0;
YouTahDoug 0:444ff736d35e 66 wait(0.2);
YouTahDoug 0:444ff736d35e 67 }
YouTahDoug 0:444ff736d35e 68 }