![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
andyhuynh273
Joined: 23 Nov 2010 Posts: 2
|
PIC 18LF45K22 , UART2 is not working properly |
Posted: Tue Nov 23, 2010 1:00 am |
|
|
Hi all
I am using the PIC18LF45K22 with UART1 and UART2. The UART1 is connected with my PC, UART2 is used to read the GPS data. I'm using the crystal 16M.
The setting is as below:
Code: |
#include <18F45K22.h>
#include <stdio.h>
#include "string.h"
#fuses HS,INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=16M)
#use rs232(baud=9600,UART1,parity=N,bits=8,timeout=5,stream=PC)
#use rs232(baud=9600,UART2,parity=N,bits=8,timeout=5,stream=GPS)
#use FAST_IO(A)
#use FAST_IO(B)
#use FAST_IO(C)
#use FAST_IO(D)
#use FAST_IO(E)
//manual setting for the UART2 since it's not working properly
#byte BAUDCON2 = 0xF70
#bit BRG16 = BAUDCON2.3
#bit ABDEN = BAUDCON2.0
#byte RCSTA2 = 0xF71
#bit CREN = 0xF71.5
#bit SPEN = 0xF71.7
#byte TXSTA2 = 0xF72
#bit BRGH = TXSTA2.2
#bit SYNC = TXSTA2.4
#byte SPBRG2 = 0xF75
void main()
{
char gps_string[200]={0};
delay_ms(10);
setup_oscillator(OSC_4MHZ | OSC_PLL_ON);
//setup the UART2 manually
SYNC = 0;
BRGH = 0;
BRG16 = 0;
SPBRG2 = 25; //baud=9600
CREN = 0;
SPEN = 0;
delay_us(100);
CREN = 1;
SPEN = 1;
while(1)
{
fgets(gps_string,GPS);
fputs(gps_string,PC);
}
} |
My compiler: PCWHD 4.110
MPLAB 8.56
The UART2 is working but sometimes i get the garbage, for example:
//expected
$GPRMC,064840.00,A,0116.99533,N,10348.84593,E,0.100,,231110,,,A*71
//garbage
$GÂb’ÂbŠ‚b‚ÊbŠšb‚¢b‚ªbbbŠr¢ºb‚ršbŠr’ŠR‚šj
//wrong, not as expected
$G011,22,12,15,320,15,13,09,070,12*7B
However, when I move to use the software-UART by selecting the GPIO pin D4 and D6 (don't use the UART2 anymore), there're no more garbage
Code: | #use rs232(baud=9600,xmit=PIN_D6,rcv=PIN_D4,parity=N,bits=8,timeout=5,ERRORS,stream=GPS) //test with the IO |
The result is very good:
$GPRMC,051009.00,A,0116.99713,N,10348.85047,E,0.187,,231110,,,A*70
$GPGSV,4,2,14,08,31,179,25,09,01,303,,10,59,185,31,13,11,115,25*73
$GPRMC,051010.00,A,0116.99710,N,10348.85045,E,0.106,,231110,,,A*70
...
Anyone got this problem before ?
Or would you please help me to solve this.
Thanks
Andy |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Tue Nov 23, 2010 1:20 pm |
|
|
First. add ERRORS to both use RS232(....) functions....
Second. be sure to use MAX232 or equal on serial pins. |
|
![](templates/subSilver/images/spacer.gif) |
andyhuynh273
Joined: 23 Nov 2010 Posts: 2
|
|
Posted: Tue Nov 23, 2010 8:07 pm |
|
|
Thanks for your reply temtronic. I did try to add ERRORS but no change.
Code: | #use rs232(baud=9600,UART1,parity=N,bits=8,timeout=5,ERRORS,stream=PC)
#use rs232(baud=9600,UART2,parity=N,bits=8,timeout=5,ERRORS,stream=GPS) |
I'm using the MAX232 for serial pins. I am so sure about the hardware.
For UART1: xmit:C6, rcv:C7
For UART2: xmit:D6, rcv:D7 |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Wed Nov 24, 2010 8:16 am |
|
|
You read data from the GPS into a string and then immediately output the data via a string. The characters in equal the characters out. RS232 is asynchronous. At any any any any time a character can be received! What happens if a character is received while you are transmitting the via fputs.
The PIC is busy sending chars so it can't receive chars. There is if you use a hardware uart a very very small buffer of two chars.
With Rs232 often very little of practical use can be done without an Isr on RDA and a circular buffer. This is especially true of a GPS which squawks long sentences whenever it sees fit.
There are times when a TBE isr is needed (with a circular buffer ) to resolve the blocking action that can occur for lengthy output. |
|
![](templates/subSilver/images/spacer.gif) |
peterpanic
Joined: 05 Apr 2010 Posts: 17 Location: UK
|
|
Posted: Sat Dec 04, 2010 11:39 am |
|
|
Yes, I have the same problem - just sent trivial "proof" code to support (wish I'd seen your post before spending all that time reducing it) _________________ - peter - |
|
![](templates/subSilver/images/spacer.gif) |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Dec 04, 2010 9:10 pm |
|
|
As Douglas Kennedy said above, fgets/fputs isn't going to work. fputs won't return until the entire received data has been transmitted. In that time data will have been received on the other port and been lost. What will probably work is fgetc/fputc: Code: | fputc(fgetc(GPS),PC); |
_________________ Andrew |
|
![](templates/subSilver/images/spacer.gif) |
peterpanic
Joined: 05 Apr 2010 Posts: 17 Location: UK
|
|
Posted: Sun Dec 05, 2010 5:24 am |
|
|
To clarify my post of 4th December; the problem I confirmed is that the compiler generates bad code for UART2. It accepts that there is a second UART, but doesn't know about the correct addresses, so in setting up the #use RS232 it writes the config data to low RAM (not insurmountable) but when Tx'ing data, it writes that data not to the Rx register but to register 0x02. Then it's crash-time. _________________ - peter - |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|