0
\$\begingroup\$

I have a microcontroller set up to capture serial data via RS-232 so that I can debug directly and view registers, variables, etc. I am using PuTTY through a USB to RS-232 converter to send data to it (9600 baud, 8N1).

The code is written to capture UART data in interrupt mode, so the interrupt fires after, say, four bytes. However, when I type four "0"s into PuTTY, I don't get four "0x00"s in my micro; I get four 0x06.

If I look at the data on a scope, I also don't see a "0"; I see something that looks like the attached scope capture.

What am I doing wrong?

enter image description here

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Is the microcontroller really using RS-232 or does it have a logic-level UART? Your signal appears to have levels of 0 and 10V, which is odd. \$\endgroup\$ Commented Feb 8, 2023 at 20:54
  • 1
    \$\begingroup\$ Mark(1)=low, Space(0)=high, right? And it’s LSB-first, with one start bit (Mark)… maybe the “0” character 0x30 (1)00110000(0) has been reversed to (1)00001100(0) which is 0x06 as observed? \$\endgroup\$ Commented Feb 8, 2023 at 21:10
  • 3
    \$\begingroup\$ Yup, I see a 0x30 there...first rising edge begins the start-bit, followed by four 0-bits, followed by two 1-bits, followed by two 0-bits ending with a string of 1's. The scope time markers measure about 2 bits \$\endgroup\$ Commented Feb 8, 2023 at 21:20
  • 1
    \$\begingroup\$ Some microcontrollers (I'm thinking of PICs) allow serial data stream to be inverted . I commonly run into this when using a USB-to-UART dongle, while my microcontroller is straight TTL-level. I must program the microcontroller's "inverting" feature to get sensible transfer. It shouldn't really work, because voltage levels violate RS-232 levels, but it has worked so far. \$\endgroup\$ Commented Feb 8, 2023 at 21:36
  • 1
    \$\begingroup\$ "through a USB to RS-232 converter" <- this is your problem. You've feeding the inverted RS232 levels into a microcontroller pin which is expecting non-inverted "TTL" levels. Either get yourself a USB to "TTL UART" converter, or add a RS232 to TTL interface to your circuit. \$\endgroup\$ Commented Feb 8, 2023 at 22:10

1 Answer 1

2
\$\begingroup\$

Apparently your microcontroller expects the serial signal with common logic voltages, mark (1) as near VDD ("high") and space (0) as near GND ("low").

The USB-to-RS232 converter inverts the voltages according to the standard RS232. This standard does not only specify pins on connectors, control signals, and more, but also the voltage levels as mark = -15 to -3V and space = +3 to +15V. By the way, it does not define the serial protocol, which is also relevant here.

You sent the characters '0' and '7', coded 0x30 = 0b00110000 and 0x37 = 0b00110111, respectively, in the asynchronous serial bit-stream protocol.

Appending some idle bits before and after the character, as your recording shows, you get the following.

Legend:
'i': idle bit (mark)
's': start bit (space)
'0' to '7': data bits (20 to 27, LSB to MSB)
'm': stop bit (mark)
'M': expected stop bit, received as space
'-': receiver waiting for a falling edge (mark-space combination) for the start bit

transmission of '0':

sent: iiiiiis01234567miiiiiiiii 1111110000011001111111111 received: 0000001111100110000000000 ------iiiiis01234567M---- 

transmission of '7':

sent: iiiiiis01234567miiiiiiiii 1111110111011001111111111 received: 0000001000100110000000000 ------is01234567M-------- 

As we can see, the receiver gets no valid stop bit, but apparently you did not program its detection.

The resulting values are clearly 0b00000110 = 0x06 and 0b01100100 = 0x64, respectively.

The recording shows the the lower voltage clamped to GND. This is commonly non-destroying because the current delivered from a RS232 driver is quite limited. However, you should avoid it.

Anyway, use a USB-to-TTL converter instead of a USB-to-RS232 converter to get a non-inverted signal. Or insert an inverter in the lines TxD and RxD, for example an RS232 level converter.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.