10 years, 9 months ago.

Help about uart format.

Hi people.

I have one problem about uart format, my code is very simple:

  1. include "mbed.h"

Serial pc(p13, p14); tx, rx

int main() { pc.printf("a"); while(1) { pc.putc(pc.getc() + 1); } }

I always get two bytes, see scope image annexed.

What is wrong? I should receive 'a' in my serial port, I think.

Thank in advance.

You cannot re-target printf to the usart on pins p13 and p14.

Dave.

posted by David Fletcher 20 Feb 2014

5 Answers

10 years, 9 months ago.

Getc will return with something, so you get a "A"+ whatever getc returns adding 1. If you dont like this you have to first check if there is something to get with if(pc.readable ()) pc.getc...

10 years, 9 months ago.

hi Gert,

I´m working with Nilton and I tested anothers codes getting same results in 2 differents mBed's ( LPC1768 mbed-005.1) It seems to me a stop bit problem as shown in the image. Someone has a clue what is wrong? /media/uploads/hulemo/chfcidfd.png

Follow the last code we used:

#include "mbed.h"

Serial pc(USBTX, USBRX);
Serial serial_(p13,p14);

int main() {
        pc.baud(9600);
    serial_.baud(9600);
    DigitalOut led1(LED1);
    DigitalOut led2(LED2);

    while(1) {
        serial_.putc(0x17);
        if(serial_.readable())
        {
            pc.putc(serial_.getc());
            led1=!led1;
        }
        if(pc.readable())
        {
            serial_.putc(pc.getc());  
            led2=!led2;
        }      
    }
}

How are you connecting USBTX, USBRX to p13, p14 ????????

Dave.

posted by David Fletcher 21 Feb 2014

Hi Dave, There was a new line before "Serial serial_(p13,p14);" in the code. Fixed. I´m using the code serial_passthrough by Tyler Weaver (http://mbed.org/users/tylerjw/code/serial_passthrough/) The serial over USB communicating with Windows is working perfectly. The problem is the Uart through p13,p14 (or p9,p10)

posted by Hugo Morais 21 Feb 2014
10 years, 9 months ago.

What are we looking at in the scope image above. Is that the TX or the RX pin on mbed? Seems to me that the bitrate is correct (9600 baud, 104 us per bit). Looks like 2 sequential bytes 1 start, 8 data, 1 stop. The undefined level at the start looks suspicious. Is that trigger issue or is something else wrong?

Hi wim, this is TX pin. The byte sent was only 1 'a' (0x61), but the image shows (if i'm not wrong):

start 10000110 stop star 10000000 stop => 0x86 0x80

more strange, the terminal on windows show me 0x4F 0x00.

posted by Hugo Morais 21 Feb 2014

The UART serial sends LSB first. So in the order Bit0, Bit1,...Bit7. That means you actually see a 0x61 and then a 0x01. That does not seem to match your code above. Are you sure that piece of code is actually running on the processor?

posted by Wim Huiskamp 21 Feb 2014
10 years, 9 months ago.

Guys, It works for me. I used this code.

#include "mbed.h"

Serial pc(USBTX, USBRX);
Serial serial_(p13,p14);
DigitalOut led1(LED1); 
DigitalOut led2(LED2);

int main() {
    DigitalOut reset(p12);
    reset = 0;
    wait(0.5);
    reset = 1;
    pc.baud(9600);
    serial_.baud(9600);
    while(1) {
        
        serial_.putc(68);
        if(serial_.readable()){
            pc.putc(serial_.getc());
            led1=!led1;}
        if(pc.readable()){
            serial_.putc(pc.getc()); 
            led2=!led2;}       
    }
}

I don't know why the code tags are not working. Have you also connected pins p13 and p14 together?

There should be no spaces in the code tags, and also make sure they are on seperate lines from the code itself.

posted by Erik - 21 Feb 2014

Hi wim, this is TX pin. The byte sent was only 1 'a' (0x61), but the image shows (if i'm not wrong):

start 10000110 stop star 10000000 stop => 0x86 0x80

more strange, the terminal on windows show me 0x4F 0x00.

Dave, what the function of "reset" in your code?

posted by Hugo Morais 21 Feb 2014

@Erik Thankyou!

@Hugo I just copied Tyler's code and added the leds. The reset is nothing! I will ask the question again Have you connected pins p13 and p14 together?

posted by David Fletcher 21 Feb 2014

Hi Dave, the pins are not connected together. They are connected in a notebook running Windows XP through a serial by a DB9 connector. I´m already tested connecting the rx and tx together and surprisingly, works fine. The TX and RX have the same issue, i.e., if I send 0x61 to p14 (RX), the mbed receive 0x4F 0x00, and if the mbed send 0x4F, the terminal on Windows receive 0x61 0x00.

posted by Hugo Morais 22 Feb 2014

So how is the windows xp machine connected. I assume you use a ttl to rs232 converter such as the max3232 to translate the 3v3 levels to actual rs232 level. Have you checked that tbe GND pin of mbed is connected also to the windows xp machine. Incorrect levels can lead to all sorts of problems. The fact that direct rx to tx seems to work fine should be a hint towards level or ground issues.

posted by Wim Huiskamp 22 Feb 2014

Wow, my mistake. I was using max232. I didn´t notice the levels of mbed was 3v3. Although the max232 datasheet says the mim high-level input voltage is 2V, I think he is incapable drive out to rs232 level. Now is everithing working fine. Thanks Guys.

posted by Hugo Morais 22 Feb 2014
10 years, 9 months ago.

Hi Win, It´s the problem! We are using RS232, not RS3232...

Many thanks!