Docsity

UZAM PLC EW 22 articles, Notas de estudo de Automação

Toggle sidebar
bg1
PLC WITH PIC16F648A
MICROCONTROLLER
(PART 1)
PLC/MCU
www.electronicsworld.co.uk Electronics World - November 08 ❙21
Feature
The Series
The articles to be published are as
follows:
1. The Hardware: Run in this issue.
2. The Basic Software: This article explains the basic software
structure of the UZAM_PLC. A PLC scan cycle includes the following:
1. obtain the inputs, 2. run the user program, 3. update the outputs.
In addition, it is also necessary to define and initialise all variables
used within a PLC. Necessary functions are all described as PIC
Assembly macros to be used in UZAM_PLC. The macros described in
this article could be summarised as follows: “HC165” (for handling the
inputs), “HC595” (for sending the outputs), “dbncr” (for debouncing the
inputs), “initialise”, “get_inputs”, “send_outputs”.
3. Contact and Relay Based Macros: The following contact and relay
based macros are described in this article: ld (load), ld_not (load_not),
not, or, or_not, nor, and, and_not, nand, xor, xor_not, xnor, out, out_not,
in_out, inv_out, set, reset. These macros are defined to operate on one
bit variables.
4. Flip-Flop Based Macros: The following flip-flop based macros are
described in this article: r_edge (rising_edge), f_edge (falling_edge),
latch0, latch1, dff_r (rising edge triggered D flip-flop), dff_f (falling
edge triggered D flip-flop), tff_r (rising edge triggered T flip-flop), tff_f
(falling edge triggered T flip-flop), jkff_r (rising edge triggered JK flip-
flop), jkff_f (falling edge triggered JK flip-flop).
5. Timer Macros: The following timer macros are described in this
article: TON_8 (ON delay timer),TOF_8 (OFF delay timer), TEP_8
(Extended Puls timer), TOS_8 (Oscillator timer).
6. Counter Macros: The following counter macros are described in this
article: CTU_8 (Up Counter), CTD_8 (Down Counter), CTUD_8 (Up/Down
Counter).
7. Comparison Macros: The following comparison macros are
described in this article: The contents of two registers (R1 and R2) are
compared according to the following: GT (Greater Than – “>”), GE
(Greater than or Equal to – “≥”), EQ (EQual to – “=”), LT (Less Than –
“<”), LE (Less than or Equal to – “≤”), NE (Not Equal to – “≠”). Similar
comparison macros are also described for comparing the contents of
an 8 bit register (R) with an 8 bit constant (K).
8. Arithmetical Macros: The following arithmetical macros are
described in this article: The following operators are applied to the
contents of two registers (R1 and R2): ADD, SUB (subtract), INC
(increment), DEC (decrement). Similar arithmetical macros are also
described to be used with the contents of an 8 bit register (R) and an 8
bit constant (K).
9. Logical Macros: The following logical macros are described in this
article: inv_R, AND, NAND, OR, NOR, XOR, XNOR.These macros are
applied to an 8 bit register (R1) with another register (R2) or an 8 bit
constant (K).
10. Shift&Rotate Macros: The following Shift&Rotate macros are
described in this article: SHIFT_R (shift right the contents of register R),
SHIFT_L (shift left the contents of register R), ROTATE_R (rotate right
the contents of register R), ROTATE_L (rotate left the contents of
register R), SWAP (swap the nibbles of the register).
11. Multiplexer Macros: The following multiplexer macros are
described in this article: mux_2_1 (2x1 MUX), mux_2_1_E (2x1 MUX
with Enable input), mux_4_1 (4x1 MUX), mux_4_1_E (4x1 MUX with
Enable input), mux_8_1 (8x1 MUX), mux_8_1_E (8x1 MUX with Enable
input).
12. DeMultiplexer Macros: The following demultiplexer macros are
described in this article: Dmux_1_2 (1x2 DMUX), Dmux_1_2_E (1x2
DMUX with Enable input), Dmux_1_4 (1x4 DMUX), Dmux_1_4_E (1x4
DMUX with Enable input), Dmux_1_8 (1x8 DMUX), Dmux_1_8_E (1x8
DMUX with Enable input).
13. Decoder Macros: The following decoder macros are described in
this article: decod_1_2 (1x2 decoder), decod_1_2_E (1x2 decoder with
Enable input), decod_2_4 (2x4 decoder), decod_2_4_E (2x4 decoder
with Enable input), decod_3_8 (3x8 decoder), decod_3_8_E (3x8
decoder with Enable input).
14. Priority Encoder Macros: The following priority encoder macros
are described in this article: encod_4_2_p (4x2 priority encoder),
encod_4_2_p_E (4x2 priority encoder with Enable input), encod_8_3_p
(8x3 priority encoder), encod_8_3_p_E (8x3 priority encoder with
Enable input), encod_dec_bcd_p {decimal to BCD (Binary Coded
Decimal) priority encoder}, encod_dec_bcd_p_E (decimal to BCD
priority encoder with Enable input).
15. An Example: This article describes an example of a remotely-
controlled model gate system and makes use of UZAM_PLC to control
it for different control scenarios.
The reader is expected to be familiar with PLCs, PIC microcontrollers
and PIC Assembly language. UZAM_PLC macros are developed by using
PIC Assembly language. The program development environment used is
that of MPLAB IDE (www.microchip.com).
ASSOCIATE PROFESSOR DR MURAT UZAM
FROM NIGDE UNIVERSITY IN TURKEY
KICK-STARTS A SERIES OF ARTICLES ON A
PROJECT THAT FOCUSES ON A
MICROCONTROLLER-PLC
UZAM 16/9/08 10:25 Page 36
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d

Pré-visualização parcial do texto

Baixe UZAM PLC EW 22 articles e outras Notas de estudo em PDF para Automação, somente na Docsity!

PLC WITH PIC16F648A

MICROCONTROLLER

(PART 1)

PLC/MCU

www.electronicsworld.co.uk Electronics World - November 08 ❙ 21

Feature

The Series

The articles to be published are as follows:

  1. The Hardware: Run in this issue.
  2. The Basic Software: This article explains the basic software structure of the UZAM_PLC. A PLC scan cycle includes the following:
  3. obtain the inputs, 2. run the user program, 3. update the outputs. In addition, it is also necessary to define and initialise all variables used within a PLC. Necessary functions are all described as PIC Assembly macros to be used in UZAM_PLC. The macros described in this article could be summarised as follows: “HC165” (for handling the inputs), “HC595” (for sending the outputs), “dbncr” (for debouncing the inputs), “initialise”, “get_inputs”, “send_outputs”.
  4. Contact and Relay Based Macros: The following contact and relay based macros are described in this article: ld (load), ld_not (load_not), not, or, or_not, nor, and, and_not, nand, xor, xor_not, xnor, out, out_not, in_out, inv_out, set, reset. These macros are defined to operate on one bit variables.
  5. Flip-Flop Based Macros: The following flip-flop based macros are described in this article: r_edge (rising_edge), f_edge (falling_edge), latch0, latch1, dff_r (rising edge triggered D flip-flop), dff_f (falling edge triggered D flip-flop), tff_r (rising edge triggered T flip-flop), tff_f (falling edge triggered T flip-flop), jkff_r (rising edge triggered JK flip- flop), jkff_f (falling edge triggered JK flip-flop).
  6. Timer Macros: The following timer macros are described in this article: TON_8 (ON delay timer), TOF_8 (OFF delay timer), TEP_ (Extended Puls timer), TOS_8 (Oscillator timer).
  7. Counter Macros: The following counter macros are described in this article: CTU_8 (Up Counter), CTD_8 (Down Counter), CTUD_8 (Up/Down Counter).
  8. Comparison Macros: The following comparison macros are described in this article: The contents of two registers (R1 and R2) are compared according to the following: GT (Greater Than – “>”), GE (Greater than or Equal to – “≥”), EQ (EQual to – “=”), LT (Less Than – “<”), LE (Less than or Equal to – “≤”), NE (Not Equal to – “≠”). Similar comparison macros are also described for comparing the contents of an 8 bit register (R) with an 8 bit constant (K).
  9. Arithmetical Macros: The following arithmetical macros are described in this article: The following operators are applied to the contents of two registers (R1 and R2): ADD, SUB (subtract), INC (increment), DEC (decrement). Similar arithmetical macros are also described to be used with the contents of an 8 bit register (R) and an 8 bit constant (K).
    1. Logical Macros: The following logical macros are described in this article: inv_R, AND, NAND, OR, NOR, XOR, XNOR. These macros are applied to an 8 bit register (R1) with another register (R2) or an 8 bit constant (K).
    2. Shift&Rotate Macros: The following Shift&Rotate macros are described in this article: SHIFT_R (shift right the contents of register R), SHIFT_L (shift left the contents of register R), ROTATE_R (rotate right the contents of register R), ROTATE_L (rotate left the contents of register R), SWAP (swap the nibbles of the register).
    3. Multiplexer Macros: The following multiplexer macros are described in this article: mux_2_1 (2x1 MUX), mux_2_1_E (2x1 MUX with Enable input), mux_4_1 (4x1 MUX), mux_4_1_E (4x1 MUX with Enable input), mux_8_1 (8x1 MUX), mux_8_1_E (8x1 MUX with Enable input).
    4. DeMultiplexer Macros: The following demultiplexer macros are described in this article: Dmux_1_2 (1x2 DMUX), Dmux_1_2_E (1x DMUX with Enable input), Dmux_1_4 (1x4 DMUX), Dmux_1_4_E (1x DMUX with Enable input), Dmux_1_8 (1x8 DMUX), Dmux_1_8_E (1x DMUX with Enable input).
    5. Decoder Macros: The following decoder macros are described in this article: decod_1_2 (1x2 decoder), decod_1_2_E (1x2 decoder with Enable input), decod_2_4 (2x4 decoder), decod_2_4_E (2x4 decoder with Enable input), decod_3_8 (3x8 decoder), decod_3_8_E (3x decoder with Enable input).
    6. Priority Encoder Macros: The following priority encoder macros are described in this article: encod_4_2_p (4x2 priority encoder), encod_4_2_p_E (4x2 priority encoder with Enable input), encod_8_3_p (8x3 priority encoder), encod_8_3_p_E (8x3 priority encoder with Enable input), encod_dec_bcd_p {decimal to BCD (Binary Coded Decimal) priority encoder}, encod_dec_bcd_p_E (decimal to BCD priority encoder with Enable input).
    7. An Example: This article describes an example of a remotely- controlled model gate system and makes use of UZAM_PLC to control it for different control scenarios.

