© James R. Strickland 2016

James R. Strickland, Junk Box Arduino, 10.1007/978-1-4842-1425-1_2

2. Cestino

James R. Strickland

(1)Highlands Ranch, Colorado, USA

Electronic supplementary material

The online version of this chapter (doi:10.​1007/​978-1-4842-1425-1_​2) contains supplementary material, which is available to authorized users.

Arduinos have a lot of useful stuff baked into them, so much so that they seem a little magical. I’m here to tell you that there is no magic. More than that, I’m going to prove it to you.

I’m a writer with a background in computer science. One thing I’ve found, as I’ve pursued technical topics over the years, is that the best way for me to understand a given technology is to dive right in and use it. Breaking up the magic and exposing how the given thing really works goes a long way, at least for me, toward understanding it, and being able to create new projects with it. So it was with Arduino. My first one was a kit. If I seem, occasionally, to be an Adafruit fanboy, it’s because that first Arduino kit came from them. It was the first piece of digital electronics I ever soldered that worked. I would not be writing this book without having built that kit, and the first time the LED flashed under program control on that kit was kind of like magic. More so because I did it myself.

In light of that experience, I thought building an Arduino-derived board on the breadboard would be a great first project. Flashing the LED on an Arduino you bought is fun. I do it on all of them as a basic health check. Flashing the LED on one you built? That’s more fun.

There is another benefit. Most Arduino topics deal with digital and analog pins , digital_read and digital_write . Most of the electronics in your junk box probably communicate in eight-bit parallel, and to really talk to them efficiently, we need to have the Arduino speak eight-bit parallel as well. It can do that. Most ATmega microcontrollers have eight-bit ports, but there’s a catch. While the Arduino core software can re-map the pins of nearly any AVR microcontroller to the standard pin layout, the ports themselves are hardware devices. They are where Atmel says they are. If you need more ports than the ATmega328p (used in the Arduino Uno r3 and many, many others) can provide, your ports may literally be anywhere, and clone boards may wire them differently still. If you ever wondered why most Arduino topics pretty much ignore eight-bit ports, this is why. With Cestino, we can bypass that problem entirely. You’ll have exactly the same hardware on your breadboard that I do, and your ports will be wired the same way.

There’s yet another benefit. We’re going to use the Cestino’s hardware a lot more deeply than normal Arduino programming. Since I had to develop a configuration for both the bootloader and the Arduino core software (I’ll talk about both of these in chapter 3), I changed the pin layout to correspond exactly to the pinout of the ATmega1284P microcontroller. Pin 1 on the chip is digital pin 1. The analog pins are backwards, just as they are on the pinout diagram from Atmel, starting at 40 for analog 0 and going to analog 7 on pin 33.

There are some downsides. The usual Pin 13 blink sketch won’t work without modification. The Cestino has no digital pin 13. Pin 13 is where the clock signal goes in. And so on. Since we’re developing our own sketches, it won’t get in our way too much, and it’s not difficult to convert normal Arduino sketches to use the Cestino’s pin layout.

The Stuff You Need

The Cestino is not much more than the ATmega microcontroller , an oscillator, some passive components and some LEDs. The parts list is fairly short.

New Parts

  • 1 Atmel ATmega1284P sometimes called the ATmega1284P-PU microcontroller, in PDIP 40 (40 pin through-hole.) See the warning in Chapter 1 about ATmega1284P-PU vs ATmega1284-PU. You want the P-PU version.

  • 4 0.1uF capacitors, any ceramic or tantalum that will fit on the breadboard. See A Word on Capacitors.

  • 1 10kΩ resistor.

  • 1 330Ω resistor.

  • Hookup Wire.

  • USB to 5v TTL level Serial Cable/board/whatever.

New or Used Parts

  • 1 20 MHz Full Can TTL crystal oscillator.

  • 1 Tactile button, momentary-on (only on while pressed)

  • 1 existing Arduino.

  • 2 Solderless Breadboards: 6.5 inches by 2.1 inches (165.1mm x 54.36mm) with 820 tiepoints (holes.)

  • The ATmega1284P Datasheet. It’s available at http://www.atmel.com/images/doc8059.pdf

Anatomy of the Cestino

The Cestino, shown in schematic form in Figure 2-1, is an Arduino derivative, stripped down to the bare minimum components it needs to run.

A340964_1_En_2_Fig1_HTML.jpg
Figure 2-1. Cestino Schematic

Unlike a modern Arduino, say, the Uno r3, Cestino does not have its own voltage regulation circuit for external power. It does not have a USB-Serial solution built in. It does not share the standard Arduino pin arrangement, either. What it has is an Atmel ATmega 1284P microcontroller, a 20Mhz TTL crystal oscillator to generate the clock signal, a reset button and a resistor to generate the reset signal, and a pin header to connect an external USB to TTL level RS-232 cable or board, wired to the same standard used by most Arduinos, which also provides power to the Cestino.

There are only two complex components in the Cestino: the ATmega1284P itself (Figure 2-2) and the crystal oscillator, and I’ll talk about them next .

A340964_1_En_2_Fig2_HTML.jpg
Figure 2-2. ATmega1284P Pinout Diagram

The ATmega1284P Microcontroller

Meet the Atmel ATmega1284P AVR microcontroller in the PU, or plastic, 40 pin dual inline pin package, hereafter referred to as the ATmega or ATmega1284P. According to the datasheet, it is an eight bit AVR RISC microcontroller, has 128K Bytes of flash, 4K Bytes of EEPROM, 16K Bytes of internal SRAM, that can run at speeds up to 20Mhz, producing about 20MIPS of performance.

That’s great. What does it mean? Let’s break it down.

Microcontroller

