Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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.
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 27 Sep 2017Yes, 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 27 Sep 2017I 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 28 Sep 2017