Verifying baud rate

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Verifying baud rate

Post by Don_Kirby »

I've added an option to a user interface to allow the user to change the interface's baud rate. I need to verify that the number that they enter is a valid rate before applying it. As this is menu driven, I'd prefer not to offer all of the choices in menu form, hence the direct number entry.

How would I go about calculating the validity of the number without explicitly looking for each valid value?

From the system reference:
the baud rate for any given channel must be an integral divisor of the maximum rate
This seems like the way to go, but I can come up with many more integral divisors than there are valid baud rates.

Obviously, I can verify by looking for any one of the valid baud rates, it's just that I'm looking for a more elegant solution.


-Don
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

There are several different aspects to what you are doing here.

1. Are you only going to allow baud rates with familiar numbers such as 19200 or will you also allow valid but unusual baud rates such as 46080?

2. You can set the baudrate on the PC to any value you like, but how does the micro know what value to use? If this was C you could write a automatic baud rate detection routine.

3. The standard formula (which is what I think you are looking for) is as follows:

Code: Select all

If &#40;14745600\16 mod CLng&#40;baudrate&#41;&#41; <> 0 Then
	baudrate = 19200
End If
I personally prefer a input field that is self-checking whenever possible. In this case a drop down list of single-selectable values. The user by definition can only select a valid value and there one less thing to worry about.
Mike Perks
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

mikep wrote:1. Are you only going to allow baud rates with familiar numbers such as 19200 or will you also allow valid but unusual baud rates such as 46080?
Yes, standard baud rates from 100 to 115200
mikep wrote:2. You can set the baudrate on the PC to any value you like, but how does the micro know what value to use?
The user specifies the baud rate to use in the devices' setup routine. There will be a default value for the purpose of connecting the first time.
mikep wrote:3. The standard formula (which is what I think you are looking for) is as follows:

Code: Select all

If &#40;14745600\16 mod CLng&#40;baudrate&#41;&#41; <> 0 Then
	baudrate = 19200
End If
This works, but doesn't rule out non-standard baud rates, such as 3600. While that wouldn't be a problem with the uC, it may be a problem with the PC side client, which may or may not be able to operate at those speeds. I need to avoid the user locking him/herself out of the device by selecting a baud rate that is not available on the majority of PC applications (read: Hyperterminal).
mikep wrote:[...]In this case a drop down list of single-selectable values. The user by definition can only select a valid value and there one less thing to worry about.
I agree completely, but the menu driven interface does not lend itself to the use of comboboxes or other similar selection methods. I could get the same functionality via the menu system, but it would require 2 more screens of choices (10 items per screen) which I am trying to avoid.

-Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Don_Kirby wrote:I agree completely, but the menu driven interface does not lend itself to the use of comboboxes or other similar selection methods.
Perhaps the best alternative, then, is to create a list of allowable rates in a ProgMem array.

Code: Select all

Dim baudRates as LongVectorData &#40;&#123;
  100, 300, 600, 1200,
  2400, 9600, 19200, 28800,
  38400, 57600, 115200
&#125;&#41;
An alternate implementation that might use less space is to use Integer values of 1/10th of the actual rate (e.g. 10, 30, 120, etc.) and then multiply by 10 before comparing with the specified rate.

The list of rates in the example are all supported by the hardware UART but not so with the software UARTs (Com3 - Com6).
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

mikep wrote:3. The standard formula (which is what I think you are looking for) is as follows:

Code: Select all

If &#40;14745600\16 mod CLng&#40;baudrate&#41;&#41; <> 0 Then
	baudrate = 19200
End If
A question for Don Kinzer. Are the hardware USARTs defined with double speed or single speed? If double speed then the divisor for this test should be 32 and not 16.

You should substitute the hardcoded value with a constant such as DEFAULT_BAUD_RATE which I omitted to do in my quick test.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

mikep wrote:Are the hardware USARTs defined with double speed or single speed?
Generally, single speed is used. However, double speed is used if the rate is too high to be implemented using single speed. The highest supported rate for single speed is the crystal frequency divided by 32.
- Don Kinzer
Post Reply