Communication interfaces of Ansteron Board
A microcontroller usually has fixed amount of memory and peripherals. When other functionalities and extra data storage are needed, they can be added using communication interfaces which are built-in features of the microcontroller. Basic things like sending a message to be displayed on the screen, reading date and time from a digital clock, saving data to a memory chip...can be done using communication interfaces.
Ansteron Board has three communication interfaces which are commonly used. The first one is usually called "serial", technical name is "Universal Asynchronous Receiver/Transmitter" (UART). The second one is TWI (Two Wires Interface). It is similar and compatible with I2C (Inter-Integrated Circuit, referred to as I-squared-C, I-two-C, or IIC), which is Philips' patented technology. The third one is SPI, or Serial Peripheral Interface, which requires more pins than the other two. However, it can operate at higher speed.
Note that all of the above interfaces are serial (in term of type), which means one bit is transferred at a time. Don't confuse the term when the first interface is commonly called "serial".
All communication interfaces mentioned here are only for communication locally, within a circuit board or a device enclosure, where signal lines are not too long. For longer distance, additional transceiver may be needed.
2. Basic terms
- Bus refers one set of signal lines can be shared by many devices. This term doesn't apply to Serial interface, which is one to one. In this case, if you need to connect with more devices, you will need more Serial interfaces. TWI, on the other hand, can be a bus, capable of connecting up to 127 devices sharing two signal lines (each device is identified by its address). SPI can be a bus as well, sharing 3 signal lines. Each device on SPI bus will need one additional signal line to be individually identified.
- Master and Slave are the two terms associate with the bus type connection. Since there are more than one device on the bus, collision will occur when two devices try to send data at the same time or in the middle of the other. So, there will be one master and many slaves on a bus. The master, which is the microcontroller, will manage all the transfers and each slave will only respond when the master selects it.
- Bitrate is the speed at which data is transferred, measured in bit per second. For Serial interface, bitrate is called baurate, and both devices must have the same baurate for the data to be transferred correctly. The bitrate of TWI and SPI is decided by the master, and usually can be changed on the go. Each device acting as slave on TWI or SPI bus can only handle a certain maximum bitrate; the master can select suitable bitrate for each device or operate at the lowest speed that all the slaves can handle.
3. Serial Interface(UART)
Serial interface is usually used to communicate with the computer. Ansteron Board has one serial port connected to on-board interface chip which connects to the computer's USB port. Data can be sent through that to display on the screen with Serial Terminal software, a built-in feature of Ansteron IDE. Data can be sent from the computer to the board on the same port as well. Users can also create their own software on the computer to communicate with the microcontroller via this interface.
Serial interface has two signal lines, one is data transmit (TXD), the other is data receive (RXD). Therefore, serial interface can transmit and receive data at the same time. The diagram below is the connection between the CPU and interface chip of Ansteron Board. When the interface chip is not active, these signal lines are virtually disconnected and they can be used for connecting to other devices. Serial interface shares pins D0 and D1 with digital I/O. When serial interface is activated, digital I/O function is automatically disconnected.
Two Ansteron Boards can "talk" with each other using serial interface as well. Note that TXD of the first board is connected to RXD of the second one and TXD of the second one is connected to RXD of the first one (transmit pin of one goes to receive pin of the other). The two boards must have their GND connected.
In the program, Serial interface must be set up before use. Function
setup_serial(DEFAULT); will start Serial interface with default setting (bautrate is 115200bps). For other settings, refer standard library for the complete list.
Once the interface is ready, data can be sent and received using functions below. Data sent to computer can be viewed with Serial Terminal. To run this utility, in Ansteron IDE, go to menu "Tools" then select "Serial Terminal". After setting appropriate port and baurate, click on "connect" button to start capturing data on serial port.
For more details, see Standard Library.
Two Wire Interface has two signal lines, SCL (serial clock) and SDA (serial data). The master of TWI bus provides clock signal on SCL line and data is sent or received between master and slave on SDA line. All transactions on TWI bus are triggered and controlled by the master.
Each device acting as slave on TWI has a unique address. The master starts a transaction by sending the address of the device it wants to talk with. Only the one has that address will send or receive data (could be 1 byte or more) until the master issue a stop signal.
Under the view of user programs, using TWI bus comes down to two basic operations, read and write. The master can read or write one byte or more to a device acting as a slave on bus. Usually, each device on TWI bus contains a set of registers whose values will result in different operations of the device.
TWI on Ansteron Board shares pin C4 and C5. The example circuit below shows how to connect a nonvolatile memory chip (24C512) to Ansteron Board. Note that the two 10K resistors are optional if the signal lines are not too long. Ansteron Board has two internal pull-up resistors that are automatically connected when TWI is initialized.
Before using, TWI must be set up. Function
setup_TWI(DEFAULT); will initialize TWI interface to work as a master with 100KHz bitrate. Internal pull-up resistors (around 50K Ohm) are also enabled. To send (write) data to slave devices, use function
TWI_send(device_add, data_ptr, byte_count); where
device_add is the address of device,
data_ptr is the pointer to data and
byte_count is number of byte to be sent (255 bytes or less). For example, to send 1 byte to a device at address 0xA0, type
TWI_send(0xA0,&data,1); In the same manner, function
TWI_read(device_add, buffer, byte_count); will read
byte_count byte(s) from device that has address
device_add and place them into a buffer pointed to by
Every TWI device may have a different way to work with. The protocol, address and memory structure should be described in device�s datasheet. There are many easy to use libraries built on these functions that handle the connection with common devices. User programs can use those libraries instead of re-doing the protocol part.
When using the two functions above, it may be confusing to new users when there is a pointer input as one of the arguments. To make it simple, just remember when a byte ( char variable) is sent or read, use operator "
&" in front of the variable to get the pointer to it. Integer variables (
int, 16 bit = 2 bytes) or large integers (
long, 32 bit = 4 bytes) can be sent or read in the similar way as with one byte
5. Serial Peripheral Interface (SPI)
SPI can be a bus with one master and many slaves. The bus has three required signal lines, SCK (Serial Clock) which sync the master and the slave, one bit is transferred every SCK clock cycle. MISO (Master In Slave Out) is data signal line from Slave to Master. MOSI (Master Out Slave In) is data signal line from Master to Slave. With this structure, data are actually exchanged between master and slave. Whenever a byte is send from the master to a slave, a byte from the slave is also sent to the master. Therefore, to write, the master can send data in a way that the slave can understand and ignore what came back from the slave (which could be a byte the master sent previously). To read, the master can just keep sending no-meaning data (0x00 or 0xFF) and check for valid data coming back from the slave.
There is no limit on the number of devices can connect to one bus. However, each device acting as slave on SPI bus must have one additional signal line, Slave Select (SS), from the master, to signal the device when master wants to send or read data. Note that SS signal is active low and a 10K resistor connecting the line to +5V may be needed to keep this line high when it is inactive.
SPI of Ansteron Board shares pins with digital I/O, B5 (SCK), B4 (MISO), B3 (MOSI) and B2 (SS). When Ansteron Board acts as master, SS signals to other slaves can be any digital I/O but one of that must be pin B2. Note that pin B2 cannot be set as input when Ansteron Board works as SPI master.
The example circuit above show the connection of memory chip 25LC1024 on SPI bus. When creating programs for Ansteron Board that utilizes SPI, there are two basic functions. To set up SPI with default setting, master mode, 250KHz bitrate, use
setup_SPI(DEFAULT);. Other settings options are available as well, refer Standard Library for the complete list.
SPI_transfer(data); performs a data transfer between master and slave. Prior to issuing this command, SS signal of the slave must be set low (using function
clear_pin(PIN_xx);). The transfer function also returns one byte data which came back from the slave (ex.
Handling the protocol to different devices could be complicated. There are libraries for many SPI devices built by experienced users which can be easily added into any project. New users are recommended to use those libraries which can also be modified if needed as they are mostly open source.
Important note: SPI is also used by on-board programmer to download programs to CPU. Three out of 4 pins (SCK, MISO, MOSI) of SPI are used for this purpose. One 10K pull-up resistor on SS pin of each Slave device is required to avoid unintended connection while the new program is being downloaded to the microcontroller.
6. Further reading
There is more about electrical signals and operations of these interfaces which were not mentioned in this tutorial to keep it simple for new users. For detailed information on these interfaces, please refer ATMEGA328P datasheet (CPU of Ansteron Board) and the links below: