Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Nucleo-getc by
main.cpp@4:472805b710a0, 2015-08-05 (annotated)
- Committer:
- mfwic
- Date:
- Wed Aug 05 23:03:52 2015 +0000
- Revision:
- 4:472805b710a0
- Parent:
- 3:e316c5e314e1
Test publish for imported code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mfwic | 4:472805b710a0 | 1 | //RK: User enters mulitple characters from keyboard terminated with Enter/Return. |
| mfwic | 4:472805b710a0 | 2 | //Only recognizes numbers. Limit of 10. Overrun caused negative results. |
| dousape2 | 1:4486f14a88ad | 3 | /********************************************************************************** |
| dousape2 | 1:4486f14a88ad | 4 | * @file main.cpp |
| dousape2 | 1:4486f14a88ad | 5 | * @author Petr Dousa |
| dousape2 | 1:4486f14a88ad | 6 | * @version V1.00 |
| dousape2 | 1:4486f14a88ad | 7 | * @date 22-March-2015 |
| Foxnec | 3:e316c5e314e1 | 8 | * @brief Every single second push to serial message and read from serial |
| mfwic | 4:472805b710a0 | 9 | * Serial speed is set to 9600. |
| dousape2 | 1:4486f14a88ad | 10 | ***********************************************************************************/ |
| dousape2 | 1:4486f14a88ad | 11 | |
| dousape2 | 1:4486f14a88ad | 12 | /* Includes ----------------------------------------------------------------------*/ |
| dousape2 | 0:2a0d1131965d | 13 | #include "mbed.h" |
| dousape2 | 0:2a0d1131965d | 14 | |
| dousape2 | 1:4486f14a88ad | 15 | /* Defines -----------------------------------------------------------------------*/ |
| dousape2 | 1:4486f14a88ad | 16 | #define buffer_size 256 // incoming buffer size |
| dousape2 | 1:4486f14a88ad | 17 | #define buffer_fill buffer_size+1 // number, when buffer is ready |
| dousape2 | 1:4486f14a88ad | 18 | |
| dousape2 | 1:4486f14a88ad | 19 | /* Function prototypes -----------------------------------------------------------*/ |
| dousape2 | 1:4486f14a88ad | 20 | void serialRx(); |
| dousape2 | 2:416bbbc23e82 | 21 | int serialGetBuffer(char * & data); |
| dousape2 | 0:2a0d1131965d | 22 | |
| dousape2 | 1:4486f14a88ad | 23 | /* Variables ---------------------------------------------------------------------*/ |
| Foxnec | 3:e316c5e314e1 | 24 | // additional variables for incoming data |
| dousape2 | 1:4486f14a88ad | 25 | char serial_buffer[buffer_size]; // buffer to save incoming data |
| dousape2 | 1:4486f14a88ad | 26 | char serial_buffer2[buffer_size]; // second buffer to save incoming data |
| dousape2 | 1:4486f14a88ad | 27 | int serial_buffer_where=0; // index array for buffer |
| dousape2 | 1:4486f14a88ad | 28 | int serial_buffer2_where=0; // index array for second buffer |
| dousape2 | 1:4486f14a88ad | 29 | bool serial_end_line = false; // searching for end line |
| dousape2 | 1:4486f14a88ad | 30 | |
| dousape2 | 1:4486f14a88ad | 31 | //mbed - initialization of peripherals |
| dousape2 | 0:2a0d1131965d | 32 | Serial pc(SERIAL_TX, SERIAL_RX); |
| dousape2 | 0:2a0d1131965d | 33 | DigitalOut myled(LED1); |
| dousape2 | 0:2a0d1131965d | 34 | |
| dousape2 | 1:4486f14a88ad | 35 | /* Functions----------------------------------------------------------------------*/ |
| dousape2 | 0:2a0d1131965d | 36 | |
| dousape2 | 1:4486f14a88ad | 37 | /******************************************************************************* |
| dousape2 | 1:4486f14a88ad | 38 | * Function Name : serialRx. |
| dousape2 | 2:416bbbc23e82 | 39 | * Description : Incoming interruption fom serial. Fill two buffers with |
| Foxnec | 3:e316c5e314e1 | 40 | * incoming data and prepare it to serialGetBuffer. |
| dousape2 | 1:4486f14a88ad | 41 | * Input : None. |
| dousape2 | 1:4486f14a88ad | 42 | * Output : None. |
| dousape2 | 1:4486f14a88ad | 43 | * Return : None. |
| dousape2 | 1:4486f14a88ad | 44 | *******************************************************************************/ |
| dousape2 | 0:2a0d1131965d | 45 | void serialRx() |
| dousape2 | 0:2a0d1131965d | 46 | { |
| dousape2 | 1:4486f14a88ad | 47 | while(pc.readable()) { // read all data from serial |
| dousape2 | 1:4486f14a88ad | 48 | char character=pc.getc(); // get a char form serial |
| dousape2 | 1:4486f14a88ad | 49 | if(((int)character==10 || (int)character==13) && serial_end_line) { // search for end line /r or /n |
| Foxnec | 3:e316c5e314e1 | 50 | serial_end_line=true; // end was find in the previous charascter, skip |
| dousape2 | 0:2a0d1131965d | 51 | continue; |
| dousape2 | 1:4486f14a88ad | 52 | } else { |
| dousape2 | 1:4486f14a88ad | 53 | serial_end_line=false; // clear serial_end_line flag |
| dousape2 | 1:4486f14a88ad | 54 | } |
| Foxnec | 3:e316c5e314e1 | 55 | if(serial_buffer_where!=buffer_fill && !(serial_buffer2_where != 0 && serial_buffer2_where!=buffer_fill)) { |
| dousape2 | 0:2a0d1131965d | 56 | serial_buffer[serial_buffer_where++]=character; |
| Foxnec | 3:e316c5e314e1 | 57 | if(serial_buffer_where==buffer_size) { // check if incoming data are smaller than buffer size |
| dousape2 | 1:4486f14a88ad | 58 | serial_buffer[buffer_size-1]='\0'; // posibility to lost data, if the incoming data are too big |
| dousape2 | 1:4486f14a88ad | 59 | serial_buffer_where=buffer_fill; // set index array to indicate buffer fill |
| dousape2 | 2:416bbbc23e82 | 60 | continue; |
| dousape2 | 0:2a0d1131965d | 61 | } |
| Foxnec | 3:e316c5e314e1 | 62 | if(character==13 || character==10) { // if end of line (\r \n) is indicated, prepare the buffer to serialGetBuffer |
| dousape2 | 1:4486f14a88ad | 63 | serial_buffer[serial_buffer_where-1]='\0'; // set end of buffer with 0 |
| dousape2 | 1:4486f14a88ad | 64 | serial_buffer_where=buffer_fill; // set index array to indicate buffer fill |
| dousape2 | 1:4486f14a88ad | 65 | serial_end_line=true; // end of line was find, set serial_end_line flag |
| dousape2 | 0:2a0d1131965d | 66 | } |
| dousape2 | 1:4486f14a88ad | 67 | } else if(serial_buffer2_where!=buffer_fill) { // same for second buffer |
| dousape2 | 0:2a0d1131965d | 68 | serial_buffer2[serial_buffer2_where++]=character; |
| dousape2 | 0:2a0d1131965d | 69 | if(serial_buffer2_where==buffer_size) { |
| dousape2 | 0:2a0d1131965d | 70 | serial_buffer2[buffer_size-1]='\0'; |
| dousape2 | 0:2a0d1131965d | 71 | serial_buffer2_where=buffer_fill; |
| dousape2 | 2:416bbbc23e82 | 72 | continue; |
| dousape2 | 0:2a0d1131965d | 73 | } |
| dousape2 | 0:2a0d1131965d | 74 | if(character==13 || character==10) { |
| dousape2 | 0:2a0d1131965d | 75 | serial_buffer2[serial_buffer2_where-1]='\0'; |
| dousape2 | 0:2a0d1131965d | 76 | serial_buffer2_where=buffer_fill; |
| dousape2 | 0:2a0d1131965d | 77 | serial_end_line=true; |
| dousape2 | 0:2a0d1131965d | 78 | } |
| Foxnec | 3:e316c5e314e1 | 79 | } /*else { // the buffers are full and thedata are lost |
| dousape2 | 0:2a0d1131965d | 80 | while(!pc.writeable()); |
| dousape2 | 0:2a0d1131965d | 81 | pc.printf("Code is too slow!\n"); |
| dousape2 | 1:4486f14a88ad | 82 | //pc.printf("I get: \"%c\", DEC: %d\n",character,(int)character); |
| dousape2 | 0:2a0d1131965d | 83 | }*/ |
| dousape2 | 0:2a0d1131965d | 84 | } |
| dousape2 | 0:2a0d1131965d | 85 | } |
| dousape2 | 0:2a0d1131965d | 86 | |
| dousape2 | 1:4486f14a88ad | 87 | /******************************************************************************* |
| dousape2 | 1:4486f14a88ad | 88 | * Function Name : serialGetBuffer. |
| dousape2 | 1:4486f14a88ad | 89 | * Description : Timer overflow handle. Blinks the LED. |
| dousape2 | 1:4486f14a88ad | 90 | * Input : Char array with size buffer_size. |
| dousape2 | 1:4486f14a88ad | 91 | * Output : Char array with incoming data. |
| dousape2 | 1:4486f14a88ad | 92 | * Return : 0 - on no data, 1 - one buffer is fill, 2 -two buffers are fill (posibility to lost data) |
| dousape2 | 1:4486f14a88ad | 93 | *******************************************************************************/ |
| dousape2 | 2:416bbbc23e82 | 94 | int serialGetBuffer(char * & data) |
| dousape2 | 0:2a0d1131965d | 95 | { |
| dousape2 | 0:2a0d1131965d | 96 | if(serial_buffer_where==buffer_fill && serial_buffer2_where==buffer_fill) { |
| dousape2 | 2:416bbbc23e82 | 97 | data=serial_buffer; |
| dousape2 | 2:416bbbc23e82 | 98 | //memcpy(data, serial_buffer, strlen(serial_buffer)+1); |
| dousape2 | 0:2a0d1131965d | 99 | serial_buffer_where=0; |
| dousape2 | 0:2a0d1131965d | 100 | return 2; |
| dousape2 | 0:2a0d1131965d | 101 | } else if(serial_buffer2_where==buffer_fill) { |
| dousape2 | 2:416bbbc23e82 | 102 | data=serial_buffer2; |
| dousape2 | 2:416bbbc23e82 | 103 | //memcpy(data, serial_buffer2, strlen(serial_buffer2)+1); |
| dousape2 | 0:2a0d1131965d | 104 | serial_buffer2_where=0; |
| dousape2 | 0:2a0d1131965d | 105 | return 1; |
| dousape2 | 0:2a0d1131965d | 106 | } else if(serial_buffer_where==buffer_fill) { |
| dousape2 | 2:416bbbc23e82 | 107 | data=serial_buffer; |
| dousape2 | 2:416bbbc23e82 | 108 | //memcpy(data, serial_buffer, strlen(serial_buffer)+1); |
| dousape2 | 0:2a0d1131965d | 109 | serial_buffer_where=0; |
| dousape2 | 0:2a0d1131965d | 110 | return 1; |
| dousape2 | 0:2a0d1131965d | 111 | } else { |
| dousape2 | 0:2a0d1131965d | 112 | return 0; |
| dousape2 | 0:2a0d1131965d | 113 | } |
| dousape2 | 0:2a0d1131965d | 114 | } |
| dousape2 | 0:2a0d1131965d | 115 | |
| dousape2 | 1:4486f14a88ad | 116 | /*********************************************************************************** |
| dousape2 | 1:4486f14a88ad | 117 | * Function Name : main. |
| dousape2 | 1:4486f14a88ad | 118 | * Description : Main routine. |
| dousape2 | 1:4486f14a88ad | 119 | * Input : None. |
| dousape2 | 1:4486f14a88ad | 120 | * Output : None. |
| dousape2 | 1:4486f14a88ad | 121 | * Return : None. |
| dousape2 | 1:4486f14a88ad | 122 | ***********************************************************************************/ |
| dousape2 | 1:4486f14a88ad | 123 | int main() |
| dousape2 | 1:4486f14a88ad | 124 | { |
| dousape2 | 1:4486f14a88ad | 125 | int i = 1; |
| dousape2 | 1:4486f14a88ad | 126 | int j = 0; |
| dousape2 | 2:416bbbc23e82 | 127 | int k = 0; |
| dousape2 | 1:4486f14a88ad | 128 | |
| Foxnec | 3:e316c5e314e1 | 129 | //array for data that are coming to user |
| dousape2 | 2:416bbbc23e82 | 130 | char * prijata_data; |
| dousape2 | 1:4486f14a88ad | 131 | |
| mfwic | 4:472805b710a0 | 132 | pc.baud(9600); // set serial speed to 9600 |
| Foxnec | 3:e316c5e314e1 | 133 | pc.attach(&serialRx,Serial::RxIrq); // attach serialRx to reception interrupt |
| mfwic | 4:472805b710a0 | 134 | //pc.attach(&serialRx); |
| mfwic | 4:472805b710a0 | 135 | pc.printf("Hello World !\r\n"); |
| dousape2 | 1:4486f14a88ad | 136 | |
| dousape2 | 1:4486f14a88ad | 137 | while(1) { |
| mfwic | 4:472805b710a0 | 138 | wait_ms(10); // wait 10 milliseconds |
| dousape2 | 1:4486f14a88ad | 139 | if(serialGetBuffer(prijata_data)) { // get char array from serial |
| dousape2 | 1:4486f14a88ad | 140 | sscanf (prijata_data,"%d",&j); // sscanf for searching integer |
| mfwic | 4:472805b710a0 | 141 | pc.printf("From PC:%d.\r\n" ,j); // write to serial incoming integer |
| dousape2 | 1:4486f14a88ad | 142 | } |
| dousape2 | 2:416bbbc23e82 | 143 | |
| dousape2 | 2:416bbbc23e82 | 144 | k++; |
| dousape2 | 2:416bbbc23e82 | 145 | |
| mfwic | 4:472805b710a0 | 146 | if (k>=6000) { //print once per second |
| mfwic | 4:472805b710a0 | 147 | pc.printf("This program running for %d minutess.\r\n", i++); // write some string to serial |
| dousape2 | 2:416bbbc23e82 | 148 | |
| dousape2 | 2:416bbbc23e82 | 149 | myled = !myled; // blink LED |
| dousape2 | 2:416bbbc23e82 | 150 | |
| dousape2 | 2:416bbbc23e82 | 151 | k=0; |
| dousape2 | 2:416bbbc23e82 | 152 | } |
| dousape2 | 1:4486f14a88ad | 153 | } |
| dousape2 | 1:4486f14a88ad | 154 | } |
| dousape2 | 1:4486f14a88ad | 155 |