A microcontroller is like a CPU, but there are important differences. Most microcontrollers, the AVRs included, use a modified Harvard architecture. This means that the ATmega has two different memory systems, one for instructions, and one for data. The two meet only under very controlled circumstances, and the ATmega can access them simultaneously. You can see this very clearly in the specs, where the ATmega has 128K Bytes of flash for program memory. This is not a flash drive. The ATmega does not load the program from flash into RAM and then execute it. It reads its instructions (the program) straight from the flash memory and executes them.

The 16K Bytes of internal SRAM (Static RAM), by contrast, are for data only. No instructions can be executed from SRAM. Data and Instructions remain separate. The AVR is a modified Harvard architecture, which allows data to be copied from flash into RAM, but there is no way to write the data back to flash, and data in SRAM will go away when the power is turned off. What if you have configuration data you need to store?

That, in fact, is what the 4K Bytes of EEPROM (Electronically Erasable Programmable Read Only Memory) are for: a place to stash data, like software configuration, that persists even if you turn the power on and off. You can’t store instructions there either. They won’t be executed. The EEPROM, like the SRAM, is for data only.

The thing to remember most about the Harvard architecture is that the program can’t modify itself or the flash it is stored in. (Well, it can, but it’s complicated to get at.) Want to create a flash data drive out of empty space in the internal flash? You can’t. Want to have one program load another into flash? You can’t. A lot of the capabilities (and vulnerabilities) we take for granted with our desktop computers, our phones, tablets, and so on, are not possible on Harvard Architecture designs. Hardware configuration is likewise out of reach. It is set with a series of “fuses” which we’ll cover in chapter 3.

Note

If you’re curious, the other architecture, the one that your desktop computers, phones, tablets, and so on, use is called the Von-Neumann or Princeton architecture . This is the familiar “All the memory is one big bucket, and keeping memory separate from program instructions is a software job” setup you’re familiar with.

Microcontrollers also typically have a lot of peripherals built in. RS-232, I2C, and SPI serial communication, timer/counters, and so on. What is a peripheral and what is on-chip is a fuzzy line in modern times, since a system-on-a-chip (SOIC) such as the one that drives the Raspberry Pi, is a Von-Neumann architecture CPU with almost all the functionality of the system rolled up into one piece of silicon. In days gone by it was an important distinction. When we get to Chapter 11, we’ll be using the ATmega’s peripherals to communicate with a vintage microprocessor, born with no peripherals at all .

RISC

RISC is one of those terms that used to mean something very specific, but has become fuzzy over the years. It’s an acronym that stands for Reduced Instruction Set Computing. It was introduced in the 1980s. To understand RISC, you need a little background.

The Clock

We tend to think of the CPU or microcontrollers we deal with in automotive terms, as the engine of the computer. It’s really not quite like that. A CPU is a series of tasks that are useful for computing, implemented in electronics. It’s more like an electronic program itself than an engine. Each task, or instruction, takes a certain amount of time to accomplish. That time is determined by the physics of the transistors, resistances, and capacitances of the circuits that carry it out. Each one uses resources inside or outside the CPU, and most need exclusive access to those resources. If one instruction is copying a byte from memory into a register, there’s no way to know what the result would be if another instruction’s mechanism simultaneously erased that byte. That would be bad. On the hardware level, everything in a computer must be predictable at all times.

The clock breaks up the time available into discrete chunks, so that instructions don’t mess each other up. If copying a byte from memory into a register in the CPU takes less than 50 nanoseconds(ns) then, on our 20 MHz ATmega, it can be done in one clock cycle, and one clock cycle will be allocated to doing that job. When that clock cycle is done, another instruction can be carried out. More complex instructions often take more than one clock cycle to be carried out, and so more clock cycles are allocated.

Reduced Instruction Set

In the old days, RAM was astonishingly expensive. Every byte had value, so you wanted your programs to use as little as possible. Processor and microprocessor designers went to great lengths to give you a list of instructions that did complex operations in a single assembly instruction. Some of these could take lots of clock cycles to execute, and they took a lot of the available transistors on a given IC to implement, so niceties like cache and so forth weren’t an option.

By the 1980s, it was clear that the price of RAM was plummeting, the amount of RAM a given microprocessor might have available was skyrocketing, and programmers were using compilers (like AVR GCC, which we’ll use to program the Cestino) to generate code instead of using assembly language. Since compilers seldom used the entire breadth of a complex instruction set, the RISC pioneers decided that they could reduce the instruction set to the bare minimum required. This allowed them to optimize each instruction’s underlying circuitry. RISC was fast, for most applications. Most RISC instructions could be executed in one clock cycle. Contrast this, when you get to Chapter 11, with the fact that the Z80, a CISC (Complex Instruction Computing) microprocessor we’ll be using, can do nothing in less than three clock cycles. Most operations take many more .

The trade off, at least traditionally, is that some operations take a lot more instructions to get done. More instructions, more clock cycles, more memory.

Today, the difference is fuzzy indeed, as the instructions a given CPU provides may be in microcode, which is more or less software executed by even faster electronics. Transistor counts are in the billions and climbing, and caching and pipelining allow even a “complex” instruction set (CISC) CPU to execute an instruction every clock cycle under ideal conditions.

When applied to the Atmel AVR line, of which our ATmega1284 is a member, RISC mostly means a small instruction set, capable of executing each instruction in one clock cycle. Because AVR is also a Harvard architecture, it can read an instruction and execute the previous one at the same time. When they say the ATmega1284P can produce 20 MIPS (million instructions per second) with a 20 MHz clock (20 million hertz, or cycles per second) this is what they’re talking about, and how they got there.

Eight Bits

