Page 1 of 1

Terminal Character Issue

PostPosted: Sun Nov 24, 2019 11:03 am
by GettinBetter
HW: Explorer 16/32, 8MHz, PIC24FJ256GB110 PIM
SW: MPLABX v5.2, MCC, terminal via UART1/USB
Aims: Output data to terminal via UART.

PIN Config:
Debug (PGEC2- pin 26, PGED2 - Pin 27) Previously Tested.
UART (Tx - 50, Rx out - Pin 49 in ) Previously Tested.

Hi Guys,
During messing around with an orientation sensor programming exercise yesterday, the character output to terminal has gone wrong.
So as part of the diagnostic process I loaded a known simple working program, which now had the same issue, then created a completely new simple 'hello world' example which also has the same issue.
Hello World
becomes
d-E_Pëev

Its consistent but wrong.

My initial exploration leads to the possibility that the baud rate is wrong?

from the uart1.c file I see
Code: Select all
   // BaudRate = 19200; Frequency = 2000000 Hz; BRG 25;
   U1BRG = 0x0019;


With BRGH=1
and using the Eq 17-2 from the 'family' data sheet... I get Baud Rate = 4000000/(4*(25+1)) = 38461.53 which seems wrong to me?

Am I on the right track? What's more strange is that it's only just started to happen, could it be and update error? I have got my IDE set to check at every start.

Regards
Les

EDIT...
When I tested my echo back to terminal prog which worked in in the past, it echos back all the standard chars normally, but any numbers are resolved as foreign language fonts. The text sent from the prog has the wrong formatting and chars as well, in fact complete garbage.

Re: Terminal Character Issue

PostPosted: Sun Nov 24, 2019 5:50 pm
by Roche
Are you using a serial to USB converter into the PC? try resetting it - they sometimes go awry. Also - try changing the baud rate on the terminal program - see if you can get a match. Also - is the clock/PLL getting changed in your code somewhere that you aren't expecting it to happen....

Re: Terminal Character Issue

PostPosted: Tue Nov 26, 2019 2:29 am
by AussieSusan
Are you actually setting BRGH to 1 or leaving it at the default of 0.
When your comment says 'Frequency=2000000 Hz', is that the Fosc or the crystal frequency or what? In either case why do you use 4000000 in your formula?
Susan

Re: Terminal Character Issue

PostPosted: Wed Nov 27, 2019 7:58 pm
by GettinBetter
Roche wrote:Are you using a serial to USB converter into the PC? try resetting it - they sometimes go awry.

Did that...after reading your post

Roche wrote:Also - try changing the baud rate on the terminal program - see if you can get a match.

... and that first, before posting.

Roche wrote:Also - is the clock/PLL getting changed in your code somewhere that you aren't expecting it to happen....

Yes checked.

Also....Updated MPLABX to 5.3, wrote another 'hello world' prog, to test UART with no joy.

Bought a couple of new PIM(best have a couple just in case) even if it is ok. Today they sent dsPIC33CH128MP508, :cry: ordered them from work so will have to recheck the order there tomorrow. :?

AussieSusan wrote:Are you actually setting BRGH to 1 or leaving it at the default of 0.

The MCC sets the U1BRG to 1 based on the Rate I want/chose (i.e. 19200) & the oscillator being used. I looked it up in the MCC files to confirm which equation to use.

AussieSusan wrote:When your comment says 'Frequency=2000000 Hz', is that the Fosc or the crystal frequency or what? In either case why do you use 4000000 in your formula?
Susan

Good point :o
Went back and rechecked.
I used the FRC crystal on the board (8MHz) but I have used the FRC prescaler 1:2 so making it 4MHz/2 .... Oops, my bad. I don't normally use the prescaler.
So now we get Baud Rate = 2000000/(4*(25+1)) = 19230 (error = 0.15625%), which is definitely in the right ball park. I was barking up the wrong tree there.
So thank you for your comment AussieSusan.

Regards
Les

Re: Terminal Character Issue

PostPosted: Thu Nov 28, 2019 10:29 pm
by ric
So is your problem sorted?
If not, do you have a scope you can use to inspect the Tx signal itself, to directly measure the baud rate?

Re: Terminal Character Issue

PostPosted: Fri Nov 29, 2019 7:56 pm
by GettinBetter
No, not yet, real life chores and exhaustion :( getting in the way, will pick it up again later this evening or tomorrow.

Regards
Les

Re: Terminal Character Issue

PostPosted: Sat Nov 30, 2019 8:52 pm
by GettinBetter
WOW!!
You need the patience of a saint to work with the MCC... I need to wean myself off it.

So now that the MCC has decided to mess with my UART baud rate, I have to manually set it, by trial and error (using the logic analyser to check timing) found the following.

For a required baud rate of 19200 [ bit duration 52us]: ( using Fcy = 8MHz, Fcy/2 = 4MHz , & no prescaler)

MCC output BR =19200 BRGH = 1, BRG = 0x0033. Result = garbage character baud (bit duration 43 us)
Manually BR =19200 BRGH = 0, BRG = 0x000E. Result = Expected text and formatting (bit duration 49.5us, the closest I could get it to the required 52).

As I had the plug-in settings, set to 'every startup' it must have updated recently? maybe. Would love to know what really happened.

Thank you ric, the scope sorted it.

Re: Terminal Character Issue

PostPosted: Mon Dec 02, 2019 2:59 am
by AussieSusan
Or you can use the 'old school' way and look at the data sheet.
Section 17.1 gives you the formulae.
Also something is wrong with the information you have used. The formulae all use Fcy so the options should be:
BRGH = 0: BRG = Fcy/(18 * BaudRate) - 1 = 8e6/(16*19200) - 1 = 25 (0x19) - Error -0.16%
BGRH = 1: BRG = Fcy/(4 * BaudRate) - 1 = 8E6/(4*19200) - 1 = 103 (0x67) - Error -0.16%
This should be closer to the actual Baud rate than the -4.8% error you show.
Susan