Serial Communication: UART
UART is the simplest way to get a PIC talking to a computer or another microcontroller: one wire out, one wire in, and an agreed-upon speed.
UART (Universal Asynchronous Receiver/Transmitter) is a peripheral that sends and receives data one bit at a time over a pair of wires — one dedicated to transmitting (TX) and one to receiving (RX) — without needing a shared clock signal between the two devices. "Asynchronous" is the key word: instead of a clock line telling both sides exactly when to read each bit, both sides simply agree in advance on a speed, called the baud rate, and time their bits accordingly.
Because there's no shared clock, both ends must be configured to the same baud rate — a common choice for simple projects is 9600 bits per second — or the receiving side will misread the timing and see garbled data. Beyond baud rate, UART also has small configurable details like the number of data bits per byte (typically 8) and whether a parity bit is used for basic error checking (commonly none, for simple hobby projects).
UART is commonly used to send debugging text from a running PIC back to a computer, viewed with a simple terminal program — an enormously useful technique once you move past blinking LEDs, since it lets your chip "talk" to you about what it's doing while it runs.
void uart_send(char c) {
while (!TXIF); // wait until transmitter is ready
TXREG = c; // load the byte to send
}
void main(void) {
// (baud rate & TX/RX pin setup omitted for brevity)
while(1) {
uart_send('H');
uart_send('i');
__delay_ms(1000);
}
}