Enrico Marinoni
/
NUCLEOL152RE_U2andU1_Inter
How to use USART1 and USART2 under Interrupt
main.cpp@0:a6494748f42e, 2014-06-29 (annotated)
- Committer:
- emcu
- Date:
- Sun Jun 29 10:07:37 2014 +0000
- Revision:
- 0:a6494748f42e
NUCLEO-L152RE How to use USART1 and USART2 under Interrupt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emcu | 0:a6494748f42e | 1 | // |
emcu | 0:a6494748f42e | 2 | // U2andU1_Inter |
emcu | 0:a6494748f42e | 3 | // |
emcu | 0:a6494748f42e | 4 | // From: www.emcu.it |
emcu | 0:a6494748f42e | 5 | // Date: 29-06-2014 |
emcu | 0:a6494748f42e | 6 | // Tested on NUCLEO_L152RE |
emcu | 0:a6494748f42e | 7 | // for more info see here: http://www.emcu.it/NUCLEOevaBoards/NUCLEOevaBoards.html#Tutorial |
emcu | 0:a6494748f42e | 8 | // |
emcu | 0:a6494748f42e | 9 | /* |
emcu | 0:a6494748f42e | 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emcu | 0:a6494748f42e | 11 | * of this software and associated documentation files (the "Software"), to deal |
emcu | 0:a6494748f42e | 12 | * in the Software without restriction, including without limitation the rights |
emcu | 0:a6494748f42e | 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emcu | 0:a6494748f42e | 14 | * copies of the Software, and to permit persons to whom the Software is |
emcu | 0:a6494748f42e | 15 | * furnished to do so, subject to the following conditions: |
emcu | 0:a6494748f42e | 16 | * |
emcu | 0:a6494748f42e | 17 | * The above copyright notice and this permission notice shall be included in |
emcu | 0:a6494748f42e | 18 | * all copies or substantial portions of the Software. |
emcu | 0:a6494748f42e | 19 | * |
emcu | 0:a6494748f42e | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emcu | 0:a6494748f42e | 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emcu | 0:a6494748f42e | 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emcu | 0:a6494748f42e | 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emcu | 0:a6494748f42e | 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emcu | 0:a6494748f42e | 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
emcu | 0:a6494748f42e | 26 | * THE SOFTWARE. |
emcu | 0:a6494748f42e | 27 | |
emcu | 0:a6494748f42e | 28 | |
emcu | 0:a6494748f42e | 29 | ++++++++++++++++++++++ CONFIGURATIONS +++++++++++++++++++++++++++++++ |
emcu | 0:a6494748f42e | 30 | |
emcu | 0:a6494748f42e | 31 | USART2 it is used for debug (connected to the PC) using TeraTerm |
emcu | 0:a6494748f42e | 32 | USART1 it is used for show the way to use another USART |
emcu | 0:a6494748f42e | 33 | Both USART are not used under Interrupt |
emcu | 0:a6494748f42e | 34 | |
emcu | 0:a6494748f42e | 35 | The USART parameters are: |
emcu | 0:a6494748f42e | 36 | Baud Rate: 115200 |
emcu | 0:a6494748f42e | 37 | Data: 8 |
emcu | 0:a6494748f42e | 38 | Parity: NONE |
emcu | 0:a6494748f42e | 39 | Stop: 1 |
emcu | 0:a6494748f42e | 40 | Flow Control: NONE |
emcu | 0:a6494748f42e | 41 | |
emcu | 0:a6494748f42e | 42 | |
emcu | 0:a6494748f42e | 43 | ++++++++++++++++++++++ How to use this example ++++++++++++++++++++++ |
emcu | 0:a6494748f42e | 44 | |
emcu | 0:a6494748f42e | 45 | For test this example connect with a jumper D2 (SERIAL1_RX) to D10 (SERIAL1_TX). |
emcu | 0:a6494748f42e | 46 | |
emcu | 0:a6494748f42e | 47 | Press and release the Blue Button and you must see the green LED ON for 1 second. |
emcu | 0:a6494748f42e | 48 | At the same time some message are displayed via USB Virtual COM on your PC. |
emcu | 0:a6494748f42e | 49 | |
emcu | 0:a6494748f42e | 50 | Similar, if you press E on your Tera Term (PC) you must see the green LED ON for 1 second. |
emcu | 0:a6494748f42e | 51 | |
emcu | 0:a6494748f42e | 52 | |
emcu | 0:a6494748f42e | 53 | */ |
emcu | 0:a6494748f42e | 54 | |
emcu | 0:a6494748f42e | 55 | #include "mbed.h" |
emcu | 0:a6494748f42e | 56 | |
emcu | 0:a6494748f42e | 57 | Serial pc(SERIAL_TX, SERIAL_RX); // This is USART2 tx, rx |
emcu | 0:a6494748f42e | 58 | // It is used for Debug via VirtualCOM |
emcu | 0:a6494748f42e | 59 | // I suggest to use Tera TERM on PC |
emcu | 0:a6494748f42e | 60 | Serial Serial1(PB_6, PA_10); // This is USART1 tx, rx |
emcu | 0:a6494748f42e | 61 | DigitalOut myled(LED1); // This LED is on NUCLEO-L152RE |
emcu | 0:a6494748f42e | 62 | DigitalIn BlueButton(USER_BUTTON); // This is Blue-Button and is on NUCLEO-L153RE |
emcu | 0:a6494748f42e | 63 | |
emcu | 0:a6494748f42e | 64 | #define Pressed 0 |
emcu | 0:a6494748f42e | 65 | #define NotPressed 1 |
emcu | 0:a6494748f42e | 66 | |
emcu | 0:a6494748f42e | 67 | int Car='\0'; |
emcu | 0:a6494748f42e | 68 | int CarSerial1='\0'; |
emcu | 0:a6494748f42e | 69 | int n=0; |
emcu | 0:a6494748f42e | 70 | |
emcu | 0:a6494748f42e | 71 | void callback2() // USART2 used for Debug |
emcu | 0:a6494748f42e | 72 | { |
emcu | 0:a6494748f42e | 73 | // Note: you need to actually read from the serial to clear the RX interrupt |
emcu | 0:a6494748f42e | 74 | Car = pc.getc(); |
emcu | 0:a6494748f42e | 75 | } |
emcu | 0:a6494748f42e | 76 | |
emcu | 0:a6494748f42e | 77 | void USART1_INT() // USART1 |
emcu | 0:a6494748f42e | 78 | { |
emcu | 0:a6494748f42e | 79 | // Note: you need to actually read from the serial to clear the RX interrupt |
emcu | 0:a6494748f42e | 80 | CarSerial1 = Serial1.getc(); |
emcu | 0:a6494748f42e | 81 | } |
emcu | 0:a6494748f42e | 82 | |
emcu | 0:a6494748f42e | 83 | |
emcu | 0:a6494748f42e | 84 | int main() { |
emcu | 0:a6494748f42e | 85 | myled = 0; |
emcu | 0:a6494748f42e | 86 | |
emcu | 0:a6494748f42e | 87 | // SetUp the baud rate |
emcu | 0:a6494748f42e | 88 | pc.baud(115200); |
emcu | 0:a6494748f42e | 89 | Serial1.baud(115200); |
emcu | 0:a6494748f42e | 90 | |
emcu | 0:a6494748f42e | 91 | Serial1.attach(&USART1_INT); |
emcu | 0:a6494748f42e | 92 | pc.attach(&callback2); |
emcu | 0:a6494748f42e | 93 | |
emcu | 0:a6494748f42e | 94 | pc.printf("\n\r\n\r START MAIN \n\r"); |
emcu | 0:a6494748f42e | 95 | |
emcu | 0:a6494748f42e | 96 | while(1) |
emcu | 0:a6494748f42e | 97 | { |
emcu | 0:a6494748f42e | 98 | |
emcu | 0:a6494748f42e | 99 | // Test the Blue Button |
emcu | 0:a6494748f42e | 100 | if (BlueButton == Pressed) |
emcu | 0:a6494748f42e | 101 | { |
emcu | 0:a6494748f42e | 102 | while(BlueButton == Pressed) |
emcu | 0:a6494748f42e | 103 | { |
emcu | 0:a6494748f42e | 104 | if (n == 0) |
emcu | 0:a6494748f42e | 105 | pc.printf("Please release the BLUE Button\n\r"); |
emcu | 0:a6494748f42e | 106 | n++; |
emcu | 0:a6494748f42e | 107 | } |
emcu | 0:a6494748f42e | 108 | n = 0; |
emcu | 0:a6494748f42e | 109 | pc.printf("Send char. E to USART1\n\r"); |
emcu | 0:a6494748f42e | 110 | Serial1.putc('E'); // Send char. E to USART1 |
emcu | 0:a6494748f42e | 111 | } |
emcu | 0:a6494748f42e | 112 | |
emcu | 0:a6494748f42e | 113 | // Test the char. received from USART2 that is USB Virtual COM |
emcu | 0:a6494748f42e | 114 | if (Car == 'E') |
emcu | 0:a6494748f42e | 115 | { |
emcu | 0:a6494748f42e | 116 | { |
emcu | 0:a6494748f42e | 117 | pc.printf("Send char. E (received from USB_VirtualCOM USART2) to USART1\n\r"); |
emcu | 0:a6494748f42e | 118 | Serial1.putc('E'); // Send char. E to USART1 |
emcu | 0:a6494748f42e | 119 | } |
emcu | 0:a6494748f42e | 120 | Car = '\0'; |
emcu | 0:a6494748f42e | 121 | } |
emcu | 0:a6494748f42e | 122 | |
emcu | 0:a6494748f42e | 123 | // Test the char. received from USART1 |
emcu | 0:a6494748f42e | 124 | if (CarSerial1 == 'E') |
emcu | 0:a6494748f42e | 125 | { |
emcu | 0:a6494748f42e | 126 | { |
emcu | 0:a6494748f42e | 127 | pc.printf("RX char. E from USART1\n\r"); |
emcu | 0:a6494748f42e | 128 | myled = 1; |
emcu | 0:a6494748f42e | 129 | wait(1); |
emcu | 0:a6494748f42e | 130 | } |
emcu | 0:a6494748f42e | 131 | myled = 0; |
emcu | 0:a6494748f42e | 132 | CarSerial1 = '\0'; |
emcu | 0:a6494748f42e | 133 | } |
emcu | 0:a6494748f42e | 134 | |
emcu | 0:a6494748f42e | 135 | } |
emcu | 0:a6494748f42e | 136 | } |