See discussions, stats, and author profiles for this publication at: https://www.researchgate.
net/publication/291773586
PLC with PIC16F648A microcontroller (part 1)
Article  in  ELECTRONICS WORLD · November 2008
CITATION                                                                                               READS
1                                                                                                      7,456
1 author:
            Murat Uzam
            Yozgat Bozok University
            150 PUBLICATIONS   2,053 CITATIONS   
              SEE PROFILE
Some of the authors of this publication are also working on these related projects:
             An Image Interpreter for Vision-Based Logic Control of Manufacturing Plant View project
             Analog Input / Analog Output Modules for a 5V Microcontroller Based PLC View project
 All content following this page was uploaded by Murat Uzam on 20 August 2017.
 The user has requested enhancement of the downloaded file.
                                                              Volume 114, Issue 1871
                                                                9 770959 833110   11
                                                              NOVEMBER 2008 £4.60
                                                              www.electronicsworld.co.uk
THE ESSENTIAL ELECTRONICS ENGINEERING MAGAZINE
  Special Issue –
  Microcontrollers:
  • BUILD YOUR OWN DATA LOGGER
  • BUILD YOUR OWN MCU-BASED PLC
  • CHOOSING BETWEEN ARM7 AND CORTEX-M3
                                                             PURCHASING
                                                             NEW OSCILLOSCOPES?
                                                             CONSIDER THE UPDATE
                                                             RATES!
           TECHNOLOGY                    UKDL                        PRODUCTS
           LEARNING                RIGHT LASER             COMMS TEST SYSTEMS,
           FROM                        FOR THE                    POSTERS AND
           NATURE                    RIGHT JOB                      ANALYSERS
       ALSO IN THIS ISSUE: THE TROUBLE WITH RF: SO MANY TRANSMITTERS, SO SMALL A BAND
                                                                Electronics World                                                                                                       CONTENTS
 05 EDITOR’S COMMENT
                                                                                                                                                         Issue 1871
                                                                                                                                         Volume 114,
                                                                                                                                                                  11
                                                                                                                                               9 770959 833110
                                                                                                                                                   2008 £4.60
                                                                                                                                          NOVEMBER       world.co.uk
                                                                                                                                          www.electronics
      CALL FOR ARTICLES AND ARTICLE GUIDELINES                       THE ESSENT
                                                                               IAL ELECTR
                                                                                         ONICS ENGINE
                                                                                                         ERING MAGAZI
                                                                                                                          NE
                                                                                                                                                                                     Page 17 - the place
                                                                                       –
                                                                                                                                                                                     to find more about
 06 TECHNOLOGY                                                           Special Issue s:
                                                                         Microcontrol
                                                                                      ler
                                                                                       OWN          DATA LOGGE
                                                                                                              R
                                                                                                                                                                                     Microchip's mTouch
                                                                                                                                                                                     Sensing Solutions
                                                                          • BUILD YOUR
                                                                                  YOUR OWN MCU-B
                                                                                                ASED PLC
                                                                          • BUILD SING     EN ARM7 AND
                                                                                                       CORTEX-M3
                                                                          • CHOO BETWE
 09 NEW READER OFFER                                                                                                                               PURCHASING
                                                                                                                                                   NEW OSCILLOSC
                                                                                                                                                   CONSIDER THE
                                                                                                                                                                 OPES?
                                                                                                                                                                UPDATE
                                                                                                                                                    RATES!
                                                                                                                                                            PRODUC TS
 11 FOCUS
                                                                                                                                                              SYSTEM S,
                                                                                                                            UKDL                   COMMS TEST
                                                                                                                                                          POSTERS AND
                                                                                        TECHNO LOGY                  RIGHT LASER                            ANALYS ERS
                                                                                        LEARNING                          FOR THE
                                                                                        FROM                            RIGHT JOB
                                                                                        NATURE                                                                              A BAND
                                                                                                                                                                 SO SMALL
                                                                                                                                               TRANSMITTERS,
                                                                                                                                     SO MANY
                                                                                                                 TROUBL   E WITH RF:
                                                                                                    ISSUE: THE
                                                                                     ALSO IN THIS
      CRACKING THE CRACKERS
      by Steve Rogerson                                            FEATURES
 13 ROHS
      Gary Nevison answers readers’ questions relating to the
                                                                   18 CHOOSING ARM PROCESSOR ARM7 VS
                                                                           CORTEX-M3
      RoHS, REACH, WEEE and EuP directives
                                                                           Anders Lundgren considers the criteria when
                                                                           making the choice of microcontrollers based
 14 INSIGHT
                                                                           on the ARM Cortex-M3 core
      TAIWANESE ICT INDUSTRY FORECAST IN 2008
      by Katie Chen
                                                                   21 PLC WITH PIC16F648A
                                                                           MICROCONTROLLER PART 1
 15 THE TROUBLE WITH RF…
                                                                           Dr Murat Uzam kick-starts a series of articles
      SO MANY TRANSMITTERS, SO SMALL A BAND
                                                                           on a project that focuses on microcontroller-
      by Myk Dormer
                                                                           based PLCs
                                                                   26 MICROCONTROLLER AND SD-CARD BASED
                                                                           MULTICHANNEL DATA LOGGER
                                  21
                                                                           Dogan Ibrahim describes the design of a
                                                                           microcontroller-based multichannel data
                                                                           logger device with SD card and real-time clock
                                                                           interface
                                                                   32 OSCILLOSCOPE UPDATE RATE AND ITS
                             6                                             IMPACT ON CAPTURING INFREQUENT
                                                                           AND RANDOM EVENTS
                                                                           Johnnie Hancock and Joel Woodward look
                                                                           at update rates as one of the primary
REGULARS                                                                   considerations when purchasing a new
                                                                           oscilloscope
35 CIRCUIT IDEAS
39 UKDL
     Dr Ric Allott reviews some of the fundamental
     properties of lasers and how these properties influence
     the choice of laser for a particular manufacturing
     operation
42 TIPS ‘N’ TRICKS
45 PRODUCTS                                                                                                                                       26                                                       32
www.electronicsworld.co.uk                                                                                                                                                     Electronics World - November 08 ❙ 3
UZAM   16/9/08      10:25     Page 36
                                                                                                                        Feature                 PLC/MCU
       PLC WITH PIC16F648A
       MICROCONTROLLER
       (PART 1)                                                                   ASSOCIATE PROFESSOR DR MURAT UZAM
                                                                                  FROM NIGDE UNIVERSITY IN TURKEY
          The Series                                                              KICK-STARTS A SERIES OF ARTICLES ON A
          The articles to be published are as                                     PROJECT THAT FOCUSES ON A
          follows:                                                                MICROCONTROLLER-PLC
          1. The Hardware: Run in this issue.
          2. The Basic Software: This article explains the basic software                 9. Logical Macros: The following logical macros are described in this
          structure of the UZAM_PLC. A PLC scan cycle includes the following:             article: inv_R, AND, NAND, OR, NOR, XOR, XNOR. These macros are
          1. obtain the inputs, 2. run the user program, 3. update the outputs.           applied to an 8 bit register (R1) with another register (R2) or an 8 bit
          In addition, it is also necessary to define and initialise all variables        constant (K).
          used within a PLC. Necessary functions are all described as PIC                 10. Shift&Rotate Macros: The following Shift&Rotate macros are
          Assembly macros to be used in UZAM_PLC. The macros described in                 described in this article: SHIFT_R (shift right the contents of register R),
          this article could be summarised as follows: “HC165” (for handling the          SHIFT_L (shift left the contents of register R), ROTATE_R (rotate right
          inputs), “HC595” (for sending the outputs), “dbncr” (for debouncing the         the contents of register R), ROTATE_L (rotate left the contents of
          inputs), “initialise”, “get_inputs”, “send_outputs”.                            register R), SWAP (swap the nibbles of the register).
          3. Contact and Relay Based Macros: The following contact and relay              11. Multiplexer Macros: The following multiplexer macros are
          based macros are described in this article: ld (load), ld_not (load_not),       described in this article: mux_2_1 (2x1 MUX), mux_2_1_E (2x1 MUX
          not, or, or_not, nor, and, and_not, nand, xor, xor_not, xnor, out, out_not,     with Enable input), mux_4_1 (4x1 MUX), mux_4_1_E (4x1 MUX with
          in_out, inv_out, set, reset. These macros are defined to operate on one         Enable input), mux_8_1 (8x1 MUX), mux_8_1_E (8x1 MUX with Enable
          bit variables.                                                                  input).
          4. Flip-Flop Based Macros: The following flip-flop based macros are             12. DeMultiplexer Macros: The following demultiplexer macros are
          described in this article: r_edge (rising_edge), f_edge (falling_edge),         described in this article: Dmux_1_2 (1x2 DMUX), Dmux_1_2_E (1x2
          latch0, latch1, dff_r (rising edge triggered D flip-flop), dff_f (falling       DMUX with Enable input), Dmux_1_4 (1x4 DMUX), Dmux_1_4_E (1x4
          edge triggered D flip-flop), tff_r (rising edge triggered T flip-flop), tff_f   DMUX with Enable input), Dmux_1_8 (1x8 DMUX), Dmux_1_8_E (1x8
          (falling edge triggered T flip-flop), jkff_r (rising edge triggered JK flip-    DMUX with Enable input).
          flop), jkff_f (falling edge triggered JK flip-flop).                            13. Decoder Macros: The following decoder macros are described in
          5. Timer Macros: The following timer macros are described in this               this article: decod_1_2 (1x2 decoder), decod_1_2_E (1x2 decoder with
          article: TON_8 (ON delay timer), TOF_8 (OFF delay timer), TEP_8                 Enable input), decod_2_4 (2x4 decoder), decod_2_4_E (2x4 decoder
          (Extended Puls timer), TOS_8 (Oscillator timer).                                with Enable input), decod_3_8 (3x8 decoder), decod_3_8_E (3x8
          6. Counter Macros: The following counter macros are described in this           decoder with Enable input).
          article: CTU_8 (Up Counter), CTD_8 (Down Counter), CTUD_8 (Up/Down              14. Priority Encoder Macros: The following priority encoder macros
          Counter).                                                                       are described in this article: encod_4_2_p (4x2 priority encoder),
          7. Comparison Macros: The following comparison macros are                       encod_4_2_p_E (4x2 priority encoder with Enable input), encod_8_3_p
          described in this article: The contents of two registers (R1 and R2) are        (8x3 priority encoder), encod_8_3_p_E (8x3 priority encoder with
          compared according to the following: GT (Greater Than – “>”), GE                Enable input), encod_dec_bcd_p {decimal to BCD (Binary Coded
          (Greater than or Equal to – “≥”), EQ (EQual to – “=”), LT (Less Than –          Decimal) priority encoder}, encod_dec_bcd_p_E (decimal to BCD
          “<”), LE (Less than or Equal to – “≤”), NE (Not Equal to – “≠”). Similar        priority encoder with Enable input).
          comparison macros are also described for comparing the contents of              15. An Example: This article describes an example of a remotely-
          an 8 bit register (R) with an 8 bit constant (K).                               controlled model gate system and makes use of UZAM_PLC to control
          8. Arithmetical Macros: The following arithmetical macros are                   it for different control scenarios.
          described in this article: The following operators are applied to the
          contents of two registers (R1 and R2): ADD, SUB (subtract), INC                 The reader is expected to be familiar with PLCs, PIC microcontrollers
          (increment), DEC (decrement). Similar arithmetical macros are also              and PIC Assembly language. UZAM_PLC macros are developed by using
          described to be used with the contents of an 8 bit register (R) and an 8        PIC Assembly language. The program development environment used is
          bit constant (K).                                                               that of MPLAB IDE (www.microchip.com).
       www.electronicsworld.co.uk                                                                                                   Electronics World - November 08      ❙ 21
