User selectable baud rate


+ Reply to Thread
Results 1 to 9 of 9
  1. #1

    Default User selectable baud rate

    Hi all
    I am trying to select the baud rate via dip switches at start-up
    first attempt
    IF bRate = 2 then
    'DEFINE HSER_BAUD 14400 ' 14400 Baud
    ENDIF
    IF bRate = 3 then
    DEFINE HSER_BAUD 19200 ' 19200 Baud
    ENDIF

    Error - mutable defines of HSER_BAUD

    Second attempt
    DEFINE HSER_BAUD baud - Setting the baud equal to variable and then manipulating the variable through if statements
    error - variable not defined (has been defined 2 line higher

    Has any one done some thing like this before or got any more ideas I can try

    Thanks in advance
    Andre

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,945

    Default Re: User selectable baud rate

    as you have discovered something can only be defined once
    a workable option is to
    DEFINE HSER_BAUD 19200 ; your default baud rate
    then at boot up read your dip sw
    if its necessary to alter baud rate then
    the SPBRG register can be altered to suit the new baud rate.
    you have provided no code or pic type or osc freq so you will need to calculate the spbrg value yourself
    or find PicMulticalc by misterE
    Last edited by richard; - 27th September 2014 at 11:50. Reason: spelling

  3. #3

    Default Re: User selectable baud rate

    Hi all here is the code I use now and it works, 48M crystal on a PIC18F4550
    Next will be parity, I will copy the code again when it works

    If bRate = 0 THEN
    RCSTA = $90 ' Enable serial port & continuous receive
    TXSTA = $24 ' Enable transmit, BRGH = 1
    SPBRG = 225 ' 9600 Baud @ 0.0%
    SPBRGH = 4
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    ENDIF

    If bRate = 1 THEN
    RCSTA = $90 ' Enable serial port & continuous receive
    TXSTA = $24 ' Enable transmit, BRGH = 1
    SPBRG = 112 ' 19200 Baud @ 0.0%
    SPBRGH = 2
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    ENDIF

    If bRate = 2 THEN
    RCSTA = $90 ' Enable serial port & continuous receive
    TXSTA = $24 ' Enable transmit, BRGH = 1
    SPBRG = 56 ' 38400 Baud @ -0.16%
    SPBRGH = 1
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    ENDIF

    If bRate = 3 THEN
    RCSTA = $90 ' Enable serial port & continuous receive
    TXSTA = $20 ' Enable transmit, BRGH = 0
    SPBRG = 51 ' 57600 Baud @ 0.16%
    SPBRGH = 0
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    ENDIF

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,817

    Default Re: User selectable baud rate

    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5

    Default Re: User selectable baud rate

    Thanks for this I had a look and it could have saved me a few hours - will definitely remember it now.
    I have another question in my quest for user select ability :is there some were I can read up on what the PBP command HSER_EVEN does?
    I have set it up now for 8 and 9 bit operation but as soon as I add HSER_EVEN or ODD even in if commands every thing stops it is as if a define is processed no matter what.

    Any thoughts on this

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    1,945

    Default Re: User selectable baud rate

    defines are set in concrete when your program is compiled ,they cannot be used to alter precompiled code at all '
    if you wish to change parity or bit length on the fly you will need to write your own version of hserin/hserout
    2.3 DEFINEs
    The DEFINE keyword in PBP is used to set parameters for compilation and
    assembly. This should not be confused with #DEFINE, which is used for
    conditional compilation. Further explanation of DEFINE and how it works can be
    found at the end of this section.
    before you ask

    The #DEFINE directive creates a compile-time constant and, optionally, assigns a
    value to the constant.

  7. #7

    Default Re: User selectable baud rate

    Thanks Richard
    Just a question from what manual did you get the quote I had a look in the manual I got when I bought PBP and it does not match, is there a advance manual for PBP ?

  8. #8
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,256

    Default Re: User selectable baud rate

    He is probably on v3 and you are on v2.6 on less (like me).

    Robert

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    1,945

    Default Re: User selectable baud rate

    Robert is right , the excerpt is from the pbp3 online pdf .
    I had a look at my old pbp2.6 printed book and its definitely not as informative on this issue , I hope you get the concept though
    any defines are set in concrete after compliation and not really expected to be alterable at runtime

Similar Threads

  1. RS232 Baud Rate
    By gadelhas in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th August 2010, 01:06
  2. MCLoader baud rate
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 14th July 2010, 02:41
  3. Baud rate calculation
    By Demon in forum Serial
    Replies: 12
    Last Post: - 21st July 2006, 23:02
  4. Detect baud rate
    By Dick M in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd July 2005, 22:10
  5. User Selectable LED Brightness
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th April 2005, 19:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts