Файл: Introduction to microcontrollers (G. Gridling, 2006).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1054
Скачиваний: 2
2.3. DIGITAL I/O |
37 |
Noise Cancellation
Although the PIN register of the controller should normally follow the state of the input pin as closely as possible, this is quite undesired if the signal is noisy. Here, electromagnetic interference from the environment produces short voltage spikes on the line, and these voltage changes should normally not be taken over by the controller, where they could produce erroneous reactions, especially in conjunction with interrupts.
Therefore, some controllers provide noise cancellation. If enabled, the controller samples the pin not just once but several times, e.g. k times, and only takes over a new value if all k samples were equal. Obviously, this adds another constant dncanc = k − 1 cycles to the overall input delay, so the bounds on the delay become
din = dlatch + dsync + dncanc |
(2.1) |
clock cycles.
Pull Resistors
Many controllers integrate pull resistors into their input circuitry. Most of the time, they provide pull-up resistors, some controllers also offer pull-down resistors (e.g. the HCS12). The task of the pull resistor is to connect the input pin to a defined voltage if it is not driven by the external hardware. Pull resistors are controlled via a register, where they can be enabled or disabled for each pin independently. The ATmega16, for example, uses the PORT register bits of input pins for controlling their pull resistors. Other controllers provide dedicated registers for this purpose.
It can occur quite frequently that an input pin is not driven by hardware all the time, most notably when using simple mechanical switches, like DIP switches or buttons. Here, the input pin is connected to a defined value as long as the switch is closed, but left floating (that is, unconnected and at an undefined voltage level) whenever the switch is open. Since floating pins are A Bad Thing (they are very prone to noise!), a pull resistor must be used to set the pin to a defined level while the switch is open, see Figure 2.12 for an example.
Controller−Input. |
1 2 |
Figure 2.12: Attaching a switch to an input pin with activated pull-up resistor.
In the figure, we connected the switch to an input with activated pull-up, also called an open-drain input. While the switch is open, the input pin is connected to VCC and the controller will read a 1. Closing the switch connects the pin to ground, and the controller will read 0.
There is another interesting thing to note in Figure 2.12: Due to the pull-up resistor, whenever the switch is closed, current flows from the controller via the input pin to the external ground. Without a pull resistor, there would not be any notable current flow from or to the controller pin, since the
38 |
CHAPTER 2. MICROCONTROLLER COMPONENTS |
controller should not influence the external circuit just by reading it. With the pull-up enabled, however, the controller takes an active role in determining the state of the line and hence current will flow between the input pin and the external circuitry.
If current4 flows from the controller to the external circuit, the input is called a source input because it provides current. If current flows from the hardware into the controller, this is called a sink input. Controllers are very particular about the amount of current they can source and sink, and exceeding the bounds stated in the datasheet may destroy the pin and even the controller itself. Controllers can generally handle about 4-20 mA, and if they make a difference between sourcing and sinking at all, they can generally sink more current than they can source.
2.3.2Digital Output
The digital output functionality is used to set output pins to given voltage levels. The levels corresponding to high and low are again specified by the controller and depend on the controller’s operating voltage. For the ATmega16 at VCC = 5V, the maximum output low voltage is 0.7 V, and the minimum output high voltage is 4.2 V.
Whenever the DDR of a pin is set to output, the controller drives the pin according to the value given in the PORT register. An output pin generally has to sink or source current, so we can again distinguish between a sink output and a source output. The maximum current ratings discussed in the previous section apply, so we are talking about 4-20 mA maximum current5.
Output pins are more critical than input pins in the sense that they heavily depend on external current protection. After all, you could connect an output pin directly to GND and then set it to 1, thus creating a short-circuit. Although controllers tend to tolerate such short-circuits for a brief amount of time (generally less than a second), a short will eventually destroy the controller. So the hardware designer must ensure that the external hardware cannot produce a short-circuit. If it can, or if the application programmer prefers to be on the safe side, the controller at least offers the possibility to read back the current state of the pin via the PIN register. Of course, the PIN register suffers from the input delay, so a short-circuit will only become visible in the PIN register ddine clock cycles after the output pin has been set. Hence, the application must wait for this amount of time before it can check the PIN. If a mismatch is detected, the application program should set the pin to input immediately and notify the user.
Note that some microcontrollers only drive the 0, but using an open-drain input for generating the 1.
Finally, we want to draw your attention to the question of which register to set first for output pins, PORT or DDR. After a reset, the pin is generally set to input. If your controller does not use the PORT bits of input pins for other purposes (like the ATmega16, who uses them to control the pull-ups), and if the controller allows write access to the PORT bits of input pins, then the answer is obvious: you first set the PORT and then the DDR, thus ensuring that the correct value is put on the line from the beginning.
For Atmel’s AVR controllers like the ATmega16, however, the matter is more complex. Here, the PORT controls the pull-ups, so if you want to output 1 and first set PORT and then DDR, you will briefly enable the pull-up resistors before setting the port to output. Most of the time, this will
4We refer to the technical direction of the current here, from the positive pole to the negative pole.
5The 20 mA is a magic number, by the way, because this is the amount of current required to directly drive a normal LED. Being able to drive a LED directly is often a useful feature.
2.3. DIGITAL I/O |
39 |
not matter, but you should nevertheless study the hardware to make sure enabling the pull-ups has no adverse effects.
Note that a prudent hardware designer will make sure that you can set the two registers either way by designing the external circuit in such a manner that the default state of the hardware if it is not driven by the controller (this occurs e.g. during a reset of the controller, so the hardware designer must provide for it) is the same as when the controller pin outputs its PORT reset value. This way, if you set the pin to output first, there will be no change in its value and you can set the PORT pin at your leisure.
To conclude, if the controller allows and the hardware does not mind, set PORT first and DDR afterwards. But always check to be sure this has no ill side-effects!
2.3.3Exercises
Exercise 2.3.1 The sole task of your microcontroller is to monitor a digital signal which has impulses of duration ≥ 1µs. To do so, you continuously read and process the pin state. Reading takes 1 cycle, processing the value takes 3 cycles. How fast does your microcontroller have to be so that you will never miss any impulses?
Exercise 2.3.2 If you enhance the previous example by another line of code that puts the read value on an output pin as well (1 cycle), what is you minimum operating frequency? How much can the output signal generated by you differ from the input signal (delay and distortions)?
Exercise 2.3.3 Can you think of a situation where enabling the pull-up resistor of a pin before setting it to output can cause a problem in the external circuit?
Exercise 2.3.4 Assume that the Schmitt-trigger of Figure 2.11 has a propagation delay (that is, the time the signal is delayed while passing through the component) of k1 ns, and a latch has a propagation
delay of k2 ns. Augment din accordingly.
Exercise 2.3.5 In Figure 2.12, we have connected the switch directly to the controller pin without any current limiting resistor in series with the switch. Was this wise? Even if the controller can handle the current, what reasons might a hardware designer have to put in a resistor anyway?
40 |
CHAPTER 2. MICROCONTROLLER COMPONENTS |
2.4 Analog I/O
In the previous section, we have covered digital I/O. There, analog signals were mapped to two discrete values 0 and 1. Although this is already very useful, there are situations in which the actual voltage of the line transports information, e.g. when using a photo transistor as light sensor: The voltage drop it produces at its output is directly proportional to the amount of light falling on the transistor, and to adequately evaluate the sensor output, the microcontroller must deal with the analog value. On the other hand, the microcontroller is inherently digital, so we need appropriate ways of converting analog signals into the digital world and back again. This problem is addressed by the analog module of the microcontroller.
In the following text, we will give an overview on analog interfacing techniques and problems. A thorough treatment of this subject can be found e.g. in [Hoe94] or in [Bal01].
2.4.1Digital/Analog Conversion
Since digital-to-analog conversion is a prerequisite for some analog-to-digital converters, we begin with analog output. This means that we have an r-bit digital value B = (br−1 · · · b0)2, r ≥ 1, in the range [0, 2r − 1] and want to generate a proportional analog value Vo.
Yet, as powerful as they are when it comes to analog input, microcontrollers often have little or no analog output capabilities. So if the application requires a d/a converter, most of the time it has to be fitted externally. Fortunately, it is fairly easy to construct a simple and cheap 1-bit d/a converter by using a PWM (pulse-width modulation) output, see Section 2.6, in conjunction with an RC low-pass filter. The idea here is to generate a PWM signal whose high time to period ratio is proportional to the digital value B. The PWM signal is smoothened by the RC filter, resulting in an (average) analog voltage that is proportional to the high time to period ratio of the PWM signal and hence to B, see Figure 2.13. Of course, the resulting analog signal is delayed by the filter before it stabilizes, and it does not show very good quality, as it will oscillate around the desired output voltage. Still, it will be sufficient for some applications like motor control.
Vo |
||
Vmax |
||
MCU |
||
PWM |
R |
|
Vo |
||
t |
||
C |
PWM |
|
t |
high time |
period |
(a) |
(b) |
Figure 2.13: Digital-to-analog conversion using a PWM signal and an RC low-pass filter; (a) circuit,
(b) output voltage in reaction to PWM signal.
2.4. ANALOG I/O |
41 |
The oscillation depends on R and C as well as on your choice of period; Figure 2.13 greatly exaggerates the effect. To reduce the amount of oscillation, either make R and C larger (at the cost of a longer stabilization time) or use a shorter period.
Disadvantages of using PWM are that you require a dedicated timer to generate the PWM signal and that you need to wait for a few periods until the output signal stabilizes. As an advantage, the d/a converter only uses up one single output pin.
A different way to achieve d/a conversion is to use a binary-weighted resistor circuit, see Figure 2.14. Here, we have an r-bit input which is converted into the appropriate analog output. To this aim, each bit of the binary input switches its path between Vref and GND. The output voltage of the circuit (for no output load) is given by
r |
1 |
||||
Vo = Vref · |
Xi |
(2.2) |
|||
2i br−i , |
|||||
=1 |
|||||
where (br−1 · · · b0)2 is the digital value to be converted.
R/2r−1
b r−1 . Vo
..
b 1 |
R/2 |
|
R |
||
b 0 |
||
R
Figure 2.14: Digital-to-analog conversion based on a binary-weighted resistor circuit.
The main disadvantage of the binary-weighted resistor circuit is that it requires many different resistor types with have to be high precision to keep the ratio correct. This is hard to achieve. As an alternative, an R-2R resistor ladder can be employed, see Figure 2.15.
This type of DAC has the advantage that it only requires two types of resistors, R and 2R. The output voltage of the ladder circuit is again given by Equation 2.2.
2.4.2Analog Comparator
The simplest way to deal with analog inputs in a microcontroller is to compare them to each other or to a known reference voltage. For example, the phototransistor we mentioned previously could be used to implement a twilight switch, turning on a light whenever its output voltage indicates that the ambient light level is below some threshold. For such purposes, some microcontrollers which