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

PORTB priorities on 16F1827 SOLVED

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



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PORTB priorities on 16F1827 SOLVED
PostPosted: Fri Nov 24, 2017 1:47 am     Reply with quote

Hy.
I use PIC16F1827 with UART native for rs232 TTL.
Also APFCON0 and APFCON1 for RX, TX are set to "1" in register (page122 in datasheet).
Now communication work fine.
My question is:
*how to use pin RB5 for TX when I need UART connection and how to use for output as "output_high (B5)" when I need logical control output ?
*how to use pin RB2 for RX when I need UART connection and how to use for input as "input (B2)" when I need to see logical input ?
On datasheet is at page 131 but nothing sayng how to change priority or deactivate / activate function.
Thank you.


Last edited by nailuy on Fri Nov 24, 2017 9:53 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 2:24 am     Reply with quote

The key is to turn the UART on/off.

When the UART is off, the pins are normal I/O.
When the UART is on. it takes priority over the normal I/O functions. You can in fact still 'read' the RX pin (if you look at the connections shown on the pin the read buffer is still enabled), but you can't drive the output pin.

setup_uart(FALSE);

Turns the UART off.

setup_UART(TRUE);

Turns it back on.

It is also possible to just disable the TX side of the UART, but to do this you have to go 'manual' and control the UART registers:
Code:

#BIT=getenv("BIT:TXEN")

    //Then in your code
    TXEN=FALSE; //disables just the transmit of the UART

    TXEN=TRUE; //enables just the transmit side (assuming the UART is
    //enabled).
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 5:50 am     Reply with quote

Dear Ttelmah
yes is worked with some adjusting:
in header:
Code:
#bit TXEN=getenv("BIT:TXEN")

in my program with:
Code:
if (input(A1)==0){
   TXEN=0;
   output_bit(B5,1);
   delay_ms (10);
   output_bit(B5,0);
   delay_ms (15);
   TXEN=1;
   putc(1);
   }


With this example code I must have on output: [10 ms "1" logic, 15 ms "0" logic, UART signal 0x01], and repeat until A1 is not true.
in reality I have:[10 ms "1" logic, 15 ms "0" logic, 10 ms "1" logic, 15 ms "0" logic and repeat until until A1 not true and in the end appear, UART signal 0x01] one single time.

Can you explain why is running haoticaly in my opinion?
Thank you.

Oh in header I have also:
Code:
#use delay(internal=1MHz)
#use rs232(UART1,baud=1200)
temtronic



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

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 6:22 am     Reply with quote

What is the signal source for the 'input(A1)' ?
If it's a push button , that is probably your problem.
ALL mechanical switches have 'bounce'. They do not produce clean '1' and '0's. Search here for 'debounce' and you'll get a lot of hits. Adding a small (.68mfd) cap across the switch can minimize bounce.

also

'sharing' a pin for both LED and UART can have problems as whatever is connected to it will see the LED flashes as data. Hopefully the PC or other device has been programmed to ignore that 'data'.

Jay
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 6:34 am     Reply with quote

I have cap with 1K and 10nf for that input.
Problem is not from there because program is begin and running without UART signal if condition of button is out sided. (only 10 and 15 ms of logic levels I have).
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 9:44 am     Reply with quote

Problem is it takes time to send a character.

If the button stays '1', you immediately turn off the UART while the character is still sending, so it doesn't get sent....

You need to pause for the character to send after the putc. The putc places it into the TX buffer, and it then takes 10 bit times to send (so at 9600bps, 1.04mSec - assuming 8 bit no parity).
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 9:51 am     Reply with quote

Yes Ttelmah is true.
Simple delay code at the end solve the problem.

Code:
...
putc(1);
delay_ms (10);
...


Thank you Ttelmah best wishes.
temtronic



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

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 10:56 am     Reply with quote

just a comment..

Are you aware that putc(1) will send 0b00000001 and NOT an ASCII '1', which is 0x31 ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 11:04 am     Reply with quote

As a comment, if you wanted the timing to be 'exact', you can use:
Code:

#BIT TRMT=getenv("BIT:TRMT")

   putc(1);
   while(TRMT==0)
       ;

This will then wait just for the character to finish transmitting.

It is (of course) one of the great powers of the UART that you can be doing other things while it transmits, but brings problems like this when you need to wait for things to finish!..
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 12:34 pm     Reply with quote

Yes Ttelmah

Code:
#BIT TRMT=getenv("BIT:TRMT")

putc(1);
while(TRMT==0) ;


is a good solution is more than I expected.

About putc(1)

putc(1) is sending 0x01
putc(100) is sending 0x64
for me is okay this value 0b00000001, I know Smile

Thank you again, without your help I never think for this solution's.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 24, 2017 1:11 pm     Reply with quote

I'm guessing it is some form of 'starter' marker, followed by the character?.
I twigged that you were making sense if this was the case, hence tried to help. Smile
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