Hello,
I seem to recall a previous thread on one of these forums (although I could not find it), that addressed the issue of 16-bit registers, where high byte must be written first. It was confirmed that these registers may be properly handled by writing a 16-bit value directly to the register, rather than two separate 8-bit values.
For example, in assembler the following behaves as required -
LDHX #8
STHX SCI1BD
The CW compiler should also give the correct result for the following code -
SCI1BD = 8;
I don't consider a run-time calculation for the baud rate divider value to be a good method. I would not be concerned about the number of cycle, but the number of bytes of flash wasted by the calculations. Even if multiple rates are required, for the majority of instances they will conform with standard rates, and are best handled as a look-up table of divisor values IMHO. Perhaps something like the following code would work -
#define BUSCLK 12000000L
#define SCICLK BUSCLK/16
#define DIV1152 SCICLK/115200
#define DIV576 SCICLK/57600
#define DIV384 SCICLK/38400
#define DIV192 SCICLK/19200
#define B1152 0 /* baudndx values */
#define B576 1
#define B384 2
#define B192 3
const word baudtab[4] = {DIV1152, DIV576, DIV384, DIV192};
void setbaud1( byte baudndx)
{
word temp;
temp = SCI1BD & 0xC000;
SCI1BD = baudtab[baudndx] | temp;
}
JimDon wrote:
In my case I can't change the clock, and it seems that 115200 with an 8.5% error talks to the pc fine
This amount of error is about twice the maximum allowable baud rate error for the SCI module (assuming the PCs baud rate setting has zero error). It is possible that this amount of error may seem to give correct operation, due to the re-synchronisation process within the receive part of the SCI module. Any timing errors are corrected at each data transition, and will prevent errors occurring provided the number of sequential 0's or 1's is limited. You may find that errors start to occur for received characters containing four or more successive 0's or 1's.
Regards,
Mac