|
|
View previous topic :: View next topic |
Author |
Message |
kam
Joined: 15 Jul 2005 Posts: 59
|
PIC24 RS232 issues? |
Posted: Wed Aug 01, 2012 8:15 pm |
|
|
Hi all,
Okay, here is the deal. I can make the rs232 work just fine at 9600 baud, but at 38400 no go!
pic24 series mcu. (24HJ128GP306 ccs dev board)
I have 2 usb serial dongles, connected to the CCS pic24 dev board, have 2 serial monitors active, and at 9600 baud, I can type in one and it receives in the other, and vice a versa.
If I change to 38400, no go...just garbage. I've passed this to CCS and no real answers...
I've included a complete working/not working demo...
Can the guru's check it out and see if I have any issues?...
thanks in advance.
~Kam
Code: |
#include <24HJ128GP306.h>
#include <stdio.h>
#include <string.h>
#IGNORE_WARNINGS 203
#zero_ram
#FUSES HS
#FUSES NOWDT
#FUSES PR
//#use delay(clock=40Mhz, crystal=20Mhz)
#use delay(clock=20Mhz)
// assign hardware uarts
#use rs232(UART1, baud=9600,parity=N,bits=8, STREAM=RS1)
#use rs232(UART2, baud=9600,parity=N,bits=8, STREAM=RS2)
#define MAX_PACKET_SIZE 4096 // bytes
#define MAX_TIMEOUT 500 // in milli seconds
#define TIMEOUT_DELAY 10 // in milli seconds
// data for uarts
char RS2Data[MAX_PACKET_SIZE];
int16 RS2Counter = 0;
int16 RS2Timeout = 0;
char RS1Data[MAX_PACKET_SIZE];
int16 RS1Counter = 0;
int16 RS1Timeout = 0;
// From RS1 - RS232 UART1 interrupt handler
#INT_RDA
void serial_isr_RS1()
{
char data;
// reset timeout
RS1Timeout = 0;
// get and save data
data = fgetc(RS1);
RS1Data[RS1Counter] = data;
RS1Counter++;
// account for overflow
if(RS1Counter > MAX_PACKET_SIZE - 1)
{
RS1Counter = 0;
RS1Data[RS1Counter] = 0;
}
}
// From RS2 - RS232 UART2 interrupt handler
#INT_RDA2
void serial_isr_RS2()
{
char data;
// reset timeout
RS2Timeout = 0;
// get and save data
data = fgetc(RS2);
RS2Data[RS2Counter] = data;
RS2Counter++;
// account for overflow
if(RS2Counter > MAX_PACKET_SIZE - 1)
{
RS2Counter = 0;
RS2Data[RS2Counter] = 0;
}
}
void main()
{
set_tris_b(0); // port B are all outputs
RS2Data[0] = 0;
RS2Counter = 0;
RS2Timeout = 0;
RS1Data[0] = 0;
RS1Counter = 0;
RS1Timeout = 0;
// start uart ints
enable_interrupts(INT_RDA); // RS1
enable_interrupts(INT_RDA2); // RS2
delay_ms(1000); // let things startup...
fprintf(RS1, "Okay, all ready...\r\n");
while (1)
{
// pause then update timeout vars
delay_ms(TIMEOUT_DELAY);
// handle any RS2 data
RS2Timeout++;
if(RS2Timeout >= (MAX_TIMEOUT / TIMEOUT_DELAY))
{
// have data?
if(RS2Counter > 0)
{
fprintf(RS1, "Received From RS2 - [%s]\r\n", RS2Data);
memset(RS2Data, 0, MAX_PACKET_SIZE);
RS2Counter = 0;
}
RS2Timeout = 0;
}
// handle any RS1 data
RS1Timeout++;
if(RS1Timeout >= (MAX_TIMEOUT / TIMEOUT_DELAY))
{
// have data?
if(RS1Counter > 0)
{
fprintf(RS2, "Received From RS1 - [%s]\r\n", RS1Data);
memset(RS1Data, 0, MAX_PACKET_SIZE);
RS1Counter = 0;
}
RS1Timeout = 0;
}
}
}
| [/code] |
|
|
AtakaN
Joined: 01 Aug 2012 Posts: 4
|
|
Posted: Thu Aug 02, 2012 12:38 am |
|
|
Hi Kam
As far as I understand from the various examples of dspic's uart, you should check out your bus frequency of the uart. when you change the working-frequency, all bus's working style change.
You can look up this website to get what I mean.
http://www.microchip.com/forums/m399816.aspx
And if you make a project by using the ccs c's project menu, and when you select your own mcu on its list, you are gonna see how to add " brgval " coefficient to your program (uart section - software rs232). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Aug 02, 2012 2:10 am |
|
|
Do you have the IDE?.
If so, go into the device editor, and change the first erratum "BRGH can not be 1" to enabled.
This was covered a couple of weeks ago in another thread here.
There is a bug in the BRG on these chips. Unfortunately, the original 'engineering samples', did not have this error, but all production ones seem to have it.... CCS did not therefore enable this by default.
If you haven't got the IDE, you will have to change the BRG value, manually with a byte statement, and select the closest timing, with BRGH set to 0.
Best Wishes |
|
|
kam
Joined: 15 Jul 2005 Posts: 59
|
SOLVED! |
Posted: Thu Aug 02, 2012 2:02 pm |
|
|
Hi all,
After much time with CCS support, it comes down to this.
The design of the pic24HJ128GP306 dev board IS WRONG.
Here is the write up from them
"It seems to work on my board that I have okay, but I remembered a similar problem another customer was having. So I looked up the specs for the level converts that are on the board and they are rated for speeds up to 20 kbps. The part number for the chips are DS276S if you want to check for yourself. One difference about my setup is that my board is connected to my computer's serial port not to a serial to USB converter cable. Maybe that's the difference for why it works for me and it isn't for you."
Thats right! they are using an underpowered level converter!...and they did not feel it necessary to inform anyone (like the site!) that that board can only work up to 19200 baud because of a design problem.
I can go to 19200 just fine...but the second I exceed that, POOF! nada!
You have to be kidding me.
So now what do I do?
~Kam |
|
|
kam
Joined: 15 Jul 2005 Posts: 59
|
Really solved... |
Posted: Thu Aug 02, 2012 5:03 pm |
|
|
Thank you Ttelmah
Your suggestion worked just fine!
I am amazed that CCS said what they said. And yes, the level converter is really wrong and needs to be replaced IMHO, but it works. As long as I can get 38k baud, I'm okay with this dev board.
Thanks again all!
~Kam |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Fri Aug 03, 2012 2:02 am |
|
|
I'm not sure that the level converter really is a problem for what the board is designed for. 99% of the examples, 'standardise' at 9600bps. 'RS232' as originally specified, only supported a maximum of 20Kb/sec, at 50' max, having a driver that has the same limits, is quite reasonable.
In fact though it is not the driver chip, but the supply that imposes the limit. Run the chip off 12v, and it happily supports 100K signalling. Off 5v, it is into the 'grey area' of not quite meeting RS232C signalling standards, but it shares this with 90% of the (supposedly) RS232 equipment in use today. As already covered recently, 90+ percent of things like laptops, do not actually meet the signalling standards, but generally work quite well.
However as you have found, the signalling chip was not the problem, but the PIC erratum.
I suspect some of the poster's here know the errata sheets better than CCS. A few (myself included), have probably found errata.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Aug 04, 2012 6:11 am |
|
|
Quote: | 'RS232' as originally specified, only supported a maximum of 20Kb/sec, at 50' max, having a driver that has the same limits, is quite reasonable. |
I don't know the devboard RS232 circuit, but I think it's really difficult to make a circuit that meets the RS-232 specifications and doesn't work up to 57 or 115 kbaud. Even the simplified circuits I'm often using in embedded systems with +/- 5V or 0/+5V TX level mostly do. This is the case at least for short cables up to 2 or 5 m. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Sat Aug 04, 2012 6:43 am |
|
|
Yes, The point is that the chip switches using 5v for the output drive if run off 5v. As such, this won't quite meet the minimum signalling voltage standard for RS232. (requires minimum of +/-5v at the connector, and even a CMOS switch, won't quite give 5v, when running off 5v).
However it works. It wasn't the actual problem, but the BRGH fault with these chips was.
What was really silly, was CCS coming back and basically saying "it won't work over 20K, because the chip we used won't do this"....
As you say, hundreds of thousands of devices use such chips, and merrily work at such rates.
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
|