|
|
View previous topic :: View next topic |
Author |
Message |
X-Rocka Guest
|
Switching between Hardware / Software RS232 ? |
Posted: Wed Dec 14, 2005 10:33 am |
|
|
Hello,
I've got the following setup:
I'm using a PIC16F628A, only the RX-pin connected to a serial one-wire bus with other PICs.
Receiving works great, I use the hardware USART.
Here comes the problem:
I try to switch to software RS232 using another #USE RS232 ... directive, and now the RX pin becomes the transmitter.
But that doesn't work! I tried another pin to use SW RS232 (not the TX pin though), that worked without problems.
So my guess is that the CCS compiler refuses to use a hardware RX pin as a software transmitter.
Can anybody confirm that? Or better tell me a solution?
thx,
X-Rocka |
|
|
X-Rocka Guest
|
|
Posted: Wed Dec 14, 2005 10:37 am |
|
|
I forgot to mention:
YES, I do switch TRIS to make the RX pin an output!
X |
|
|
Ttelmah Guest
|
|
Posted: Wed Dec 14, 2005 11:30 am |
|
|
You need to disable the hardware UART to do this.Basically if the UART is enabled, it takes over the driving of the serial pins. So, once you have a hardware UART operating on it's pins, you cannot use these pins for normal I/O (which a software UART will involve), till the UART is turned off.
Something like:
Code: |
#ifdef __PCM_
#bit SPEN = 0x18.7
#else
#bit SPEN = 0xFAB.7
#endif
#define UART_ON SPEN=1
#define UART_OFF SPEN=0
|
This will only work for the 'single UART' 18 family chips, on the latter units with multiple UARTs, you will need to redefine the SPEN bit.
You can then use your hardware UART, then execute:
UART_OFF;
Then use the software stream on the same pins, and when it has finished use:
UART_ON;
to turn the UART back on.
Best Wishes |
|
|
X-Rocka Guest
|
|
Posted: Wed Dec 14, 2005 12:43 pm |
|
|
thanks Ttelmah!
I'll try that tomorrow, but I'd say that will work.
Somehow I didn't think of accessing these UART bits
directly...
X |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
X-Rocka Guest
|
|
Posted: Thu Jan 05, 2006 6:49 am |
|
|
Hello again to all,
first of all thanks, disabling the UART was the solution!
So now I am able to use the hardware RX pin also to transmit,
works perfect! I simply disable the HW UART with the following lines:
//************************
// These functions needed to use RX pin as a RS232 transmitter!
void enable_RS_RX( void ) {
#ASM
bsf tris_b, 1 // RS_RX = input.
bsf rcsta, bCREN // Enable continuous receive.
bsf rcsta, bSPEN // Enable USART.
#ENDASM
}
void disable_RS_RX( void ) {
#ASM
bcf rcsta, bCREN // Disable continuous receive.
bcf rcsta, bSPEN // Disable USART.
bcf tris_b, 1 // RS_RX = output.
#ENDASM
}
but now for another problem...
on another card I have the PIC16F628A's hardware TX pin on the same
serial bus, and guess what, I also want to receive data via that pin!
but when I disable the UART and set the TX pin to input, it still seems
to be an always high output.
maybe you find an error in the following lines?!:
void read_serbus(void)
{
unsigned long read_cnt = 0;
unsigned int read_okay = 0;
char bus_serin = 'x';
while (!HWTX_DONE || read_cnt++ < 10) // Wait for end of HW transmit.
{
delay_ms(1);
}
read_cnt = 0;
disable_USART();
// Here's the line to define SW RS232:
#USE RS232 (BAUD=9600, XMIT=SW_TX, RCV=SW_RX, FORCE_SW, BITS=8, ERRORS)
putc(CR);
do // waiting for answer on HW TX pin...
{
if ( kbhit() )
{
bus_serin = getc(); // Get character from TAS serial bus.
read_okay = 1; // Reading TAS successful.
putc(bus_serin); // Send character to PC.
//read_cnt = 20001;
break;
}
read_cnt++;
//else putc('.'); //*******DEBUG
} while (read_cnt < 20000);
if (read_okay == 0) printf("read failed");
else read_okay = 0;
...
no matter what happens on the bus, I don't get into the PIC. :(
it never gets into the if (kbhit())... routine.
but at least the putc / printf works...
hoping for help,
X |
|
|
X-Rocka Guest
|
|
Posted: Thu Jan 05, 2006 6:50 am |
|
|
Hello again to all,
first of all thanks, disabling the UART was the solution!
So now I am able to use the hardware RX pin also to transmit,
works perfect! I simply disable the HW UART with the following lines:
//************************
// These functions needed to use RX pin as a RS232 transmitter!
void enable_RS_RX( void ) {
#ASM
bsf tris_b, 1 // RS_RX = input.
bsf rcsta, bCREN // Enable continuous receive.
bsf rcsta, bSPEN // Enable USART.
#ENDASM
}
void disable_RS_RX( void ) {
#ASM
bcf rcsta, bCREN // Disable continuous receive.
bcf rcsta, bSPEN // Disable USART.
bcf tris_b, 1 // RS_RX = output.
#ENDASM
}
but now for another problem...
on another card I have the PIC16F628A's hardware TX pin on the same
serial bus, and guess what, I also want to receive data via that pin!
but when I disable the UART and set the TX pin to input, it still seems
to be an always high output.
maybe you find an error in the following lines?!:
void read_serbus(void)
{
unsigned long read_cnt = 0;
unsigned int read_okay = 0;
char bus_serin = 'x';
while (!HWTX_DONE || read_cnt++ < 10) // Wait for end of HW transmit.
{
delay_ms(1);
}
read_cnt = 0;
disable_USART();
// Here's the line to define SW RS232:
#USE RS232 (BAUD=9600, XMIT=SW_TX, RCV=SW_RX, FORCE_SW, BITS=8, ERRORS)
putc(CR);
do // waiting for answer on HW TX pin...
{
if ( kbhit() )
{
bus_serin = getc(); // Get character from TAS serial bus.
read_okay = 1; // Reading TAS successful.
putc(bus_serin); // Send character to PC.
//read_cnt = 20001;
break;
}
read_cnt++;
//else putc('.'); //*******DEBUG
} while (read_cnt < 20000);
if (read_okay == 0) printf("read failed");
else read_okay = 0;
...
no matter what happens on the bus, I don't get into the PIC. :(
it never gets into the if (kbhit())... routine.
but at least the putc / printf works...
hoping for help,
X |
|
|
X-Rocka Guest
|
|
Posted: Thu Jan 05, 2006 6:51 am |
|
|
sorry for the double post... |
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 05, 2006 8:08 am |
|
|
A couple of comments. Have you go the redefines of the bits 'right' for the 16 chip. Remember all the peripheral control will be on different ports/pins (address 0x18, bit 7).
Second comment, 'errors'basically does nothing on a software UART.
You talk about setting TRIS for te TX pin to an input. It should already be set as an input (this is how it hs to be set for the UART to take over). Are you sure you are setting it correctly?.
Have you possibly got prtB pull-ups enabled?. This would make the pin pull high (only weakly, about 50uA, but enough for the pin to obviously not be floating).
Are you sure the SPI is disabled?. This also uses this pin.
Best Wishes |
|
|
X-Rocka
Joined: 05 Jan 2006 Posts: 2
|
|
Posted: Thu Jan 05, 2006 12:59 pm |
|
|
Hello Ttelmah,
first of all I found out that yet another PIC on the bus was set to output and
hi level. One more reason I'm happy that I got serial resistors everywhere!
That 16F628A has no SPI.
I'll check the default port direction of port B, maybe there's something rotten.
thx again,
X _________________ I actually prefer PLDs... ;) |
|
|
X-Rocka
Joined: 05 Jan 2006 Posts: 2
|
|
Posted: Wed Jan 11, 2006 12:05 am |
|
|
hi,
now everything works fine.
there was some strange problem with a loop I programmed...
but I'm not 100% sure.
thanks again,
X _________________ I actually prefer PLDs... ;) |
|
|
|
|
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
|