|
|
View previous topic :: View next topic |
Author |
Message |
koenbielen
Joined: 23 Apr 2009 Posts: 42
|
#use rs232 |
Posted: Tue Jul 08, 2014 2:37 am |
|
|
Hello
Im having troubles with a analog input and RS232
At startup i put
#use rs232(baud=38400,xmit=pin_b5,rcv=pin_a2,stream=EEPROMPORT)
im doing some checks on this port for 5 seconds.
If no reply is done by receiving a service command I continue the program.
But the pin is never going back to input xmit=pin_b5
even if I place a set_tris_b(0xFF) command.
so how can u undo the
#use rs232(baud=38400,xmit=pin_b5,rcv=pin_a2,stream=EEPROMPORT) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Tue Jul 08, 2014 3:10 am |
|
|
#USE RS232, on it's own doesn't do anything to stop the chip.
It is a configuration that is used by the functions getc, and putc.
It should be at the top of your file as a configuration. It is not code to use in the program.
Only getc can hang. Read the manual entry for it:
"This function waits for a character to come in over the RS232 RCV pin and returns the character."
Note 'waits'.....
The sequence should be:
Code: |
#include <18F46K22.h> //chip definition
//ADC setup here if used.
//Now the fuses - depend on your device
#FUSES NOWDT,NOBROWNOUT,NOLVP,NOXINST
//Now the clock
#use delay(crystal=20000000)
//Now the RS232 configuration STREAM name if required
#use rs232(baud=38400,parity=N,UART1,bits=8,,errors)
//You must _always_ use ERRORS with a hardware UART, unless
//you generate your own error handling code.
void main(void)
{
int16 delay_ctr;
char x;
//Now your code.
//If you want to wait for a command, key is that you must _not_
//call 'getc', unless a character is available
for (delay_ctr=0;delay_ctr<50000;delay_ctr++)
{
if (kbhit())
{
//Now there _is_ a character so you can call getc
x=getc();
//and do what you want with the character parse the command
//preferably with a state machine
}
else
delay_us(100));
}
//now, the delay must be shorter than the time between
//characters (260uSec for 38400bps). To avoid characters being missed.
//If no character is available, the delay gets executed, so the unit
//pauses for 100*50000uSec
|
Alternatively, you can set it so getc, won't hang (by adding TIMEOUT=xx to the RS232 declaration). Where 'xx' is a time in mSec to wait for a character. However your code then needs to change to test if the returned character is ==0, and RS232_ERRORS==0, to know that a timeout has occurred.
Do a search in the manual, for 'getc_timeout'.
Understand that if you are using gets, this calls getc. |
|
|
koenbielen
Joined: 23 Apr 2009 Posts: 42
|
|
Posted: Tue Jul 08, 2014 3:29 am |
|
|
Hello
Thanks for fast reply but you misunderstood my post
The problem in have is that my pin is set to output with the
#use rs232(baud=38400.... code
But never can be set again as analog input.
This is a analog input connector and is used at startup to go into service mode via serial commands.
So for a short time i check the pin.
If no command is received i continue the program.
So far so good.
Only thing is that when I further in my code specify the pin to be an analog input --> I will stay output.
Kind Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Tue Jul 08, 2014 3:59 am |
|
|
You need to turn the UART off before the pin can be used for other things.
setup_UART(FALSE);
Tells the compiler to disable the UART. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|