The bit-width of a computer is another of those terms that gets bandied about without much understanding. Most people talk about it in terms of how much RAM a system can have. That’s not really the important part of the story.

Eight-bit computers like the ATmega1284P can act upon 8 bits of memory simultaneously. That’s it. That’s all that’s required. Typically, they also have 8 bit instruction sets, that is, most instructions will have a code from 0 to 255 that designates them in memory. Eight bit computers also process large numbers (such as 32 or 64 bit floating point numbers) more slowly because they must deal with them in 8 bit chunks, requiring more instructions per computation.

Bit width can be related to memory size, but it isn’t always. Even the ancient Z80 we’ll use in Chapter 11 has two bytes of memory address space (64K Bytes), and 16 bit registers inside for memory addresses internally. The fact that certain 16 and 32 bit processors limited their memory registers to the bit-width of the CPU was a design decision. It probably conserved transistors or improved performance (or both). It didn’t have to be that way. One need look no further than the ATmega1284, which addresses 128K of flash, to see that it needn’t be so.

Package

The ATmega1284P-PU, the one you have, looks like a flat, 40 legged bug with shiny metal legs. That is not actually the chip. That’s the package. It’s called a DIP (Dual Inline Package ), a DIL (Dual InLine), or a DIPP (Dual Inline Pin Package) , a PDIP (Plastic Dual Inline Package – a CDIP would be ceramic) or more precisely as a DIP40 (A Dual Inline Package with 40 pins). According to Wikipedia, DIPs were invented in 1964. The actual chip, or die as it’s more properly known, is probably about the size of the word “DIP” on this page, depending on font size.

The largest number of pins commonly used in the DIP format was 64, with 40 pin DIPs being much more common. Most new integrated circuits need more leads than that, including larger and more powerful ATmega microprocessors. Why did I choose the ATmega1284P? It was the largest pin-count ATmega in a DIP package that is breadboard friendly.

20 MHz TTL Full Can Oscillator

The 20Mhz TTL full can oscillator , also known as a clock oscillator , a crystal oscillator, or simply an oscillator, depending on who writes the datasheets, is a combination of a quartz crystal resonator and the drive electronics to produce a square wave, 50 percent duty cycle clock signal at 20Mhz, +- 100PPM.

What does that mean? Simply this: When you connect the oscillator to power and ground, every 0.05 microseconds (μsec), it will generate one full off pulse and one full on pulse, that the on and off pulses will be of equal length (50% duty cycle), and that as a clock, it’s accurate to 0.01 percent. In human readable numbers, it will gain or lose a little more than a second every hour. That’s close enough. The on or high pulses will be at least 4.5 volts, and the low, or off pulses will be at maximum of 0.5 volts. Running on 5 volts, as we’ll be configuring it, the ATmega’s clock input pin will take low voltages up to 0.5 volt, and high voltages as low as 3.5 volts, so those values are fine.

Placing Components

So let’s get started. If you haven’t already snapped your two breadboards together into one wide breadboard, now’s a good time. Things get a little fragile once the little components and wires are in place. My breadboards have double-face tape on the back, so I stuck them to a piece of cardboard for stability.

Take your ATmega1284P and look closely at it. You’ll note a notch in one end. This is one of several standard notations for where pin 1 is. With that notch up, pin 1 is on the top left, two is below it, and so on to pin 20. Pin 21 is on the bottom right, continuing to pin 40 at the top right. In that orientation find tiepoint row 11 on your left breadboard (refer to Figure 2-3). and gently press the ATmega into the tiepoints with its pin 1 in row 11. The ATmega is wide. It will reach two columns beyond the channel on the other. I recommend placing it so that you have three columns of open tiepoints on the left and two on the right, to make room for the Cestino’s internal wiring (which is almost all on the left) and still have some tiepoints open on that side for jumpers later.

A340964_1_En_2_Fig3_HTML.jpg
Figure 2-3. Breadboard Layout
Note

you don’t have to follow my layout for your Cestino. That’s the beauty of breadboards. You can put components in where you want. The really important thing is that the connections are the same and that the high frequency line from the oscillator to the ATmega is as short as possible. Weird signal distortions from too-long high frequency wires are not something we want on the Cestino’s clock.

Skip a row of tiepoints so you can get a screwdriver under the ATmega later if you need to remove it.

Now take the 20 MHz TTL full can oscillator, hereafter referred to as the oscillator, and look at it closely. Three of its corners are rounded. One is not. The sharp corner is where pin 1 is. There may also be a dot painted on the shell on that corner as well. Put pin 1 of the oscillator into row 33 or so of your breadboard, and again, I’d put it so that it lines up with the ATmega on the left, but this is mostly for aesthetics.

Skip a couple rows of tiepoints to leave room for wires, and the all-important screwdriver access. Then install the momentary tactile button below the oscillator so that two contacts that will connect when the button is pushed are to the left of the valley in the center of the breadboard, as shown in Figure 2-3.

My tactile buttons are weirdos. Instead of having connections from pins 1 to 4 and 2 to 3 as you’d expect, they connect pin 1 to 2 and 3 to 4 when closed. You need to read the datasheet on yours to find out which pins need to go where, or you can figure it out with your multimeter in the Ohms setting. Your reset circuit may wind up looking different from mine .

If you have the extra-long pin headers, as from Adafruit and others, you want six pins worth, and stick them in column E, the one closest to the channel on the left, starting at tiepoint row 1 on the breadboard. This leaves just enough room for the reset circuitry the LED and its resistor. We’ll get to those right after we get done wiring the power circuits. You might want to bend these pins over so the TTL RS-232 cable will rest in the empty space on your breadboard. If so, don’t do it in the breadboard. The little metal connectors in the breadboard weren’t designed to take much stress. I suggest two pairs of pliers, and be sure to hold all the pins on both sides of the plastic connector, or you’ll snap the pins out of the plastic connector.

