[ Log In ]

Introduction to the Atmel Atmega32 AVR microcontroller and the pin assignments (what each pin of the chip does). The pins include PORTs (PORTA, PORTB, PORTC and PORTD) and 8 pins for each port, ADC (Analog to Digital Converter), PWM (Pulse Width Modulation), and serial forms of communicatgion like I2C, SPI, UART, USART, and much more.

An interrupt is an event that stops regular program flow to run a separate set of code that relates to the interrupt. Interrupts have many uses and applications. Interrupts can be used with:
- Timers and counters (When a timer matches a specific value)
- Serial communications (USART, UART - tell you when data is received, or when the transmit is ready to take data to transmit)
- When a pin goes from high to low or vice verse
- ADC (Analog to Digital Converter) completes the conversion
- Software interrupt (custom)
- And many others.

With UART (Universal Asynchronous Receiver/Transmitter) and USART (Universal Synchronous Asynchronous Receiver/Transmitter), microcontroller communicate using two data lines called RX and TX. Rx is used for listening (receiving) to other sources like other chips or computers, and TX is used for talking (transmitting).

The speed that the two devices are communicating must be the same in asynchronous mode since there is no clock line connected between them. If each device has a different speed (baud rate), then the two would not be able to communicate.

In synchronous mode, the two devices are connected together by a wire that has a clock tick on it. One of the devices produces this clock on the clock line and the other device conforms to this clock frequency.

Two way communication can actually happen one wire and this mode is called half duplex. The transmission takes turns with the receiving. Full duplex is where the communication happens on two lines and the communication can happen simultaneously.

The communication that is sent from one device to another is sent as a data frame. The data frame consists of a start bit, data bits (i.e. 8-bit number), and one or two stop bits. The data frame may or may not include a parity bit.

The parity bit is a way for the microcontroller to determine if the data is valid or not. If something happened to the data on the way to the receiving end, the parity bit will give a clue to this error.

The Dynamixel line of servos are digital servos that use the UART form of communication. The mode of communication is Half Duplex and Asynchronous sinc ethe full communication happens only on one line.

There can be as many as 256 servos on the communication line. Each servo has a unique ID, so when there is a transmission, the data that is sent must contain the ID of the servo that is to be controlled.

The UART and USART communication happens using a data frame. The data frame consists of a start bit, data bits, parity bit and one or two stop bits.

The start bit informs the receiving device that the data bits are coming. The data bits are numbers of bits of varying lengths (i.e 8-bits). The parity bit is a check is the data frame contains an error. The stop bits inform the receiving end that the data bits are complete and the data can be processed.

The parity bit in the data frame of the UART or USART communication tells the receiving device if there is any error in the data bits. The parity mode can be set for even parity or odd parity.

The transmitting device sets the parity bit. If the parity is set for even, the transmitting device will put a 0 in the parity bit if there is an even number of 1's in the data bits. This makes the number of 1's including the parity bit even.

If the data bits have an odd number of 1's in the data bits, then the transmitting device will put a 1 in the parity bit to make the number of 1's including the parity bit to an even number.

The receiving device will determine if the data bits have an error. If the parity mode is set for even, then there must be an even number of 1's including the parity bit. If not, then the receiving device should reject that data frame and request another to be sent.

This is the same for odd parity. The number of 1's in the data bit must have an odd number of 1's including the parity bit.

The transmitting microcontroller will output the communication from its TX transmitter pin to the RX pin of the receiving microcontroller. A wire will be connected from the TX of the transmitting microcontroller to the RX of the receiving microcontroller.

A button on the transmitting microcontroller will initiate the transmission through the TX pin. The LED on the transmitting microcontroller will serve as an indicator that the push button is working. An LED on the receiver microcontroller will serve as an indication of a successful communication.

The UART communicates at a agreed upon speed set by the baud rate. This baud rate must be set in both the transmitting and receiving microcontrollers to work.

The the TX and RX pins will be used for communication and not for the general purpose I/O that exists at those pins, then the TX and RX need to be enabled.

The stop bits are at the end of the data frame and signal the end of the that frame for the receiver.

Setting for stop bits, use the USBS (UART Stop Bit Select) in the UCSRC (UART/USART Control and Status Register C)

To send data on the TX pin of the microcontroller, the transmitter must not be busy with another transmission of data. The UDRE (USART Data Register Empty) is a flag that tells us when the transmitter is ready.

This is the result of the one way communication of one microcontroller transmitting through its TX pin and another microcontroller receiving through the RX pin. A push button switch is connected to the transmitting microcontroller to initiate the transmit. Both microcontrollers have an LED to show the successful push button switch press and the show a successful transmission on the other microcontroller.

Before the receive data can be read, the receive data ready flag must tell us that the data is ready to take. If the RXC (Receive Complete) flag in the UCSRA (USART Control and Status Register A) must be on, so a while (! (UCSRA (1 << RXC)) ); is used to wait for this flag to be set.

If there is more than one USAR/UART, then RXC is RXC0 and UCSRA is UCSR0A.

A function can be created from existing code, as seen in this example. Surround the code with {} braces. Above the braces, add the function return type (if no return type, use void) followed by the function name and then add parameters within the parentheses(). The code may need to change is there are parameters and there may need to be a return if there is a return type included in the function declaration.

This is a function that is created for a library to be reused whenever the microcontroller needs to receive data from the UART/USART. The RXC is the Receive Complete Flag and lets us know when the receive is complete so we can get the data in the UDR (UART Data Register). The return type is unsigned char because we want the returned data in the positive realm of 0-255 so it conforms to the ASCII set of characters.

The 0's at the end of each register is used because the microcontroller has two USARTs.

Since we are transmitting data, not return is necessary, but we will need to include a parameter. The parameter will be the data that will be transmitted. The UDRE (USART Data Register Empty) is a flag that tells us when the data register is empty so we can populate the UDR (USART Data Register). When it is ready, we can assign the data to the UDR register. The following code is for microcontroller with more than one USART/UART and uses the first one (the 0's at the end of the registers):

The section of the UART/USART initialization for determining the baud rate had an error. The error pertained to first not containing the product of 16 and baud so the F_CPU is not divided by the 16 before the 16 is multiplied by the baud. The second problem was that the letter 'L' was not added after the 16. The L, in the C programming language informs the compiler that the 16 is a long integer.

The section of the UART/USART initialization for determining the baud rate had an error. The error pertained to first not containing the product of 16 and baud so the F_CPU is not divided by the 16 before the 16 is multiplied by the baud. The second problem was that the letter 'L' was not added after the 16. The L, in the C programming language informs the compiler that the 16 is a long integer.