Where I describe the process of writing and using a serial communications program for the Microchip ATtiny13A.
Introduction#
Even with a microcontroller as the ATtiny13A, it is desirable to have the ability to communicate via the serial port. The ATmega328P and others have a UART built-in which makes it much easier to integrate serial communications. With the ATtiny13A, serial communications has to be performed completely in software and using assembly language makes it possible and reliable.
The code for this article and other articles on the ATtiny13A can be found in the ATtiny13A repository. This particular example is examples/asm_softserial.
Serial Background#
Serial communications can either be synchronous (using a common clock) or asynchronous (using an agreed protocol). The latter is more prevelant and the mode we’ll use. The hardware required, other than two pins on the ATtiny13A, will be a USB to serial interface, such as USB to TTL Serial Cable or Olimex Black USB Type A to 3 Wire.
The concept is simple, send a start bit, 7-8 data bits, then a stop bit, all at a constant rate. On both ends, have the software wait for a start bit, compile the next 7-8 bits into a data byte, confirm the stop bit and repeat. To be successful, it requires a fairly accurate clock and the ability to respond quickly. The ATtiny13A has an RC oscillator which can be used, however, it frequently needs to be “tuned”. Using assembly language solves the second problem as the clock cycles for an instruction will be in the sub-microsecond range, while the bit rates will be in the millisecond range.