Usart Baud Rate Error

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

hansolo
Posts: 8
Joined: Sat Sep 22, 2007 11:48 pm
Location: South East Asia

Usart Baud Rate Error

Post by hansolo » Sun Sep 23, 2007 12:04 am

David,

I am currently using EasyPic4 Board with PIC18F452 and 20MHz crystal. I tested the following program with the usart.bas library.

Code: Select all

Device = 18F452 
Clock = 20 

// import modules... 
Include "DS18S20.bas" 
Include "convert.bas" 
Include "usart.bas" 
Include "utils.bas"

'My DS18S20 ID's 
// ROM ID...FAMILY $10 ($8A) ($000800E9B136) 
Const DeviceROMID(8) As Byte = ($10, $36, $B1, $E9, $00, $08, $00, $8A) 
                    
// working variables... 
Dim 
   TempA As ShortInt, 
   TempB As Byte
    
// program start... 
RomID = DeviceROMID 
SetAllDigital()
SetBaudrate(br4800)
SetPin(PORTE.2) 

USART.Write(13,10,"Reading DS18S20 Device",13,10) 

   While true 
      Convert 
      GetTemp(TempA, TempB) 
      USART.Write(DecToStr(TempA),".",DecToStr(TempB), $B0, "C", 13, 10)      
      DelayMS(1000) 
   Wend 
I found that if the baudrate is 4800 bps or less the SF IDE Serial Communicator display garbage. If I set the baudrate at 9600 bps or higher, the SF IDE Serial Communicator display correctly.

This is a bug report.

Hansolo
PIC Rules !!

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Sep 23, 2007 7:27 am

Try adding the following line before your module includes:

Code: Select all

#option USART_BRGH = False
I've just tested it on an EasyPIC3 at 2400 and it works OK with this.

Regards,

Steve

hansolo
Posts: 8
Joined: Sat Sep 22, 2007 11:48 pm
Location: South East Asia

Post by hansolo » Sun Sep 23, 2007 7:58 am

Steven wrote:Try adding the following line before your module includes:

Code: Select all

#option USART_BRGH = False
I've just tested it on an EasyPIC3 at 2400 and it works OK with this.

Regards,

Steve
What is this option for?

I never encounter this when I was working with MikroBasic from mE.

I will test out and see.

Thanks for your input.

Hansolo
PIC Rules !!

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Sep 23, 2007 8:09 am

It sets the baudrate high or low bit in the USART config registers - the datasheets might be useful to see the full details. I'm not an expert on it by any means, but I think that you can choose to set or not set the BRGH bit to get the best possible baudrate match at a certain combination of xtal speed and baudrate.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sun Sep 23, 2007 8:56 am

Thats correct Steven, from the Datasheet of 18F452

From Page 166
bit 2 BRGH: High Baud Rate Select bit
Asynchronous mode:
1 = High speed
0 = Low speed
From Page 168
In Asynchronous
mode, bit BRGH (TXSTA<2>) also controls the baud
rate. In Synchronous mode, bit BRGH is ignored.
Example 16-1 shows the calculation of the baud rate
error for the following conditions:
• FOSC = 16 MHz
• Desired Baud Rate = 9600
• BRGH = 0
• SYNC = 0
It may be advantageous to use the high baud rate
(BRGH = 1) even for slower baud clocks. This is
because the FOSC/(16(X + 1)) equation can reduce the
baud rate error in some cases.
Regards
Octal

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Sep 23, 2007 8:56 am

