NEMA 17 Stepping Motor (62 oz-in 5mm single shaft)
10K Trimmer Potentiometer (Through Hole)
16x2 LCD (Liquid Crystal Display)
White Prototyping Breadboard (2x30 columns of tie strips and 2x2 rows of power strips)
Clear Prototyping Breadboard (2x30 columns of tie strips and 2x2 rows of power strips)
Single Red Through Hole LED (Light Emitting Diode)
Single Green Through Hole LED (Light Emitting Diode)
Single Yellow Through Hole LED (Light Emitting Diode)
Here is the plain english information to understand the USART (Universal Synchronous
Asynchronous Transmitter/Receiver). That is to say, the information contained here
is an interpretation of the datasheet so it is easily understandable. The USART
feature of the AVR microcontroller can communicate with another microcontroller
(not necessarily another AVR microcontroller), multiple microcontrollers, or a computer
using a voltage level shifter or converter. The USART can transmit data using a
buffer and a shift register (more on that later), receive data using a shift register
and buffer, create a frame of data that is recognized on both the receiving end
and the transmitting end. All of this works according to an agreed upon speed from
both sides, or with synchronous mode where the clock line (a wire) is directly connected.
Sending information and receiving information in UART and USART communication is
like riding on a train. The train sends you to a destination like it sends data
to another computer or microcontroller. The train station is like the internal transmitter
and receiver inside the microcontroller. There must be a place that the train riders
queue to get on the train. First, a passenger will get the ticket and sit down to
wait for the next train to arrive. The passenger then must queue up to get on the
train. Then the passenger gets on the train. This is exactly how a USART tranmitter
functions. The data is sent to a buffer, which is like the waiting room for the
train. The shift registers is like the queue to get on the train. The data moves
from the buffer and onto the shif registers. This is after the previous data has
left the shift registers. From the shift registers, the data moves along the transmit
wire, just like a train moves along a track.
Receiving information is the same but in reverse. In the human analogy, the train
arrives to the train station. The person gets off of the train and must wait in
a queue so that the people in front of them makes space. In the microcontroller
reception of data, the data gets off the wire and goes straight into the shift registers.
With the Atmega32, there are two buffers that the data can use to wait to be used
by the program in the microcontroller. With the shift register, this gives the receive
area three total buffers for the data to sit, waiting to be used. This is established
with the chip so Data Over Runs (DOR) are less likely. The DOR is a flag that you
can look at.
The train is actually similar to the baud rate. The baud is the clock that pushes
the data along the line. There are a couple types of clocks you can apply using
UART and USART. The UART only allows you to apply an agreed upon baud rate from
both parties (each microcontroller must be set with this specific baud rate). In
USART, a clock wire must be connected between each microcontroller. This wire will
pulse like a heartbeat. In the case of asynchronous, each microcontroller has its
own clock and since the data is being sent with the transmitting microcontroller
using this clock (baud rate), the receiving microcontroller must be receiving this
data at the same pace, so its clock (baud rate) must be the same.
Pick between Synchronous or Asynchronous
Asynchronous is where the microcontroller's clock is not connected together by a
wire with the other microcontroller, but they need the same clock beat to process
the data on the data line.
It's 25 because the UBBR will not accept a decimal value, but that introduces a
0.2% error, which is acceptable. You can find a table of commonly used UBBR numbers
in the "Examples of baud rate settings" under the USART section of the datasheet.
Finding the baud from the UBBR is optional:
Pretty close to the standard 2400 baud with 0.2% error which is acceptable. Check
your datasheet for acceptable error percentages.
Setting the baud rate:
The URSEL is set to 0 because UBBRH shares the same I/O as UCSRC, the URSEL must
be set to 0 to write to UBBRH. The UBBRH sets the high portion of the baud rate
- from bit 8 to bit 11. The UBBRL puts the remaining bits 0 to 7 into the low UBBR
Setting the Asynchronous mode:
Where the clock is connected with a wire between the two microcontrollers - DDR_XCK
Data Direction Register controls which microcontroller is the master and which is
the slave. If the DDR_XCK is set to output, then the XCK pin on that microcontroller
is the master since it will be creating the clock output on that XCK pin.
This is the actual data that is transmitted. It's the train with the data riding
on-board. This is with 9 bits for the data bits, one start bit, two stop bits, one
parity bit which is the maximum frame size.
There is a maximum of 13 bits in the largest data frame.
Setting the Data Bit Size - Number of data bits you want in the frame. Use
the UCSZ2:0 (UCSZ0, UCSZ1, UCSZ2) bits in the UCSRC register. Note, keeping all
the bits in UCSZ2:0 not set establishes a 5-bit data bit length.
UCSZ - Character Size
Huh, what's that "2, 3 and 7"?!? Well, it's about time you learned a shorthand way
of setting bits. The UCSZ0 is the equavilant to the one's place in binary. If you
put another number, like 7, you are actually putting the binary equivalent of the
number 7 in that location which fills up the next binary digits, because the number
7 is 111 in binary.
Note: If you want to change any of this frame stuff shown below, take a look at
the TXC and RXC flags and make sure they are not set. If they are set, then there
is still transmissions going on. For most microcontroller projects, this may never
be needed, unless you want control of the port I/O pins onthe chip where TX and
Another note: For some strange reason, UBBRH and UCSRC share the same I/O location,
so you need to make sure:
Setting the Parity Mode - This is where you set the error checking feature
of UART and USART communication. The parity is set only by the transmitter.
If the parity is set for "even" then there are an odd number of ones in the
data bits, the transmitter will set the parity bit to a "1" so there will be an
even number of ones including the parity bit.
If the parity is set for "odd" then there are an even number of ones in the
data bits, the transmitter will set the parity bit to a "1" so there will be an
odd number of ones including the parity bit.
UPM - Parity Mode
Setting the number of Stop Bits to use - Do you want one or two cabooses
on the end of your train? Remember that the receiver will ignore the second stop
bit, so why did they put it in there in the first place? Who knows, but I think
that it adds a "bit" of breathing room for the next data frame. How do you like
that pun? Huh!
USBS - Stop Bit Select
Note: If the microcontroller receiving the data frame (the choo choo train) sees
a stop bit that is low (it is supposed to be high), then the Frame Error Flag Bit
(FE) will be set.
In this example, the baud is being set, the transmitter (TXEN - Transmitter Enable)
and receiver (RXEN - Receiver Enable) is being enabled, and in the UCSRC, the URSEL
is set so we can modify UCSRC, 2 stop bits used and the length of the data bits
You'll notice that, first, this is a function and, second, an unsigned int is being
passed into this function. The void in the beginning means that this function will
not return anything when it completes. This is the same example that the Atmega324
datasheet shows. Notice that the parity is not set in this example.
Note: When TXEN is set (Transmitter Enabled), the General Purpose function of that
pin is not availble until the TXEN is disabled. In other words, you cannot use TX
pin to light up LEDS, or receive button presses and stuff like that. Same idea goes
for the RXEN and the RX pin. One other thing, the TXEN cannot be disabled while
there is a pending transmission. If you are using the XCK pin for synchronous operation,
then the other general purpose function at the XCK pin is disabled.
In this next example, we will actually transmit something. We are gonig to throw
our data into the UDR (USART I/O Data Register) train station. I will let you in
on a secret here, because you may get confused a bit later: You will put the data
you want to transmit in the UDR register, and you will also get received data from
the UDR as well. That seems totally off, doesn't it. If that's true then how would
we be able to transmit and receive at the same time when we only have one data register
to use... I mean, if full duplex is available, why is there only one register to
transmit data and receive data. Seems odd.
Well, here is the secret! Shhhhh, don't tell anyone! The UDR actually has two places.
The secret locations are called TXB and RXB (these are 8-bit locations). The microcontroller
is so smart that when you place information into the UDR register, the stuff you
put in is actually going into the TXB location. If you take the data from the UDR
register, then you are actually taking the stuff out of the RXB location. You might
be asking, what if we decided to set our data-bit lenght to 9 bits, cause TXB and
RXB is 8-bits only? Those bits are located in the UCSRB register and their called,
you guessed it, TXB8 and RXB8. Oh, one more thing, this is a polling example which
means that we are going to wait (tap our toes) for the UDRE (USART Data Register
Empty) signal to say its ok to go. It's like a stop light. You can't put anything
into the UDR if the system is not ready for you. Ok enough jibber jabber, lets get
to the example:
The condition in the while statement may look foriegn to you. It is seemingly complicated,
but it is the same type of (and) and (not) bitwise operation that we learned in
an earlier video. The ! is a (not) and & is an (and). Remember when we say register
&=~(1 << bit)? We are doing an & (and) calculation and reversing the bits with the
! (not). We can also put this in a condition like this: If you want to check if
a bit is (not) 0, then we use (! (register & (1 << bit)) ).
Ok, so here is an explanation of the flags that inform us about the transmit process:
Interrupt Resources: Remember to set your global interrupt variable before using
these cool resources!
The process for the microcontroller to receive data that exists on a wire from the
other microcontroller goes like this:
Here is an example how to receive data:
Do you notice the unsigned char at the beginning of our function statement? That
is the type of return we will provide. The UDR will be an 8-bit unsigned value (char).
We use the return statement to release the data from the UDR and pass it back to
the main program.
Let me know if you want to see a 9-bit example.
Ok, so here is an explanation of the flags that inform us about the receive process:
Interrupt Resource: Remember to set your global interrupt variable when using this!