ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3139
Скачиваний: 0
102 7 Interfacing to Analog Signals
INIT_ADC:
LDI R16,$89
OUT ADCSR,R16
RET
SX7.5
Write a subroutine that initializes the ADMUX register with the value of the variable CHAD (Channel Address), then starts a conversion.
Solution
START_ADC:
LDS |
R16,CHAD |
;read channel address |
OUT |
ADMUX,R16 |
;select channel |
SBI |
ADCSR,7 |
;start conversion |
RET |
;The interrupt service routine must get the ADC data ;and store it in RAM variables
7.8 Digital-to-Analog Converters
7.8.1 The Principles of the D/A Conversion
In many situations, is not enough for a microcontroller to measure analog signals – it is also required to generate analog signals. There is a variety of industrial equipment that is controlled by analog signals. To communicate with this type of equipment, the microcontroller system must be able to generate analog signals with precisely controlled amplitudes.
One simple way to do this is to use a PWM timer. By applying a PWM signal to a low-pass filter, the resulting output signal has the amplitude Vout = K × VM where VM is the amplitude of the VPWM signal, and K is the duty cycle (refer to Fig. 7.4).
VPWM
Vmax |
t |
Tp |
T |
VOUT |
Vmax |
Vout |
t |
Fig. 7.4. The analog signal associated with a PWM signal
7.8 Digital-to-Analog Converters |
103 |
RF
-
Vout
+
S3 |
S2 |
S1 |
S0 |
|
2R |
2R |
2R |
2R |
|
Vref |
||||
R |
R |
R |
2R |
Fig. 7.5. Simplified schematic of a 4-bit DAC, using R-2R ladder network
In cases when a PWM timer is not available, or when the number of PWM channels is less than the required number of analog outputs, the solution is to use special external circuits, called Digital-to-Analog Converter, or DACs. There is a variety of such circuits with different resolutions (the number of bits of the converted word), different conversion time, or different ways to present the result (serial/parallel). The majority of them operate on the same principle, by dividing the current in a ladder type R-2R resistor network. The simplified schematic of a 4-bit DAC based on this principle is presented in Fig. 7.5.
In this circuit, the operational amplifier acts in such a way that the potential of the inverting input is maintained equal to the potential of the non-inverting input, which is connected directly to ground. As a consequence, regardless of the status of the switches S3–S0, the network acts as if all of the 2R resistors, have a terminal connected to ground. This means that the equivalent resistance that loads Vref is constant, equal to R, and the total absorbed current from Vref is also constant, Iref = Vref/R. The currents flowing through the four switches (S3, S2, S1, S0) are I3 = Iref/2, I2 = Iref/4, I1 = Iref/8, I0 = Iref/16. The output voltage is
Vout = −IF × RF
where IF is a fraction of Iref, directed by the switches S3–S0 to the inverting input of the operational amplifier,
IF = b3 × I3 + b2 × I2 + b1 × I1 + b0 × I0
IF = Iref(b0/2 + b1/4 + b2/8 + b3/16)
were (b3–b0) are the bits of the word that must be converted to an analog value.
It follows that the output voltage Vout is proportional to the binary value to be converted.
A typical example of a circuit, built on this principle, is MX7224 from Maxim. The functional block diagram and the pin configuration of the circuit are shown in Fig. 7.6.
The circuit is designed so that it can be directly connected to a data bus. When both the CS\ and WR\ signals are active, the data from the bus is transferred to the
104 |
7 |
Interfacing to Analog Signals |
|||||||||||
2 |
|||||||||||||
13 |
DB0 |
Vout |
|||||||||||
LDAC |
|||||||||||||
12 |
DB1 |
||||||||||||
11 |
DB2 |
||||||||||||
10 |
DB3 |
DAC REGISTER |
|||||||||||
9 |
DB4 |
||||||||||||
8 |
DB5 |
||||||||||||
7 |
DB6 |
WR |
|||||||||||
6 |
DB7 |
VDD |
18 |
||||||||||
3 |
VREF |
||||||||||||
1 |
|||||||||||||
16 |
VSS |
||||||||||||
LDAC |
|||||||||||||
CS |
|||||||||||||
15 |
WR |
||||||||||||
14 |
4 |
INPUT REGISTER |
|||||||||||
CS |
AGND |
||||||||||||
17 |
5 |
RESET |
|||||||||||
RESET |
DGND |
||||||||||||
MX7224 |
|||||||||||||
DATA BUS
Fig. 7.6. Block diagram and pin configuration of a typical DAC IC
Input Register. The LDAC\ signal controls the transfer of data to the DAC register. When CS\ = 0, WR\ = 0 and LDAC\ = 0, both registers are transparent and the data from the DB0–DB7 inputs is converted to Vout. With this configuration of the control signals the circuit can be directly connected on an output port of the microcontroller
7.8.2 Exercise on Using MX7224
SX 7.6
Using the MX7224 circuit, connected to the PORTA of a 68HC11F1 microcontroller, write a program that generates a signal with the characteristics shown in Fig. 7.7. The reference voltage used is 2.5 V, and the frequency of the internal E clock is 2 MHz. The output signal is periodic with 10-ms period. Figure 7.7 gives the values of the samples for one period.
Solution
The values of the 10 samples, corresponding to a period of the output signal, must be written in the port connected to the DAC at 1-ms intervals. To generate the time
Vout |
|
2.1 |
|
1.2 |
|
0.5 |
|
t |
|
1 |
10ms |
Fig. 7.7. Waveform of the signal referred in the exercise from paragraph 7.8.2
7.8 Digital-to-Analog Converters |
105 |
intervals a timer is required, TOC1 for example, which generates periodic interrupts. The timer interrupt service routine must update the port data. The initiation sequence must configure the PORTA in output mode, enable the TOC1 interrupt, and initialize the pointer XTAB with the address of the ROM table that contains the values of the samples.
INIT_DAC |
LDD |
TOC1 |
|
ADDD |
#2000 |
;next interrupt in 1 ms |
|
STD |
TOC1 |
||
LDX |
#DACTAB |
||
STX |
XTAB |
;init pointer in DACTAB |
|
BSET |
TFLG1,$80 |
;clear OC1F if any |
|
BSET |
TMSK1,$80 |
;enable TOC1 interrupts |
|
LDAA |
#$FF |
||
STAA |
DDRA |
;PORTA all lines output |
|
RTS |
;end of initialization |
The interrupt service routine must do the following:
•Erase the interrupt flag.
•Prepare the next interrupt from TOC1 to come in 1 ms.
•Write the value from DACTAB in PORTA
•Increment the XTAB pointer, and check if the end of table has been reached. If the end of the table is detected, XTAB must be reinitialized with the starting address.
TOC1_ISR |
BSET |
TFLG1,$80 |
;clear interrupt flag |
LDD |
TOC1 |
||
ADDD |
#2000 |
;next interrupt in 1 ms |
|
STD |
TOC1 |
||
LDX |
XTAB |
||
LDAA |
0,X |
;get data from table |
|
STAA |
PORTA |
;write it to DAC |
|
INX |
;increment pointer |
||
CPX |
#ENDTAB |
;check for end of table |
|
BHS |
TOC1X |
||
STX |
XTAB |
;update pointer and exit |
|
RTI |
|||
TCO1X |
LDX |
#DACTAB |
;reload pointer if end of |
;table detected |
|||
STX |
XTAB |
||
RTI |
The table with the DAC values for the interpolation, considering that the value Vout input $FF.
specified curve is obtained by linear = Vref = 2.5 V corresponds to a binary
106 7 Interfacing to Analog Signals
DACTAB |
DB |
$00 |
|
DB |
$33 |
||
DB |
$D6 |
||
DB |
$A3 |
||
DB |
$7A |
||
DB |
$4C |
||
DB |
$33 |
||
DB |
$7A |
||
DB |
$A3 |
||
DB |
$4C |
||
ENDTAB |
EQU |
* |
;end of table |
Obviously, by modifying the data in DACTAB, it is possible to obtain any waveform for the output signal. The only limitation is imposed by the speed of the processor. For HC11, the 1-ms interval between two successive interrupts is close to the upper limit. The AVR microcontrollers are much faster.
8
Using the Internal EEPROM Memory
8.1 In this Chapter
This chapter contains a description of the EEPROM memory as implemented in the HC11 and AVR families of microcontrollers, as well as a description of the EEPROM control registers, and software examples for erasing and programming the EEPROM.
8.2 Overwiew of the EEPROM Subsystem
In many situations it is required that some program parameters, calibration tables, etc. are stored in nonvolatile memory, able to keep its contents indefinitely after the system is powered off.
The solution to this problem is to include in the structure of the microcontroller an EEPROM memory area (Electrically Erasable Programmable Read Only Memory). Most modern microcontrollers include between 128 bytes and 2 kilobytes of EEPROM.
For technological reasons, erasing and programming the EEPROM requires a 20 V Vpp voltage, obtained by means of a so-called charge pump. The current capability of this internal source is very low, and therefore the charge pump requires a time of around 10 milliseconds to stabilize. The following restrictions apply when accessing the EEPROM:
•Before programming, an EEPROM bit must be erased. The value of an erased bit is 1.
•After an erase or program operation, a 10-ms delay is required for the charge pump to stabilize.
8.3 The EEPROM Memory and the CONFIG Register of HC11
Depending on the model, the microcontrollers of the HC11 family have between 512 and 2048 EEPROM memory bytes, mapped in the general memory map. The starting
108 8 Using the Internal EEPROM Memory
address of the EEPROM memory area differs from one model to another, but, in some cases, the whole EEPROM block can be remapped, by means of the control bits [EE3:EE0] from the CONFIG register.
8.3.1 The Registers Controlling the EEPROM of HC11
HC11 has two registers involved in the control of the EEPROM. These are the PPROG register (EEPROM Programming Control Register) and the BPROT register (EEPROM Block Protect Register). The PPROG register has the following structure:
PPROG |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
ODD |
EVEN |
– |
BYTE |
ROW |
ERASE |
EELAT |
EEPGM |
|
RESET |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
The ODD and EVEN bits are used in the special test operating mode and they will not be discussed here.
The BYTE, ROW and ERASE bits control the erase process of the EEPROM, according to Table 8.1.
Table 8.1. The effect of programming the bits BYTE, ROW, and ERASE
BYTE |
ROW |
ERASE |
Erase mode |
x |
x |
0 |
Normal read/program |
0 |
0 |
1 |
All locations erased |
0 |
1 |
1 |
A 16 bytes “row” is erased |
1 |
0 |
1 |
One byte is erased |
1 |
1 |
1 |
One byte is erased |
•EELAT – EEPROM Latch Control bit
EELAT = 0 EEPROM address and data buses are prepared for normal read op-
erations.
EELAT = 1 EEPROM address and data buses are prepared for write or erase operations. Write operations to EEPROM with EELAT = 1 cause the values of address and data to be retained in special latches.
•EEPGM – EEPROM Program Control bit.
EEPGM = 1 starts the charge pump assuring the programming tension for the EEPROM. The EEPGM bit can be written only if EELAT = 1.
The BPROT register has the following structure:
BPROT |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
– |
– |
– |
PTCON |
BPRT3 |
BPRT2 |
BPRT1 |
BPRT0 |
|
RESET |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |