(Parte 1 de 3)


MICROCONTROLLER (PART 1) w.electronicsworld.co.ukElectronics World - November 08❙21


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 flipflop),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). 1.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 remotelycontrolled 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 (w.microchip.com).

UZAM 16/9/08 10:25 Page 36

Programmable 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.

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 PIC16F84 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.

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

2❙November 08- Electronics Worldwww.electronicsworld.co.uk

Feature UZAM 16/9/08 10:25 Page 37

board is shown in Figure 1. The main board contains mainly five sections: power, programming, CPU (Central Processor Unit), inputs and outputs.

The power section accepts 12V AC input and produces as outputs 12V DC to be used as the operating voltage of relays, and 5 V DC to be used for ICs, inputs, etc. The programming section deals with the programming of PIC16F648A microcontroller. For programming the PIC16F648A in circuit, it is necessary to use a PIC programmer hardware and software with ICSP (In Circuit Serial Programming) capability. For those who have a PC with serial port, PCB files for a PIC programmer hardware, called JDM programmer, and related software can be downloaded (please contact the editorial office of

Electronics Worldfor details).

For other types of USB or parallel port

PIC programmers the reader is expected to make necessary arrangements. ICSP connector takes the lines VPP(MCLR), VDD, VSS(GND), DATA (RB7), CLOCK (RB6) from the PIC programmer hardware through a properly prepared cable and it serves them to a 4PDT (four pole double throw) switch.

There are two positions of the 4PDT switch. As seen in Figure 1, in one position of 4PDT switch, PIC16F648A is ready to be programmed and in the other position the loaded program is run. For properly programming the PIC16F648A by means of a PIC programmer and the 4PDT switch, it is also a necessity to switch offthe power switch. The CPU section consists of the

PIC16F648A microcontroller. Although it can run up to 20MHz, in UZAM_PLC it is fixed to run at 4MHz with its internal oscillator. This frequency is fixed because time-delays are calculated based on this speed. However, by means of two switches, namely SW1 and SW2, it is also possible to use an external oscillator with different crystal frequencies. When doing so, time-delay functions must be calculated accordingly. SW3 directs the RA5 pin either to one pole of 4PDT switch or to the future extension connector.

When programming PIC16F648A, RA5 should be connected to 4PDT switch. RB0, RB6 and RB7 pins are all reserved to be used for an 8-bit parallel to serial converter w.electronicsworld.co.ukElectronics World - November 08❙23

Feature Figure 2: The schematic diagram of the UZAM_PLC I/O extension board

UZAM 16/9/08 10:25 Page 38

FeaturePLC/MCU 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.

for the UZAM_PLC, called I0.0, I0.1,,

In this case it is necessary to take care of the usage of RAM data memory. The inputs section introduces 8 discrete inputs 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 (RB7 (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

UZAM 16/9/08 10:25 Page 39

The 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 userprogram 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 2aand 2brespectively. 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



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


(Parte 1 de 3)