6 years, 8 months ago.

Find time between two bytes received by UART

Hello, I am new to arm MBED. I want to measure time between two bytes. Bytes are continuously coming from a sensor. i thought to do it with UART byte receive interrupt and timer. when one byte is received and interrupt occurs, the timer starts and count until the next interrupt occurs. I tried a lot but didn't found a way to code it. it would be great if you could suggest me some code. Thanks.

Kind Regards, Saba

1 Answer

6 years, 7 months ago.

Use a Timer object. You can use it in an interrupt.

Accepted Answer

Thanks for replying Jan. I did it already now i am able to find this time. Can you please tell me how to receive continuously more than one byte because with pc.getc() command i can receive only one byte at a time.

posted by Saba Asif 27 Sep 2017

Yes, you'll get the data byte by byte. You can implement a buffer yourself. E.g. something like:

char buffer[1024] = { 0 };
uint16_t buffer_ix = 0;

void uart_rx() {
    buffer[buffer_ix++] = serial.getc();
}

int main() {
    serial.attach(callback(&uart_rx), /* other parameters */);

    while (1) {
       // check the value of buffer to see if you need to do something
    }
}

Of course you need to make sure that you don't overflow the buffer.

posted by Jan Jongboom 27 Sep 2017

I want to ask that for this line "serial.attach(callback(&uart_rx), /* other parameters */);" why you used callback outside (&uart_rx) because i tried with my code and i observed that the functionality in same when i use callback(&uart_rx) and when i don't use.

posted by Saba Asif 28 Sep 2017

Yeah, it still works with callback(), but it's deprecated syntax. We use the callback class now to make it easier to bind both C-style callbacks and to point to member functions. So better wrap it.

posted by Jan Jongboom 28 Sep 2017

Thanks a lot Jan. I tried this buffer code with a bit modification and its reading only one index. how can i see all data inside buffer?and also how i can i check that my buffer is not overflowed.

posted by Saba Asif 28 Sep 2017