i just want to share my experience. Once i also had the same case, my micro would get 'hang' everytime it drives a DC motor. The solution was to separate the ground tracks for high current purposes (motors, relays, etc) with the ground for digital system part.
In other words, we should always never forget about power supply noise (noise in Vcc or ground), that is why we should put decoupling caps near the Vcc pin of the micro.
I also had some unpredictable microcontroller behaviour which was caused by stack overwriting data part, or not enough RAM space for stack, or because of wrong initialization of stack pointer.
As for your program, i cant tell for sure because i am not familiar with C library functions. Usually i do UART communication the old fashion way (adopting the way we do it in ASM, but now using C language instead of assembly).