ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3130
Скачиваний: 0
168 13 Simple RS485 Network with Microcontrollers
The data packets have the following structure:
Header Opcode Address Data field CRC EOT
Since all data transmitted is represented in ASCII, the header and end of transmission marker (EOT) can be defined by single ASCII characters, without the risk of misinterpreting other characters in the data field as false packet delimiters. For this particular example, the header is the ASCII character STX (Start of Text –$02), and the end of transmission marker is the ASCII character for EOT (End of Text –$04).
The opcode and the address fields are one character (ASCII) each. The dimension of the data field is related to the number of inputs or outputs of the slave devices referred to by the packet. In this example, the digital input module has only three inputs, and therefore three ASCII characters are enough to encode the status of all inputs.
Similarly, the 10-bit value of the result of an analog-to-digital conversion, which ranges between 0 and $3FF, can also be represented using three ASCII characters, corresponding to the three hex digits of the ADC result. This means that a three-byte data field covers the requirements of both the SLD and SLA modules.
The CRC field is, in this example, a simple checksum consisting of the two-byte ASCII representation of the sum of the bytes in the packet starting with the opcode, and ending with the last byte of the data field.
Note that this is not a real cyclic redundancy control sum, but it serves the purpose of this simple example.
With these specifications, the detailed structure of the data packets corresponding to this protocol becomes:
STX OPC ADR D1 D2 D3 CRCH CRCL EOT
D1, D2, D3 are the three bytes of the data field, and CRCH–CRCL are the two ASCII characters corresponding to the two nibbles of the CRC checksum.
The packets sent by the master device have opcodes in the range ‘0’–‘9’ ($30– $39), as described in Table 13.2.
Table 13.2. List of valid opcodes for data packets sent by MASTER
Opcode |
Command |
‘0’ ($30) |
Reserved |
‘1’ ($31) |
Reserved |
‘2’ ($32) |
Read digital inputs from the SLD module specified by ADR |
‘3’ ($33) |
Read analog input 0 from the SLA module specified by ADR |
‘4’ ($34) |
Read analog input 1 from the SLA module specified by ADR |
‘5’ ($35) |
Read analog input 2 from the SLA module specified by ADR |
‘6’ ($36) |
Reserved |
‘7’ ($37) |
Reserved |
‘8’ ($38) |
Reserved |
‘9’ ($39) |
Reserved |
13.3 The Software |
169 |
The packets returned by the slaves can only have the opcode ACK (Acknowledge – $06), when the command has been received and executed correctly, or NAK (Negative Acknowledge –$15), for packets received with the wrong CRC, or an invalid opcode.
Here is an example of a data packet containing a command to read digital inputs (opcode $32) from the slave module SLD having the address ‘1’:
$02 $32 $31 $30 $30 $30 $46 $33 $04
Note that the data field of the packets sent by the master is filled with dummy ‘0’ characters. The reason for this is to maintain the same structure for all packets, and to allow adding new commands. The CRC field is two-bytes long, $46 (‘F’), $33 (‘3’), which corresponds to $F3 = $32 + $31 + $30 + $30 + $30.
The answer from the slave device that reports the status of all digital inputs to logic 1 is a data packet having the opcode ACK ($06), like this:
$02 $06 $31 $31 $31 $31 $43 $41 $04
13.3.2 The Software for the SLD Module
The software is structured according to the general principles described in the previous chapters. The main module, called SLDMAIN.ASM, is listed below:
.include |
"8535def.inc" |
;general definitions |
.include |
"macro.asm" |
;macro definitions |
.include |
"map.asm" |
;specific variables |
.cseg |
||
.org |
0 |
|
reset: |
||
rjmp |
init |
|
init: |
||
.include |
"init.asm" |
;all initializations |
main_loop: |
||
rcall |
get_io |
;read digital input |
rcall |
send |
;send char if any |
rcall |
get_cmd |
;check rec. buffer |
brcc |
main_loop |
|
rcall |
make_pk |
;prepare the answer |
rjmp |
main_loop |
|
.include |
"uart.asm" |
;uart routines |
.include |
"lib.asm" |
;general routines |
.include |
"io.asm" |
;i/o routines |
MAP.ASM contains definitions of the application-specific variables and a number of equations:
170 |
13 Simple RS485 Network with Microcontrollers |
||
.dseg |
;data segment |
||
starts at $60 |
|||
.org |
$60 |
||
Buf_io: .byte |
1 |
;store the status of |
|
the input |
|||
Buf_rx: .byte |
9 |
;Rx buffer |
|
Buf_tx: .byte |
9 |
;Tx buffer |
|
Stat_rx: .byte |
1 |
;Tx status |
|
Stat_tx: .byte |
1 |
;Rx status |
|
Ack_nak: .byte |
1 |
;error flag |
|
.def |
tmp1=r16 |
||
.def |
tmp2=r17 |
||
.def |
tmp3=r18 |
||
.def |
op1l=r19 |
||
INIT.ASM contains the usual initialization routines for the resources involved in the application. The uart is programmed for 9600 baud, the lines 0, 1, and 2 of port C are configured as inputs, with internal pull-up resistors, and port D, bit 2 is configured as output.
The core of the application is the subroutine get_cmd, located in the module LIB.ASM. Get_cmd receives the data packets from the uart, checks its structure and CRC, and sets the carry flag and the variable Ack_nak to inform the main program about the result of the analysis. If no command is received, get_cmd returns carry = 0. Valid commands are signaled by carry = 1, and Ack_nak = ACK. Invalid commands (i.e. commands with invalid opcode, or with the wrong CRC) are indicated by carry = 1, and Ack_nak = NAK. Valid commands having different slave address are ignored (carry = 0).
Get_cmd uses two macros, aimed to improve the readability of the program, both defined in MAP.ASM.
The macro Get_adr expects two parameters: the first parameter is an 8-bit value, the second is a 16-bit value. Get_adr adds the two parameters and stores the result in register X. This is useful to create a pointer in a table, starting from the address of the first location of the table, and an 8-bit offset. Here is the definition of the Get_adr macro:
.macro get_adr |
; two parameters @0 and @1 |
|
ldi |
xl,low(@1) |
; X<-@1 |
ldi |
xh,high(@1) |
|
clr |
tmp2 |
|
add |
xl,@0 |
|
adc |
xh,tmp2 |
;xh+0+carry !! |
.endm |
||
Example: get_adr |
op1l, buf_rx |
The second macro, Mk_crc, computes the CRC on a buffer indicated by the first 16-bit parameter taking into consideration a number of bytes indicated by the second parameter. Here is the definition of Mk_crc:
13.3 The Software |
171 |
||
.macro |
mk_crc |
;two parameters @0 ands @1 |
|
ldi |
xl,low(@0) |
||
ldi |
xh,high(@0) |
||
ldi |
tmp2,@1 |
||
rcall crc |
|||
.endm |
|||
Example: mk_crc |
Buf_rx, pack_len |
||
Get_cmd starts by calling get_uart, which returns carry = 1 if a character has been received from the serial line. The character received is available in tmp1. If the offset in buf_rx, stored in the variable stat_rx, is zero then get_cmd expects the character STX, which is the marker of the beginning of a data packet. Any other character is ignored. The characters received are stored in the buffer buf_rx, and their count is checked against the maximum length of the packet, pack_len (9). If the last character is not EOT, the whole packet is rejected.
If the packet received has the expected structure, get_cmd checks the opcode and the CRC and sets the variable Ack_nak accordingly, then exits with carry = 1. When carry = 1, the main program calls the subroutine make_pk, which prepares the transmission buffer buf_tx. The content of buf_tx is sent to the serial line, one character at a time until the counter stat_tx is decremented to zero.
See the accompanying CD for the full listing of all the program modules involved in this application.
13.3.3 The Software for the MASTER Device
The accompanying CD contains a small executable, called MASTER485.EXE, which generates and sends data packets according to the communication protocol described in this chapter. The program is able to interrogate up to four slaves, having the addresses ‘1’, ‘2’, ‘3’, and ‘4’.
The slaves with the addresses ‘1’ and ‘2’ are assumed to be digital input modules, SLD, and the slaves with the addresses ‘3’ and ‘4’ are assumed to be SLAs.
The program can operate in manual mode, when the user is expected to click on the buttons Query1–Query4 to generate query packets for the slaves with the corresponding addresses, or automatically when the user presses the Start button.
The default communication port is COM2, but the user can select any port in the range COM1–COM4. The communication parameters are 9600, N, 8, 1, and the DTR signal is used to control the RS485 line driver.
When interrogating one of the SLD modules, the opcode is automatically selected to be ‘2’. For the SLA modules, the user can select one of the opcodes ‘3’, ‘4’, or ‘5’.
The data fields are updated with the values returned by the slaves, or with one of the following error messages:
Timeout – when the slave has failed to answer within a specified time interval. NAK – when the slave returned a NAK type packet.
CRC Error – when the packet received has the wrong CRC.
A snapshot of the main screen presented by this program is shown in Fig. 13.7.
172 13 Simple RS485 Network with Microcontrollers
Fig. 13.7. Snapshot for MASTER485.EXE
13.4 Exercises
X13.1
Write the software for the analog input module SLA.
X13.2
Based on the examples presented in this chapter, write a software application so that the AVR development board acts like two distinct slaves, one SLA and one SLD, having distinct addresses.
14
PI Temperature Controller
14.1 In this Chapter
This chapter is an introduction to the basic principles of control systems. It also contains a description of a didactic implementation of a PI temperature controller that uses the HC11 development board described in Chap. 9.
14.2 Basic Concepts
A control system is a system comprising physical and decisional elements, designed to control (to interferewith, to influence, to modify) a process.
In the classic example of a switch that controls an electric heater, the human operator has both decision and execution functions. A system where the intervention of a human operator is required is called manual control. If in this example the human operator is replaced by a time relay that switches the heater on and off at predetermined time intervals, the system becomes an automatic sequential system.
This system does not check whether the controlled heater actually produces heat, and the temperature of the environment does not influence the time relay. When the interaction between the control system and the controlled process is unidirectional, the control system is called an open-loop control system (see Fig. 14.1) .
Open-loop control systems are often associated with manual control. Most automatic control systems have at least one active feedback loop which allows the system to evaluate the response of the controlled process and adjust the control action, so that the value of a controlled variable is maintained close to a set-point value. The general block diagram of a closed-loop control system is shown in Fig. 14.2.
CONTROL Vout PLANT
Fig. 14.1. Block diagram of an open-loop control system
174 14 PI Temperature Controller
Disturbances
Vs
Vm CONTROL Vout PLANT
SENSOR
Fig. 14.2. General structure of a closed-loop control system
In this configuration, the sensor measures the value Vm of the process variable (which can be temperature, pressure, speed, flow, pH, etc.) and submits it to the control unit, which compares it to the desired value, or set-point value Vs, and adjusts the output Vout to reduce the error e = Vs − Vm.
Figure 14.3 describes the simplest control algorithm, where the controlled process variable is the temperature. The output of the control circuit Vout turns the heating element on, when the measured temperature Tm is lower than the set-point temperature Ts, and off, when Tm > Ts.
In practice, the turn-on (T1) and turn-off (T2) threshold temperatures are deliberately made to differ by a small amount (called hysteresis), to prevent switching of the heating element rapidly and unnecessarily, when the measured temperature Tm is close to the set-point value Ts (refer to Fig. 14.4). Most domestic thermostats use this control algorithm.
The problem with the on–off control systems is that the fluctuations of the controlled process variable are often too large to be acceptable. A better solution, from this point of view, is proportional control. In this case, the output of the control circuit Vout adjusts the power applied to the heater in proportion to the error signal
Tm
Ts
t
Vout
t
Fig. 14.3. Waveforms for the on–off control
Tm
T2
Ts
T1
t
Vout
t
Fig. 14.4. On–Off control with hysteresis