How to determine baud rate of microchip peripheral at runtime

baudratemicrocontrolleruart

I'm using a PIC24H Microchip microcontroller to communicate via UART with a Bluetooth Module. The Bluetooth module's baud rate can be configured at runtime via software (over bluetooth or over UART) and the baud rate will change after a power cycle.

Unfortunately the only way for the microcontroller to find out what baud rate the Bluetooth module's UART is using is by asking it–over UART.

Is there a way that I can have the microcontroller test (maybe by trial and error) what the baud rate of the bluetooth module is set to? It can only be a limited set of values and will probably be set to either 115K or 230K if that makes it easier.

Best Answer

I once had a similar problem with a microcontroller which didn't have a UART nor exactly known clock frequency (crystal was too expensive). I let the other party send a specific byte, for instance 0x01 and measured the time the bus was low. This was 8 bit times (startbit + 7 zeros of data). So dividing this by 8 I got the value I should set my timer to.
So, before you configure the I/O pins for UART use them as common I/O until the Bluetooth module sent this agreed upon character and time it. You could do this running a loop, or use timer capture to detect the falling and rising edges (more accurate).