The reader is expected to be familiar with PLCs, PIC microcontrollers and PIC Assembly language. UZAM_PLC macros are developed by using PIC Assembly language. The program development environment used is that of MPLAB IDE (www.microchip.com).

ASSOCIATE PROFESSOR DR MURAT UZAM

FROM NIGDE UNIVERSITY IN TURKEY

KICK-STARTS A SERIES OF ARTICLES ON A

PROJECT THAT FOCUSES ON A

MICROCONTROLLER-PLC

P

rogrammable Logic Controllers (PLCs) have been extensively used in industry for the past five or six decades. PLC manufacturers offer different devices in terms of functions, program memories and the number of inputs/outputs (I/O), ranging from a few to thousands of I/Os. The design and implementation of PLCs have long been a secret of the PLC manufacturers. As a microprocessor-based technology, the functionality of a PLC is well known from the end-user/programmer point of view, but by now no serious work has been reported to describe a microprocessor/microcontroller based implementation of a PLC. With a series of articles I aim to describe a PIC microcontroller based design and implementation of a PLC, called

UZAM_PLC with PIC16F648A. First in the series is this article, which describes the hardware of UZAM_PLC with PIC16F648A.

DESIGN AND
IMPLEMENTATION

This project has been completed in search for how to design and implement a PLC. Some ideas can be found in the freely available PLC project called “PICBIT” from around 10 years ago. An Internet search for the keyword “PICBIT” leads to this project. PICBIT describes a PIC16F microcontroller based PLC with five discrete inputs and eight discrete outputs. The file called “picbit.inc” of the PICBIT contains the basic PLC macro definitions. The UZAM_PLC project has been completed by the inspiration of these

macros. In addition, many new features have been included within UZAM_PLC project to make it more of an engulfing PLC. However, this project does not include graphical interface PC software as in PICBIT or in other PLCs for developing PLC programs. Rather, PLC programs are developed by using macros as done in the Instruction List (IL) PLC programming language. An interested and skilled reader could well – and encouraged to – develop graphical interface PC software for easy use of UZAM_PLC.

THE HARDWARE WITH
PIC16F648A

The hardware of UZAM_PLC with PIC16F648A consists of two parts: “main board” and “I/O extension board”. The schematic diagram of UZAM_PLC main

Figure 1: The schematic diagram of the UZAM_PLC main board

PLC/MCU

22 ❙ November 08 - Electronics World www.electronicsworld.co.uk

Feature

PLC/MCU Feature

register 74HC/LS165. Through these three pins and with added 74HC/LS165 registers we can describe as many inputs as we wish. RB0, RB6, and RB7 are the “data in”, the “clock in” and the “shift/load” pins, respectively. Similarly, RB3, RB4 and RB5 pins are all reserved to be used for 8-bit serial to parallel converter register/driver TPIC6B595. Through these three pins and with added TPIC6B595 registers we can describe as many outputs as we wish. RB3, RB4 and RB5 are the “clock out”, the “data out” and the “latch out” pins, respectively. The remaining unused pins of the PIC16F648A are connected to the “future extension connector”.

PIC16F648A provides the following: 4096 words of Flash program memory; 256 bytes of RAM data memory and 256 bytes of EEPROM data memory. The UZAM_PLC macros make use of registers defined in RAM data memory. Note that it may be possible to use PIC16F628A as the CPU, but one has to bear in mind that PIC16F628A provides the following: 2048 words of Flash program memory; 224 bytes of RAM data memory and 128 bytes of EEPROM data memory. In this case it is necessary to take care of the usage of RAM data memory. The inputs section introduces 8 discrete inputs for the UZAM_PLC, called I0.0, I0.1, ..., I0.7. Each input can accept 5V DC or 24V

