Only imu output

Dependencies:   Servo mbed

Fork of FYDP_Final2 by Mark Vandermeulen

Committer:
tntmarket
Date:
Wed Mar 25 09:58:50 2015 +0000
Revision:
5:d2e955a94940
Parent:
0:21019d94ad33
only imu output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
majik 0:21019d94ad33 1 #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
majik 0:21019d94ad33 2
majik 0:21019d94ad33 3 /*
majik 0:21019d94ad33 4 * To run this test program, link p9 to p10 so the Serial loops
majik 0:21019d94ad33 5 * back and receives characters it sends.
majik 0:21019d94ad33 6 */
majik 0:21019d94ad33 7
majik 0:21019d94ad33 8 #include "mbed.h"
majik 0:21019d94ad33 9 #include "MODSERIAL.h"
majik 0:21019d94ad33 10
majik 0:21019d94ad33 11 DigitalOut led1(LED1);
majik 0:21019d94ad33 12 DigitalOut led2(LED2);
majik 0:21019d94ad33 13 DigitalOut led3(LED3);
majik 0:21019d94ad33 14 DigitalOut led4(LED4);
majik 0:21019d94ad33 15
majik 0:21019d94ad33 16 MODSERIAL pc(USBTX, USBRX);
majik 0:21019d94ad33 17
majik 0:21019d94ad33 18 /*
majik 0:21019d94ad33 19 * As experiement, you can define MODSERIAL as show here and see what
majik 0:21019d94ad33 20 * effects it has on the LEDs.
majik 0:21019d94ad33 21 *
majik 0:21019d94ad33 22 * MODSERIAL uart(TX_PIN, RX_PIN, 512);
majik 0:21019d94ad33 23 * With this, the 512 characters sent can straight into the buffer
majik 0:21019d94ad33 24 * vary quickly. This means LED1 is only on briefly as the TX buffer
majik 0:21019d94ad33 25 * fills.
majik 0:21019d94ad33 26 *
majik 0:21019d94ad33 27 * MODSERIAL uart(TX_PIN, RX_PIN, 32);
majik 0:21019d94ad33 28 * With this, the buffer is smaller than the default 256 bytes and
majik 0:21019d94ad33 29 * therefore LED1 stays on much longer while the system waits for
majik 0:21019d94ad33 30 * room in the TX buffer.
majik 0:21019d94ad33 31 */
majik 0:21019d94ad33 32 MODSERIAL uart(TX_PIN, RX_PIN);
majik 0:21019d94ad33 33
majik 0:21019d94ad33 34 // This function is called when a character goes from the TX buffer
majik 0:21019d94ad33 35 // to the Uart THR FIFO register.
majik 0:21019d94ad33 36 void txCallback(MODSERIAL_IRQ_INFO *q) {
majik 0:21019d94ad33 37 led2 = !led2;
majik 0:21019d94ad33 38 }
majik 0:21019d94ad33 39
majik 0:21019d94ad33 40 // This function is called when TX buffer goes empty
majik 0:21019d94ad33 41 void txEmpty(MODSERIAL_IRQ_INFO *q) {
majik 0:21019d94ad33 42 led2 = 0;
majik 0:21019d94ad33 43 pc.puts(" Done. ");
majik 0:21019d94ad33 44 }
majik 0:21019d94ad33 45
majik 0:21019d94ad33 46 // This function is called when a character goes into the RX buffer.
majik 0:21019d94ad33 47 void rxCallback(MODSERIAL_IRQ_INFO *q) {
majik 0:21019d94ad33 48 led3 = !led3;
majik 0:21019d94ad33 49 pc.putc(uart.getc());
majik 0:21019d94ad33 50 }
majik 0:21019d94ad33 51
majik 0:21019d94ad33 52 int main() {
majik 0:21019d94ad33 53 int c = 'A';
majik 0:21019d94ad33 54
majik 0:21019d94ad33 55 // Ensure the baud rate for the PC "USB" serial is much
majik 0:21019d94ad33 56 // higher than "uart" baud rate below.
majik 0:21019d94ad33 57 pc.baud(PC_BAUD);
majik 0:21019d94ad33 58
majik 0:21019d94ad33 59 // Use a deliberatly slow baud to fill up the TX buffer
majik 0:21019d94ad33 60 uart.baud(1200);
majik 0:21019d94ad33 61
majik 0:21019d94ad33 62 uart.attach(&txCallback, MODSERIAL::TxIrq);
majik 0:21019d94ad33 63 uart.attach(&rxCallback, MODSERIAL::RxIrq);
majik 0:21019d94ad33 64 uart.attach(&txEmpty, MODSERIAL::TxEmpty);
majik 0:21019d94ad33 65
majik 0:21019d94ad33 66 // Loop sending characters. We send 512
majik 0:21019d94ad33 67 // which is twice the default TX/RX buffer size.
majik 0:21019d94ad33 68
majik 0:21019d94ad33 69 led1 = 1; // Show start of sending with LED1.
majik 0:21019d94ad33 70
majik 0:21019d94ad33 71 for (int loop = 0; loop < 512; loop++) {
majik 0:21019d94ad33 72 uart.printf("%c", c);
majik 0:21019d94ad33 73 c++;
majik 0:21019d94ad33 74 if (c > 'Z') c = 'A';
majik 0:21019d94ad33 75 }
majik 0:21019d94ad33 76
majik 0:21019d94ad33 77 led1 = 0; // Show the end of sending by switching off LED1.
majik 0:21019d94ad33 78
majik 0:21019d94ad33 79 // End program. Flash LED4. Notice how LED 2 and 3 continue
majik 0:21019d94ad33 80 // to flash for a short period while the interrupt system
majik 0:21019d94ad33 81 // continues to send the characters left in the TX buffer.
majik 0:21019d94ad33 82
majik 0:21019d94ad33 83 while(1) {
majik 0:21019d94ad33 84 led4 = !led4;
majik 0:21019d94ad33 85 wait(0.25);
majik 0:21019d94ad33 86 }
majik 0:21019d94ad33 87 }
majik 0:21019d94ad33 88
majik 0:21019d94ad33 89 /*
majik 0:21019d94ad33 90 * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
majik 0:21019d94ad33 91 * machine that is connected to the "pc" USB serial port.
majik 0:21019d94ad33 92 *
majik 0:21019d94ad33 93 * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
majik 0:21019d94ad33 94 * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
majik 0:21019d94ad33 95 * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
majik 0:21019d94ad33 96 * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
majik 0:21019d94ad33 97 * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
majik 0:21019d94ad33 98 * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
majik 0:21019d94ad33 99 * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
majik 0:21019d94ad33 100 *
majik 0:21019d94ad33 101 * Of interest is that last "R" character after the system has said "Done."
majik 0:21019d94ad33 102 * This comes from the fact that the TxEmpty callback is made when the TX buffer
majik 0:21019d94ad33 103 * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
majik 0:21019d94ad33 104 * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
majik 0:21019d94ad33 105 * when the TxEmpty callback is made, the TX buffer is empty, but that just means
majik 0:21019d94ad33 106 * the "last few characters" were written to the TX FIFO. So although the TX
majik 0:21019d94ad33 107 * buffer has gone empty, the Uart's transmit system is still sending any remaining
majik 0:21019d94ad33 108 * characters from it's TX FIFO. If you want to be truely sure all the characters
majik 0:21019d94ad33 109 * you have sent have left the Mbed then call txIsBusy(); This function will
majik 0:21019d94ad33 110 * return true if characters are still being sent. If it returns false after
majik 0:21019d94ad33 111 * the Tx buffer is empty then all your characters have been sent.
majik 0:21019d94ad33 112 *
majik 0:21019d94ad33 113 * In a similar way, when characters are received into the RX FIFO, the entire
majik 0:21019d94ad33 114 * FIFO contents is moved to the RX buffer, assuming there is room left in the
majik 0:21019d94ad33 115 * RX buffer. If there is not, any remaining characters are left in the RX FIFO
majik 0:21019d94ad33 116 * and will be moved to the RX buffer on the next interrupt or when the running
majik 0:21019d94ad33 117 * program removes a character(s) from the RX buffer with the getc() method.
majik 0:21019d94ad33 118 */
majik 0:21019d94ad33 119
majik 0:21019d94ad33 120 #endif