|
|
View previous topic :: View next topic |
Author |
Message |
ralpok
Joined: 21 Dec 2005 Posts: 25
|
RS232 Receive Problems |
Posted: Wed Jan 24, 2007 12:22 pm |
|
|
When I have a very simple program running to simply receive bytes from the UART, the reads are randomly incorrect. When I have an RS232 sniffer in line so that I can read the data going down the bus on the computer, the values show up on the computer fine but are randomly different on the PIC when I read it. I have tried it with interrupts and polling with the same result. If I send the same packet the bytes are jumbled in different ways so there is not pattern to the incorrect bits. Also, not all the bytes get read incorrectly, only about 1 in every 10 bytes. This random issue has got me stumped and any help would be apreciated.
Here is my code:
#include <18F6680.h>
#device ADC=10
#device ICD=TRUE // ADDED FOR DESKTOP USE //
#use delay(clock=20000000)
#fuses HS,NOWDT,PUT,NOLVP,BROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, PARITY=N, BITS=8, stream=AMM, ERRORS)
//RS232 Vars
int rs232Buffer[20];
int count = 0;
int32 testDelay = 0;
void Main()
{
int32 x = 0;
for (x = 0; x <20>=18)
{
count = 0;
}
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 24, 2007 12:27 pm |
|
|
Quote: | for (x = 0; x <20>=18) |
Please read Humberto's post at the top of the forum about how to
post code. |
|
|
ralpok
Joined: 21 Dec 2005 Posts: 25
|
|
Posted: Wed Jan 24, 2007 12:41 pm |
|
|
Hardware: PIC18f6680. The RS232 to TTL chip that we use is the MAX3232 chip.
Compiler Version: 4.020
Code: |
#include <18F6680.h>
#device ADC=10
#device ICD=TRUE // ADDED FOR DESKTOP USE //
#use delay(clock=20000000)
#fuses HS,NOWDT,PUT,NOLVP,BROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, PARITY=N, BITS=8, stream=AMM, ERRORS)
//RS232 Vars
int rs232Buffer[20];
int count = 0;
int32 testDelay = 0;
void Main()
{
int32 x = 0;
for (x = 0; x < 20; x++)
rs232Buffer[x] = 0;
while(1)
{
if (kbhit())
{
rs232Buffer[count++] = getc();
}
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 24, 2007 2:45 pm |
|
|
I compiled your program with PCH vs. 4.020. The baud rate setup
looks OK. It's using the 16-bit baud rate generator, and it's loading it
with 0x208. This gives a baud rate of 9596, which is pretty close to 9600.
There are a few things about your test program that aren't quite optimum.
You're using a 32-bit index for the array, and only an 8-bit index is
required. You're testing for kbhit, but in fact, getc() includes waiting
for kbhit. You have a stream specified but you're not referencing it
in your function calls. Your getc() loop has no limit on the number of
received characters and it could blow past the end of the array. Normally
the #use delay() statement is placed after the #fuses statement.
Most of these things won't actually cause a problem in your test program,
although blowing past the end of the array could do so.
Are you sure your crystal frequency is exactly 20.000 MHz ?
Are you trying to do autobaud at any point ? You didn't show this in your
program, but Microchip has got a lot of erratas on auto baud for this PIC.
If you're getting randomly incorrect bytes, I would check the bit timing
with an oscilloscope. Send upper case 'U' repeatedly. This is 0x55, so
you can sync on it. Check the frequency and the pulse duration.
Check the levels. You said you're using a MAX232 type chip. Look
at the pins that show the +/- 10v output of the voltage doubler circuits
inside the Maxim chip. Make sure the voltages are correct. It could
be that a capacitor is missing.
I would also try putting a lot of time between the transmission of each
character. See if that affects the problem. Try 10 ms, or more. |
|
|
|
|
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
|