Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 9 months ago.
Help about uart format.
Hi people.
I have one problem about uart format, my code is very simple:
- 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.
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?
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; } } }
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 21 Feb 201410 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?
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 21 Feb 2014Hi 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 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 21 Feb 2014Hi 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 22 Feb 2014So 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 22 Feb 201410 years, 9 months ago.
Hi Win, It´s the problem! We are using RS232, not RS3232...
Many thanks!
You cannot re-target printf to the usart on pins p13 and p14.
Dave.
posted by David Fletcher 20 Feb 2014