You could also leave them straight. I bent mine, as shown in Figure 2-3.

Power Circuits

There are no unimportant circuits in the Cestino, except maybe the LED, but if the power circuits don’t work right, nothing else will either.

If you look along the edges of your breadboards, you’ll (probably) note that there are two columns of tiepoints, one with a red line next to it, and a + marking and one with a black or blue line and a - marking. There is probably a duplicate pair on the opposite edge. If yours don’t have the lines, that’s ok. If you have older breadboards with only one column of connectors, wiring the power circuits will be a little more complicated for you, but your breadboards will still work.

These columns of tiepoints are called busses. All the tiepoints in each column are connected together. The busses on each edge of each breadboard are not connected to each other, and obviously the busses of the left breadboard aren’t connected to the busses of the right breadboard. That’s the first job we’re going to do. It’s not a big deal (refer to Figure 2-4). At the bottom of your first breadboard, run a piece of hookup wire from the + bus on one side to the + bus on the other side of the same board. Convention is to use red wire for the + bus. Run another from the + bus on the right of your first breadboard to the + bus on the left of your second. Run a third from the + bus on the left of your second breadboard to the + bus on the right. All the + busses should be tied together.

A340964_1_En_2_Fig4_HTML.jpg
Figure 2-4. The Power Circuits

Do the same thing with the - busses. Go from the left edge of the first breadboard to the right edge, to the left edge of the second, to the right edge. All the - busses should be tied together, too. Convention is to use black wire for the – bus.

These connections will make wiring the Cestino easier, and as we do projects, they will make powering the project ICs and components much easier as well. I try to leave four or five millimeters of bare wire to reach down fully into the tiepoint. If the wire’s not quite long enough, it can result in a flakey connection, and those can be a real pain to track down.

Note

if you’re using other types of breadboards, the busses may also be divided on the vertical center, with one set on the top of the breadboard and one on the bottom. If you don’t have the manual, grab a couple jumpers, set your multimeter for ohms, and see if you get continuity from the top of the bus to the bottom. If you don’t, add a jumper or a piece of wire to bridge the spot where they divide.

Although we’re on the subject of power (not to mention messing with the red and black hookup wire), let’s go ahead and connect up power to all the components we’ve put in place.

Start with the ATmega. Looking at the pinout diagram on page 2 of the ATmega1284P datasheet, we can see that pin 10 is Vcc and pin 11 is GND, or ground. Wire the GND pin to the nearest - bus, and Vcc to the nearest + bus. These pins provide 5 volt power to the ATmega. If you use the tiepoints closest to the busses, it will leave room for the capacitor you will install later .

Note

Vcc, Vdd, and so forth are, for our purposes, all the same. They indicate the peak voltage applied to the pin, and that it is the positive voltage. The subscript—cc, dd, and so on—actually call out various parts of different transistor technologies. The first c in this case is for the collector (we’ll cover transistors at length in Chapter 5), and the second c means it’s a supply voltage. This comes from a standard, IEEE 255, but is often misrepresented in digital schematic software.

On the other side of the ATmega, on pin 30 is the AVcc pin, and on 31 is another GND pin. (Remember, count from pin 21 up the right-hand side.) These supply power to one of the ports, and to the analog to digital converter. Wire pin 30 to the nearest + bus and pin 31 to the nearest - bus. We’ll talk about ports in Chapter 4, and the analog to digital converter in Chapter 5.

Note

that pin 30 is AVcc and 31 is GND. They are internally connected to Vcc and GND, and they are backward from the Vcc and GND terminals on the other side. I mixed them up once, and got an ATmega hot enough to burn my finger on contact. I haven’t tested that ATmega to see if it’s damaged or not. I won’t be surprised if it is.

Next, we’ll wire up the oscillator. The diagram in the data sheet for the ATmega doesn’t really show you the pins for the oscillator, but you’ll find that this is a standard part. The datasheet for the oscillator itself should have the pin assigments listed. It’s only got four pins, and it’s oriented just like it is in Figure 2-1.

I’ve represented the oscillator in the schematic as though it has 14 pins, as it fits in a 14 pin socket, but of these, only four pins actually exist, and three are used. Starting from the pin 1 marker on the top left, where the sharp corner and possibly a black dot are, pin 1 is not connected to anything. Pins 2, 3, 4, 5, and 6 don’t exist. Pin 7 is ground. Pin 8 on the other side at the bottom is the output pin that we’ll use later, and pin 14 on the top right is the power pin, called Vcc. Wire pin 7 to a - bus, and pin 14 to a + bus.

Next is the TTL Level RS-232 connector. It’s the pin header at the top of your breadboard, and it’s labelled CONN1 on the schematic in Figure 2-1.

Note

the pinout shown in Figure 2-1 and described below are how the FTDI USB to 5 volt serial cable is wired. This is a de facto Arduino standard but check your datasheet. If your USB to serial cable is wired differently, you will need to wire this connector differently.

Connect Vcc out to the nearest + bus and GND to the nearest - bus. These connections will power the Cestino from the USB to TTL Level Serial cable/board/whatever.

Finally wire one side of your tactile button to a - bus. It doesn’t make any difference which side, but it makes things easier to wire if the ground connection is furthest from the ATmega.

Done? Almost. There’s one more thing.

If you look at the schematic, you’ll see a number of capacitors (c1 through c3) that go from a Vcc pin to ground. That little symbol with three lines of decreasing length? That’s ground. These are 0.1 μF (microfarad) decoupling capacitors .

As I mentioned in Chapter 1, capacitors only conduct alternating, or varying voltages. When the voltage on both sides hits equilibrium, they stop conducting. We’re using that feature here.