DC signals. These external input signals are isolated from the other parts of the hardware by using NPN type opto-couplers (e.g. 4N25). For simulating input signals, one can use on board push-buttons as temporary inputs and slide switches as permanent inputs. In the beginning of each PLC scan cycle (get_inputs) 74HC/LS165 is loaded (RB (shift/load) = 0) with the level of 8 inputs and then this data is serially clocked in (when RB7 = 1; through RB0 “data in” and RB6 “clock in” pins). If there is no I/O extension board used, then 8 clock_in signals are enough to get the 8 input signals. For each I/O extension board, 8 more clock_in signals are necessary.

Figure 3: Photographs of (a) the UZAM_PLC main board, (b) UZAM_PLC I/O extension board, (c) UZAM_PLC main board with a PIC programmer, (d) UZAM_PLC main board plus an I/O extension board and a USB PIC programmer, (e) UZAM_PLC main board plus two I/O extension boards and a USB PIC programmer

A
D E
B C

T

he basic software of this programmable logic controller (PLC) called UZAM_PLC makes use of general purpose 8-bit registers of RAM data memory of PIC16F648A. For the sake of simplicity, we restrict ourselves to use only BANK 0, i.e. all macros, including the basic definitions explained here, to be defined by means of 8-bit registers of BANK 0. The file “definitions.inc” (all the files explained here including “definitions.inc” can be downloaded from http://host.nigde.edu.tr/muzam /). It contains all of the basic macros and definitions necessary for UZAM_PLC. In this article we will explain the contents of this file. First of all, let’s have a look at the file called “UZAM_plc_8i8o_1.asm” shown in Figure 1. It is well-known that

the PLC’s scan cycle includes the following: obtain the inputs, run the user program, update the outputs. This cycle repeats as long as the PLC runs. Before getting into the endless PLC scan cycles, the initial conditions of the PLC are set up in the initialisation stage. These main steps can be seen from Figure 1, where “initialise” is a macro for setting up the initial conditions; “get_inputs” is a macro for getting the inputs; and “send_outputs” is a macro for updating the outputs. The “user PLC program” must be placed between

“get_inputs” and “send_outputs”. The endless PLC scan cycles are obtained by means of the label “scan” and the instruction “goto scan”. UZAM_PLC is fixed to run at 4MHz with PIC16F648A’s internal oscillator. The watchdog timer is used to prevent user- program lock-ups. As will be explained later, the hardware timer TMR0 is utilised to obtain free-running reference timing signals.

FILE DEFINITIONS

Next, let’s consider the inside of the file “definitions.inc”. The definition of 8-bit variables to be used for the basic software and their allocation in BANK 0 of RAM data memory are shown in Figure 2a and 2b respectively. Although we can define as many inputs and outputs as we want, for the sake of simplicity we define four 8-bit input registers and four 8-bit output registers (Q0, Q1, Q2, Q3). It is well-known that inputs taken from

PLC/MCU

www.electronicsworld.co.uk

Feature

Figure 1: The view of the file “UZAM_plc_8i8o_1.asm”

PLC WITH PIC16F648A

MICROCONTROLLER

PART 2 ASSOCIATE PROFESSOR^ DR MURAT UZAM^ FROM NIGDE

UNIVERSITY IN TURKEY PRESENTS A SERIES OF ARTICLES ON A PROJECT THAT FOCUSES ON A MICROCONTROLLER-

BASED PLC. THIS IS THE SECOND ARTICLE OF THE SERIES

AND IT COVERS THE BASIC SOFTWARE STRUCTURE OF

THE UZAM_PLC

It is also necessary to define and initialise all variables used within a PLC. Necessary functions are all described as PIC Assembly macros to be used in UZAM_PLC. The macros described in this article could be summarised as follows: “HC165” (for handling the inputs), “HC595” (for sending the outputs), “dbncr” (for debouncing the inputs), “initialise”, “get_inputs”, “send_outputs”.

Electronics World - December 08 ❙ 29

The low byte of free-running timing signals is stored in TMR0 (recalled as Timer_1). For accessing the RAM data memory easily, BANK macros are defined as shown in Figure 3. The definitions of one bit variables are depicted in Figure 4. The following definitions are self explanatory: 74HC165, TPIC6B595, 8 INPUTS, 8 OUTPUTS, 32 Memory Bits. Let us now have a look at the others.

OTHER VARIABLES

The variable “LOGIC0” is defined to hold logical “0” value throughout the PLC operation. At the initialisation stage it is deposited with this value. Similarly, the variable “LOGIC1” is defined to hold logical “1” value throughout the PLC operation. At the initialisation stage it is deposited with this value. The special memory bit “FRSTSCN” is arranged to hold the value of “1” at the first PLC scan cycle only. In the other PLC scan cycles following the first one it is reset. The other special memory bit “SCNOSC” is arranged to work as a “scan oscillator”. This means that in one PLC scan cycle this special bit will hold the value of “0”, in the next one the value of “1”, in the next one the value of “0” etc. This will keep on going for every PLC scan cycle. Let us now consider the 16 reference timing signals. As seen later, TMR0 of PIC16F648A is set up to count the 1 / 4 of 4MHz internal oscillator signal, i.e. 1MHz with a prescaler arranged to divide the signal to 256. As a result by means of TMR0 bits (also called Timer_1) we obtain eight free-running reference timing signals with the “T” timing periods starting from 0.512ms to 65.536ms. We’ll see later that the register “Timer_2” is incremented on Timer_ overflow. This also gives us (by means of Timer_2 bits) eight more free-running reference timing signals with the “T” timing periods starting from 131.072ms to

16777.216ms. Timing diagram of the free-running reference timing signals is depicted in Figure 5. Note that the evaluation of TMR0 (Timer_1) is independent from PLC scan cycles, but Timer_2 is incremented within the “get_inputs” stage of PLC scan cycle on Timer_1 overflow. This is justified as long as the PLC scan cycle takes less than 65.536ms.

THE MACROS

The macro “HC165” is shown in Figure 6. The input signals are serially taken from the related 74HC/LS165 registers and stored in the registers such as bI0, bI1, bI and bI3 by means of this macro. The “num” defines the number of 74HC/LS165 registers to be considered. This means that with this macro we can obtain inputs from as many 74HC/LS registers as we wish. However, as explained before, we restrict this number to be 4 at most for the sake of simplicity; “var0” is the beginning of the registers to which the state of inputs taken from 74HC/LS165 registers will be stored. This implies that there should be enough RAM locations reserved after “var0” and, also, there should be enough 74HC/LS165 registers to get the inputs from. There are some explanations within the macro to describe how it works. As can be seen, this macro makes use of previously defined “data_in”, “clock_in” and “sfht_ld” bits to obtain the input

PLC/MCU

www.electronicsworld.co.uk Electronics World - December 08 ❙ 31

Feature

Figure 6: The macro “HC165”

Figure 4: Definitions of one-bit variables

Figure 5: Timing diagram of the free- running reference timing signals (T = 0.512, 1.024... 16777.216ms)

PLC/MCU Feature

signals from 74HC/LS165 registers. The macro “HC595” is shown in Figure 7. The output signals are stored in the 8- bit registers such as Q0, Q1, Q2 and Q3 in

RAM locations, and serially sent out to and stored in the related TPIC6B595 registers by means of this macro. The “num” defines the number of TPIC6B595 registers

to be used. This means that with this macro we can send output data serially to as many TPIC6B595 registers as we wish. However, as explained before, we restrict this number to be 4 at most for the sake of simplicity; “var0” is the beginning of the 8-bit registers such as Q0 in RAM from which the state of outputs are taken and serially sent to TPIC6B595 registers. This implies that there should be enough RAM locations reserved after “var0” and, also, there should be enough TPIC6B registers to hold the outputs. There are some explanations within the macro to describe how it works. As can be seen, this macro makes use of previously defined “data_out”, “clock_out” and “latch_out” bits to send the output signals serially to TPIC6B595 registers. The macro “dbncr” is shown in Figure 8. It can be used for debouncing eight independent buttons, switches, relay or contactor contacts etc. The timing diagram of one channel of this debouncer is provided in Figure 9. It can be seen that the output changes its state only after the input becomes stable and waits in the stable state for the predefined debouncing time “dt1” or “dt2”. The debouncing is applied to both rising and falling edges of the input signal. In this macro, each channel is intended for a “normally open contact” connected to the PIC by means of a pull-down resistor, as this is the case with UZAM_PLC. It can also be used without any problem for a “normally closed contact” connected to the PIC by means of a pull-up resistor. The “debouncing times” such as 20ms, 50ms or 100ms can be selected as required depending on the application. It is possible to pick up different debouncing times for each channel. It is also possible to choose different debouncing times for rising and falling edges of the same input signal, if necessary. This gives a good deal of flexibility. This is simply done by chancing the related time constant “tcnst_01” or “tcnst_10” defining the debouncing time delay for each channel and for both edges within the assembly program. Note that if the state-change-of-the- contact is shorter than the predefined “debouncing time”, this will also be regarded as bouncing and it will not be taken into account. Therefore, no state-

32 ❙ December 08 - Electronics World www.electronicsworld.co.uk

Figure 7: The macro “HC595”

Figure 8: The macro “dbncr”

loaded with the following initial values: LOGIC0 (Temp_2,0) = 0, LOGIC (Temp_2,1) = 1, FRSTSCN (Temp_2,2) = 1, SCNOSC (Temp_2,3) = 0.

THE MACRO “GET_INPUTS”

The macro “get_inputs” is shown in Figure 11. There are mainly three tasks carried out within this macro. In the first one, the macro “HC165” is called with the parameters “.1” and “bI0”. This means that we will use only the “main board” and, therefore, the macro “HC165” is called with the parameter “.1”. As explained before, the input information taken from the macro is rated as “bouncing” information and, therefore, this information is stored in “bI0” register. For example if we decide to use the “main board” connected to three of the “I/O extension board” then we must call the macro “HC165” as follows: “HC165 .4,bI0”. This will take four 8-bit bouncing input information from the 74HC/LS165 ICs and will put them to the four successive registers starting with the register bI0. In the second task within this macro, each bit of bI0, i ( i = 0, 1… 7) is debounced by the macro “dbncr” and each debounced input signal is stored in the related bit I0, i ( i = 0, 1… 7). In general, 20ms time delay is enough for debouncing both rising and falling edges of an input signal. Therefore, to achieve these time delays, the reference timing signal, obtained from Timer_1, is chosen as T00 (0.512ms period) and both “tcnst_01” and “tcnst_10” are chosen to be “40”. Then we obtain the following: dt1 = T00 x tcnst_01 = (0.512ms) x 40 = 20.48ms, dt2 = T00 x tcnst_10 = (0.512ms) x 40 = 20.48ms. Note that in this series of articles for the sake of simplicity we use just 8 inputs and 8 outputs. If we want to add more inputs and to debounce these inputs, then we must organise the macro “dbncr” and locate necessary number of registers within the RAM. The last task is about incrementing the Timer_2 on overflow of Timer_1. In this task, “Timer_2” is incremented by one when the falling edge of the bit “Timer_1,7” is detected. In order to detect the falling edge of the bit “Timer_1,7”, “Temp_2,4” bit is utilised.

PLC/MCU Feature

Figure 11: The macro “get_inputs”

Figure 12: The macro “send_outputs”

Figure 13: A fraction of the first example program “UZAM_plc_8i8o_ex1.asm”

Figure 14: A fraction of the second example program “UZAM_plc_8i8o_ex2.asm”

34 ❙ December 08 - Electronics World www.electronicsworld.co.uk

PLC/MCU Feature

THE MACRO “GET_OUTPUTS”

The macro “send_outputs” is shown in Figure 12. There are mainly four tasks carried out within this macro. In the first one, the macro “HC595” is called with the parameters “.1” and “Q0”. This means that we will use only the “main board” and, therefore, the macro “HC595” is called with the parameter “.1”. As explained before, the output information is taken from the register Q and this macro sends the bits of Q0 serially to TPIC6B595 register. For example, if we decide to use the “main board” connected to three of the “I/O extension board” then we must call the macro “HC595” as follows: “HC595 .4,Q0”. Then, the macro “HC595” will take four 8-bit output information stored in Q3, Q2, Q1 and Q and send them serially to the four TPIC6B595 register ICs respectively. In the second task within this macro, the watchdog timer is cleared. In the third task, the “FRSTSCN” special memory bit is reset. As the final task, within this macro the “SCNOSC” special memory bit is toggled after a program scan, i.e. when it is “1” it is reset, when it is “0” it is set.

EXAMPLES

Up to now we have seen the hardware and basic software necessary for UZAM_PLC. It is now time to consider a few examples. Before you can run the two simple examples considered here, you are expected to construct your own UZAM_PLC hardware by using the necessary PCB files and producing PCBs with its components. For the first example, include the user program shown in Figure 13 within the “UZAM_PLC_8i8o_1.asm” and then save it as “UZAM_PLC_8i8o_ex1.asm”. The next thing to do is to open it by MPLAB IDE and to compile it. After that, by using the PIC programmer software take the compiled file “UZAM_PLC_8i8o_ex1.hex” and by your PIC programmer hardware, send it to the program memory of PIC16F648A microcontroller within the UZAM_PLC. After loading the “UZAM_PLC_8i8o_ex1.hex”, switch the 4PDT in “RUN” and the power switch in “ON” position. Now, you are ready to test the first example program. You can see that all the inputs are transferred to the

respected outputs. That is to say that if I0.0 = 0 then Q0.0 = 0 and, similarly, if I0.0 = 1 then Q0.0 = 1. This applies to all 8 inputs and 8 outputs. For the second example, please include the user program shown in Figure 14 within the “UZAM_PLC_8i8o_1.asm” and then save it as “UZAM_PLC_8i8o_ex2.asm”. Next open it by MPLAB IDE and compile it. After that by using the PIC programmer software take the compiled file “UZAM_PLC_8i8o_ex2.hex” and by your PIC programmer hardware send it to the program memory of PIC16F648A microcontroller within the UZAM_PLC. After loading the “UZAM_PLC_8i8o_ex2.hex”, switch the 4PDT in “RUN” and the power switch in “ON” position. Now, you are ready to test the second example program. In this case the contents of “Timer_2” register are transferred to 8 outputs. Of course, in these two examples the user programs include only PIC Assembly instructions, but we will start using our own macro-based PLC instructions in the next article. ■

PLC/MCU

Microcontroller Part 3

Table 1: Contact and relay-based macros Continues on the next page

www.electronicsworld.co.uk ❙ 31

PLC/MCU

32 ❙ January 09 - Electronics World

Let us now consider these four example programs. The first example program, “UZAM_plc_8i8o_ex3.asm”, is shown in Figure 1. It shows the usage of the following contact and relay-based macros: ld, ld_not, not, out, out_not, in_out, inv_out. The schematic and ladder diagrams of the program are depicted in Figures 2a and 2b. The second example program, “UZAM_plc_8i8o_ex4.asm”, is shown in Figure 3. It shows the usage of the following contact and relay-based macros: ld, out, or, or_not, nor. The schematic and ladder diagrams of this program are depicted in Figures 4a and 4b.

Continues from the previous page Table 1: Contact and relay-based macros

Figure 1: The user program of UZAM_plc_8i8o_ex3.asm

PLC/MCU

34 ❙ January 09 - Electronics World

Figure 5: The user program of UZAM_plc_8i8o_ex5.asm

Figure 7: The user program of UZAM_plc_8i8o_ex6.asm

Figure 8: (a) above; Schematic diagram and (b) below; Ladder diagram for the user program Figure 6: of UZAM_plc_8i8o_ex6.asm (a) left; Schematic diagram and (b) right; Ladder diagram for the user program of UZAM_plc_8i8o_ex5.asm

PLC/MCU

34 ❙ February 09 - Electronics World

IN THIS ARTICLE , the following

flip-flop based macros are described: r_edge (rising edge detector), f_edge (falling edge detector), latch0, latch1, dff_r (rising edge triggered D flip-flop), dff_f (falling edge triggered D flip-flop), tff_r (rising edge triggered T flip-flop), tff_f (falling edge triggered T flip-flop), jkff_r (rising edge triggered JK flip-flop), jkff_f (falling edge triggered JK flip-flop). Each macro defined here requires an edge detection mechanism, except for latch0 and latch1. The following 8-bit variables are used for this purpose: RED − Rising Edge Detector, FED − Falling Edge Detector, DFF_RED − Rising Edge Detector for D flip-flop, DFF_FED − Falling Edge Detector for D flip-flop, TFF_RED − Rising Edge Detector for T flip-flop, TFF_FED − Falling Edge Detector for T flip-flop, JKFF_RED − Rising Edge Detector for JK flip-flop, JKFF_FED − Falling Edge Detector for JK flip-flop. They are declared within the RAM data memory as shown in Figure 1. Each 8-bit variable enables us to declare and use 8 different functions defined by the related macro. The macros latch0 and latch1 are

exception for this, which means that we can use as many latches of latch0 or latch1 as we wish. The file “ff_mcr_def.inc” (all the files considered

in this article including “ff_mcr_def.inc” can be downloaded from ( http://host.nigde.edu.tr/muzam/ ) contains all flip-flop based macros defined for UZAM_PLC.

Macro “r_edge” (Rising Edge Detector) The macro “r_edge” defines eight rising edge detector functions (or contacts), selected with the num = 0, 1...7. It has a Boolean input variable, namely IN, passed into the macro through “W” and a Boolean output variable, namely OUT, passed out through “W”. This means that the input signal IN should be loaded into W before this macro is run, and the output signal OUT will be provided within

Associate Professor Dr Murat Uzam from Nigde University in Turkey presents a

series of articles on a project that focuses on a microcontroller-based PLC. This is

the fourth article of the series describing the flip-flop based macros

PLC with PIC16F648A Micro

Figure 1: (a) The definition of 8-bit variables to be used for the flip-flop based macros (b) Their allocation in BANK 0 of RAM data memory

Table 1: The macro “r_edge” (rising edge detector), symbols and timing diagram of “r_edge”

PLC/MCU

36 ❙ February 09 - Electronics World

detector macros, in which eight functions are described, this function defines only one D latch function. However, we are free to use this macro as many times as we need, with different input/output variables. The macro “latch1” has two Boolean input variables, namely EN: passed into the macro through “W” and D: regi,biti; and a single Boolean output variable, Q: rego,bito. The input signal EN (active high enable input) should be loaded into W before this macro is run. Table 3 shows the macro “latch1”, together with its symbol and truth table. When the active high enable input EN is OFF (0), no state change is issued for the

output Q and it holds its current state. When the active high enable input EN is ON (1), the output Q is loaded with the state of the input D.

Macro “latch0” (D Latch with Active Low Enable) The macro “latch0” defines a D latch function with active low enable. Unlike the edge triggered flip-flops and the edge detector macros, in which eight functions are described, this function defines only one D latch function. However, we are free to use this macro as many times as needed, with different input/output variables. The macro “latch0” has two Boolean

input variables, namely EN: passed into the macro through “W” and D: regi,biti; and a single Boolean output variable, Q: rego,bito. The input signal EN (active low enable input) should be loaded into W before this macro is run. Table 4 shows the macro “latch0”, together with its symbol and truth table. When the active low enable input EN is ON (1), no state change is issued for the output Q and it holds its current state. When the active low enable input EN is OFF (0), the output Q is loaded with the state of the input D.

Macro “dff_r” (Rising Edge Triggered D Flip-Flop) The macro “dff_r” defines eight rising edge triggered D flip-flop functions, selected with the num = 0, 1...7. It has two Boolean input variables, namely clock input C: passed into the macro through “W” and data input D: regi,biti; and a single Boolean output variable, flip-flop output Q: rego,bito. The clock input signal C should be loaded into W before this macro is run. Table 5 shows the macro “dff_r”, together with its symbol and truth table. When the clock input signal C is ON (1), or OFF (0), or changes state from ON to OFF ( ↓ ), no state change is issued for the output Q and it holds its current state. When the state of clock input signal C is changed from OFF to ON ( ↑ ), the output Q is loaded with the state of the input D.

Macro “dff_f” (Falling Edge Triggered D Flip-Flop) The macro “dff_f” defines eight falling edge triggered D flip-flop functions selected with the num = 0, 1...7. It has two Boolean input variables, namely clock input C: passed into the macro through “W” and data input D: regi,biti; and a single Boolean output variable, flip-flop output Q: rego,bito. The clock input signal C should be loaded into W before this macro is run. Table 6 shows the macro “dff_f”,

Table 5: The macro “dff_r” (rising edge triggered D flip-flop), and the symbol and truth table of “dff_r”

Table 4: The macro “latch0” (D latch with active low enable), and the symbol and truth table of “latch0”

PLC/MCU

together with its symbol and truth table. When the clock input signal C is ON (1), or OFF (0), or changes state from ON to OFF ( ↑ ), no state change is issued for the output Q and it holds its current state. When the state of clock input signal C is changed from ON to OFF ( ↓ ), the output Q is loaded with the state of the input D.

Macro “tff_r” (Rising Edge Triggered T Flip-Flop) The macro “tff_r” defines eight rising edge triggered T flip-flop functions, selected with the num = 0, 1...7. It has two Boolean input variables, namely clock input C: passed into the macro through “W” and toggle input T: regi,biti; and a single Boolean output variable, flip-flop output Q: rego,bito. The clock input signal C should be loaded into W before this macro is run. Table 7 shows the macro “tff_r”, together with its symbol and truth table. When the clock input signal C is ON (1), or OFF (0), or changes state from ON to OFF ( ↓ ), no state change is issued for the output Q and it holds its current state. When the state of clock input signal C is changed from OFF to ON ( ↑ ), if T = 0, then no state change is issued for the output Q and it holds its current state. When the state of clock input signal C is changed from OFF to ON ( ↑ ), if T = 1, then the output Q is toggled.

Macro “tff_f” (Falling Edge Triggered T Flip-Flop) The macro “tff_f” defines eight falling edge triggered T flip-flop functions selected with the num = 0, 1...7. It has two Boolean input variables, namely clock input C: passed into the macro through “W” and toggle input T: regi,biti; and a single Boolean output variable, flip-flop output Q: rego,bito. The clock input signal C should be loaded into W before this macro is run. Table 8 shows the macro “tff_f”, together with its symbol and truth table. When the clock input signal C is ON (1),

or OFF (0), or changes state from OFF to ON ( ↑ ), no state change is issued for the output Q and it holds its current state. When the state of clock input signal C is changed from ON to OFF ( ↓ ) then if T = 0 no state change is issued for the output Q; if T = 1, then the output Q is toggled.

Macro “jkff_r” (Rising Edge Triggered JK Flip-Flop) The macro “jkff_r” defines eight rising edge triggered JK flip-flop functions, selected with the num = 0, 1...7. It has three Boolean input variables, namely clock input C: passed into the macro through “W”, data inputs J: regj,bitj and K: regk,bitk; and a single Boolean output

www.electronicsworld.co.uk ❙ 37

Table 6: The macro “dff_f” (falling edge triggered D flip-flop), and its symbol and truth table of “dff_f”

Table 7: The macro “tff_r”(rising edge triggered T flip-flop), and the symbol and truth table of “tff_r”