Файл: Microcontroller based applied digital control (D. Ibrahim, 2006).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3269
Скачиваний: 1
254 CONTROLLER REALIZATION
Enter |
ISR: |
Enter |
Read setpoint, sk
Read reference, sk Read output, yk Input section
Calculate error, ek
Initialize A/D |
Calculate output, yk |
Output section |
Initialize
Update variables Update section
Timer interrupts
Return from interrupt
Wait for interrupt
Figure 10.15 Controller implementation: (a) main program; (b) interrupt service routine
routine (ISR) as shown in Figure 10.15(b), and it is within this routine that the actual controller algorithm is implemented. The error signal is obtained by calculating the difference between the reference values and measured values. The algorithm is then implemented and the output sample for the current sampling time is obtained. A preprocessing step is then performed by updating the variables for the next sample. On return from the ISR, the program waits in the main program until the next sampling interval, and the above process repeats.
10.5.1 Implementing Second-Order Modules
In Section 10.2 we saw how a second-order module can be realized using adders, multipliers and delay elements. The second-order module is shown in Figure 10.16. The difference equations describing such a module are (10.19) and (10.20). If we let
M1 = −b1rk−1 − b2rk−2
and
M2 = a1rk−1 + a2rk−2
then the difference equations for the second-order module become
rk = ek + M1,
uk = a0rk + M2.
The implementation of the second-order module is shown as a flow-chart in Figure 10.17. This figure does not show the initialization of the variables T1, T2, r1, r2, the A/D initialization,
256 CONTROLLER REALIZATION
Figure 10.18 Circuit diagram of the microcontroller
Assume that the digital controller to be implemented is in the form of a second-order module, and write a program in C to implement this controller. The controller parameters are assumed to be
a0 = 1, a1 = 0.8, |
a2 = 1.2, b1 = 1.85, |
b2 = 0.92, |
||
i.e. the required controller transfer function is |
||||
D(z) |
= |
1 + 0.8z−1 + 1.2z−2 |
. |
|
1 + 1.85z−1 + 0.92z−2 |
||||
Also assume that the required sampling interval is T = 0.01s.
Solution
The controller hardware is based on a PIC16F877 microcontroller. The microcontroller is operated from a 4 MHz crystal, connected to OSC1 and OSC2 inputs. With a 4 MHz crystal, the basic clock rate of the microcontroller is 1µs (the crystal frequency is divided by 4 to obtain the basic timing rate). The analog output of the plant (y) is connected to A/D converter channel AN0 of the microcontroller. Similarly, port B digital outputs of the microcontroller are connected to an AD7302 type D/A converter. The operation of this D/A converter is very simple. Digital data is applied to eight inputs D0–D7, while the write control input, WR, is at logic high. The analog data appears at the output after the WR input is lowered to logic low. The WR input of the D/A converter is controlled from port pin RC0 of the microcontroller. The D/A converter is set to operate with a full-scale reference voltage of +5 V. The resolution of the converter is 8 bits, i.e. there are 28 = 256 quantization levels. With a full-scale reference voltage of +5 V,
MICROCONTROLLER IMPLEMENTATIONS |
257 |
the resolution is 5000/256 = 19.53 mV, i.e. the digital bit pattern ‘00000001’ corresponds to 19.53 mV, the bit pattern ‘00000010’ corresponds to 2 × 19.53 = 39.06 mV and so on.
The first program attempt is shown in Figure 10.19. Comments are used to describe operation of various parts of the program. The program consists of the main program and the functions:
Initialize AD, Initialize Timer, Read AD Input, and ISR.
Main program. The coefficients of the controller are defined at the beginning of the main program. Also, the A/D converter and the timer initialization functions are called here. The main program then enables global interrupts and enters an endless loop waiting for timer interrupts to occur.
ISR. This is the interrupt service routine. The program jumps to this function every 10 ms. The function reads a sample, and calculates the error term ek . The output value yk is then calculated and sent to the D/A converter. In the final part of the ISR, the variables are updated for the next sample, and the timer interrupt is re-enabled.
Initialize Timer. This function initializes the timer TMR0 so that timer interrupts can be generated at 10 ms intervals. As described in Chapter 3, the timing interval depends on the clock frequency, the pre-scaler value, and the data loaded into the TMR0 register. It can be shown that the timing interval is given by:
Timing interval = 4*clock period*prescaler*(256 − TMR0 value)
and the value to be loaded into TMR0 register for a required timing interval is given by:
TMR0 value = 256 − timing interval/(4*clock period*prescaler)
The clock frequency is chosen as 4 MHz, i.e. the clock period = 0.25 µs. If we choose a prescaler value of 64, the value to be loaded into the timer register for a 10 ms (10 000 µs) timing interval can be calculated as:
TMR0 value = 256 − 10000 ms/(4*0.25*64) = 99.75
We can choose 100 as the nearest value. This will give a timing interval of
Timing interval = 4*0.25*64*(256 − 100) = 9.984ms,
which is very close to the required value.
Thus, the Initialize Timer function is programmed as follows:
/* This function initilizes the timer TMR0 so that interrupts can be generated at every 10ms intervals */
void Initialize Timer(void)
{
T0CS = 0; |
/* Select f/4 clock for the TMR0 */ |
PSA = 0; |
/* Select pre-scaler */ |
PS0 = 1; |
/* Set pre-scaler to 64 */ |
PS1 = 0; |
/* PS2,PS1,PS0 = 101 */ |
PS2 = 1; |
|
TMR0 = 100; |
/* Load TMR0 = 100 */ |
T0IE = 1; |
/* Enable TMR0 interrupts */ |
T0IF = 0; |
/* Clear TMR0 interrupt flag */ |
}