ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 3099

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

180

14

PI Temperature Controller

CODE

VECTOR_RESET

;RESET entry point

RESET

EQU

*

INCLUDE

INIT.ASM

;initializations

MLOOP

EQU

*

INCLUDE

TIMER.ASM

;software timers

INCLUDE

INPUT.ASM

;digital&analog

INCLUDE

INTEGRAL.ASM

INCLUDE

CONTROL.ASM

INCLUDE

DISPLAY.ASM

JMP

MLOOP

INCLUDE

OUTPUT.ASM

END

The first two include files, 68HC11F1.DEF and AS11.MAC, are present unchanged in all HC11 applications described in this book. They contain the symbolic definitions of the I/O registers, and a set of useful macro definitions.

MAP.ASM defines the memory map, by specifying the beginning of the DATA and CODE segments, and also contains the definitions of all RAM variables used by the program. Although MAP.ASM contains application-specific code, a similar file must be present in all applications, along with INIT.ASM, which contains the initialization routines for all the resources used by the application. In this particular project, INIT.ASM has the following contents:

TITLE INITIALIZATION MODULE

CODE

SEI

;disable all interrupts

;during init sequence

LDAA

#$0A

STAA

HPRIO

;TIC3 has the highest

;priority

LDAA

#$05

;Enable CSPROG

STAA

CSCTL

;for a 32k memory

LDS

#RAMEND

;init stack pointer

LDAA

#$80

;init I/O ports

STAA

DDRA

LDAA

#$20

STAA

DDRD

JSR

ITIMER

;init TOC2

JSR

IADC

;init ADC

JSR

ITIC3

;init TIC3

JSR

ISPI

;init SPI

JSR

INITVAR

;init variables

CLI

;enable interrupts

END

Note that the actual initialization sequences, associated with specific resources, are organized as subroutines (ITIMER, ISPI, IADC), and are located in the soft-ware modules dealing with the respective resources.


14.4 Software Implementation of a PI Temperature Controller

181

The rest of the modules are included in an endless program loop. TIMER.ASM defines a set of software timers, as described in Chap. 9. INPUT.ASM reads the analog input lines AN0–AN3, connected to the temperature sensor and three external potentiometers, and updates the variables TS, TM, KP and TI. For scaling purposes, the 8-bit value read from AN2 is truncated to the four most significant bits, so that KP is limited to take values in the range [0–15]. Similarly, TI is reduced to take values in the range [0–63]. The digital input lines PA0–PA6 are read to update the variables SWSTATUS, which encodes the status of the rotary switch, and QSTART, a Boolean variable set to $FF when the START button is pressed, and cleared by pressing the STOP button. The subroutine CERR (Compute Error) is called here and updates the variable ERR – a 16-bit signed integer, defined as the difference (TS − TM).

The module INTEGRAL.ASM approximates the term:

Ti

1 × e(t)dt . Ti

0

Using the notation Ei = e(i × T0), then the integral can be approximated by the sum:

N

N

(T0 × Ei ) = T0 ×

Ei .

(14.4)

i=1

i=1

If Ti = N × T0, then:

Ti

N

Ei

1

×

e(t)dt =

i=1

(14.5)

.

Ti

N

0

This means that the integral term in the expression (14.3) can be approximated by the average of the last N values of the variable ERR = TS − TM. The number N of samples considered for computing the integral is determined by the variable TI. T0 is a constant, equal to 100 ms, implemented by means of a software timer.

The values of the samples Ei are stored in a buffer IBUF. The size of IBUF must be dimensioned so that it can store the maximum value of samples, as indicated by the variable TI. Since TI can have one of 64 possible values, and the error ERR is a 16-bit integer, then IBUF must be128 bytes in length.

Two additional bytes are reserved for a pointer in IBUF, named XIBUF. IBUF is organized as a circular buffer. Each time the timer that defines T0 expires, the computed value of ERR is stored in the buffer at the location indicated by XIBUF, and then the pointer is incremented by 2. When the end of buffer is reached, the pointer is reloaded with the starting address of IBUF.

