• AVR Freaks

AnsweredHot!UART printf encoding error

Author
Balth
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2020/11/19 14:48:48
  • Location: 0
  • Status: offline
2020/11/26 05:41:54 (permalink)
0

UART printf encoding error

Hi,
I recently had a problem I can't explain.
I'm working on a PIC18F27Q43, and for debug purposes I need to use UART1 and printf.
I first created a project and used MCC, did the UART setup and redirected STDIO to UART1. It works like a charm with printf that sends data to my serial terminal on the PC.
 
Then, I copied the MCC generated UART to use it in another project (same PIC device)  that doesn't uses MCC. And surprinsingly, when I use printf like in the previous project, I receive bad encoded characters on the serial terminal. I don't know how to solve this issue, as long as the UART code and configuration is the same in both projects.
Do some of you already had the trick ?
 
Thank you !
#1
mbrowning
USNA79
  • Total Posts : 1846
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: online
Re: UART printf encoding error 2020/11/26 06:06:21 (permalink)
+1 (1)
Sounds like a baud issue. I would guess oscillator is not configured to the same frequency.
#2
Balth
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2020/11/19 14:48:48
  • Location: 0
  • Status: offline
Re: UART printf encoding error 2020/11/26 06:15:27 (permalink)
0
I thought it was that, but my oscillator settings are the same in both projects :
 
// NOSC HFINTOSC; NDIV 1; 
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x00;
    // HFFRQ 64_MHz;
    OSCFRQ = 0x08;
    // TUN 0;
    OSCTUNE = 0x00;
    // ACTUD enabled; ACTEN disabled;
    ACTCON = 0x00;

 
and my UART init :
// P1L 0; 
    U1P1L = 0x00;
    // P1H 0;
    U1P1H = 0x00;
    // P2L 0;
    U1P2L = 0x00;
    // P2H 0;
    U1P2H = 0x00;
    // P3L 0;
    U1P3L = 0x00;
    // P3H 0;
    U1P3H = 0x00;

    // BRGS high speed; MODE Asynchronous 8-bit mode; RXEN disabled; TXEN enabled; ABDEN disabled;
    U1CON0 = 0xA0;

    // RXBIMD Set RXBKIF on rising RX input; BRKOVR disabled; WUE disabled; SENDB disabled; ON enabled;
    U1CON1 = 0x80;

    // TXPOL not inverted; FLO off; C0EN Checksum Mode 0; RXPOL not inverted; RUNOVF RX input shifter stops all activity; STP Transmit 1Stop bit, receiver verifies first Stop bit;
    U1CON2 = 0x00;

    // BRGL 130;
    U1BRGL = 0x82;

    // BRGH 6;
    U1BRGH = 0x06;

    // STPMD in middle of first Stop bit; TXWRE No error;
    U1FIFO = 0x00;

    // ABDIF Auto-baud not enabled or not complete; WUIF WUE not enabled by software; ABDIE disabled;
    U1UIR = 0x00;

    // ABDOVF Not overflowed; TXCIF 0; RXBKIF No Break detected; RXFOIF not overflowed; CERIF No Checksum error;
    U1ERRIR = 0x00;

    // TXCIE disabled; FERIE disabled; TXMTIE disabled; ABDOVE disabled; CERIE disabled; RXFOIE disabled; PERIE disabled; RXBKIE disabled;
    U1ERRIE = 0x00;

#3
mbrowning
USNA79
  • Total Posts : 1846
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: online
Re: UART printf encoding error 2020/11/26 07:10:30 (permalink)
0
What does your scope tell you?
#4
Balth
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2020/11/19 14:48:48
  • Location: 0
  • Status: offline
Re: UART printf encoding error 2020/11/26 09:32:16 (permalink)
0
In my code, I do a printf("FOO"). So it's supposed to be 0x46 0x4F 0x4F
On the scope, the signal is the following :

 
Pretty strange issue
#5
BroadwellConsultingInc
Super Member
  • Total Posts : 97
  • Reward points : 0
  • Joined: 2020/06/09 06:07:55
  • Location: 0
  • Status: offline
Re: UART printf encoding error 2020/11/26 10:01:38 (permalink)
+1 (1)
Is this coming straight out of your Micro?  Do you expect your idle state to be high or low?   What did it look like on your prior system on the scope?  Looks like you're shooting for 9600 bps (~100uS per bit)?
#6
dan1138
Super Member
  • Total Posts : 4157
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: UART printf encoding error 2020/11/26 11:22:24 (permalink)
+1 (1)
@Balth,

Based on your oscilloscope picture and the data you send it appears that the idle state of the transmit line is not set correctly.

This is how I interpret your photo:


When idle the TX line should be the same level as the STOP bit.
 
You can test my observation by sending these bytes: 0xFF, 0x46, 0x4F, 0x4F
post edited by dan1138 - 2020/11/26 11:35:46

Attached Image(s)

#7
davea
Super Member
  • Total Posts : 585
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: offline
Re: UART printf encoding error 2020/11/26 14:12:24 (permalink) ☼ Best Answerby Balth 2020/11/27 00:52:52
-1 (1)
Bit 5 – TXEN Transmit Enable Control
Value Description
1 Transmit is enabled. TX output pin drive is forced on when transmission is active, and controlled by
PORT TRIS control when transmission is idle.

make sure port is output and set (most likely hi)
@ dan U1CON0 is set 0000 Asynchronous 8-bit UART mode no parity
 
#8
Balth
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2020/11/19 14:48:48
  • Location: 0
  • Status: offline
Re: UART printf encoding error 2020/11/27 00:52:35 (permalink)
0
davea
Bit 5 – TXEN Transmit Enable Control
Value Description
1 Transmit is enabled. TX output pin drive is forced on when transmission is active, and controlled by
PORT TRIS control when transmission is idle.

make sure port is output and set (most likely hi)
@ dan U1CON0 is set 0000 Asynchronous 8-bit UART mode no parity
 




 
You were right ! I'm using UART for debugging, on a pin that is supposed to be a digital input in my appliance.
So I configured it as output in UART, but after I made it an input for production.
So the solution was as you said, making the pin as output !
I feel idiot as it was so simple, problem solved !
#9
Jump to:
© 2021 APG vNext Commercial Version 4.5