Try the PicMultiCalc utility from MisterE (http://mister-e.org/pages/utilitiespag.html) - it is quite instructive.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sun Sep 23, 2007 9:27 am

yes Steven, I agree. This utility app is very fine. Thanks to mister_e.

I hope, when I'll have a bit of free time to write such tool as plugin. I think it's the best experience to know all subtleties of PIC UART modules.

Regards

Octal

hansolo
Posts: 8
Joined: Sat Sep 22, 2007 11:48 pm
Location: South East Asia

Post by hansolo » Sun Sep 23, 2007 11:01 am

How can a beginner possibly know when and how to set the BRGH bit?

The usart library should be more user friendly to set the BRGH bit automatically based on the PIC used and the set baudrate.

Hope that David can improve the USART.bas library for the benefit of the beginners like me and others that do not bother to read the PIC datasheet.

Hansolo
PIC Rules !!

hansolo
Posts: 8
Joined: Sat Sep 22, 2007 11:48 pm
Location: South East Asia

Post by hansolo » Sun Sep 23, 2007 12:16 pm

Yup, add the codes below before Includes works :

Code: Select all

#option USART_BRGH = false
Thanks Steven.

Hansolo
PIC Rules !!

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Sep 23, 2007 3:54 pm

I'd encourage users to have a close look through some of the included modules - you can learn a lot from looking at how they are done. In addition, by looking at the #option statements at the top of a module (i.e. at the default values included at the top of modules), you can see what things can be set and then check out any that might apply - this is all I did. Hope this helps and good to hear that the USART is now working!

Kind regards,

Steve

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sun Sep 23, 2007 4:36 pm

I agree with Steven,
and I'll add that you should have a look at the Library Explorer Plugin written by John Barrat from the plugin page of the wiki http://www.sfcompiler.co.uk/wiki/pmwiki ... er.Plugins


Regards
Octal

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sun Sep 23, 2007 11:15 pm

More good points to add to Tim's WIKI article "Beginner's Guide...". 8)

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Mon Sep 24, 2007 2:28 am

A good point for the beginers guide is also that a compiler is a tool to aid a programmer in writing code quickly and efficiently.
It is not a replacement for reading the technical data on a product manufactured by another company(Microchip in this case and the PIC datasheet) that has thousands of options available within.
It is not a "Big Dummies" teaching aid.

The compiler will only do what you tell it, if you tell it to do things wrong it will do them wrong all day, that is not a bug it is poor programming or lack of understanding of the facts needed to accomplish the task in hand.

Hansolo you ask "How can a beginner possibly know when and how to set the BRGH bit?", the answer is really simple, read the data sheet and ask questions(politely) on the forum, please do not accuse the compiler of a bug where there is non, if there is problem the authour will hold his hans up and fix it no problem, but some one reading this without further comment may just think there are bugs without looking at the actual facts.

We have all been through the learning process and it can be a pain so I can understand, I am just a user like you so please do not misunderstand my reasoning for the post.

hansolo
Posts: 8
Joined: Sat Sep 22, 2007 11:48 pm
Location: South East Asia

Post by hansolo » Mon Sep 24, 2007 10:40 pm

Doj wrote:A good point for the beginers guide is also that a compiler is a tool to aid a programmer in writing code quickly and efficiently.
It is not a replacement for reading the technical data on a product manufactured by another company(Microchip in this case and the PIC datasheet) that has thousands of options available within.
It is not a "Big Dummies" teaching aid.

The compiler will only do what you tell it, if you tell it to do things wrong it will do them wrong all day, that is not a bug it is poor programming or lack of understanding of the facts needed to accomplish the task in hand.

Hansolo you ask "How can a beginner possibly know when and how to set the BRGH bit?", the answer is really simple, read the data sheet and ask questions(politely) on the forum, please do not accuse the compiler of a bug where there is non, if there is problem the authour will hold his hans up and fix it no problem, but some one reading this without further comment may just think there are bugs without looking at the actual facts.

We have all been through the learning process and it can be a pain so I can understand, I am just a user like you so please do not misunderstand my reasoning for the post.
Sorry, it is my mistake to write the posting this way.

The reason I had this impression is that I had not encountered this command when I was working with MBasic, MikroBasic, MikroC and CCS PICC compiler.

I really feel odd then this BRGH is required for such well acclaimed software like SF with so many praises.

Hansolo
PIC Rules !!

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Tue Sep 25, 2007 12:14 am

Hello hansolo, you make a point that on the surface seems perfectly valid, you quote that you are a beginner, yet you have experience with 4 other compilers and you have never come accross the requirement for an understanding of the REGISTER BRGH.
It is NOT a command in ANY compiler, it is a register in the PIC micro, it is a very important part of the setup for any PIC, it allows a user to control certain important aspects of the PICs serial transpot mechanism.
Incorrect use will stop use of the port, the fact that other compilers take away this control and present you with a less flexible but sometimes more initially aceptable way of using the port has nothing to do with the fact that the reason you are not aware of this register is due to the fact you do not read the data sheets and make no effort to learn about the inner workings of the PIC micros.

Unfortunately for you, admitting that you have never encountered the BRGH register is showing me that your level of understanding is very basic and trying to blame the compiler for your lack of undersatanding is a cover for you not wishing to understand more.

As you have tried 4 compilers before this one and have obviously not found anyone to write your code for you, why not try all the others before you come back and tell me I am talking rubbish.

This and many other compilers are all very good, the secret is to stick with one and figure out how to use it, the compiler can not make your code good, that is down to the user, a good programmer will make a poor compiler work well, a poor programmer will make all compilers work equally poorly.
You choose which category you wish to fit in to.

I stick by the original intention of my reply, that is that the compiler can not write your code for you, if you find a compiler that you are comfortable with then stick with it, as you have admitted, with your testing of 4 previous compilers you have not found one that suits your requirements, and I bet you have not paid for one of them, please tell me I am wrong, I will be happy to say well done for supporting all these compilers with your money, but I bet you are a freeloader who steals software or never moves beyond the free trial packages.

Just so you understand totally, you saying SF is not as good as the other compilers you mention is because SF does not tell you something about the PIC you that should find out for yourself ,is complete rubbish, it is because you do not know how to use a PIC correctly and are too lazy to figure it out.

The other opinion is that I am 100% wrong. mmmmm.

Post Reply