Only the last TI values in IBUF are used to compute the average. The result is stored in the variable INTEGRAL This is used by the module CONTROL.ASM, which computes VOUT = KP (ERR + INTEGRAL). VOUT is adjusted to be an 8-bit value in the range [0–255].


182 14 PI Temperature Controller

The computed value of VOUT is used in OUTPUT.ASM to determine the moment at wich to generate the pulse to open the triac that drives the heater. This is done in the interrupt service routine of TIC3. When an edge on IC3 (PA0) occurs, the TIC3 interrupt service routine prepares a value for TOC1, based on the value of VOUT, then enables the TOC1 interrupt.

To avoid time-consuming calculations in the interrupt service routine, VOUT is used as an offset in a table (TOCTAB) that contains for each possible value of VOUT, a 16-bit value that is added to the current TCNT and written to TOC1. These cause a TOC1 interrupt to be generated after a number of E cycles equal to the value extracted from TOCTAB.

The TOC1 interrupt service routine simply generates a pulse on PA7, then disables further TOC1 interrupts. A new interrupt will only be generated when an edge is sensed on IC3 and QSTART = $FF.

Here is what the interrupt service routines for TIC3 and TOC1 look like:

VECTOR_TIC3

LDAA

TFLG1

;clear interrupt flag

ORAA

#$01

STAA

TFLG1

LDX

#TOCTAB

LDD

VOUT

ABX

;add VOUT as offset

LDD

0,X

;get data from table

ADDD

TCNT

STD

TOC1

;prepare TOC1 interrupt

LDAA

#$80

;enable TOC1 interrupts

ANDA

QSTART

;if QSTART=$FF

BEQ

TIC310

ORAA

TMSK1

STAA

TMSK1

RTI

TIC310 LDAA

TMSK1

;disable TOC1 interrupts

ANDA

#$7F

;if QSTART=0

STAA

TMSK1

RTI

VECTOR_TOC1

LDAA

TFLG1

ORAA

#$80

;clear interrupt flag

STAA

TFLG1

LDAA

#$80

;pulse on PA7

STAA

PORTA

NOP

;wait a few microseconds

NOP

NOP

NOP

NOP


14.4

Software Implementation of a PI Temperature Controller

183

LDAA

TMSK1

;disable TOC1

ANDA

#$7F

STAA

TMSK1

CLRA

STAA

PORTA

RTI

The module DISPLAY.ASM contains the software routines needed to control the four-digit seven-segments display unit described in Appendix A14.

15

Fuzzy Logic Temperature Controller

15.1 In this Chapter

This chapter contains an introduction to the principles of fuzzy control and the description of a simple temperature controller based on these principles.

15.2 The Principles of Fuzzy Control

Tuning a PID controller, i.e. adjusting the values of K p, Ti , and Kd , to obtain the desired response of the controlled process, can be a difficult task. The mathematical solution to this problem requires that the transfer function of the controlled process is known. In practice, real-world systems are seldom described by a simple and obvious transfer function. It would be much more convenient to define the behavior of the control system through simple sentences like this: “If the temperature is higher than the set-point and rising fast, then the output of the controller must be very low”.

Fuzzy controllers work this way. Basically, a fuzzy controller is a control system that operates according to fuzzy logic. In fuzzy logic, the sentence “the element x is member of the set A” can be true to a degree of y%, which is equivalent to “the element x has a y% degree of membership to the set A”. For example, when speaking about temperatures, the domain from 0 to 100 ◦C can be divided in two subsets: cold, and warm. An object having a temperature of 15 ◦C could be considered to be 60% cold and 40% warm.

Ts

e(t)

FUZZY

Vout

Tm

Preprocessor

d(t)

Process

CONTROL

Sensor

Fig. 15.1. Block diagram of a fuzzy controller