|
|
View previous topic :: View next topic |
Author |
Message |
Dabadi
Joined: 03 Jan 2015 Posts: 2
|
how receive data on software rs232 |
Posted: Sat Jan 03, 2015 6:55 pm |
|
|
Hi all, i'm a new and inexperienced P16F877 microcontroller's user
I'm writing a simple program with CCS official documentation but i have a lot of troubles.
I open three instance of terminal (TeraTerm):
1) receive data from hardware rs232 and print on terminal
2) and 3) send data each other with a usb+rs232 adapter cable
That cable has additional connection to wire with PIC pin, I use pin d1 and d2 to receive.
This is my code:
Code: |
#include <16F877.h>
#use delay(clock=4M)
#use rs232(baud=9600, UART1, stream = COM_0) //hw uart
#use rs232(baud=9600, rcv=pin_d1, stream = COM_1) //pc1 uart
#use rs232(baud=9600, rcv=pin_d2, stream = COM_2) //pc2 uart
void main() {
int8 c1 = 0;
int8 c2 = 0;
while(1)
{
fprintf(COM_0,"ready\n\n");
if(kbhit(COM_1)){
c1 = fgetc(COM_1);
fputc(c1,COM_0);
}
if(kbhit(COM_2)){
c2 = fgetc(COM_2);
fputc(c1,COM_0);
}
delay_ms(1000);
}
}
|
when i run it, if i write on terminal 2) or 3) nothing appear on 1).
Probably is a beginner mistake, it's few time that i use microcontroller and i have a lot to learn, hope on your help to understand what is wrong and maybe suggestions for a tutorial or guide for beginners! Thank you. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Jan 03, 2015 9:39 pm |
|
|
Short answer: software emulated UARTs are terrible. With effort you can make it work but there are some limitations that will cause you many headaches later on. Save yourself a lot of troubles and choose a PIC with enough hardware UARTs.
The CCS software UART can not buffer incoming data. It will have to scan the data input for the whole time that data is being received. This means that when you are receiving data on COM_1, all the data on COM_2 will be lost.
For kbhit() to work you have to call this function minimum 10 times faster than your baudrate, that will be more than 96,000 times per second. You have a delay of 1 second in the loop.... This is never going to work.
Code: | if(kbhit(COM_2)){
c2 = fgetc(COM_2);
fputc(c1,COM_0); | bug: c1 must be c2 |
|
|
Dabadi
Joined: 03 Jan 2015 Posts: 2
|
|
Posted: Sun Jan 04, 2015 2:32 am |
|
|
ckielstra wrote: | Short answer: software emulated UARTs are terrible. With effort you can make it work but there are some limitations that will cause you many headaches later on. Save yourself a lot of troubles and choose a PIC with enough hardware UARTs.
The CCS software UART can not buffer incoming data. It will have to scan the data input for the whole time that data is being received. This means that when you are receiving data on COM_1, all the data on COM_2 will be lost.
For kbhit() to work you have to call this function minimum 10 times faster than your baudrate, that will be more than 96,000 times per second. You have a delay of 1 second in the loop.... This is never going to work.
Code: | if(kbhit(COM_2)){
c2 = fgetc(COM_2);
fputc(c1,COM_0); | bug: c1 must be c2 |
thank you for explanation and to signal the bug for c1 c2
Back to the problem, sadly i must use this pic, there isn't another way that can work for my problem? Considering that delay is not really necessary, i put this only to see at terminal output every second. Now i make some try without delay and hope change something |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sun Jan 04, 2015 2:33 am |
|
|
I have in the past posted in the code forum, a 'tick' based software RS232 receive, that will receive data while waiting etc.. However it requires a fast interrupt, and I can't remember the rates involved, but at 4MHz, it won't cope with 9600bps. You need perhaps 16MHz, to allow a fast enough timer interrupt to cope properly with this rate. It was called 'timerrs232'. It is relatively easy to expand to multiple channels.
Key thing with the standard software RS232, is that the code either needs to be sitting 'waiting' for the character to arrive, or as Ckielstra says, needs to be testing the stream at a high rate. Unless you are waiting for characters, they _will_ be lost. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sun Jan 04, 2015 6:43 am |
|
|
you _might_ have better success with
1) use a 20MHz xtal to get the PIC to run as fast as it can go.
2) tie the nonUART serial lines to interrupt pins, use an ISR to capture the serial data
3) slow down the nonUART serial line to say 600 baud.
4) use serial 'buffers', see the CCS ex_sisr.c for example. Capture data in ISRs, send in main() when not busy.
You need to understand the 16F877 is, well, an 'end of life' product, about 20 years old!
Providing the PIC has NOTHING else to do but listen/talk to 3 serial ports is 'should' be doable
hth
jay |
|
|
|
|
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
|