Seriale Rx/Tx
Dependencies: mbed
Revision 3:d63922c90649, committed 2022-05-03
- Comitter:
- pinofal
- Date:
- Tue May 03 12:39:29 2022 +0000
- Parent:
- 2:eeab69a684ad
- Commit message:
- Seriale Rx/Tx
Changed in this revision
COMM-RxTx.cpp | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/COMM-RxTx.cpp Tue May 03 12:39:29 2022 +0000 @@ -0,0 +1,45 @@ +#include "mbed.h" + +DigitalOut led(LED1); +DigitalOut Din(PB_9); +Serial pc(USBTX, USBRX, 9600); // seriale di comunicazione con il PC. Associati a PA_11 e PA_12 + +// carattere in arrivo dal PC +volatile unsigned char cReadChar; + + +void RxInterrupt(void) +{ + // ricevi caratteri su seriale, se disponibili + while((pc.readable())) + { + // leggi carattere e riscrivi carattere su UART + cReadChar = pc.getc(); + pc.printf("\r\nRx [CHAR]: %c\n\r",cReadChar); + pc.printf("Rx [HEX]: %#x\n\r", cReadChar); + pc.printf("Rx [reverse BIN]: "); + for(int i = 0; i< 8; i++) + pc.printf("%d",((cReadChar>>i)&0x01)); + pc.printf("\n\r"); + + + } +} + +//+++++++++ +// MAIN +//+++++++++ +int main() +{ + pc.printf("\n\r*** Welcome TxRx ***\n\r"); + + // definisci callback di IRQ + pc.attach(&RxInterrupt,Serial::RxIrq); + + // ciclo main + while(true) + { + led = !led; // Toggle LED + wait_ms(500); + } +} \ No newline at end of file
--- a/main.cpp Tue Jan 23 10:14:58 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -#include "mbed.h" - -// dimensione del pacchetto di comunicazione tra PC e uC -#define PACKETDIM 33 - -DigitalOut led(LED1); -Serial pc(SERIAL_TX, SERIAL_RX); - -// indice per i cicli -int nIndex; - -// indice dell'array caRxPacket[] -volatile int nRxIndex; -// carattere in arrivo dal PC -volatile char caReadChar; - -// paccchetto ricevuto dal PC -char caRxPacket[PACKETDIM]; -int nRxPacketSize; - -void RxInterrupt(void) -{ - // reset pacchetto ricevuto - nIndex=0; - for(nIndex=0;nIndex<PACKETDIM;nIndex++) - { - caRxPacket[nIndex]='\0'; - } - - // ricevi caratteri su seriale, se disponibili - while((pc.readable())) - { - pc.gets(caRxPacket,sizeof(caRxPacket)); - nRxPacketSize = strlen(caRxPacket); - //pc.printf("*** pc.readable = %2d \n\r",nRxPacketSize); - //pc.scanf("%s", &caRxPacket); - //+++pc.putc(pc.getc()); // read data from UART - - } - //+++pc.printf("%s",caRxPacket); - pc.printf("You also say goodbye to yours tk"); -} - -int main() -{ - // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto - //pc.baud(921600); //921600 bps - pc.baud(256000); //9600 bps - //pc.printf("*** SineWave Generation ***\n\r"); - - pc.attach(&RxInterrupt,Serial::RxIrq); - - while(true) - { - led = !led; // Toggle LED - } -} \ No newline at end of file