voltage at various points in that ground/common may not
be at the same 0V. The space between two chips or even
two pins on the same chip will have resistance and current
flow will cause a small voltage difference. This difference is
usually called ground bounce and it is considered to be a
“bad” thing; most especially for analog circuits. It is bad for
analog circuits because the difference between 0.1V and
0.15V may be of great value to us (think of the analog
output from a Sharp IR rangefinder.) We want the power
lines to analog circuits to be “quiet.” One of the ways to
keep this power quiet is to have minimal ground bounce.
The other is to have a nicely filtered voltage on it, but this is
another topic. Digital circuits are more tolerant of small
ground bounce because there is a fixed logic low maximum
and logic high minimum that allows a range of correct values.
When we have high powered devices in the circuit like
a motor H-bridge, this could affect even the digital circuitry,
so even more caution is required in how we connect up the
power and common. Figure 1 shows an example of a bad
ground/common bus and Figure 2 shows an example of a
good ground common bus. In these figures, the fat lines
are high current paths and the skinny lines are low current
paths. You want to avoid putting devices that have low
current needs in the middle of paths flowing between
devices that have high current needs.
In the bad ground bus, we have connected battery and
device grounds anywhere we please. This means that high
currents and low currents can be seen anywhere on the
board. If that H-bridge is putting out two amps of current,
then lots of different ground current loops in the board and
ground bounce is going to affect chips. In the good ground
bus, we see that the digital, analog, and high current
grounds are kept separate and that all grounds are tied
together at a single point — what we usually call a star
configuration. Note that the microcontroller has both
analog and digital grounds. Keep those separate until you
tie to the common return point. Note also that the H-bridge
has a logic and a power ground; again, keep those separate
until you tie into the common return
point. Multiple digital components
can be tied together along the way
to the common return, as well as
multiple analog devices. The secret is
to keep the ground/common paths
of the various signal types isolated
from each other until they tie
together at the common return
point. I think that I’ve stressed this
point enough now. You can have lots
of problems that are difficult to understand with a controller board if you
don’t follow these basic guidelines.
Q. How can I set up the
PWM on an Atmel ATMEGA
part? What frequency is
the best to use?
A. The first part of this question is fairly simple; I’ll use
the Timer/Counter1 timer which creates the OCR1A
and OCR1B PWM outputs. I have no idea what
compiler you are using so I’ll pick two popular ones:
BASCOM/AVR® and gcc-avr. To keep this simple and easy
to use, I will also use what Atmel calls Fast PWM and eight-bit mode. (Please see their datasheet for what this means.)
I’ve chosen a frequency around 1 kHz (976 Hz) since this
will work with even (ahem) inexpensive DC brushed motors.
This assumes that your clock frequency is 16 MHz;
adjust your prescale value accordingly to get as close to 1
kHz as you can. Both 500 Hz and 2 kHz will typically work
well if you can’t get to 1 kHz. You have a limited number of
prescale options to choose from (1, 8, 64, 256, and 1024.)
Your PWM pins are dependent upon the actual ATMEGA
device that you are using.
BASCOM/AVR
Config Timer 1 = Pwm, Pwm = 8, Prescale = 64, Compare A
Pwm = Clear down, Compare B Pwm = Clear down
If this doesn’t work correctly, you can bypass the
hardware abstraction call above and punch numbers
directly into the associated registers like this:
Tccr1a = &HA1
Tccr1b = &H0B
‘Set type and mode of PWM and turn it on
‘Set mode of PWM and prescale value
Ocr1a = 0
Ocr1b = 0
‘set the PWM duty cycle for OCR1A output
‘set the PWM duty cycle for OCR1B output
gcc-avr
TCCR1A = 0xA1;
TCCR1B = 0x0B;
OCR1A = 0;
OCR1B = 0;
//Fast PWM, 8 bit
//8 bit 976 Hz
//No signal out when = 0
The key is to get the right configuration bits set and a
usable PWM frequency. This configuration uses the Fast
Figure 1
— Tran Pham
Figure 2
SERVO 05.2008 15