PDA

View Full Version : User selectable baud rate



Andre_Pretorius
- 27th September 2014, 10:09
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

richard
- 27th September 2014, 10:49
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

Andre_Pretorius
- 27th September 2014, 13:46
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

Archangel
- 27th September 2014, 17:27
It's in here too:
http://www.picbasic.co.uk/forum/content.php?r=171-LCD-serial-backpacks

Andre_Pretorius
- 27th September 2014, 19:53
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

richard
- 27th September 2014, 23:13
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.

Andre_Pretorius
- 28th September 2014, 19:02
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 ?

Demon
- 28th September 2014, 19:48
He is probably on v3 and you are on v2.6 on less (like me).

Robert

richard
- 29th September 2014, 02:59
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