The world is a noisy place, electrically speaking. There are as many ways for electronic noise to get into your breadboard as there are tiepoints on the breadboard. Our ICs—the ATmega and the oscillator—expect DC. Noise will produce unpredictable results, which are the very last thing you want in an electronics project.

Noise, by definition, is fluctuating voltage. A capacitor will conduct it. These decoupling capacitors, then, take any variable voltage signal on the positive terminal of the IC and short-circuit it to ground. DC, on the other hand, does not fluctuate, so the capacitors won’t conduct it at all. That’s exactly what we want. The DC arrives at the ATmega and the oscillator with its AC or variable voltage noise damped down.

Go ahead and put those capacitors in now, as they’re part of the power system. Put one from pin 10 to pin 11 of the ATmega, one from pin 30 to pin 31 of the ATmega, and one from pin 14 to pin 13 of the oscillator.

But wait, you say. There is no pin 13 on the oscillator.

You’re right. We’re using that row of tiepoints as a convenient place to connect the capacitor. Wire a piece of hookup wire from pin13 to the nearest - bus. Now the capacitor forms a circuit from power to ground, but only for varying voltage, or AC noise .

Figure 2-4 is photo of my board. You can see the power wires tying the busses together across the bottom, and the various power and ground connections from the oscillator, the ATmega, and the TTL RS-232 connector.

A Dab of Ohm’s Law

I’ve thrown around the word voltage a lot, made mention of current in a number of places, and said that 1/4 watt resistors are fine for all of our projects. Before we go any further, I need to be clear on what those words actually mean, and how they’re interrelated. It’s time for a dab of Ohm’s law .

Ohm’s law states that the current (amps) between two points on a conductor is proportional to the voltage difference between those two points. That proportion is the resistance of the conductor.

How It Works

Rather than go to the usual plumbing analogy, I’m going to try to explain the physics. Metals like copper are made of a lattice of atoms that have a particularly useful property: each one has an electron that can be easily pulled out of its normal orbital and move along the lattice. The lattice is also quite dense, so there are a lot of electrons available, and they’re close together. When you raise the voltage of one end of a conductor relative to the other (voltages are always relative) you nudge the electrons toward one end of the lattice. This is called electron drift.

The electrons don’t move much, and they don’t move fast, but because they are so close together, they don’t have to. When the electron is pulled out of the outermost orbital of a copper atom, for example, the same force (voltage) has nudged another electron close enough to be captured by that orbital. An individual electron drifts slowly, but the cascade of electrons from one atom to the next happens at nearly the speed of light.

The size of the conductor matters. The greater number of atoms in the cross section, the larger the electron “cloud”, and the more electrons can move at a given time. This is current. A thicker conductor can pass more current than a thinner one. Here’s why.

The path of an electron moving from atom to atom in the lattice isn’t smooth. If they run into an atom, they get bounced in a random direction, losing their energy in the process, usually as heat. The ratio between the current flow through a given conductor and the voltage differential between points on the conductor, is an amalgamation of the features of the conductor that prevent the passage of charges, and this is called resistance.

So Ohm’s law, despite being published in 1827, before most of the physics concepts that underlie it were discovered, describes the motion of the electron cascade through a conductor, and the physical properties of the conductor that get in the way of those charges passing through in one neat little formula: I=V/R. That is, Current (in Amps) equals Voltage between the two points on the conductor, divided by Resistance in ohms .

Examples

If I connect a 330Ω resistor between a 5 volt power supply and ground what happens? Well, that resistor has a voltage difference between its terminals of 5 volts. Divide that by 330 ohms, and we’re pulling a current of about 0.015 amps, or 15 milliamperes (mA). If, like the Cestino, you’re powering the resistor with USB, you should know that your USB port is capable of delivering at least 100mA. By itself, this load won’t even make USB break a sweat.

Consider this: the LEDs I used for my Cestinos want 2.4 volts and no more than 25mA of current through them. (I like big, bright LEDs.) The voltage coming out of the ATmega’s pins is five or zero, depending on whether they’re turned on or not. How can I protect the LED from getting voltages that are too high and currents that are too high? If I connect the LED directly, it will burn out in fairly short order.

I can use what’s called a dropping resistor. Remember how I said that resistance is a measure of how small the electron cloud to transfer the charges and how much the atoms of the conductor’s lattice get in the way, expressed as the ratio between the voltage and the current that flows through? Well, a resistor is a conductor that isn’t as good as copper, and we can exploit that to limit how much current and how much charge is propagated to our LED. Resistors, as the name suggest, add resistance to the circuit. But how big a resistor (or to put it another way, how much resistance) do I need?

We can use Ohm’s law to figure it out.

First, do a little subtraction. I have five volts, I’m dropping 2.4 volts in the LED, that leaves 2.6 volts that I need to drop.

Next, do a little algebra on Ohm’s law. In algebra, if I have an equation X = Y, that means whatever X is equals whatever Y is. If I divide both sides of the equation by Z, X/Z still equals Y/Z. As long as I change both sides the same way, their relationship doesn’t change. The same is true for multiplication, addition, and subtraction. So if I=V/R and I multiply both sides of the equation by R, multiplying V/R by R negates the division by R, leaving V, but I have to put the multiplication by R on the other side, leaving me IR=V. Dividing both sides by I, negates the multiplication of R by I in RI, leaving R, but I have to divide V by I as well. I wind up with R=V/I.

Let’s do that again in the unit names we’re more familiar with. If Amps=Volts/Ohms, and I multiply both sides by Ohms, I get Amps * Ohms=Volts. If I then divide both sides of the equation by Amps, I get Ohms=Volts/Amps .