UZAM   16/9/08        10:25     Page 37
        PLC/MCU                        Feature
            Figure 1: The schematic diagram of the UZAM_PLC main board
               rogrammable Logic Controllers           UZAM_PLC with PIC16F648A. First in the        macros. In addition, many new features
       P       (PLCs) have been extensively used
               in industry for the past five or six
       decades. PLC manufacturers offer different
                                                       series is this article, which describes the
                                                       hardware of UZAM_PLC with PIC16F648A.
                                                                                                     have been included within UZAM_PLC
                                                                                                     project to make it more of an engulfing
                                                                                                     PLC. However, this project does not include
       devices in terms of functions, program          DESIGN AND                                    graphical interface PC software as in PICBIT
       memories and the number of                      IMPLEMENTATION                                or in other PLCs for developing PLC
       inputs/outputs (I/O), ranging from a few to     This project has been completed in search     programs. Rather, PLC programs are
       thousands of I/Os.                              for how to design and implement a PLC.        developed by using macros as done in the
         The design and implementation of PLCs         Some ideas can be found in the freely         Instruction List (IL) PLC programming
       have long been a secret of the PLC              available PLC project called “PICBIT” from    language. An interested and skilled reader
       manufacturers. As a microprocessor-based        around 10 years ago. An Internet search       could well – and encouraged to – develop
       technology, the functionality of a PLC is       for the keyword “PICBIT” leads to this        graphical interface PC software for easy
       well known from the end-user/programmer         project.                                      use of UZAM_PLC.
       point of view, but by now no serious work         PICBIT describes a PIC16F84
       has been reported to describe a                 microcontroller based PLC with five           THE HARDWARE WITH
       microprocessor/microcontroller based            discrete inputs and eight discrete outputs.   PIC16F648A
       implementation of a PLC.                        The file called “picbit.inc” of the PICBIT    The hardware of UZAM_PLC with
         With a series of articles I aim to describe   contains the basic PLC macro definitions.     PIC16F648A consists of two parts: “main
       a PIC microcontroller based design and            The UZAM_PLC project has been               board” and “I/O extension board”. The
       implementation of a PLC, called                 completed by the inspiration of these         schematic diagram of UZAM_PLC main
       22   ❙ November 08 - Electronics World                                                                           www.electronicsworld.co.uk
