Question about Baudmode
            
                            
                                  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.
' -----[ Title ]----------------------------------------------------------- ' SEROUT-LCD.bs2 ' ' This program demonstrates how to use the SEROUT command based on the demo ' programs that are in the BASIC Stamp Manual. ' This program uses Conditional Compilation to select the necessary ' Baudmodes values based upon which Stamp module selected. ' The programmer no longer needs to worry about using the correct values ' for different modules since it's handled automatically. ' It also inclodes code for the older versions of the Serial LCD that ' Parallax used to carry. ' The SEROUT command starts on page 415 of the BASIC Stamp Manual, version ' 2.2, or page 419 of PDF. ' Compiler Directives ' ------------------- ' {$STAMP BS2sx} ' {$PBASIC 2.5} ' ' Use Conditional Compilation to select the appropriate value based on the Stamp ' module that's been selected. ' The following Baudmode values are for 8-Bit, No-Parity, True Polarity (T), ' and Always-Driven Output based on the formulas shown. #SELECT $STAMP #CASE BS2, BS2E, BS2PE ' INT(1,000,000 / Baud Rate) - 20 T1200 CON 813 ' 1200 Baud (aka bps or Bits Per Second) T2400 CON 396 ' 2400 Baud T9600 CON 84 ' 9600 Baud T19K2 CON 32 ' 19.2K bps T38K4 CON 6 ' 38.4K bps #CASE BS2SX, BS2P ' INT(2,500,000 / Baud Rate) - 20 T1200 CON 2063 T2400 CON 1021 T9600 CON 240 T19K2 CON 110 T38K4 CON 45 #CASE BS2PX ' INT(4,000,000 / Baud Rate) - 20 T1200 CON 3313 T2400 CON 1646 T9600 CON 396 T19K2 CON 188 T38K4 CON 84 #ENDSELECT ' -----[ Declarations ]---------------------------------------------------- ' -----[ I/O Definitions ]------------------------------------------------- PinLCD PIN 3 ' Serial output to Backpack/LCD. (BPI-216) ' -----[ Constants ]------------------------------------------------------- ' Add these to the Baudmode values above to select these optional modes Inverted CON $4000 ' Inverted Polarity (N) Open CON $8000 ' Open-drain and open-source output (O) ' These "open baudmodes" ' only actively drive the Tpin in one state ' (for the other state, ' they simply disconnect the pin; ' setting it to an input mode). ' Define additional Baudmodes for use by the Seetron LCD N2400 CON T9600 + Inverted ' 2400 Baud with Inverted Polarity N9600 CON T9600 + Inverted ' 9600 Baud with Inverted Polarity ' Scott Edwards (seetron) Serial LCD - Parallax #27923 ' Start by defining some useful constants for the Backpack. (BPI-216) BaudLCD CON N9600 ' Baudmode-9600 bps inverted. I CON 254 ' Instruction prefix value. ClrLCD CON 1 ' LCD Clear-screen instruction. HomeLCD CON 2 ' LCD Home (Cursor) instruction. Line1 CON 128 ' Address of 1st char of 1st line. Line2 CON 192 ' Address of 1st char of 2nd line. L1_C7 CON 135 ' Address of line 1, character 7. L2_C6 CON 198 ' Position: char 6 of 2nd line. L2_C9 CON 201 ' Position: char 6 of 2nd line. (192 + 9) ' -----[ Initialization ]-------------------------------------------------- ' Clear the LCD and print a message on lines 1 and 2. SEROUT PinLCD, BaudLCD,[I,ClrLCD,I,Line1,"Hello!",I,Line2,"There!."] ' -----[ Main Routine ]---------------------------------------------------- ' -----[ Subroutines ]-----------------------------------------------------