I know volts, that’s 2.6. I know amps, that’s 0.025, or 25mA. Plugging those figures in, I get 2.6/0.025 or 104 ohms.

There’s another wrinkle. Each pin on the ATmega 1284P can source (give out) no more than 40mA, and I might want to use that pin for something else at the same time. Plus, that’s a mighty bright LED. I can spare some brightness to cut my power consumption.

The resistor I chose for the prototype was a 220 ohm. Why 220? I was going for a 330Ω, the de facto standard for LEDs in a 5 volt world, and misread the resistor. The prototype works, and I know that with 220Ω I’m not overdriving the LED, but how far am I loading down that pin? Let’s find out.

If Amps=Volts/Ohms, volts are 2.6 and Ohms are 220, I’m giving the LED 0.0118 amps, or 11.8mA: a little more than a quarter of the current available from that pin, and well within the limitations of the LED.

LED Circuit

Given these examples, it’s probably no surprise that the next step in building the Cestino is to build the LED circuit . Go ahead and size the dropping resistor for your particular LED. If your LED is out of the junk box, and you don’t have a datasheet on it, a 330Ω resistor is probably close enough. Bend the leads sharply down from the body of the resistor and connect it from pin 1 of the ATmega to tiepoint row 8 of your breadboard, assuming your layout is like mine.

LEDs, or light emitting diodes are, as the name suggests, diodes. This means that, like all diodes, charges can only flow one way through them. If your LED is new, it will have different length leads coming out of it. The short one is the negative, or cathode lead, or and the long one is the positive, or anode. If your LED came out of the junk box, you’ll need your multimeter to sort out the polarity, or you can just hook it up and test it with a jumper once the board is powered. If it doesn’t light, turn it around .

To find the polarity of the LED with your multimeter, set your meter on Ohms on the lowest setting (or Diode Testing, if it has that) and connect the LED to the leads. If you get no reading and nothing happens, reverse the polarity. On my cheap meter, I still get no reading, but the LED lights up. On my good meter, the diode test voltage drops from 3.1 to 1.75 volts, reflecting the forward drop of the LED. There’s no reading in ohms, but once again, the LED lights up when the polarity is right for both the diode test setting and the Ohms setting. The red lead on most meters is positive, making that lead of your LED the anode, and the black is negative, making it the cathode.

Note

Wikipedia has a good mnemonic for keeping anodes and cathodes straight: ACID. Anode Current Into Device. In vacuum tube (aka valve) it’s easy to remember that the cathode is negative, because the plate or anode is connected to a high voltage positive circuit called B+. Electrons drift from cathode to plate, but the positive charges (lack of negative charges, really) go from plate to cathode.

I like to keep my component leads as short as possible. It keeps them from touching other components and causing shorts, and it looks better in photographs (Figure 2-5). As with hookup wire, four or five millimeters of wire to go down into the tiepoint gives a good, secure connection.

A340964_1_En_2_Fig5_HTML.jpg
Figure 2-5. The LED Circuit

LEDs aren’t quite as cheap as resistors and capacitors, though. If you want to use your LED for something else when you’re done with your Cestino, you can leave the leads long. In either case, connect the LED from row 8, or wherever the free end of your dropping resistor wound up, to the - (ground) bus. If you decide to go with short leads, bend them out horizontally from the base of the LED.

Note

The leads of an LED are very, very fragile where they come out of the LED package. If you bend them more than once, they’re likely to snap off flush with the package, which makes the LED useless.

If you chose an LED with more than two leads, you have a multicolor LED, and you’ll need to refer to your datasheet or do some probing with your multimeter to find out which leads do what. The LED circuit only needs one of the colors, so you can use the other, with another dropping resistor, for a power-on indicator or something else. If your multicolor LED has a common anode and two or more cathodes, you can use it either by using only one color or by using multiple colors wired to pin 1 (check your dropping resistor values, and be sure you don’t draw more than 40mA from the ATmega pin) .

Clock Circuit

The clock circuit is the simplest of all. It’s a piece of hookup wire, shown in Figure 2-6. Some care should be taken, though. Wire is not a perfect conductor, and we want the signal from the oscillator to arrive at the ATmega as pristine as possible. Even if your other hookup wires are luxuriant and gracefully braided, you really want to keep this wire short and neat. Run it from pin 8 of the oscillator to pin 13, XTAL 1, of the ATmega. We’ll leave XTAL 2 unconnected.

A340964_1_En_2_Fig6_HTML.jpg
Figure 2-6. The Clock Circuit

Reset Circuit

On the ATmega pinout diagram (Figure 2-2), we can see the reset pin is pin 9, and that there’s a line over the top of it. That line tells us something very important about that pin: it is active low. This means that as long as that pin has +5 volts on it, a reset doesn’t happen. On page 327 of the datasheet, Atmel tells us that the voltage threshold for a reset is somewhere between 0.2 * Vcc and 0.9 * Vcc. Because our Vcc is 5 volts, that means that as the reset pin’s voltage drops, somewhere between 4.5 volts and 1 volt, the ATmega will reset.

That pin has no voltage supplied to it by the ATmega itself. We have to make sure that pin stays at 5 volts or, more precisely, at more than 0.9*Vcc. So how can we hold the pin high until we want it to go low? It’s easy to picture a relay or a transistor doing the job, or a light switch, even, but there’s a much easier way.

