View previous topic :: View next topic |
Author |
Message |
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
PIC16F648A - using hardware TX pin for #rs232 and as input |
Posted: Fri Jan 10, 2014 5:29 pm |
|
|
I have run out of pins and I am using the hardware TX pin as TX for serial communication and as input from keyboard.
I am working at 8MHz
If inserting in any place in the software
Code: | #use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt) |
the pin won't work as input.
I tried the suggestions on the forum but I didn't succeed to make them work.
If inserting:
Code: |
#use rs232(baud=9600,parity=N,rcv=PIN_B1,bits=8,restart_wdt) |
the keyboard works as input.
I would like the pin to work as input and very rarely as TX, just after receiving a message on the RX as answer to the RX message.
The TX should be activated just after receiving a valid message by the RX and deactivated again that the pin can be used as input.
I have to mention also that I didn't work with PIC/CCS in the last 4 years so I suppose I am rusted
My compiler is still Version 3.249.
I will appreciate any help
Joseph |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Jan 10, 2014 5:57 pm |
|
|
One problem to consider is what happens when a key is pressed on the keyboard and that data is sent to the 'serial' device attached to pin B2.
We all know about 'running out of pins ' !Is there a real good reason why you can't go to a larger PIC? I often use a much larger device than what could be used, while costing maybe 50c more,I've yet to run out of pins or memory.
If that's not possible, can you 'reorder' or 'redefine' the pins you have to free up one? Not using MCLR might work,or using the onboard RC osillator( if timing isn't too critical) ?
Just thinking of options you might not have thought about.
hth
jay |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Fri Jan 10, 2014 6:59 pm |
|
|
temtronic
Thanks for the answer.
Regarding your suggestions:
-I have no place on the board to use bigger controller, the board is very cramped because the customer wants through-hole components and not SMD.
-Can't use the internal oscillator as time is critical.
-I think I can't use MCLR pin as the environment is noisy (I am driving an AC motor with PWM). Maybe I am wrong on this point, but on another project long time ago with AC motors (a dentist chair) I was not able to use this pin for something else because the program stopped working.
-The hardware RX pin is not used as input from the keyboard, used only as serial RX, so things that happening on the TX pin while used as keyboard input will not affect the TX message. Also the all variables used in the serial communication are double buffered during the program.
Joseph |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Jan 11, 2014 2:10 am |
|
|
The problem you will have is that the TX will override the TRIS. Since you are using the pin as output 'sometimes', the compiler knows that it should have the TRIS set as 0, and will force this.
The suggestions will work for 'permanent' switching, but for temporary switching, you will have to use FAST_IO, and control the TRIS yourself. So turn off the UART TX, and then set the pin as 'input', and set the pin back to 'output', before turning the UART TXback on.
You also have to remember you are going to have to disable whatever you are using as a TX buffer, otherwise everything you receive as input, will be echoed out on the serial line as 'garbage'. I hope you have considered this. Another pin needed for this, then means you have gained nothing....
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Jan 11, 2014 6:32 am |
|
|
.... wild idea:
Using the rx line as the tx line too!
What is sending your pic a serial message and who is the pic sending the answer to?
I know smartcards do something similar with async serial.
If its another pic you are talking too you could consider another serial protocol like 1wire.
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Jan 11, 2014 7:34 am |
|
|
hmm..OK, I'm a bit confused so please help me get the picture.
UART-RX(B1) gets data from some 'serial device', NOT a KBD.
UART-TX(B2) sends data to same 'serial device'.
1) You mention a keyboard. How does this get connected? If PS2 style, it's 2 signals, though I'm thinking you're using an RS-232(serial) KBD?
A simple chart or list would really help me get the picture.
2) I've used the 648A for several projects never had any issues using MCLR as an input( A5). Noise problems could be from wiring, layout,etc.
3) If you need another output, perhaps you can 'swap' an input pin to A5, and use the freed up pin for the new output ?
hth
jay |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Sat Jan 11, 2014 4:59 pm |
|
|
Ttelmah
Thanks for your reply. I read also I think everything you have posted in the subject in the past, back maybe more than 10 years.
I tried to implement what you have advised but with no success. I suppose I am writing the things in the wrong place in the program or making other mistakes.
The serial communication is used very rarely.
The TX pin is connected to the "ON" button of the keyboard (the keyboard have 4 buttons) so in regular operation mode should be an input.
The TX should be activated as TX just after a full RX message is received.
I am enabling TBE after the RX data is updated.
Disabling TBE after last word was sent. In all other projects I have done it worked fine because I never used the TX pin for something else.
temtronic
Thanks for the reply.
UART-RX(B1) gets data from the PC to update certain parameters.
Just if such update occurs
UART-TX(B2) sends data to the PC and is not used again until UART-RX(B1) gets data again from the PC.
The system is on 24 hours a day, the serial communication is used very rarely, maybe once a month, ones a year even.
Noisy environment I mean a 220 VAC motor working very close to the board. Also the 220 VAC is supplied to the motor via the same board.
This is the reason I am afraid to use the board without the MCLR function.
Sorry taking your time, I just don't know what to do.
Joseph |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jan 11, 2014 5:25 pm |
|
|
Can you win a pin by using an external oscillator module?
(OSC2 becomes RA6.)
Failing that could you tell us what is connected to ALL of the pins?
We/I need more information to be able to help.
Mike |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Sat Jan 11, 2014 5:55 pm |
|
|
Mike Walne
Yes you are right, using external oscillator will free RA6. Good idea, thanks.
2 oscillator pins
6 command outputs
4 keyboard inputs
PWM output
Low frequency input
TX
RX
MCLR
It makes total 17 pins, this is the reason I want to use RB2 for TX and input also.
Joseph |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Sat Jan 11, 2014 7:50 pm |
|
|
Gabriel
Thanks for the input.
I want to use my regular hardware TX and RX that I have written software for it. Also the communication is to a PC.
Joseph |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Jan 11, 2014 8:52 pm |
|
|
I think Mike has got it right! Using an external oscillator makes it easy !
Easy to recode the 'fuses' and all your other code/pins will stay the same.
OK, the osc. costs a bit more than xtal+2 caps but it a REAL time saver.Depending on board space , it could even be mounted at right angles to the PCB.
You can buy a miniDIP one for about $2.50, 8 pin pkg. Also made in SMT for those with GOOD eyesight.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Jan 12, 2014 3:58 am |
|
|
Using the external oscillator is by far the best way to go.
The problem with the RS232 pin in this case, is because the poster is trying to a pin configured normally for UART use as an 'output', as an input.
This is going to cause problems since everything arriving at this pin as an input signal, _is_ going to be sent over the RS232 as unexpected signals. However the reason for it not working, is that once both UART pins are actually 'used', the compiler _automatically_ overrides the TRIS setting on the pin to keep them set correctly for the UART. To override this, you have to switch to using fast_io mode, and change the TRIS settings each time....
External oscillator, is much easier.
Best Wishes |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Mon Jan 13, 2014 4:40 am |
|
|
Testing the program with the hardware I get to the conclusion that just an external oscillator will solve my problem.
If inserting:
#use rs232(baud=9600,parity=N,rcv=PIN_B1,bits=8,restart_wdt)
The RX not working also.
Inserting:
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)
Everything working fine, except the TX connected input.
Thank you all for your time spending answering me
Best wishes
Joseph |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Jan 13, 2014 4:48 am |
|
|
As soon as you use only one pin in the #use RS232, you switch to software RS232. Means no buffering, and you have to be sitting 'polling' the RS232 lines when data arrives....
External oscillator is the way to go.
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
|
|