PLC With Pic16F648A: Microcontroller (PART 1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Feature

PLC/MCU

PLC WITH PIC16F648A MICROCONTROLLER (PART 1)


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

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
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 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 (www.microchip.com).

www.electronicsworld.co.uk

Electronics World - November 08

21

PLC/MCU

Feature

Figure 1: The schematic diagram of the UZAM_PLC main board

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

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
www.electronicsworld.co.uk

22

November 08 - Electronics World

Feature

PLC/MCU

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

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
www.electronicsworld.co.uk

Electronics World for 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 off the 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.

PROGRAMMING PICS
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
Electronics World - November 08

23

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 (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

Feature

PLC/MCU

DATA INPUT
The serial data coming from the I/O extension board is taken from the SI input of 74HC/LS165. The outputs section introduces 8 discrete relay outputs for the UZAM_PLC, called Q0.0, Q0.1, ..., Q0.7. Each relay operates with 12V DC and is driven by an 8-bit serial to parallel converter register/driver TPIC6B595. Relays have SPDT (single pole double throw) contacts with C (common), NC (normally closed) and NO (normally open) terminals. At the end of each PLC scan cycle (send_outputs) the output data is serially clocked out (through RB3 clock out and RB4 data out pins) and finally latched within TPIC6B595. If there is no I/O extension board used, then 8 clock_out signals are enough to send the 8 output signals. For each I/O extension board, 8 more clock_out signals are necessary. The serial data going to the I/O extension board is sent out from the SER OUT (pin 18) of TPIC6B595. In addition, there is also an I/O extension connector DB9F for conveying the I/O data to and from all the connected extension I/O boards. The PCB design files of the main board can be received from the editorial offices of Electronics World. Note that in the PCB design of the main board, some lines of I/O extension connector are different from the ones shown in Figure 1.

As shown in Figure 2, the I/O extension board contains mainly two sections: discrete inputs (8 of them) and discrete outputs (also 8 of them). They are similar to the ones as described for the main board. The I/O extension connector DB9M seen on the left connects the I/O extension board to the main board or to a previous I/O extension board. Similarly, the I/O extension connector DB9F seen on the right connects the I/O extension board to an adjacent I/O extension board. In this way we can connect as many I/O extension boards as necessary. The 12V DC power is taken from the main board or from a previous I/O extension board and it is passed to the next I/O extension boards. The 5V DC voltage is taken from the main board or from a previous I/O extension board through the DB9M connector and it is passed on to the next I/O extension boards through the DB9F connector. The PCB design files of the I/O extension board can be had from the editorial offices of the magazine. Note that in the PCB design of the I/O extension board, some lines of I/O extension connectors DB9M and DB9F are different from the ones shown in Figure 2. Figure 3 is a selection of photos of the UZAM_PLC main board and I/O extension board with and without a PIC programmer.

www.electronicsworld.co.uk

Electronics World - November 08

25

You might also like