Friday, 8 November 2019

microcontroller - How does TTL serial work?


I've been trying to find a good description of the TTL serial "standard" without much luck. I understand that serial transmit (TX) and receive (RX) lines idle high (at VCC) and that they drop to ground when a bit is transmitted. As such, they're inverted from the norm, where a "1" is high and "0" is low.


What I don't understand is who's responsible for holding the line high and how a zero is transmitted. Does the sender drive the line to high and low? Or does the receiver hold the line high with the sender pulling the line low (open collector)?




Answer



With TTL serial, there are two unidirectional data lines. Each is driven by the sender, both high and low. A 0 bit is represented by 0V a 1 bit by VCC.


The receiver's pin should be set to an input.


So, for a microcontroller to send a byte (8-N-1 no flow control) it could do something like this:


#define BAUDRATE 9600
#define DELAY (SYS_CLK/BAUDRATE)

#define UART_BITBANG_OFF UART_BITBANG_PORT |= _BV(UART_BITBANG_PIN)
#define UART_BITBANG_ON UART_BITBANG_PORT &= ~ _BV(UART_BITBANG_PIN)


#define UART_BITBANG_BIT(bit) {if (bit) UART_BITBANG_ON; else UART_BITBANG_OFF; _delay_us(DELAY);}

void uart_bitbang_init(void)
{
UART_BITBANG_DDR &= ~ _BV(UART_BITBANG_PIN); // TX output
}

void uart_bitbang_putc(uint8_t c)
{
UART_BITBANG_BIT(1)

UART_BITBANG_BIT((c & 0x1) == 0);
UART_BITBANG_BIT((c & 0x2) == 0);
UART_BITBANG_BIT((c & 0x4) == 0);
UART_BITBANG_BIT((c & 0x8) == 0);
UART_BITBANG_BIT((c & 0x10) == 0);
UART_BITBANG_BIT((c & 0x20) == 0);
UART_BITBANG_BIT((c & 0x40) == 0);
UART_BITBANG_BIT((c & 0x80) == 0);
UART_BITBANG_BIT(0);
}


(This code reads a bit backwards as it was originally meant for inverted TTL serial)


Of course, most MCUs have hardware UARTs which do all this for you.


Here's what you'd see on a scope:


https://www.pololu.com/docs/0J25/4.a



Here's a great video from ladyada explaining serial: http://www.adafruit.com/blog/2010/09/15/usb-serial-and-you-video-an-adafruit-after-school-special/


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...