I need to work out a divide ratio on a PIC for a baud rate. The number put into the PIC's baud rate generator is 6 bits, and is one less than the divide ratio.
With a baud rate of 1000, there is no divide, so a ratio of 1, and an input number of 0.
The only other baud rates that I need are 500, 250 and 125, with divides of 2, 4 and 8 so the number to be put into the register is 1, 3, or 7 for those baud rates.
If the baud rate isn't one of those, I don't want the code to crash or to try to put numbers outside the 6 bits, but I don't care what the divide ratio ends up as.
The code that I've got is:-
Now that is obviously quite short, runs quickly, and does what I want for the defined baud rates.
The issue is that for other starting numbers, the divide ratio would far from the nearest value. If I want a different baud rate divide, I can't just enter the right value. For example, if I put in a baud rate of 200, this code will give a value of 0 and a baud rate of 1000, where something more complicated could be written that would give the correct value of 4, so a divide of 5.
However, there are lots of other baud rates, like 800, which can't be done with the simple divide, and would need other parts of the code modifying if I were to ever allow for number like that.
So is it unwise to put in code like this, that isn't really looking at the value being sent to it, but instead is relying on a bit of a quirk of the numbers?
With a baud rate of 1000, there is no divide, so a ratio of 1, and an input number of 0.
The only other baud rates that I need are 500, 250 and 125, with divides of 2, 4 and 8 so the number to be put into the register is 1, 3, or 7 for those baud rates.
If the baud rate isn't one of those, I don't want the code to crash or to try to put numbers outside the 6 bits, but I don't care what the divide ratio ends up as.
The code that I've got is:-
Code:
mov baud_rate, w0
ff1r w0, w0 ;this finds which bit is the first 1 from the right, so 125 gives 1, 250 gives 2, 500 gives 3 and 1000 gives 4
mov #0b1110, w1
lsr w1, w0, w0 ; 0b1110 is shifted right by 1, 2, 3 or 4 to give 7, 3, 1 or 0
mov w0, BRG ; Move the answer into the baud rate generator
Now that is obviously quite short, runs quickly, and does what I want for the defined baud rates.
The issue is that for other starting numbers, the divide ratio would far from the nearest value. If I want a different baud rate divide, I can't just enter the right value. For example, if I put in a baud rate of 200, this code will give a value of 0 and a baud rate of 1000, where something more complicated could be written that would give the correct value of 4, so a divide of 5.
However, there are lots of other baud rates, like 800, which can't be done with the simple divide, and would need other parts of the code modifying if I were to ever allow for number like that.
So is it unwise to put in code like this, that isn't really looking at the value being sent to it, but instead is relying on a bit of a quirk of the numbers?