View previous topic :: View next topic |
Author |
Message |
object01
Joined: 13 May 2004 Posts: 90 Location: Nashville, TN
|
SETUP_UART() vs. #use rs232 |
Posted: Tue Sep 28, 2004 8:34 am |
|
|
Other than being able to switch UART settings at run-time, is there any substantial benefit to managing UARTs with SETUP_UART() vs. a #use rs232 directive? Did the former make the latter obsolete?
--
Jeff S. |
|
|
Guest
|
|
Posted: Tue Sep 28, 2004 10:59 am |
|
|
#use rs232 means a fully static usage of the UART.
If you know the one-and-only baud rate then use it.
But if you want to implement an auto-baudrate mechanism or you have 2 or more devices connected to PIC with different speed then you have to use setup_uart() (e.g. I have 6 RS485 devices @2400Bd and one @19200Bd) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 28, 2004 11:35 am |
|
|
Quote: | Did the former make the latter obsolete ? |
No, it did not. The #use rs232() statement must still be used
to define the UART. (ie., the pins, initial baudrate, etc.)
After that's done, then you can use the setup_uart() function
to turn the UART on/off, or change the baud rate. Note that
this applies to the hardware UART only.
Also, if your PIC has an Enhanced UART, then setup_uart()
is used to control its features. For more info, see the latest
Readme.txt file in this folder: c:\Program Files\Picc
Also see the example file, RS485.C, in this folder:
c:\Program Files\Picc\Drivers |
|
|
Guest
|
|
Posted: Tue Sep 28, 2004 4:05 pm |
|
|
This is not an answer but actually an extension of the question above. How does one change the Parity setting at runtime? I see how setup_uart could be used to change the baudrate at runtime but what about parity and also stop bits? |
|
|
Guest
|
|
Posted: Tue Sep 28, 2004 4:06 pm |
|
|
This is not an answer but actually an extension of the question above. How does one change the Parity setting at runtime? I see how setup_uart could be used to change the baudrate at runtime but what about parity and also stop bits? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 28, 2004 5:03 pm |
|
|
The Readme.txt file doesn't list parity or stop bits as options for the
setup_uart() function. Here are the parameters that can be used:
This is from the Readme.txt file.
For the Enhanced UART only:
UART_AUTODETECT - Waits for a 0x55 character and sets the UART
baud rate to match.
UART_AUTODETECT_NOWAIT - As above however the function
returns before 0x55 is rcvd. KBHIT() will be true when the match is
made. A call to GETC() will clear the character.
UART_WAKEUP_ON_RDA - The UART will wake the PIC up out of sleep
when RCV goes from high to low.
----------
For all (hardware) UARTs:
UART_ADDRESS - UART only accepts data with 9th bit =1
UART_DATA - UART accepts all data
TRUE - Turn on the UART.
FALSE - Turn off the UART.
[Baud Rate] - Changes to the specified baud rate (9600, 4800, etc.)
and turns on the UART. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 28, 2004 5:31 pm |
|
|
You have to handle parity via software and the nineth bit if you want 8 data bits. If you want 2 stop bits, then you can also use the nineth bit (9 bit mode) but lose the parity if you still want 8 data bits. |
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Sun Aug 01, 2010 9:31 am |
|
|
Hi, is this possible to use UART_AUTODETECT with 18F4520 in modbus.c (ccs library) ?
There only the first byte must be 0x55 and the others stay the same as in modbus structure?
For example:
0x55, address, length, ...., crc
right?
And I've tried it as follow:
Code: |
#use rs232(baud=UART_AUTODETECT, UART1, parity=N, stream=MODBUS_SERIAL, ERRORS)
|
but it could not compile. How can be implemented in the code?
Is enough to add setup_uart(UART_AUTODETECT); after the #use rs232 code?
When I read it with getc() what I'll get? 0x55? or the next byte after 0x55?
Thanks |
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Mon Aug 02, 2010 2:58 pm |
|
|
any answer? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 02, 2010 3:16 pm |
|
|
The reason I didn't answer was because so much of it can be looked up
in the manual or the 18F4520 data sheet. I checked both of them.
Quote: |
And I've tried it as follow:
#use rs232(baud=UART_AUTODETECT, UART1, parity=N, stream=MODBUS_SERIAL, ERRORS)
but it could not compile.
|
The CCS manual doesn't say this allowed, so you can't do it.
That's why it doesn't compile.
Quote: | Is enough to add setup_uart(UART_AUTODETECT); after the #use rs232 code? |
You should make a test program and experiment. That's how I would
solve the problem.
Quote: | When I read it with getc() what I'll get? 0x55? or the next byte after 0x55? |
The answer is given in the PIC data sheet, in this section:
Quote: |
18.1.3 AUTO-BAUD RATE DETECT
|
You should make a very small test program and attempt to solve the
problem. If you can't solve it, then post the test program and your
compiler version. |
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Mon Aug 02, 2010 4:38 pm |
|
|
Thanks but how can I know how works in CCS built-in function...
ABDEN bit set by user and automatically cleared. How can I know this without asking, does the setup_uart() set it for every received data or only once...
Will I get the 0x55 value for the first time or not? Maybe CCS ignores and does not give this value when it is first byte.
I just asked that to know if there is someone who has experience in this, otherwise everyone knows everything is possible to be learned by testing. OK I'll test it for all here. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 02, 2010 4:59 pm |
|
|
Right, but you can find this information with a test program.
Create simple program like this:
Code: |
#include <18F4520.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main(void)
{
setup_uart(UART_AUTODETECT);
while(1);
}
|
Compile it and look at the ASM code produced by the compiler.
It sets the ABDEN bit. That's mentioned in the 18F4520 data sheet.
Then it tests that bit in loop, and waits for it to become 0. Then it
exits the loop. Then it reads the byte from the UART. This is why I
always say to make a little test program and look at the .LST file.
The answer will be there.
Code: |
.................... setup_uart(UART_AUTODETECT);
002C: BSF BAUDCON.ABDEN // Set ABDEN = 1
002E: BTFSC BAUDCON.ABDEN // Wait in loop until ABDEN = 0
0030: BRA 002E
0032: MOVF RCREG,W // Read byte from UART
....................
....................
.................... while(1);
0034: BRA 0034
.................... }
|
This was compiled with compiler vs. 4.109. |
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Tue Aug 03, 2010 6:55 am |
|
|
My compiler does not shows the names of registers and bits. v4.108
Code: | 002C: BSF BAUDCON.ABDEN // Set ABDEN = 1
002E: BTFSC BAUDCON.ABDEN // Wait in loop until ABDEN = 0
0030: BRA 002E |
What I understand from this code is it waits in infinity-loop. That means I can't write this as initial routine. My uart works with interrupt and how will work this I did not understand really. When I add this code it will wait until a byte has coming. If I could understand correct, this option is not usable. I hope there isn't an example code for this feature in CCS directory.
My compiler gives this:
Code: | .................... setup_uart(UART_AUTODETECT);
007A: BSF FB8.0
007C: BTFSC FB8.0
007E: BRA 007C
0080: MOVF FAE,W |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 03, 2010 12:37 pm |
|
|
Quote: |
My compiler does not shows the names of registers and bits. v4.108
|
It should. I'm using MPLAB. Go into the Project Menu. Select Build
Options. Then find the section for List File format. Select "Symbolic"
format. That will give you the register names in the .LST file.
I searched the CCS directories, and I could not find an example file
for the UART_AUTODETECT feature. I don't think they have one. |
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Tue Aug 03, 2010 12:44 pm |
|
|
Thanks for the compiler options, it become better now.
If there are some people who work using this uart option, let inform us. |
|
|
|