五力全開 / Mbed 2 deprecated Nucleo_printf

Dependencies:   mbed

Committer:
no830712
Date:
Sat May 28 09:28:59 2016 +0000
Revision:
1:f6e87237daaa
Parent:
0:d88d3f466e6a
with interrupt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nthu_Five 0:d88d3f466e6a 1 #include "mbed.h"
Nthu_Five 0:d88d3f466e6a 2
Nthu_Five 0:d88d3f466e6a 3 //------------------------------------
Nthu_Five 0:d88d3f466e6a 4 // Hyperterminal configuration
Nthu_Five 0:d88d3f466e6a 5 // 9600 bauds, 8-bit data, no parity
Nthu_Five 0:d88d3f466e6a 6 //------------------------------------
Nthu_Five 0:d88d3f466e6a 7
Nthu_Five 0:d88d3f466e6a 8 Serial pc(SERIAL_TX, SERIAL_RX);
Nthu_Five 0:d88d3f466e6a 9
Nthu_Five 0:d88d3f466e6a 10 Serial bluetooth(D10, D2); // tx,rx
no830712 1:f6e87237daaa 11
no830712 1:f6e87237daaa 12 // Circular buffers for serial TX and RX data - used by interrupt routines
no830712 1:f6e87237daaa 13 const int buffer_size = 100;
no830712 1:f6e87237daaa 14 // might need to increase buffer size for high baud rates
no830712 1:f6e87237daaa 15 uint8_t rx_buffer[buffer_size+1];
no830712 1:f6e87237daaa 16 // Circular buffer pointers
no830712 1:f6e87237daaa 17 // volatile makes read-modify-write atomic
no830712 1:f6e87237daaa 18 volatile int rx_in=0;
no830712 1:f6e87237daaa 19 volatile int rx_out=0;
Nthu_Five 0:d88d3f466e6a 20
no830712 1:f6e87237daaa 21 void Rx_interrupt();
no830712 1:f6e87237daaa 22 bool availableRxFIFO();
no830712 1:f6e87237daaa 23 uint8_t readRxFIFO();
no830712 1:f6e87237daaa 24
Nthu_Five 0:d88d3f466e6a 25 int main() {
Nthu_Five 0:d88d3f466e6a 26 pc.printf("Hello World !\n");
Nthu_Five 0:d88d3f466e6a 27
Nthu_Five 0:d88d3f466e6a 28 bluetooth.baud(9600);
no830712 1:f6e87237daaa 29 // Setup a serial interrupt function to receive data
no830712 1:f6e87237daaa 30 bluetooth.attach(&Rx_interrupt, Serial::RxIrq);
Nthu_Five 0:d88d3f466e6a 31
Nthu_Five 0:d88d3f466e6a 32 while(1) {
no830712 1:f6e87237daaa 33 if(availableRxFIFO())
no830712 1:f6e87237daaa 34 {
no830712 1:f6e87237daaa 35 uint8_t temp = readRxFIFO();
no830712 1:f6e87237daaa 36 pc.printf(" count: %d\n",temp);
no830712 1:f6e87237daaa 37 }
no830712 1:f6e87237daaa 38 wait(0.1);
Nthu_Five 0:d88d3f466e6a 39
no830712 1:f6e87237daaa 40 /*
Nthu_Five 0:d88d3f466e6a 41 if(bluetooth.readable())
Nthu_Five 0:d88d3f466e6a 42 {
Nthu_Five 0:d88d3f466e6a 43 unsigned char temp = bluetooth.getc() - 158;
Nthu_Five 0:d88d3f466e6a 44 pc.printf("%c",temp);
Nthu_Five 0:d88d3f466e6a 45 ReceiveCount++;
Nthu_Five 0:d88d3f466e6a 46 pc.printf(" count: %d ",ReceiveCount);
Nthu_Five 0:d88d3f466e6a 47 //pc.printf("%c",bluetooth.getc());
Nthu_Five 0:d88d3f466e6a 48 }
Nthu_Five 0:d88d3f466e6a 49 //pc.printf("This program runs since %d seconds.\n", i++);
Nthu_Five 0:d88d3f466e6a 50 myled = !myled;
Nthu_Five 0:d88d3f466e6a 51 //bluetooth.printf("\n Hi");
no830712 1:f6e87237daaa 52 */
Nthu_Five 0:d88d3f466e6a 53 }
Nthu_Five 0:d88d3f466e6a 54 }
no830712 1:f6e87237daaa 55
no830712 1:f6e87237daaa 56 bool availableRxFIFO()
no830712 1:f6e87237daaa 57 {
no830712 1:f6e87237daaa 58 if(rx_out != rx_in)
no830712 1:f6e87237daaa 59 {return 1;}
no830712 1:f6e87237daaa 60 else
no830712 1:f6e87237daaa 61 {return 0;}
no830712 1:f6e87237daaa 62 }
no830712 1:f6e87237daaa 63
no830712 1:f6e87237daaa 64 uint8_t readRxFIFO()
no830712 1:f6e87237daaa 65 {
no830712 1:f6e87237daaa 66 if(rx_out != rx_in) {
no830712 1:f6e87237daaa 67 uint8_t Output = rx_buffer[rx_out];
no830712 1:f6e87237daaa 68 rx_out = (rx_out + 1) % buffer_size;
no830712 1:f6e87237daaa 69 return Output;
no830712 1:f6e87237daaa 70 }
no830712 1:f6e87237daaa 71 return 0;
no830712 1:f6e87237daaa 72 }
no830712 1:f6e87237daaa 73
no830712 1:f6e87237daaa 74 // Interupt Routine to read in data from serial port
no830712 1:f6e87237daaa 75 void Rx_interrupt() {
no830712 1:f6e87237daaa 76 // Loop just in case more than one character is in UART's receive FIFO buffer
no830712 1:f6e87237daaa 77 // Stop if buffer full
no830712 1:f6e87237daaa 78 while ((bluetooth.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
no830712 1:f6e87237daaa 79 rx_buffer[rx_in] = bluetooth.getc();
no830712 1:f6e87237daaa 80 // Uncomment to Echo to USB serial to watch data flow
no830712 1:f6e87237daaa 81 // pc.putc(rx_buffer[rx_in]);
no830712 1:f6e87237daaa 82 rx_in = (rx_in + 1) % buffer_size;
no830712 1:f6e87237daaa 83 }
no830712 1:f6e87237daaa 84 return;
no830712 1:f6e87237daaa 85 }