View previous topic :: View next topic |
Author |
Message |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
PORTB priorities on 16F1827 SOLVED |
Posted: Fri Nov 24, 2017 1:47 am |
|
|
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: 19513
|
|
Posted: Fri Nov 24, 2017 2:24 am |
|
|
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
|
|
Posted: Fri Nov 24, 2017 5:50 am |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Fri Nov 24, 2017 6:22 am |
|
|
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
|
|
Posted: Fri Nov 24, 2017 6:34 am |
|
|
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: 19513
|
|
Posted: Fri Nov 24, 2017 9:44 am |
|
|
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
|
|
Posted: Fri Nov 24, 2017 9:51 am |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Fri Nov 24, 2017 10:56 am |
|
|
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: 19513
|
|
Posted: Fri Nov 24, 2017 11:04 am |
|
|
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
|
|
Posted: Fri Nov 24, 2017 12:34 pm |
|
|
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
Thank you again, without your help I never think for this solution's. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Nov 24, 2017 1:11 pm |
|
|
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. |
|
|
|