|
|
View previous topic :: View next topic |
Author |
Message |
andy_bot14
Joined: 28 Jan 2006 Posts: 1
|
How to test multiple software uart on only 1 PIC chip |
Posted: Sat Jan 28, 2006 1:37 am |
|
|
Dear all,
I have been trying to use multiple software UART on pic18f458 without success. Using 1 pic chip, I physically wired pin B0 to pin A0. Are there any way to test multiple software UART for 1 pic properly.
I appreciate any help.
Here is my code.
#include <18F458.h>
#device adc=8
#use delay(clock=40000000)
#fuses NOWDT,WDT128,H4, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, NOPUT, NOCPD, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOCPB, NOWRTC, NOEBTR, NOEBTRB
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stream=PC)
#use rs232(baud=9600,xmit=pin_a1,rcv=pin_a0,stream=com1)
#use rs232(baud=9600,invert,xmit=pin_b0,rcv=pin_b1,stream=com2)
unsigned char data;
void main()
{
fprintf(PC,"welcome \r\n");
while(1)
{
fprintf(com2,"C"); // try to send data to port A
delay_ms(50);
data = fgetc(com1); //try to get data from port B
fprintf(PC,"value is %c \r\n",data); //send data to pc's serial port
//fputc(ch[0],PC);
delay_ms(300);
}
}
Andy O |
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 28, 2006 5:33 am |
|
|
The first comment, is how is it wired?.
The next comment, is "do you understand the 'implications' of soft UART's"?.
Now, on the code,
fprintf(com2,"C"); // try to send data to port A
This line will send the letter 'C', in serial format, out of pin B0. This data will be the inverse of 'normal' TTL serial, idling low. To connect this to a PC, a driver that has no inversion will be needed. You _may_ be able to get away with a direct connection, in some case, _but_ this will not meet the RS232 signalling specs, and is likely not to work...
delay_ms(50);
data = fgetc(com1); //try to get data from port B
At this point, the code will sit waiting for pin A1, to drop. You say 'port B', but your code is using port A. Anything that has already been sent, will have been lost. Why do you delay?. If the other end is 'replying' to the character sent, then get into the receive routine ASAP.
fprintf(PC,"value is %c \r\n",data); //send data to pc's serial port
Once A1, has dropped, the code will read in the data from A1. After this is complete, this byte will be echoed out onto C6. You will only get here, if A1 drops (you have no 'timeout'). The data output will be in normal inverted TTL serial format, and will require a driver like the MAX232 to be compatible with the PC's port.
A a general comment, if you don't want to miss data, use the hardware port, for the 'receive' task. With an interrupt driven serial buffer (EX_SISR), this will grab any reply, no matter how 'quick' the PC is in replying.
I suspect you are actually 'missing' the reply, either because of incorrect wiring, or because of the delay (test the ports one at a time, and get the wiring for each working, before going to the complexity of multiple I/O).
Best Wishes |
|
|
andy_o Guest
|
multiple software uart on pic -> possible? |
Posted: Sat Jan 28, 2006 12:50 pm |
|
|
Thank for help sir,
Actually, I have been using hardware uart and max232 with interrupt facility before. it work great for me. I also be able to use software uart to send and receive the data from PC. This have been done using only single (TX,RX) method.
However, I would like to learn how to use multiple software usart so that I can have inter-communication between pics.
So far, I have gone to CCS mannual. It said that I need "INVERT" term in #USES232 for TTL level communication. This we should be able to connect PIC to PIC TTL pin without having to worry about level conversion circuit(max232). If I were wrong, please correct me for this.
I did add this term in #useRS232 for "com2" and my program could start looping but the data read from com2 was not correct(empty). It is also interesting to note that, if I add INVERT term for both com1 and com2, my program wil not loop at all.
For my setup, I have 1 wire going from pin_b0 to pin_A0. and have pin_A1 connect to max232 and goto PC serial port.
Please let me know if I still miss something
Thank you for helps
Andy O. |
|
|
specialk
Joined: 12 Nov 2005 Posts: 27
|
Re: multiple software uart on pic -> possible? |
Posted: Sat Jan 28, 2006 1:15 pm |
|
|
andy_o wrote: | Please let me know if I still miss something |
You are missing the fact that the software RS232 code can not receive data unless the microcontroller is actively looking for it during the getc routine. With the hardware USART, there is a 2-byte transmit/receive buffer which will hold the last two characters until they are removed from the buffer (hopefully by your code). However, the software RS232 routines use a bit-banging method of sending and receiving, so there is no way to receive data unless you are actively polling the pin with getc when data is being transmitted to the pin. If you connected the transmit pin of one of your software RS232s to the USART receive, then you could do:
Code: | //Connect A1 to C7
fprintf(com1,"C");
delay_ms(50);
data = fgetc(PC); |
And you would recieve data from the software RS232 with the hardware USART.
-special [k] |
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 28, 2006 5:06 pm |
|
|
Ongoing to the above, you are also missing the 'point' of the invert, and this will be causing problems.
TTL serial, as standard, is an 'inverted' signal. It idles high, and the signal drops for the data bits. This is inverted in the MAX232 driver, and then _inverted again_ in the MAX232 receiver at the other end. Giving the same signal polarity at each end of the link. The 'point' of invert, is that sometimes people want to use an alternative drive circuit, that does not provide inversion, (but still use the standard inverting receiver), and in these cases,'invert' can be used. However the problem you have, is that it sounds as if you have directly connected 'com2', to 'com1'. Com2, is implementing software inversion, but com1, is not. If you are going to use invert, you either need it at _both_ ends of any link, or one hardware inversion as well.
If you got rid of the inversion, or used it at both ends of the link, and got rid of the 50mSec delay, your code might well then work. What will happen, is that the hardware UART starts sending the character, and if you _immediately_ call the software getc, though it'll miss a few uSec of the start of the start bit, it should still be early enough to actually get the data.
Best Wishes |
|
|
|
|
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
|