The pin draws 10μA (microamperes), so it doesn’t take much current to hold it at 5 volts. If we wanted to limit it to that current, we already know how to calculate a resistance to do the job with Ohm’s law. If R=V/I (Ohms=Volts/Amps), and V is 0.5 (5 volts minus 4.5 volts, which is 0.9 * Vcc) and I is 0.000010 Amps (10μA), that puts our resistance at 50kΩ (50,000 ohms). Any value greater than that, and the current draw of the reset pin will pull the voltage down to where the ATmega might reset. We don’t want the ATmega to reset until we tell it to, so any resistance between the + bus and the ATmega reset pin should be smaller than 50kΩ. If you’re scratching your head, wondering why you’d want a resistor in the reset circuit at all, read on .

When the reset button is pushed, we want to pull the voltage on the reset line below the minimum reset threshold voltage of 1 volt, to be sure that, no matter what, the ATmega resets. There’s an easy way to do that. Short the reset pin to ground. If you look at the schematic in Figure 2-1, that’s exactly what we’re doing. The resistor, while it needs to be a lower value than 50kΩ to keep the voltage high when the button is up, also needs to provide enough resistance to limit the current flowing between Vcc and Ground so that the voltage on the + bus doesn’t drop too much. The ATmega won’t function properly at 20 MHz if Vcc drops below 4.5 volts. The oscillator is even touchier.

By now, you’ve probably recalled from the parts list that I used a 10kΩ (ten-thousand ohm) resistor for the reset pullup resistor. With a 10μA load from five volts, this gives us a drop of 0.1 volts, which leaves the voltage at 4.9 volts on the reset pin. That’s plenty to keep the ATmega from resetting. When we short the reset pin to ground, we change what the resistor is doing. It’s now dropping 5 volts from one side, on the + bus, to the other, on the - bus. Using Ohm’s law again, we discover that 5 volts/10,000 ohms is 0.5mA – half a milliampere, or about 1/1000th of what USB is delivering to the board. We can afford that. It’s not going to draw Vcc so low that the ATmega stops working at all.

There’s another consideration. Our resistor is turning that current into heat. There are strict limits to how much heat a resistor can deal with before it goes up in smoke. You may recall from Chapter 1 that resistors are rated in watts, and that I recommended using 1/4 watt resistors for all the projects in this book. Am I right? Is that safe? How many watts are we dissipating? Let’s find out.

A watt of heat is equal to volts multiplied by amps. To plug in the symbols from Ohm’s law, W=VI. We know amps: 0.0005 (0.5mA) and volts: 5. 5 times 0.0005=0.0025 watts. Our resistor can dissipate 1/4 watt, or 0.25 watts, so we’re nowhere close to its limit.

Okay. So we want a 10kΩ resistor connected to the + bus to pull the voltage on the reset pin high enough to prevent a reset, but keep the current low enough when we short the reset pin to ground that we don’t smoke the resistor or pull Vcc down so far that the ATmega stops working properly. We know the 10kΩ resistor can do all that. It’s called a pull-up resistor, because it pulls the reset pin up to 5 volts .

We know from the pinout diagram that the reset pin is pin 9 on the ATmega. It’s the one right before the Vcc connection. We already wired the tactile switch to ground when we did the rest of the power circuits, so we have all of the pieces we need. Let’s wire the reset switch.

Plug the 10kΩ pullup resistor into the + bus, and the other side into the tiepoint row containing pin 9 of the ATmega. Then run a piece of hookup wire from pin 9 of the ATmega to the side of your tactile switch that isn’t wired to the - bus. The full circuit is shown in Figure 2-7.

A340964_1_En_2_Fig7_HTML.jpg
Figure 2-7. The Reset Circuit

TTL RS-232 Circuits

The last thing we need to connect is the TTL level RS-232 circuit. I talked about RS-232 and why Arduinos and the Cestino use it in Chapter 1, but because we’re about to wire it up, we should take a moment to describe what it actually is. RS-232 is an ancient serial protocol, originally issued in 1969, to govern communications between teletypewriters. It transmits data single-file, one bit at a time, and to do this, it needs four wires: one for transmit (TX), one for receive (RX), and one for each end to signal that it’s ready to communicate, RTS (Request to Send)from the transmitting side and CTS (Clear to Send) from the receiving side.

RTS-Reset Circuit

Arduinos and host computers are fast enough that they don’t need the RTS/CTS mechanism to keep one end of the communication from overwhelming the other. Arduinos do, however, need to be reset so that the bootloader (covered in Chapter 3) will run and listen for serial communications for a few seconds before handing off control to the user sketch. Conveniently, RTS is active low, just like the ATmega’s reset pin, so the Arduino designers repurposed the RTS line from the host computer (or, really, from the USB to TTL level RS-232 cable/board/whatever) to reset the ATmega prior to programming it. The RTS line is the first one in the connector, so we’ll hook it up first.

There’s a wrinkle. We’re holding the reset pin at 5 volts with the pullup resistor. We don’t know what resistance is present from the RS-232 RTS line to ground, so it’d be a good idea to keep the DC (steady) pullup voltage out of RS-232 and whatever steady RS-232 voltages are present out of the reset circuitry unless they’re changing, which means the host computer has reset the Cestino. You may recall from earlier in this chapter and Chapter 1 that capacitors conduct only changing or alternating voltages. Just as we used them to short any pulsed or alternating voltages from our power supply lines to ground, we can use them to pass only pulsed currents from the RTS line into the reset circuitry. We’ll use the remaining 0.1μF capacitor for that .

The capacitor needs to reach from tiepoint row 1 all the way to the other side of the connector (CONN1 on the Schematic in Figure 2-1) to one of the free rows of tiepoints there. I used row 10. If you’re using a big ceramic disk capacitor as I did with the prototype, it will reach that far. If you’re using the tantalum caps I recommended and used in the pictures, that’s a problem. The leads of the capacitor can touch one of the other RS-232 connections or the dropping resistor for the LED. We need to insulate the leads of the capacitor with something. Tape would work, but if you have hookup wire, you have lots of insulation. There are probably little bits of it all over your work space right now. They’ll work fine. Grab one of these pieces of spaghetti (that’s actually the name for it—they used to sell it in rolls for vacuum tube electronics) and slip it on one of the leads of the capacitor, then bend and trim the leads of the capacitor as needed, and install it.

Now run a wire from tiepoint row 10, or wherever your capacitor wound up down to the reset pin—pin 9 on the ATmega. You can see mine in Figure 2-8.

A340964_1_En_2_Fig8_HTML.jpg
Figure 2-8. The RTS-Reset Circuit

TX, RX, and CTS Circuits

The rest of the RS-232 circuits are much simpler. Wire pin 2 of the connector, RX, to pin 15 of the ATmega. If you look at the pinout diagram in Figure 2-2, one of the functions of that pin is TXD0, or transmit data for USART0.

Note

that a USART (Universal Synchronous/Asynchronous Receiver/Transmitter) is serial communications hardware built into the ATmega. We’re using USART0 for RS-232 communication with the host computer. The ATmega1284P also has a USART1, but we won’t be using it for anything.

In RS-232, transmit pins are always connected to receive pins, and vice versa. That being the case, wire pin 3 of the connector (TX) to pin 14 of the ATmega: RXD0 – Receive Data for USART0.

Pin 4, Vcc, is already tied to the + bus, and pin 6 is tied to the - bus. That leaves pin 5, CTS, which tells the host computer it’s ok to transmit. It’s active low, so we’ll wire it to the - bus. It’s always ok to transmit. You might notice in Figure 2-9 that I moved the LED to a different - bus tiepoint to make room for the CTS line’s connection to ground.

A340964_1_En_2_Fig9_HTML.jpg
Figure 2-9. TX, RX, and CTS Circuits

Testing, Board Layout , and Static

There’s not a lot of testing we can do to make sure the Cestino is wired correctly until we get the bootloader and core software loaded, and that happens next chapter.

For right now, when you connect the TTL level RS-232 connector up (RTS/the green wire on pin 1, Ground/the black wire on pin 6) you can rest your thumb on the ATmega. If it gets more than slightly warm to the touch, unplug and check your power connections. Also run your thumb over the oscillator for the same reason. They get screaming hot when their polarities aren’t respected. If it gets that hot, you might want to break out the spare once you’ve fixed the problem.

If you’ve got a really bad short, especially in the power circuit connections, your host computer will complain that something is pulling way too much current. If that happens, disconnect and go over your power circuits again.

Nothing getting uncomfortably hot? No complaints from the host computer? Good. You can test the LED by plugging a jumper into the + bus, and connecting it to Pin 1 of the ATmega. The LED should light. If you wanted to test all the other + busses on your breadboard setup that way, it wouldn’t be a bad thing.

Other than getting power polarity wrong, there’s not much you can do that will damage the ATmega or the oscillator by connecting them to the + bus. The worst that will happen is nothing .

Likewise, you may hear dire warnings from people my age about static, and it’s true that static electricity can destroy sensitive integrated circuits. The truth is, much of the panic about static came from first generation CMOS RAM, which was very, very sensitive to even a little static, completely unprotected against it, and expensive. We can be a little more cavalier with the ATmega and the associated components. They’re cheap and robust. It’s probably a good idea to discharge yourself to a metal file cabinet or something when you sit down to work on your Cestino and related projects, but that’s as far as I’d bother. If you know you have a lot of static in your home or your workshop, or you’ve had really bad luck with it, you might want an antistatic wrist strap.

One other thing: From the photos, you’ll probably conclude I’m fussy about board layout. If you watched me re-arrange the prototype over and over again, you might think I’m actually nuts on the subject. The truth is, I knew I was laying the board out to be photographed. It needs to show the circuits clearly during construction, and be reliable and durable, since I’m carrying the hero Cestino (the one in the photos) back and forth to my photography box. Your layout doesn’t have to be so fussy. I will make one suggestion. Make sure, when all is said and done, that all the ATmega pins except the power pins have at least one empty tiepoint next to them, so you can connect those pins to the projects to come .

Credit Where Credit Is Due

For this project, I have stood on the shoulders of giants. Obviously, the Arduino LLC and community did the lion’s share of the work, but Cestino would not have been possible without the work of the Sanguino and Arduino Mighty projects as well. It was they who first added the code necessary for the Arduino core and bootloader to run on the ATmega1284P. Those additions were subsequently picked up by the Optiboot bootloader project, and the Arduino core project. All I had to do was use the most recent version of Optiboot, tie in to the standard Arduino core that comes with Arduino 1.6.5, and write a few configuration files to build a bootloader and core for a different, somewhat faster board. This is open source, when it works.

That the Cestino’s reset circuit looks exactly like the Arduino Mighty’s is likewise not a coincidence. The 20Mhz external oscillator configuration is, by contrast, my doing, after being exposed to TTL oscillators by building Sergey Malinov’s Zeta computer. You’ll hear more about that later.

Further

At the end of each project, there’s a section called “Further.” It’s a place for ideas I’ve had but not tried, or variations on projects that were fun, or tinkering I’d like to do if I had unlimited time. For the Cestino hardware, you could download the Geda schematic for the Cestino, use Geda’s board design tools to design a printed circuit board, and either have it made or make it yourself by various methods. You could then solder the Cestino up, giving you a board that’s much more robust than the breadboard version.

Hint

If you make a printed circuit board version of Cestino, use a socket for the ATmega itself, and perhaps for the oscillator. Being able to remove these can be helpful.

In truth, the “further” for this chapter is really the rest of this book, starting with Chapter 3, where we load the bootloader and the core software, and really bring the Cestino to life.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.63.95