CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Can Rx pin be toggled between UART and being an input?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
asdf85



Joined: 03 Jan 2011
Posts: 34

View user's profile Send private message

Can Rx pin be toggled between UART and being an input?
PostPosted: Tue Sep 20, 2011 3:18 am     Reply with quote

I was wondering if this can be done?
temtronic



Joined: 01 Jul 2010
Posts: 9198
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 20, 2011 5:56 am     Reply with quote

Yes, but you'll have to add a 'switch' to control which signal you want.That requires one I/O pin and say a CD4066 or similar, plus code.
Now it's more parts,more space, more code....
A simple 'OR' gate won't work as you wouldn't know that the signal was from the 'input' or the UART.
If the project is not timing sensitive,using a PIC with internal oscillator will freeup 2 I/O pins( Fuse.....INTRC_IO).

So it's one of those 'yes it can but costs you more ' problems...
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Tue Sep 20, 2011 7:05 am     Reply with quote

The pin can always be directly read as an input, even if the UART is enabled. Just try reading it!....
The only thing that needs to be 'toggled', is the UART 'receive enable', otherwise the UART will see incoming low edges, and try to receive data on the pin.
Code:

#byte RCSTA=getenv("SFR:RCSTA")
#bit CREN=RCSTA.4

#define UART_RX_ON() CREN=1
#define UART_RX_OFF() CREN=0

Then just call UART_RX_OFF(); when you want to disable the UART, and use the on version to re-enable if.

Sometimes, this can be a sensible way to work, if (for instance), the receive device is something that only talks when asked, and can be turned off, but otherwise as temtronic says, you will need extra hardware.
You should really check that the line is high, before re-enabling the UART.

Best Wishes
asdf85



Joined: 03 Jan 2011
Posts: 34

View user's profile Send private message

PostPosted: Tue Sep 20, 2011 9:00 pm     Reply with quote

Thanks. This really helped.

Ttelmah, i dont really understand how this code works.
Code:
#byte RCSTA=getenv("SFR:RCSTA")
#bit CREN=RCSTA.4


Actually i dont know what #byte and #bit does, but i think it is done to directly change the bit of a register?

Thanks again!
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Wed Sep 21, 2011 2:07 am     Reply with quote

Possibly try reading the manual!......
It allows you to declare a byte variable to be located 'at' a specific memory location (and hence address a register, or do other things). #bit allows a bit variable in a similar way.
I'm just turning off the UART receive enable (but leaving the transmit part etc., still running).

Best Wishes
asdf85



Joined: 03 Jan 2011
Posts: 34

View user's profile Send private message

PostPosted: Tue Sep 27, 2011 8:35 pm     Reply with quote

i c..

I thought if i wanted to do the same thing for TX(alternate between output and TX), it will be like this:

Code:

#byte TXSTA=getenv("SFR:TXSTA")
#bit TXEN=TXSTA.5

#define UART_TX_ON() TXEN=1
#define UART_TX_OFF() TXEN=0


However it doesnt work
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Wed Sep 28, 2011 9:36 am     Reply with quote

Basically, yes.
However for this it'll depend on the chip involved. On some chips, the TRIS has to be '1' for both pins involved in serial I/O, before serial transmit can occur. So (for example), on the 16F87xa chips, the data sheet has:
"Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to
be set in order to configure pins RC6/TX/CK and
RC7/RX/DT as the Universal Synchronous Asynchronous
Receiver Transmitter."
When you perform an output on the transmit bit, (assuming you are using the standard CCS functions, and not using fast_io), the operation will clear the TRIS on this bit, which will need to be set again before you can re-enable the serial transmit.

Best Wishes
asdf85



Joined: 03 Jan 2011
Posts: 34

View user's profile Send private message

PostPosted: Thu Sep 29, 2011 12:22 am     Reply with quote

I cant seem to get it to toggle between output and TX.
What am I doing wrong?

This is my code
Code:
#include "18F2620.h"
#include <stdio.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT,NODEBUG,NOBROWNOUT,NOMCLR
#use delay(clock=20000000)
#use RS232(baud=250000,xmit=PIN_C6,rcv=PIN_C7,parity=n,stream=d,stop=2,bits=9)
//#use RS232(baud=19200,xmit=PIN_C4,rcv=PIN_C5,parity=n,stream=serial,stop=1)
//===============================

#byte RCSTA=getenv("SFR:RCSTA")
#bit CREN=RCSTA.4
#bit FERR=RCSTA.2
#bit SPEN=RCSTA.7

#define UART_RX_ON() CREN=1
#define UART_RX_OFF() CREN=0
#define SETSPEN() SPEN=1


#byte TXSTA=getenv("SFR:TXSTA")
#bit TXEN=TXSTA.5
#bit BRGH=TXSTA.2
#bit SYNC=TXSTA.4
#bit TX9=TXSTA.6
#bit TX9D=TXSTA.0


#define TX_ON() TXEN=1
#define TX_OFF() TXEN=0

#byte TRISC=getenv("SFR:TRISC")
#bit TC7=TRISC.7
#bit TC6=TRISC.6


#byte SPBRG=getenv("SFR:SPBRG")


void main()

   output_high(pin_a5);   //TX ENABLE always on

   while(1)
   {   delay_ms(1);

//=================use pin as output from here on=====


      BRGH=1;
      SYNC=0;
      SPEN=0;
      TXEN=0;   
      output_low(pin_c6);
      delay_us(90);
      output_high(pin_c6);
      delay_us(8);

//=============use pin as TX from here on===========

      SPBRG=4;
      SYNC=1;
      SPEN=1;
      TXEN=1;
      TX9D=1;
      TX9=1;
      TC7=1;TC6=1;
      

      putc(0);
      putc(10);
      putc(20);
      putc(100);

   
   
   }

}

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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