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
SITAEL-Comm.cpp@8:7a81e89f60df, 2022-05-18 (annotated)
- Committer:
- pinofal
- Date:
- Wed May 18 14:29:48 2022 +0000
- Revision:
- 8:7a81e89f60df
Test comm
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pinofal | 8:7a81e89f60df | 1 | #include "mbed.h" |
| pinofal | 8:7a81e89f60df | 2 | #include<stdlib.h> |
| pinofal | 8:7a81e89f60df | 3 | |
| pinofal | 8:7a81e89f60df | 4 | // dimensione massima del pacchetto ricevuto su seriale |
| pinofal | 8:7a81e89f60df | 5 | #define PACKETDIM 512 |
| pinofal | 8:7a81e89f60df | 6 | |
| pinofal | 8:7a81e89f60df | 7 | |
| pinofal | 8:7a81e89f60df | 8 | // Definizione periferica USB seriale del PC |
| pinofal | 8:7a81e89f60df | 9 | Serial pc(USBTX, USBRX, 921600); // seriale di comunicazione con il PC. Associati a PA_11 e PA_12 |
| pinofal | 8:7a81e89f60df | 10 | |
| pinofal | 8:7a81e89f60df | 11 | // Definizione periferica seriale del Modulo Seriale HC05 |
| pinofal | 8:7a81e89f60df | 12 | Serial mySerial(PA_9, PA_10, 115200); //Tx, Rx, bps |
| pinofal | 8:7a81e89f60df | 13 | |
| pinofal | 8:7a81e89f60df | 14 | |
| pinofal | 8:7a81e89f60df | 15 | // User Button, LED |
| pinofal | 8:7a81e89f60df | 16 | DigitalIn myButton(USER_BUTTON); // pulsante Blu sulla scheda. Associato a PC_13 |
| pinofal | 8:7a81e89f60df | 17 | DigitalOut myLed(LED2); // LED verde sulla scheda. Associato a PA_5 |
| pinofal | 8:7a81e89f60df | 18 | |
| pinofal | 8:7a81e89f60df | 19 | |
| pinofal | 8:7a81e89f60df | 20 | |
| pinofal | 8:7a81e89f60df | 21 | // array per la ricezione dei messaggi da Seriale |
| pinofal | 8:7a81e89f60df | 22 | volatile char caRxPacket[PACKETDIM]; |
| pinofal | 8:7a81e89f60df | 23 | |
| pinofal | 8:7a81e89f60df | 24 | // contatore e indice di caratteri ricevuti da Seriale |
| pinofal | 8:7a81e89f60df | 25 | volatile char nRxCharCount; // Contatore dei caratteri ricevuti; Si aggiornerà modulo 256 a buffer circolare |
| pinofal | 8:7a81e89f60df | 26 | volatile char nRxCharIndex; // Indice dell'array dei caratteri ricevuti e già visualizzati sui LED; Si aggiornerà modulo 256 a buffer circolare |
| pinofal | 8:7a81e89f60df | 27 | |
| pinofal | 8:7a81e89f60df | 28 | /**********************************************/ |
| pinofal | 8:7a81e89f60df | 29 | // IRQ associata a Rx da PC |
| pinofal | 8:7a81e89f60df | 30 | //**********************************************/ |
| pinofal | 8:7a81e89f60df | 31 | void pcRxInterrupt(void) |
| pinofal | 8:7a81e89f60df | 32 | { |
| pinofal | 8:7a81e89f60df | 33 | // array per la ricezione dei messaggi da seriale |
| pinofal | 8:7a81e89f60df | 34 | char cReadChar; |
| pinofal | 8:7a81e89f60df | 35 | |
| pinofal | 8:7a81e89f60df | 36 | // ricevi caratteri su seriale, se disponibili |
| pinofal | 8:7a81e89f60df | 37 | while((pc.readable())) |
| pinofal | 8:7a81e89f60df | 38 | { |
| pinofal | 8:7a81e89f60df | 39 | // acquisice stringa in input e relativa dimensione |
| pinofal | 8:7a81e89f60df | 40 | cReadChar = pc.getc(); // read character from PC |
| pinofal | 8:7a81e89f60df | 41 | mySerial.putc(cReadChar); // Diagnostica: write char to Seriale |
| pinofal | 8:7a81e89f60df | 42 | pc.putc(cReadChar); // Diagnostica: write char to PC |
| pinofal | 8:7a81e89f60df | 43 | |
| pinofal | 8:7a81e89f60df | 44 | //pc.printf("W>: 0x%02x\n\r",cReadChar); // diagnostica |
| pinofal | 8:7a81e89f60df | 45 | } |
| pinofal | 8:7a81e89f60df | 46 | } |
| pinofal | 8:7a81e89f60df | 47 | |
| pinofal | 8:7a81e89f60df | 48 | //**********************************************/ |
| pinofal | 8:7a81e89f60df | 49 | // IRQ associata a Rx da Seriale |
| pinofal | 8:7a81e89f60df | 50 | //**********************************************/ |
| pinofal | 8:7a81e89f60df | 51 | void SerialeRxInterrupt(void) |
| pinofal | 8:7a81e89f60df | 52 | { |
| pinofal | 8:7a81e89f60df | 53 | |
| pinofal | 8:7a81e89f60df | 54 | // carattere ricevuto da Seriale |
| pinofal | 8:7a81e89f60df | 55 | char cReadChar; |
| pinofal | 8:7a81e89f60df | 56 | |
| pinofal | 8:7a81e89f60df | 57 | while((mySerial.readable())) |
| pinofal | 8:7a81e89f60df | 58 | { |
| pinofal | 8:7a81e89f60df | 59 | // acquisice stringa in input e memorizza in array |
| pinofal | 8:7a81e89f60df | 60 | cReadChar = mySerial.getc(); // Read character |
| pinofal | 8:7a81e89f60df | 61 | nRxCharCount++; // incrementa il contatore dei caratteri ricevuti |
| pinofal | 8:7a81e89f60df | 62 | caRxPacket[nRxCharCount]=cReadChar; |
| pinofal | 8:7a81e89f60df | 63 | |
| pinofal | 8:7a81e89f60df | 64 | // visualizza l'ultimo carattere ricevuto |
| pinofal | 8:7a81e89f60df | 65 | pc.printf("> Rx: %c \r\n",cReadChar); // diagnostica |
| pinofal | 8:7a81e89f60df | 66 | } |
| pinofal | 8:7a81e89f60df | 67 | } |
| pinofal | 8:7a81e89f60df | 68 | |
| pinofal | 8:7a81e89f60df | 69 | /**********/ |
| pinofal | 8:7a81e89f60df | 70 | /* MAIN */ |
| pinofal | 8:7a81e89f60df | 71 | /**********/ |
| pinofal | 8:7a81e89f60df | 72 | int main() |
| pinofal | 8:7a81e89f60df | 73 | { |
| pinofal | 8:7a81e89f60df | 74 | // indice per i cicli |
| pinofal | 8:7a81e89f60df | 75 | int nIndex; |
| pinofal | 8:7a81e89f60df | 76 | |
| pinofal | 8:7a81e89f60df | 77 | // LED su scheda inizialmente spento |
| pinofal | 8:7a81e89f60df | 78 | myLed = 0; |
| pinofal | 8:7a81e89f60df | 79 | |
| pinofal | 8:7a81e89f60df | 80 | |
| pinofal | 8:7a81e89f60df | 81 | // messaggio di benvenuto |
| pinofal | 8:7a81e89f60df | 82 | pc.printf("\r\n***** Hallo ***** \r\n"); |
| pinofal | 8:7a81e89f60df | 83 | |
| pinofal | 8:7a81e89f60df | 84 | |
| pinofal | 8:7a81e89f60df | 85 | // inizializza contatore e indice di caratteri ricevuti |
| pinofal | 8:7a81e89f60df | 86 | nRxCharCount = 0; |
| pinofal | 8:7a81e89f60df | 87 | nRxCharIndex =0; |
| pinofal | 8:7a81e89f60df | 88 | |
| pinofal | 8:7a81e89f60df | 89 | // inizializza array di caratteri ricevuti |
| pinofal | 8:7a81e89f60df | 90 | for(nIndex=0; nIndex<PACKETDIM; nIndex++) |
| pinofal | 8:7a81e89f60df | 91 | { |
| pinofal | 8:7a81e89f60df | 92 | caRxPacket[nIndex]=0; |
| pinofal | 8:7a81e89f60df | 93 | } |
| pinofal | 8:7a81e89f60df | 94 | |
| pinofal | 8:7a81e89f60df | 95 | // Attiva la IRQ per la RX su seriale |
| pinofal | 8:7a81e89f60df | 96 | mySerial.attach(&SerialeRxInterrupt,Serial::RxIrq); // // entra in questa routine quando riceve un carattere dalla seriale del Seriale |
| pinofal | 8:7a81e89f60df | 97 | pc.attach(&pcRxInterrupt,Serial::RxIrq); // entra in questa routine quando riceve un carattere dalla USB del PC |
| pinofal | 8:7a81e89f60df | 98 | |
| pinofal | 8:7a81e89f60df | 99 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 100 | //++++++++++++++ INIZIO Test +++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 101 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 102 | |
| pinofal | 8:7a81e89f60df | 103 | |
| pinofal | 8:7a81e89f60df | 104 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 105 | //++++++++++++++ FINE Test +++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 106 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 107 | |
| pinofal | 8:7a81e89f60df | 108 | |
| pinofal | 8:7a81e89f60df | 109 | |
| pinofal | 8:7a81e89f60df | 110 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 111 | //++++++++++++++ INIZIO Ciclo Principale +++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 112 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 113 | while(true) |
| pinofal | 8:7a81e89f60df | 114 | { |
| pinofal | 8:7a81e89f60df | 115 | //++++++++++++++++++++++++++ INIZIO Se premuto il myButton effettua un test +++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 116 | if(myButton == 0) |
| pinofal | 8:7a81e89f60df | 117 | { |
| pinofal | 8:7a81e89f60df | 118 | myLed =1; |
| pinofal | 8:7a81e89f60df | 119 | } |
| pinofal | 8:7a81e89f60df | 120 | else |
| pinofal | 8:7a81e89f60df | 121 | { |
| pinofal | 8:7a81e89f60df | 122 | myLed =0; |
| pinofal | 8:7a81e89f60df | 123 | } |
| pinofal | 8:7a81e89f60df | 124 | //++++++++++++++++++++++++++ FINE Se premuto il myButton effettua test +++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 125 | |
| pinofal | 8:7a81e89f60df | 126 | } //while(true) Ciclo principale |
| pinofal | 8:7a81e89f60df | 127 | |
| pinofal | 8:7a81e89f60df | 128 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 129 | //++++++++++++++ FINE Ciclo Principale +++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 130 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| pinofal | 8:7a81e89f60df | 131 | |
| pinofal | 8:7a81e89f60df | 132 | } // main() |