Friday, November 15, 2013

USART Communication - Sanguino (Atmega644p)!

USART is Universal Synchronous Asynchronous Receiver and Transmitter. It is the simplest form of communication used to communicate with any micro-controller either for debugging or for data transfer or for flashing! First thing we should know before using USART/UART communication is baud-rate. Baud-rate is the number of bits transferred per second. ex: 9600 i.e. 9600 bits transferred per second.

Micro-controllers needs Voltage source and Clock to run. Clock is like heart beat for any micro-controller. Clock is the word given for oscillations produced by the crystal oscillator. Oscillations produced by crystal oscillator is measured in terms of Hz. ex: 8 MHz - i.e. 8 * 10^6 oscillations per second. At every oscillation micro-controller can execute an instruction.

To establish USART communication, If baud rate is b, then b number of bits has to transferred in f number of oscillation (where, f is clock speed or number of oscillations in 1 sec). So, 1 bit should be transferred after every f / b oscillations (let, x = f / b). After every x oscillations 1 bit has to be transferred to reach a baud rate of b. We can have counter to monitor these x oscillation. Counters will increment or decrements after every oscillation. For up counter one extra clock cycle/oscillation is required, as count value needs to be loaded to counter after resetting to 0. For down counter one less clock cycle is required as once it resets it can directly reloaded with count value. Counter initialization, increment or decrements of count is nothing but execution of instructions. Let us say d (divider) is the clock cycles required to execute those instructions. Then, new counter value will be x = ( (f / b) / d ).
For Up counter x = (f / (b * d)) + 1,
for Down counter x = (f / (b * d)) - 1.

Each micro-controller will have the following registers:

  • Baud Rate Register (Counter value)
  • Data Register(s) - for Transmitting/Receiving the data
  • Configuration Registers

USART/UART configuration like Number of data bits, number of stop bits, parity bits, synchronous or asynchronous, interrupt driven, receive or transmit can be done by writing appropriate bits in Configuration Registers. Data Register(s) empty or filled information, communication error information, interrupt flag information can also be found in Configuration Registers.

Steps for writing USART/UART communication code:

  1. Go through the USART/UART part of datasheet for micro-controller (click here for Atmega644p).
  2. Initialize the Baud Rate Register (counter value calculated using the above formula. Check datasheet to verify the formula, also check the baud rate error range).
  3. Configure the USART/UART by setting appropriate bits in Configuration Registers
  4. For receiving data, wait for data to be available in Data Register and then return the Data Register content. pseudo code: 
  5. For Transmitting data, wait for Data Register to become empty and then fill the Data Register with the data to be transmitted. pseudo code: 

Click here for USART driver code files for Atmega644p (Sanguino). With that you can easily communicate with your Atmega644p (Sanguino board).

No comments:

Post a Comment