Seems as if the code does only run by chance and not by design.
I compiled first with optimisation 'none' and the code gets stuck in the first while loop in Transmit():
while (CCR0 != TAR)
CCR0 = TAR;
Reason seems to be that in optimisation `none', there is a jump inserted between setting CCR0 and comparing with TAR and so the comparison is always true (?).
Transmit:
while (CCR0 != TAR) // Prevent async capture
00FB90 9292 0170 0172 cmp.w &TAR,&TACCR0
00FB96 2404 jeq 0xFBA0
CCR0 = TAR; // Current state of TA counter
00FB98 4292 0170 0172 mov.w &TAR,&TACCR0
00FB9E 3FF8 jmp 0xFB90
CCR0 += Bitime; // Some time till first bit
00FBA0 50B2 0034 0172 add.w #0x34,&TACCR0
In higher optimisation settings the code is:
Transmit:
00FB8E 3C03 jmp 0xFB96
CCR0 = TAR; // Current state of TA counter
00FB90 4292 0170 0172 mov.w &TAR,&TACCR0
while (CCR0 != TAR) // Prevent async capture
00FB96 9292 0170 0172 cmp.w &TAR,&TACCR0
00FB9C 23F9 jne 0xFB90
CCR0 += Bitime; // Some time till first bit
00FB9E 50B2 0034 0172 add.w #0x34,&TACCR0
which works.
But, in higher optimisation (at least in 'high') the code fails again, this time due to lacking volatile declaration of some variables (I did not try to find out which, I just declared mostly all of them volatile, which is always a good start :-)).
All in all, I think that the code in its present state is not very well suited for beginners.
BR,
Jörg.
Edit: variable 'timermode' is only set but not used in main loop. Has to be declared volatile, because it is used in interrupt.