An example of usage of the X-NUCLEO-PLC01A1 Programmable Logic Controller Expansion Board.

Dependencies:   X_NUCLEO_PLC01A1

PLC with the X-NUCLEO-PLC01A1 Expansion Board

This application provides a simple example of usage of the X-NUCLEO-PLC01A1 Programmable Logic Controller Expansion Board.
It shows how to use one PLC connected to the board, driving the status of each output channels according to the status of each input channels.

Committer:
apalmieri
Date:
Fri Feb 19 12:19:51 2016 +0000
Revision:
0:4f51d6a15c93
Child:
1:331915faf16e
An example of usage of the X-NUCLEO-PLC01A1 Programmable Logic Controller Expansion Board.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apalmieri 0:4f51d6a15c93 1 /**
apalmieri 0:4f51d6a15c93 2 ******************************************************************************
apalmieri 0:4f51d6a15c93 3 * @file main.cpp
apalmieri 0:4f51d6a15c93 4 * @author AST/CLs
apalmieri 0:4f51d6a15c93 5 * @version V1.0.0
apalmieri 0:4f51d6a15c93 6 * @date Feb 5th, 2016
apalmieri 0:4f51d6a15c93 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-PLC01A1
apalmieri 0:4f51d6a15c93 8 * PLC Expansion Board.
apalmieri 0:4f51d6a15c93 9 ******************************************************************************
apalmieri 0:4f51d6a15c93 10 * @attention
apalmieri 0:4f51d6a15c93 11 *
apalmieri 0:4f51d6a15c93 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
apalmieri 0:4f51d6a15c93 13 *
apalmieri 0:4f51d6a15c93 14 * Redistribution and use in source and binary forms, with or without modification,
apalmieri 0:4f51d6a15c93 15 * are permitted provided that the following conditions are met:
apalmieri 0:4f51d6a15c93 16 * 1. Redistributions of source code must retain the above copyright notice,
apalmieri 0:4f51d6a15c93 17 * this list of conditions and the following disclaimer.
apalmieri 0:4f51d6a15c93 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
apalmieri 0:4f51d6a15c93 19 * this list of conditions and the following disclaimer in the documentation
apalmieri 0:4f51d6a15c93 20 * and/or other materials provided with the distribution.
apalmieri 0:4f51d6a15c93 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
apalmieri 0:4f51d6a15c93 22 * may be used to endorse or promote products derived from this software
apalmieri 0:4f51d6a15c93 23 * without specific prior written permission.
apalmieri 0:4f51d6a15c93 24 *
apalmieri 0:4f51d6a15c93 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
apalmieri 0:4f51d6a15c93 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
apalmieri 0:4f51d6a15c93 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
apalmieri 0:4f51d6a15c93 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
apalmieri 0:4f51d6a15c93 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
apalmieri 0:4f51d6a15c93 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
apalmieri 0:4f51d6a15c93 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
apalmieri 0:4f51d6a15c93 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
apalmieri 0:4f51d6a15c93 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
apalmieri 0:4f51d6a15c93 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
apalmieri 0:4f51d6a15c93 35 *
apalmieri 0:4f51d6a15c93 36 ******************************************************************************
apalmieri 0:4f51d6a15c93 37 */
apalmieri 0:4f51d6a15c93 38
apalmieri 0:4f51d6a15c93 39
apalmieri 0:4f51d6a15c93 40 /* Includes ------------------------------------------------------------------*/
apalmieri 0:4f51d6a15c93 41
apalmieri 0:4f51d6a15c93 42 /* Component specific header files. */
apalmieri 0:4f51d6a15c93 43 #include "PLC_class.h"
apalmieri 0:4f51d6a15c93 44 #include "CLT01_38S_class.h"
apalmieri 0:4f51d6a15c93 45 #include "VNI8200XP_class.h"
apalmieri 0:4f51d6a15c93 46
apalmieri 0:4f51d6a15c93 47
apalmieri 0:4f51d6a15c93 48 /* Definitions ---------------------------------------------------------------*/
apalmieri 0:4f51d6a15c93 49 #define SPI_MOSI D11
apalmieri 0:4f51d6a15c93 50 #define SPI_MISO D12
apalmieri 0:4f51d6a15c93 51 #define SPI_SCLK D13
apalmieri 0:4f51d6a15c93 52
apalmieri 0:4f51d6a15c93 53 #define SPI_BITS 16
apalmieri 0:4f51d6a15c93 54
apalmieri 0:4f51d6a15c93 55 #define SPI_CS1 D9
apalmieri 0:4f51d6a15c93 56 #define SPI_CS2 D10
apalmieri 0:4f51d6a15c93 57
apalmieri 0:4f51d6a15c93 58 #define OUT_EN D6
apalmieri 0:4f51d6a15c93 59
apalmieri 0:4f51d6a15c93 60 /* Uncomment this for OUTPUT_CYCLING ENABLE */
apalmieri 0:4f51d6a15c93 61 //#define OUTPUT_CYCLING
apalmieri 0:4f51d6a15c93 62
apalmieri 0:4f51d6a15c93 63 /* Variables -----------------------------------------------------------------*/
apalmieri 0:4f51d6a15c93 64
apalmieri 0:4f51d6a15c93 65 /* Array for input data from Digital Input Termination Device */
apalmieri 0:4f51d6a15c93 66 uint8_t inputArray[2] = {0x00, 0x00};
apalmieri 0:4f51d6a15c93 67 /* Array for output data to Solid State Relay */
apalmieri 0:4f51d6a15c93 68 uint8_t outputArray[2] = {0x00, 0x00};
apalmieri 0:4f51d6a15c93 69
apalmieri 0:4f51d6a15c93 70 /* Number of channels in ON state */
apalmieri 0:4f51d6a15c93 71 uint8_t Ch_On = 0x00;
apalmieri 0:4f51d6a15c93 72
apalmieri 0:4f51d6a15c93 73
apalmieri 0:4f51d6a15c93 74 /* Functions -----------------------------------------------------------------*/
apalmieri 0:4f51d6a15c93 75
apalmieri 0:4f51d6a15c93 76 /**
apalmieri 0:4f51d6a15c93 77 * @brief Receive input data from Digital Input Termination Device
apalmieri 0:4f51d6a15c93 78 * @param None
apalmieri 0:4f51d6a15c93 79 * @retval None
apalmieri 0:4f51d6a15c93 80 */
apalmieri 0:4f51d6a15c93 81 void DigitalInputArrayHandler(PLC &plc)
apalmieri 0:4f51d6a15c93 82 {
apalmieri 0:4f51d6a15c93 83 plc.plcInput().DigInpArray_GetInput(inputArray);
apalmieri 0:4f51d6a15c93 84 }
apalmieri 0:4f51d6a15c93 85
apalmieri 0:4f51d6a15c93 86 /**
apalmieri 0:4f51d6a15c93 87 * @brief Select output function and set outputs
apalmieri 0:4f51d6a15c93 88 * @param None
apalmieri 0:4f51d6a15c93 89 * @retval None
apalmieri 0:4f51d6a15c93 90 */
apalmieri 0:4f51d6a15c93 91 void SsrelayHandler(PLC &plc)
apalmieri 0:4f51d6a15c93 92 {
apalmieri 0:4f51d6a15c93 93 /* Set outputArray as DigInpArray RxBuffer */
apalmieri 0:4f51d6a15c93 94 outputArray[1] = plc.signalMirror(inputArray[1]);
apalmieri 0:4f51d6a15c93 95
apalmieri 0:4f51d6a15c93 96 /* Uncomment the relevant function as required */
apalmieri 0:4f51d6a15c93 97 //outputArray[1] = plc.signalMirror(0xFF);
apalmieri 0:4f51d6a15c93 98 //outputArray[1] = plc.outputFreeze(0xFF,5000);
apalmieri 0:4f51d6a15c93 99 //outputArray[1] = plc.outputRegroup(0xFF);
apalmieri 0:4f51d6a15c93 100 //Ch_On = plc.inputSum(&outputArray[1],0xFF);
apalmieri 0:4f51d6a15c93 101 //outputArray[1] = plc.setOutput(0xFF);
apalmieri 0:4f51d6a15c93 102 //outputArray[1] = plc.inputsAND(0xFF,0x0F);
apalmieri 0:4f51d6a15c93 103 //outputArray[1] = plc.inputsOR(0xF0,0x0F);
apalmieri 0:4f51d6a15c93 104 //outputArray[1] = plc.inputsNOT(0x00);
apalmieri 0:4f51d6a15c93 105 //outputArray[1] = plc.inputsXOR(0xFF,0x00);
apalmieri 0:4f51d6a15c93 106
apalmieri 0:4f51d6a15c93 107 /* Parity bits calculation */
apalmieri 0:4f51d6a15c93 108 plc.outputParityBits(outputArray);
apalmieri 0:4f51d6a15c93 109
apalmieri 0:4f51d6a15c93 110 /* Send output information to solid state relay */
apalmieri 0:4f51d6a15c93 111 plc.plcOutput().Ssrelay_SetOutput(outputArray);
apalmieri 0:4f51d6a15c93 112 }
apalmieri 0:4f51d6a15c93 113
apalmieri 0:4f51d6a15c93 114 void setup(SPI &spi, int bits, int mode = 0, int frequency_hz = 1E6)
apalmieri 0:4f51d6a15c93 115 {
apalmieri 0:4f51d6a15c93 116 /* Set given configuration. */
apalmieri 0:4f51d6a15c93 117 spi.format(bits, mode);
apalmieri 0:4f51d6a15c93 118 spi.frequency(frequency_hz);
apalmieri 0:4f51d6a15c93 119 }
apalmieri 0:4f51d6a15c93 120
apalmieri 0:4f51d6a15c93 121 /* Main ----------------------------------------------------------------------*/
apalmieri 0:4f51d6a15c93 122
apalmieri 0:4f51d6a15c93 123 int main()
apalmieri 0:4f51d6a15c93 124 {
apalmieri 0:4f51d6a15c93 125 /*----- Initialization. -----*/
apalmieri 0:4f51d6a15c93 126
apalmieri 0:4f51d6a15c93 127 /* Initializing SPI bus. */
apalmieri 0:4f51d6a15c93 128 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK);
apalmieri 0:4f51d6a15c93 129 setup(spi, SPI_BITS);
apalmieri 0:4f51d6a15c93 130
apalmieri 0:4f51d6a15c93 131 /* Initializing PLC IO Channels Component. */
apalmieri 0:4f51d6a15c93 132 PLC plc(SPI_CS1, SPI_CS2, OUT_EN, spi);
apalmieri 0:4f51d6a15c93 133
apalmieri 0:4f51d6a15c93 134 while(1) {
apalmieri 0:4f51d6a15c93 135 plc.plcInput().SetReadStatus(1);
apalmieri 0:4f51d6a15c93 136 /* Polling input device to refresh input state */
apalmieri 0:4f51d6a15c93 137 if(plc.plcInput().GetReadStatus()) {
apalmieri 0:4f51d6a15c93 138
apalmieri 0:4f51d6a15c93 139 plc.plcInput().SetReadStatus(0);
apalmieri 0:4f51d6a15c93 140
apalmieri 0:4f51d6a15c93 141 #ifdef OUTPUT_CYCLING
apalmieri 0:4f51d6a15c93 142 plc.outputCycling();
apalmieri 0:4f51d6a15c93 143 #else
apalmieri 0:4f51d6a15c93 144 DigitalInputArrayHandler(plc);
apalmieri 0:4f51d6a15c93 145 SsrelayHandler(plc);
apalmieri 0:4f51d6a15c93 146 #endif /* OUTPUT_CYCLING */
apalmieri 0:4f51d6a15c93 147 }
apalmieri 0:4f51d6a15c93 148 wait_ms(10);
apalmieri 0:4f51d6a15c93 149 }
apalmieri 0:4f51d6a15c93 150 }
apalmieri 0:4f51d6a15c93 151