PDA

View Full Version : Usart Questions



shawn
- 6th March 2008, 06:22
Hello
Everyone

A couple questions for ya. First of all which of these statements executes faster. Everyone says use hardware USART, but I'm not sure if it would always be as fast. Here is two sets of code which executes fastest and most efficiently out of them. Is one actually better than another.

Number = 0
WHILE Number < 3
TXREG = Data[Number]
Number = Number + 1
WEND

''''''''''''''or'''''''''''''''''''

HSEROUT [Data[0],Data[1],Data[2]]


Next question, which is faster.

Number = 0
While Number < 3
'Code in here converst all three array variables so that they will be display properly
'whereas the HSEROUT statement bellow does it for you.
wend

Number = 0
WHILE Number < 3
TXREG = Data[Number]
Number = Number + 1
WEND

''''''''''''or''''''''''''''

HSEROUT [Hex2 Data[0],Hex2 Data[1],Hex2 Data[2]]


Final question, could someone give an example of when or why you would need to poll the TXIF: USART Transmit Interrupt Flag bit. Boring questions I know, but I am just trying to better understand the on board usart.

Thanks for looking
Shawn

Darrel Taylor
- 6th March 2008, 07:30
Number = 0
WHILE Number < 3
TXREG = Data[Number]
Number = Number + 1
WEND

''''''''''''''or'''''''''''''''''''

HSEROUT [Data[0],Data[1],Data[2]]

The first example would execute much quicker.
Unfortunately, it does so by not sending all 4 bytes like you might anticipate.

When putting bytes in TXREG, you have to make sure that the USART is finished sending the last byte you put in it.

Typically, you would check the TXIF bit to make sure it's safe to send, which the first example doesn't do, and the HSEROUT does automatically.



Next question, which is faster.

Number = 0
While Number < 3
'Code in here converst all three array variables so that they will be display properly
'whereas the HSEROUT statement bellow does it for you.
wend

Number = 0
WHILE Number < 3
TXREG = Data[Number]
Number = Number + 1
WEND

''''''''''''or''''''''''''''

HSEROUT [Hex2 Data[0],Hex2 Data[1],Hex2 Data[2]]

Most likely, the HSEROUT will be faster.
But there's no way of knowing, without the actual ..."Code in here converst all three array variables so that they will be display".


Final question, could someone give an example of when or why you would need to poll the TXIF: USART Transmit Interrupt Flag bit.
Because of the first answer. :)

hth,

falingtrea
- 6th March 2008, 19:11
Unless the compiler optimizes your code, any code written in a high level language is going to be slower than a single function or procedure call for that high level language. Something like the HSEROUT function is probably written in assembly. And as Darrel points out, there are other things HSEROUT is doing that your replacement code does not.

Bruce
- 6th March 2008, 21:10
Something like the HSEROUT function is probably written in assembly
Every single line of code in the PBP library is in assembly.


any code written in a high level language is going to be slower than a single function or procedure call for that high level language
OK. I'm a little slow. Could you explain this statement in laymans terms..;o}

falingtrea
- 7th March 2008, 15:36
Basically, what I mean is if you write code in BASIC to perform a function it will generate more assembly code than the same function written in assembly. That is because typically there is overhead code generated by the BASIC routines that you can avoid when you write directly in assembly.

So your example of controlling the UART with BASIC code will be more inefficient that the HSEROUT call because of all those extra assembly code lines that the BASIC procedures create.

shawn
- 8th March 2008, 01:17
Thanks for the answers guys

Shawn