Question about Baudmode
Keith Hilton
Posts: 150
in BASIC Stamp
In a recent project I worked with Baudmode N9600. While trying to figure the code out I received the following help.
N9600 CON $4054 ' Baudmode for 9600 bps inverted serial. (Seetron LCD)
' $4054 = 16468 = 9600 Baud, 8-Bit, No-Parity, Inverted
Trying to figure out how $4054=16468=9600. In trying to figure this out I found the BS2-SX Parallax Stamp would use $40F0.
I know this is something extremely simple but I can't figure it out. Plus I can't stand passing over something that I don't understand. Help.
N9600 CON $4054 ' Baudmode for 9600 bps inverted serial. (Seetron LCD)
' $4054 = 16468 = 9600 Baud, 8-Bit, No-Parity, Inverted
Trying to figure out how $4054=16468=9600. In trying to figure this out I found the BS2-SX Parallax Stamp would use $40F0.
I know this is something extremely simple but I can't figure it out. Plus I can't stand passing over something that I don't understand. Help.
Comments
From the BS2 Manual 2.2:
https://www.parallax.com/downloads/basic-stamp-manual
pages 396 and 397 explain how to arrive at the values.
SEROUT starts on page 415 (419 in the PDF) of the BASIC Stamp Manual.
The Baudmode calculations are shown on page 418 (422) and there is a table of values on Page 419 (423).
For the BS2, it's INT(1,000,000 / Baud) - 20, and it's INT(2,500,000 / Baud) for the BS2sx.
1,000,000 can also be written as 1M.
So, 1M / 9600 = 104.16666, and INT just keeps the whole number or integer portion.
Then 104 - 20 = 84, which can also be written as $54.
Now this is for True Polarity.
For Inverted Polarity, we need to "set" Bit 14, which is 2 to the 14th power or 16384 = $4000.
Thus, 84 + 16384 = 16468, or in hexadecimal it's $4000 + $0054 = $4054.
Now then, the BS2 runs at 20 MHz and the BS2sx runs at 50 MHz, so 50 / 20 = 2.5.
Notice the BS2sx Baudmode formula uses 2.5M which is 2.5 times the BS2 value.
You could get the Baudmode directly or get it from the BS2 value.
First, we need to add back the 20 so 84 + 20 = 104, the multiply it by 2.5 to get 260.
Subtracting 20 from 260 gives 240 but let's double-check that it's correct.
So 2.5M / 9600 = 260.41666 and subtracting 20 gives 240 which can also be written as $F0.
For inverted polarity on the BS2sx we add 16384 to 240 to get 16624 or using hex $4000 + $F0 = $40F0.
If you look at pages 429 and 430 (433-434), you will see this this code segment which uses what's called Conditional Compilation.
In other words, it will only use the values for that selected Stamp module.