UZAM   16/9/08     10:25    Page 38
                                                                                                       Feature                PLC/MCU
         Figure 2: The schematic diagram of the UZAM_PLC I/O extension board
       board is shown in Figure 1. The main            Electronics World for details).                PIC16F648A microcontroller. Although it
       board contains mainly five sections: power,        For other types of USB or parallel port     can run up to 20MHz, in UZAM_PLC it is
       programming, CPU (Central Processor             PIC programmers the reader is expected to      fixed to run at 4MHz with its internal
       Unit), inputs and outputs.                      make necessary arrangements. ICSP              oscillator. This frequency is fixed because
          The power section accepts 12V AC input       connector takes the lines VPP(MCLR), VDD,      time-delays are calculated based on this
       and produces as outputs 12V DC to be            VSS(GND), DATA (RB7), CLOCK (RB6) from         speed. However, by means of two
       used as the operating voltage of relays,        the PIC programmer hardware through a          switches, namely SW1 and SW2, it is also
       and 5 V DC to be used for ICs, inputs, etc.     properly prepared cable and it serves them     possible to use an external oscillator with
       The programming section deals with the          to a 4PDT (four pole double throw) switch.     different crystal frequencies. When doing
       programming of PIC16F648A                          There are two positions of the 4PDT         so, time-delay functions must be calculated
       microcontroller. For programming the            switch. As seen in Figure 1, in one position   accordingly. SW3 directs the RA5 pin either
       PIC16F648A in circuit, it is necessary to use   of 4PDT switch, PIC16F648A is ready to be      to one pole of 4PDT switch or to the future
       a PIC programmer hardware and software          programmed and in the other position the       extension connector.
       with ICSP (In Circuit Serial Programming)       loaded program is run. For properly
       capability. For those who have a PC with        programming the PIC16F648A by means of         PROGRAMMING PICS
       serial port, PCB files for a PIC programmer     a PIC programmer and the 4PDT switch, it       When programming PIC16F648A, RA5
       hardware, called JDM programmer, and            is also a necessity to switch off the power    should be connected to 4PDT switch. RB0,
       related software can be downloaded              switch.                                        RB6 and RB7 pins are all reserved to be
       (please contact the editorial office of            The CPU section consists of the             used for an 8-bit parallel to serial converter
       www.electronicsworld.co.uk                                                                                  Electronics World - November 08   ❙ 23
UZAM   16/9/08     10:25   Page 39
       PLC/MCU                     Feature
       register 74HC/LS165. Through these three           PIC16F648A provides the following:             DC signals. These external input signals are
       pins and with added 74HC/LS165 registers        4096 words of Flash program memory; 256           isolated from the other parts of the
       we can describe as many inputs as we            bytes of RAM data memory and 256 bytes            hardware by using NPN type opto-couplers
       wish. RB0, RB6, and RB7 are the “data in”,      of EEPROM data memory. The UZAM_PLC               (e.g. 4N25). For simulating input signals,
       the “clock in” and the “shift/load” pins,       macros make use of registers defined in           one can use on board push-buttons as
       respectively.                                   RAM data memory. Note that it may be              temporary inputs and slide switches as
         Similarly, RB3, RB4 and RB5 pins are all      possible to use PIC16F628A as the CPU,            permanent inputs.
       reserved to be used for 8-bit serial to         but one has to bear in mind that                     In the beginning of each PLC scan cycle
       parallel converter register/driver TPIC6B595.   PIC16F628A provides the following: 2048           (get_inputs) 74HC/LS165 is loaded (RB7
       Through these three pins and with added         words of Flash program memory; 224 bytes          (shift/load) = 0) with the level of 8 inputs
       TPIC6B595 registers we can describe as          of RAM data memory and 128 bytes of               and then this data is serially clocked in
       many outputs as we wish. RB3, RB4 and           EEPROM data memory.                               (when RB7 = 1; through RB0 “data in” and
       RB5 are the “clock out”, the “data out”            In this case it is necessary to take care of   RB6 “clock in” pins). If there is no I/O
       and the “latch out” pins, respectively. The     the usage of RAM data memory. The                 extension board used, then 8 clock_in
       remaining unused pins of the PIC16F648A         inputs section introduces 8 discrete inputs       signals are enough to get the 8 input
       are connected to the “future extension          for the UZAM_PLC, called I0.0, I0.1, ...,         signals. For each I/O extension board, 8
       connector”.                                     I0.7. Each input can accept 5V DC or 24V          more clock_in signals are necessary.
        A                                              B                                                 C
        D                                                                      E
        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    17/9/08             14:48   Page 40
                                                                                                       Feature              PLC/MCU
       DATA INPUT                                                                As shown in Figure 2, the “I/O extension board” contains mainly
       The serial data coming from the I/O extension board is taken from      two sections: discrete inputs (8 of them) and discrete outputs (also
       the “SI” input of 74HC/LS165. The outputs section introduces 8         8 of them). They are similar to the ones as described for the main
       discrete relay outputs for the UZAM_PLC, called Q0.0, Q0.1, ...,       board.
       Q0.7. Each relay operates with 12V DC and is driven by an 8-bit           The I/O extension connector “DB9M” seen on the left connects
       serial to parallel converter register/driver TPIC6B595.                the I/O extension board to the main board or to a previous I/O
          Relays have SPDT (single pole double throw) contacts with C         extension board. Similarly, the I/O extension connector “DB9F”
       (common), NC (normally closed) and NO (normally open) terminals.       seen on the right connects the I/O extension board to an adjacent
       At the end of each PLC scan cycle (send_outputs) the output data       I/O extension board. In this way we can connect as many I/O
       is serially clocked out (through RB3 “clock out” and RB4 “data         extension boards as necessary. The 12V DC power is taken from
       out” pins) and finally latched within TPIC6B595. If there is no I/O    the main board or from a previous I/O extension board and it is
       extension board used, then 8 clock_out signals are enough to send      passed to the next I/O extension boards.
       the 8 output signals. For each I/O extension board, 8 more                The 5V DC voltage is taken from the main board or from a
       clock_out signals are necessary.                                       previous I/O extension board through the “DB9M” connector and it
          The serial data going to the I/O extension board is sent out from   is passed on to the next I/O extension boards through the “DB9F”
       the “SER OUT” (pin 18) of TPIC6B595. In addition, there is also an     connector. The PCB design files of the I/O extension board can be
       I/O extension connector “DB9F” for conveying the I/O data to and       had from the editorial offices of the magazine. Note that in the
       from all the connected extension I/O boards. The PCB design files      PCB design of the I/O extension board, some lines of I/O extension
       of the main board can be received from the editorial offices of        connectors “DB9M” and “DB9F” are different from the ones
       Electronics World. Note that in the PCB design of the main board,      shown in Figure 2. Figure 3 is a selection of photos of the
       some lines of I/O extension connector are different from the ones      UZAM_PLC main board and I/O extension board with and without a
       shown in Figure 1.                                                     PIC programmer. ■
       www.electronicsworld.co.uk                                                                                Electronics World - November 08   ❙ 25
   View publication stats