IAR Workbench Supports Next-Generation AVR MCUs

IAR Systems recently announced that its Embedded Workbench now supports a new generation of Microchip Technology 8-bit AVR microcontrollers. You can use Workbench with AVR microcontrollers to develop a wide range of low-power systems.

IAR Embedded Workbench’s features, benefits, and specs:

  • The compiler and debugger toolchain IAR Embedded Workbench incorporates the IAR C/C++ Compiler, which can create compact code for Microchip’s 8-bit AVR MCUs
  • IDE tools (editor, project manager, and library tools)
  • Build tools (compiler, assembler, and linker)
  • C-SPY debugger (simulator driver, hardware debugging, power debgging, and RTOS plugins
  • IAR Embedded Workbench offers full-scale support for AVR 32-bit MCUs as well as Microchip’s ARM®-based and 8051-based MCUs families.

Source: IAR Systems

Arduino Program Timing

Ed Nisely explains how output pulses can reveal the progress of invisible firmware routines. After presenting a straightforward way to instrument your code, he covers the detailed timing produced by Arduino statements and functions.

While writing the Arduino program for the RGB LED lights mentioned in my May column (Circuit Cellar 310), I wondered how much time the floating-point trigonometric calculations required. Rather than wrap a software measurement around the code, I added a pair of instructions to flip an output bit, then watched the results on an oscilloscope. It’s a classic technique that helps you debug software when there’s no other way to see what’s going on inside your code or when elaborate tracing routines will disturb the firmware’s normal operation.

In this column, I’ll show a simple way to instrument your code, then take a look at the detailed timing produced by Arduino statements and functions. You’ll need an oscilloscope or logic analyzer for this technique, but even the least-expensive instruments will suffice.

FUNDAMENTAL PULSE WIDTHS

Listing 1 shows the simplest Arduino program that produces a visible output. Those two lines toggle Pin 13, the LED found on nearly all Arduino boards, to form the 5.1 µs pulses in Photo 1, with each pulse marking one iteration of the loop() that executes continuously after finishing the configuration in the setup() section. Although the oscilloscope cursor readout provides three fractional digits, the true resolution is considerably worse, because each horizontal pixel covers 0.040 µs.

LISTING 1: The simplest possible Arduino program produces a single pulse on an output bit during each loop() iteration. Although the ATmega328 microcontroller can flip a bit with a single assembly language instruction, each of these high-level functions executes dozens of assembly instructions before and after the bit changes.

LISTING 1: The simplest possible Arduino program produces a single pulse on an output bit during each loop() iteration. Although the ATmega328 microcontroller can flip a bit with a single assembly language instruction, each of these high-level functions executes dozens of assembly instructions before and after the bit changes.

The Atmel ATmega328 microcontroller can toggle an output bit with a single instruction, which makes the digitalWrite() function in the Arduino runtime infrastructure seem overly elaborate: fifteen lines of code with a path length of 12 lines. Among other tasks, the code must select and verify the hardware port, produce a bitmask selecting the pin, handle the special case of a PWM pin, and disable interrupts while manipulating the port.

PHOTO 1 Each of the digitalWrite() functions producing this pulse requires 5.1 µs, roughly 50 instructions, on the 16 MHz ATmega328 microcontroller. The Arduino loop() adds 0.40 µs to the low portion of the waveform.

PHOTO 1: Each of the digitalWrite() functions producing this pulse requires 5.1 µs, roughly 50 instructions, on the 16 MHz ATmega328 microcontroller. The Arduino loop() adds 0.40 µs to the low portion of the waveform.

Homework: Find and examine the digitalWrite() function in the Arduino source code, then explain why all that additional logic isn’t optional.

The single AVR machine-level instruction that produces the waveform in Photo 1 is located near the end of the digitalWrite() function, with most of the execution time occurring before the output changes. The high part of the pulse therefore includes the short tail end of the first digitalWrite() and the longer beginning of the second, but the output pulse width shows the execution time through the complete function.

The 16 MHz crystal oscillator on the Arduino UNO clone board I used for these measurements produces a 62.5 ns instruction cycle, so each 5 µs pulse contains about 80 cycles. Most AVR arithmetic instructions require one or two cycles, branches require two or three, and subroutine CALL/RETURN pairs soak up eight, so assuming 1.6 cycles/instruction for short subroutines seems reasonable and allows you to figure the results in your head: 10 instructions per microsecond. That means the compiler translates the dozen high-level lines of code in the digitalWrite() function into 50 machine-level instructions.

Homework: Examine the compiler’s machine-level output to determine the actual number of instructions.

The low part of the pulse occurs as execution continues through the Arduino infrastructure as the loop() returns control to the first digitalWrite() line, adding 400 ns behind the scenes. That’s only six machine cycles, perhaps three or four machine instructions, and not much overhead at all.

TRACING A MISSING PULSE

Triggering your oscilloscope on the signal produced by Listing 1 should produce a steady display, but you’ll see “ghost” traces that seem to show missing pulses. Setting the trigger to produce single sweeps will eventually produce a display similar to Photo 2, with an obviously missing pulse.

PHOTO 2: A “missing pulse” occurs when a timer interrupt executes additional instructions that don’t appear in the user program. The interrupt can also occur when the pulse is high, causing an unexpectedly long pulse.

PHOTO 2: A “missing pulse” occurs when a timer interrupt executes additional instructions that don’t appear in the user program. The interrupt can also occur when the pulse is high, causing an unexpectedly long pulse.

Because the program’s logic (if you can call it that) lacks branches that could omit a pulse, the problem must lie elsewhere. Carefully examining the pulse timings shows that there’s not quite enough time for another pulse in that gap, which means the extended time came from a delay, rather than an omission.

The Arduino infrastructure includes a millis() function that reports the elapsed time in milliseconds since the microcontroller emerged from its most recent reset. A timer interrupt occurs every millisecond, with the interrupt handler updating a counter that the millis() function returns to the caller in a four byte unsigned long integer.

Because the loop() in Listing 1 iterates every 10.4 µs, the timer interrupt will occur once in every 96 pulses, stretching either the high or low part of the pulse. The advantage of an oscilloscope should be obvious: a problem that occurs 1% of the time might not appear amid all the “correct” values shown by a simple software measurement.

The if() statement in Listing 2 determines whether the return value from millis() has changed and, if so, produces a pulse on Pin 11. The lower trace in Photo 3 shows that pulse, with the oscilloscope triggering a single sweep on that pin and the trigger point at the middle of the screen. The upper trace comes from Pin 13, as before, and shows the same “missing” pulse due to the timer interrupt handler.

LISTING 2: The millis() function returns the number of milliseconds since the microcontroller emerged from the most recent reset. The comparison will be false 98.7% of the time, but that test adds 2 µs to the average loop() time.

LISTING 2: The millis() function returns the number of milliseconds since the microcontroller emerged from the most recent reset. The comparison will be false 98.7% of the time, but that test adds 2 µs to the average loop() time.

The cursors bracketing the 7.4 µs pulse in the upper trace show that the if() statement adds 2.3 µs to the execution time of the code in Listing 1, even when the value of millis() doesn’t change. The CPU uses those 35 clock cycles and two dozen instructions to call the millis() function, test its return value, then branch around the code inside the conditional block. You can see the effect of every instruction at this time scale!

PHOTO 3: The pulse in the lower trace shows that the “missing pulse” occurs when the return value of the millis() function changes. Testing the millis() value increases the loop() duration, as shown comparing the pulses in the top trace to those in Photo 2.

PHOTO 3: The pulse in the lower trace shows that the “missing pulse” occurs when the return value of the millis() function changes. Testing the millis() value increases the loop() duration, as shown comparing the pulses in the top trace to those in Photo 2.

The long pulse in the middle of the upper trace shows what happens just after the timer interrupt occurs: detecting the change, generating the sync pulse in the lower trace, and updating the Millis_Previous variable requires 22 µs, roughly 15 µs and 150 instructions more than the previous code.

A logic analyzer can trigger a capture based on the Boolean combination of many inputs, a situation that depends on having access to all those signals. A microcontroller buries those signals inside the CPU, where combining them into a trigger requires software, rather than hardware. A similar situation occurs with gate array logic, where a dab of additional logic inside an FPGA can generate suitable triggers, if you have a spare output pin, and may be the only way to isolate elusive glitches due to combinations that occur so rarely as to be invisible.

Homework: Move the statement that updates Millis_Previous between the two digitalWrite() functions, then measure the change in pulse width to determine the time required to update that variable.

As the number of statements inside the loop() increases, the likelihood that the timer interrupt will occur during the program also increases. This won’t make any difference to most programs, but when you’re watching a pulse on the oscilloscope, you don’t want a timer interrupt disturbing your measurements. The code in the next few examples will disable all interrupts just before raising the timing pulse and enable them after lowering it, forcing the timer interrupt to occur outside the critical section.

Download the entire article. This article appeared in Circuit Cellar 316 November 2016.

Ed Nisley is an electrical engineer and author in Poughkeepsie, NY. He has been writing for Circuit Cellar since Circuit Cellar 1, 1988. His regular column, “Above the Ground Plane,” appears every other month in Circuit Cellar magazine.

October Code Challenge (Sponsor: Programming Research)

Ready to put your programming skills to the test? Take the new Electrical Engineering Challenge (sponsored by Programming Research). Find the error in the code for a shot to win prizes, such as an Amazon Gift Card, a Circuit Cellar magazine digital subscription, or a discount to the Circuit Cellar webshop.

The following program will compile with no errors. It runs and completes with no errors.

Click to enlarge. Find the error and submit your answer via the online submission form below. Submission deadline: 2 PM EST, October 20.

Take the challenge now!

September Code Challenge (Sponsor: Programming Research)

Ready to put your programming skills to the test? Take the new Electrical Engineering Challenge (sponsored by Programming Research). Find the error in the code for a shot to win prizes, such as an Amazon Gift Card, a Circuit Cellar magazine digital subscription, or a discount to the Circuit Cellar webshop.

The following program will compile with no errors. It will crash when run. This is an example of working with link lists. The output should be:

LinkedList : 4321

LinkedList in reverse order : 1234

Click to enlarge. Find the error and submit your answer via the online submission form below. Submission deadline: 2 PM EST, September 20.

Click to enlarge. Find the error and submit your answer via the online submission form below. Submission deadline: 2 PM EST, September 20.

Take the challenge now!

August Code Challenge (Sponsor: Programming Research)

Ready to put your programming skills to the test? Take the new Electrical Engineering Challenge (sponsored by Programming Research). Find the error in the code for a shot to win prizes, such as an Amazon Gift Card or a Circuit Cellar magazine digital subscription.


The following is a sample piece of code that has a subtle programming error that would cause the software to fail. This code is a C++ language program but written as a function.

Specification: The program does a bubble sort for a list (array) of numbers. That means that it takes a list of numbers like this: 5 23 7 1 9 and turns it into 1 5 7 9 23.

Click to enlarge. Find the error and submit your answer via the online submission form below. Submission deadline: 2 PM EST, July 20.

Click to enlarge. Find the error and submit your answer via the online submission form below. Submission deadline: 2 PM EST, July 20.

Take the challenge now!

Find the Code Error: Take This Engineering Challenge (Sponsor: Programming Research)

Ready to put your programming skills to the test? Take the new Electrical Engineering Challenge (sponsored by Programming Research). Find the error in the code for a shot to win prizes, such as an Amazon Gift Card or a Circuit Cellar magazine digital subscription.

Click to enlarge. Find the error and submit your answer via the online submission form.

Click to enlarge. Visit the challenge page to access the submission form.

Take the challenge now!

January 2016 Electrical Engineering Challenge Live (Sponsor: NetBurner)

Ready to put your electrical engineering skills to the test? The January 2016 Electrical Engineering Challenge (sponsored by NetBurner) is live.

This month, find the error in the code posted below (and on the Challenge webpage) for a chance to win a NetBurner MOD54415 LC Development Kit ($129 value) or a Circuit Cellar Digital Subscription (1 year).

TAKE THE CHALLENGE NOW

Find the error in this code and submit your answer by January 20 via the online challenge form.

Find the error in this code and submit your answer by January 20 via the online challenge form.

PRIZES

Out of each month’s group of entrants who correctly find the error in the code or schematic, one person will be randomly selected to win a NetBurner IoT Cloud Kit and another person will receive a free 1-year digital subscription to Circuit Cellar.

  • NetBurner MOD54415 LC Development Kit: You can add Ethernet connectivity to an existing product or use it as your product’s core processor! The NetBurner Ethernet Core Module is a device containing everything needed for design engineers to add network control and to monitor a company’s communications assets. The module solves the problem of network-enabling devices with 10/100 Ethernet, including those requiring digital, analog, and serial control.NetburnerMod54415module
  • Circuit Cellar Digital Subscription (1 year): Each month, Circuit Cellar magazine reaches a diverse international readership of professional electrical engineers, EE/ECE academics, students, and electronics enthusiasts who work with embedded technologies on a regular basis.Circuit Cellar magazine covers a variety of essential topics, including embedded development, wireless communications, robotics, embedded programming, sensors & measurement, analog tech, and programmable logic.

RULES

Read the Rules, Terms & Conditions

SPONSOR

NetBurner solves the problem of network enabling devices, including those requiring digital, analog and serial control. NetBurner provides complete hardware and software solutions that help you network enable your devices.

NetBurner, Inc.
5405 Morehouse Dr.
San Diego, CA 92121 USA

December Electrical Engineering Challenge Update (Sponsor: NetBurner)

Spot the schematic error? Take the December Electrical Engineering Challenge (sponsored by NetBurner) now!

This month, find the error in the schematic posted below (and on the Challenge webpage) for a chance to win a NetBurner MOD54415 LC Development Kit ($129 value) or a Circuit Cellar Digital Subscription (1 year).

TAKE THE CHALLENGE NOW

Find the error in this schematic and submit your answer by October 20, 2015. Submit via the Challenge webpage. Click image to access submission form.

Find the error in this schematic and submit your answer by December 20, 2015. Submit via the Challenge webpage. Click image to access submission form.

PRIZES

Out of each month’s group of entrants who correctly find the error in the code or schematic, one person will be randomly selected to win a NetBurner IoT Cloud Kit and another person will receive a free 1-year digital subscription to Circuit Cellar.

  • NetBurner MOD54415 LC Development Kit: You can add Ethernet connectivity to an existing product or use it as your product’s core processor! The NetBurner Ethernet Core Module is a device containing everything needed for design engineers to add network control and to monitor a company’s communications assets. The module solves the problem of network-enabling devices with 10/100 Ethernet, including those requiring digital, analog, and serial control.NetburnerMod54415module
  • Circuit Cellar Digital Subscription (1 year): Each month, Circuit Cellar magazine reaches a diverse international readership of professional electrical engineers, EE/ECE academics, students, and electronics enthusiasts who work with embedded technologies on a regular basis.Circuit Cellar magazine covers a variety of essential topics, including embedded development, wireless communications, robotics, embedded programming, sensors & measurement, analog tech, and programmable logic.

RULES

Read the Rules, Terms & Conditions

SPONSOR

NetBurner solves the problem of network enabling devices, including those requiring digital, analog and serial control. NetBurner provides complete hardware and software solutions that help you network enable your devices.

NetBurner, Inc.
5405 Morehouse Dr.
San Diego, CA 92121 USA

October Electrical Engineering Challenge Update (Sponsor: NetBurner)

Spot the schematic error? Take the October Electrical Engineering Challenge (sponsored by NetBurner) now!

This month, find the error in the schematic posted below (and on the Challenge webpage) for a chance to win a NetBurner MOD54415 LC Development Kit ($129 value) or a Circuit Cellar Digital Subscription (1 year).

TAKE THE CHALLENGE NOW

Click the image to enlarge. Find the error in this schematic and submit your answer by October 20, 2015.

Find the error in this schematic and submit your answer by October 20, 2015. Submit via the Challenge webpage. Click image to access submission form.

PRIZES

Out of each month’s group of entrants who correctly find the error in the code or schematic, one person will be randomly selected to win a NetBurner IoT Cloud Kit and another person will receive a free 1-year digital subscription to Circuit Cellar.

  • NetBurner MOD54415 LC Development Kit: You can add Ethernet connectivity to an existing product or use it as your product’s core processor! The NetBurner Ethernet Core Module is a device containing everything needed for design engineers to add network control and to monitor a company’s communications assets. The module solves the problem of network-enabling devices with 10/100 Ethernet, including those requiring digital, analog, and serial control.NetburnerMod54415module
  • Circuit Cellar Digital Subscription (1 year): Each month, Circuit Cellar magazine reaches a diverse international readership of professional electrical engineers, EE/ECE academics, students, and electronics enthusiasts who work with embedded technologies on a regular basis.Circuit Cellar magazine covers a variety of essential topics, including embedded development, wireless communications, robotics, embedded programming, sensors & measurement, analog tech, and programmable logic.

RULES

Read the Rules, Terms & Conditions

SPONSOR

NetBurner solves the problem of network enabling devices, including those requiring digital, analog and serial control. NetBurner provides complete hardware and software solutions that help you network enable your devices.

NetBurner, Inc.
5405 Morehouse Dr.
San Diego, CA 92121 USA

October Electrical Engineering Challenge Live (Sponsor: NetBurner)

Ready to put your electrical engineering skills to the test? The October Electrical Engineering Challenge (sponsored by NetBurner) is live.

This month, find the error in the schematic posted below (and on the Challenge webpage) for a chance to win a NetBurner MOD54415 LC Development Kit ($129 value) or a Circuit Cellar Digital Subscription (1 year).

TAKE THE CHALLENGE NOW

Click the image to enlarge. Find the error in this schematic and submit your answer by October 20, 2015.

Find the error in this schematic and submit your answer by October 20, 2015. Submit via the Challenge webpage. Click image to access submission form.

PRIZES

Out of each month’s group of entrants who correctly find the error in the code or schematic, one person will be randomly selected to win a NetBurner IoT Cloud Kit and another person will receive a free 1-year digital subscription to Circuit Cellar.

  • NetBurner MOD54415 LC Development Kit: You can add Ethernet connectivity to an existing product or use it as your product’s core processor! The NetBurner Ethernet Core Module is a device containing everything needed for design engineers to add network control and to monitor a company’s communications assets. The module solves the problem of network-enabling devices with 10/100 Ethernet, including those requiring digital, analog, and serial control.NetburnerMod54415module
  • Circuit Cellar Digital Subscription (1 year): Each month, Circuit Cellar magazine reaches a diverse international readership of professional electrical engineers, EE/ECE academics, students, and electronics enthusiasts who work with embedded technologies on a regular basis.Circuit Cellar magazine covers a variety of essential topics, including embedded development, wireless communications, robotics, embedded programming, sensors & measurement, analog tech, and programmable logic.

RULES

Read the Rules, Terms & Conditions

SPONSOR

NetBurner solves the problem of network enabling devices, including those requiring digital, analog and serial control. NetBurner provides complete hardware and software solutions that help you network enable your devices.

NetBurner, Inc.
5405 Morehouse Dr.
San Diego, CA 92121 USA

September Electrical Engineering Challenge Live (Sponsor: NetBurner)

Ready to put your electrical engineering skills to the test? The September Electrical Engineering Challenge (sponsored by NetBurner) is live.

This month, find the error in the code posted below (and on the Challenge webpage) for a chance to win a NetBurner MOD54415 LC Development Kit ($129 value) or a Circuit Cellar Digital Subscription (1 year).

TAKE THE CHALLENGE NOW

Find the error in this code and submit your answer by September 20, 2015.

Find the error in this code and submit your answer by September 20, 2015.

PRIZES

Out of each month’s group of entrants who correctly find the error in the code or schematic, one person will be randomly selected to win a NetBurner IoT Cloud Kit and another person will receive a free 1-year digital subscription to Circuit Cellar.

  • NetBurner MOD54415 LC Development Kit: You can add Ethernet connectivity to an existing product or use it as your product’s core processor! The NetBurner Ethernet Core Module is a device containing everything needed for design engineers to add network control and to monitor a company’s communications assets. The module solves the problem of network-enabling devices with 10/100 Ethernet, including those requiring digital, analog, and serial control.NetburnerMod54415module
  • Circuit Cellar Digital Subscription (1 year): Each month, Circuit Cellar magazine reaches a diverse international readership of professional electrical engineers, EE/ECE academics, students, and electronics enthusiasts who work with embedded technologies on a regular basis.Circuit Cellar magazine covers a variety of essential topics, including embedded development, wireless communications, robotics, embedded programming, sensors & measurement, analog tech, and programmable logic.

RULES

Read the Rules, Terms & Conditions

SPONSOR

NetBurner solves the problem of network enabling devices, including those requiring digital, analog and serial control. NetBurner provides complete hardware and software solutions that help you network enable your devices.

NetBurner, Inc.
5405 Morehouse Dr.
San Diego, CA 92121 USA

July Electrical Engineering Challenge Live (Sponsor: NetBurner)

Ready to put your electrical engineering skills to the test? The July Electrical Engineering Challenge (sponsored by NetBurner) is live.

This month, find the error in the code posted below (and on the Challenge webpage) for a chance to win a NetBurner MOD54415 LC Development Kit ($129 value) or a Circuit Cellar Digital Subscription (1 year).

TAKE THE CHALLENGE NOW

Find the error in the code and submit your answer via the online Submission Form by the deadline: 2 PM EST on July 20, 2015. Two prize winners from the pool of respondents who submit the correct answer will be randomly selected.

Find the error in the code and submit your answer via the online Submission Form by the deadline: 2 PM EST on July 20, 2015. Two prize winners from the pool of respondents who submit the correct answer will be randomly selected.

PRIZES

Out of each month’s group of entrants who correctly find the error in the code or schematic, one person will be randomly selected to win a NetBurner IoT Cloud Kit and another person will receive a free 1-year digital subscription to Circuit Cellar.

  • NetBurner MOD54415 LC Development Kit: You can add Ethernet connectivity to an existing product or use it as your product’s core processor! The NetBurner Ethernet Core Module is a device containing everything needed for design engineers to add network control and to monitor a company’s communications assets. The module solves the problem of network-enabling devices with 10/100 Ethernet, including those requiring digital, analog, and serial control.NetburnerMod54415module
  • Circuit Cellar Digital Subscription (1 year): Each month, Circuit Cellar magazine reaches a diverse international readership of professional electrical engineers, EE/ECE academics, students, and electronics enthusiasts who work with embedded technologies on a regular basis.Circuit Cellar magazine covers a variety of essential topics, including embedded development, wireless communications, robotics, embedded programming, sensors & measurement, analog tech, and programmable logic.

RULES

Read the Rules, Terms & Conditions

SPONSOR

NetBurner solves the problem of network enabling devices, including those requiring digital, analog and serial control. NetBurner provides complete hardware and software solutions that help you network enable your devices.netburneroffer

NetBurner, Inc.
5405 Morehouse Dr.
San Diego, CA 92121 USA

Specs & Code Matter (EE Tip #136)

No matter how many engineering hours you’ve logged over the years, it’s always a good idea to keep in mind that properly focusing on specs and code can make or break a project. In 2013, Aubrey Kagan—an experienced engineer and long-time Circuit Cellar author—explained this quite well in CC25:

There was a set of BCD thumbwheel switches that I was reading into a micro. In order to reduce the number of input lines required, each 4 bits of a digit was multiplexed with the other digits and selection was made by a transistor enabling the common line of each switch in turn. This was standard industry practice. The problem was that in order to economize, I had used the spare transistors in a Darlington driver IC. Everything worked fine in the lab, but on very hot days the unit would fail in the field with very strange results.

Long story short, the saturation voltage on the Darlington transistor would increase with temperature to above the digital input threshold and the micro would read undefined switch settings and then jump to non-existing code. I learned three things: read and understand all the specifications on a datasheet, things get a lot hotter in a cabinet in the sun than the lab, and you should make sure your code can handle conditions that are not supposed to occur.—Aubrey Kagan, CC25 (2o13)

Want to share an EE tip of your own? Email our editors to share your tips and tricks.

Bit Banging

Shlomo Engelberg, an associate professor in the electronics department of the Jerusalem College of Technology, is well-versed in signal processing. As an instructor and the author of several books, including Digital Signal Processing: An Experimental Approach (Springer, 2008), he is a skilled guide to how to use the UART “protocol” to implement systems that transmit and receive data without a built-in peripheral.

Implementing serial communications using software rather than hardware is called bit-banging, the topic of his article in Circuit Cellar’s June issue.

“There is no better way to understand a protocol than to implement it yourself from scratch,” Engelberg says. “If you write code similar to what I describe in this article, you’ll have a good understanding of how signals are transmitted and received by a UART. Additionally, sometimes relatively powerful microprocessors do not have a built-in UART, and knowing how to implement one in software can save you from needing to add an external UART to your system. It can also reduce your parts count.”

In the excerpt below, he explains some UART fundamentals:

WHAT DOES “UART” MEAN?
UART stands for universal asynchronous receiver/transmitter. The last three words in the acronym are easy enough to understand. “Asynchronous” means that the transmitter and the receiver run on their own clocks. There is no need to run a wire between the transmitter and the receiver to enable them to “share” a clock (as required by certain other protocols). The receiver/transmitter part of the acronym means just what it says: the protocol tells you what signals you need to send from the transmitter and what signals you should expect to acquire at the receiver.

The first term of the acronym, “universal,” is a bit more puzzling. According to Wikipedia, the term “universal” refers to the fact that the data format and the speed of transmission are variable. My feeling has always been that the term “universal” is basically hype; someone probably figured a “universal asynchronous receiver/transmitter” would sell better than a simple “asynchronous receiver/transmitter.”

Figure 1: The waveform output by a microprocessor’s UART is shown. While “at rest,” the UART’s output is in the high state. The transmission begins with a start bit in which the UART’s output is low. The start bit is followed by eight data bits. Finally, there is a stop bit in which the UART’s output is high.

Figure 1: The waveform output by a microprocessor’s UART is shown. While “at rest,” the UART’s output is in the high state. The transmission begins with a start bit in which the UART’s output is low. The start bit is followed by eight data bits. Finally, there is a stop bit in which the UART’s output is high.

TEAMWORK NEEDED
Before you can use a UART to transfer information from device to device, the transmitter and receiver have to agree on a few things. First, they must agree on a transmission speed. They must agree that each transmitted bit will have a certain (fixed) duration, denoted TBIT. A 1/9,600-s duration is a typical choice, related to a commonly used crystal’s clock speed, but there are many other possibilities. Additionally, the transmitter and receiver have to agree about the number of data bits to be transmitted each time, the number of stop bits to be used, and the flow control (if any).

When I speak of the transmitter and receiver “agreeing” about these points, I mean that the people programming the transmitting and receiving systems must agree to use a certain data rate, for example. There is no “chicken and egg” problem here. You do not need to have an operational UART before you can use your UART; you only need a bit of teamwork.

UART TRANSMISSION
Using a UART is considered the simplest way of transmitting information. Figure 1 shows the form the transmissions must always make. The line along which the signal is transmitted is initially “high.” The transmissions begin with a single start bit during which the line is pulled low (as all UART transmissions must). They have eight data bits (neither more nor less) and a single stop bit (and not one and a half or two stop bits) during which the line is once again held high. (Flow control is not used throughout this article.)

Why must this protocol include start and stop bits? The transmitter and the receiver do not share a common clock, so how does the receiver know when a transmission has begun? It knows by realizing that the wire connecting them is held high while a transmission is not taking place, “watching” the wire connecting them, and waiting for the voltage level to transition from high to low, which it does by watching and waiting for a start bit. When the wire leaves its “rest state” and goes low, the receiver knows that a transmission has begun. The stop bit guarantees that the line returns to its “high” level at the end of each transmission.

Transmissions have a start and a stop bit, so the UART knows how to read the two words even if one transmits that data word 11111111 and follows it with 11111111. Because of the start and stop bits, when the UART is “looking at” a line on which a transmission is beginning, it sees an initial low level (the start bit), the high level repeated eight times, a ninth high level (the stop bit), and then the pattern repeats. The start bit’s presence enables the UART to determine what’s happening. If the data word being transmitted were 00000000 followed by 00000000, then the stop bit would save the day.

The type of UART connection I describe in this article only requires three wires. One wire is for transmission, one is for reception, and one connects the two systems’ grounds.

The receiver and transmitter both know that each bit in the transmission takes TBIT seconds. After seeing a voltage drop on the line, the receiver waits for TBIT/2 s and re-examines the line. If it is still low, the receiver assumes it is in the middle of the start bit. It waits TBIT seconds and resamples the line. The value it sees is then used to determine data bit 0’s value. The receiver then samples every TBIT seconds until it has sampled all the data bits and the stop bit.

Engelberg’s full article, which you can find in Circuit Cellar’s June issue, goes on to explain UART connections and how he implemented a simple transmitter and receiver. For the projects outlined in his article, he used the evaluation kit for Analog Devices’s ADuC841.

“The transmitter and the receiver are both fairly simple to write. I enjoyed writing them,” Engelberg says in wrapping up his article. “If you like playing with microprocessors and understanding the protocols with which they work, you will probably enjoy writing a transmitter and receiver too. If you do not have time to write the code yourself but you’d like to examine it, feel free to e-mail me at shlomoe@jct.ac.il. I’ll be happy to e-mail the code to you.”

Client Profile: ImageCraft Creations, Inc.

CorStarter prototyping board

CorStarter prototyping board

2625 Middlefield Road, #685,
Palo Alto, CA 94306

CONTACT: Richard Man,
richard@imagecraft.com
imagecraft.com

EMBEDDED PRODUCTS:ImageCraft Version 8 C compilers with an IDE for Atmel AVR and Cortex M devices are full-featured toolsets backed by strong support.

CorStarter-STM32 is a complete C hardware and software kit for STM32 Cortex-M3 devices. The $99 kit includes a JTAG pod for programming and debugging.

ImageCraft products offer excellent features and support within budget requisitions. ImageCraft compiler toolsets are used by professionals who demand excellent code quality, full features, and diligent support in a timely manner.

The small, fast compilers provide helpful informational messages and include an IDE with an application builder (Atmel AVR) and debugger (Cortex-M), whole-program code compression technology, and MISRA safety checks. ImageCraft offers two editions that cost $249 and $499.

The demo is fully functional for 45 days, so it is easy to test it yourself.

EXCLUSIVE OFFER: For a limited time, ImageCraft is offering Circuit Cellar readers $40 off the Standard and PRO versions of its Atmel AVR and Cortex-M compiler toolsets. To take advantage of this offer, please visit http://imagecraft.com/xyzzy.html.


 

Circuit Cellar prides itself on presenting readers with information about innovative companies, organizations, products, and services relating to embedded technologies. This space is where Circuit Cellar enables clients to present readers useful information, special deals, and more.