#include // Standard 8051 defines // Author: Sunrom Electronics 28-Feb-2017 // Board: Output Driver Board - Expandable Model: 1452 // Board URL: http://www.sunrom.com/m/1452 // The program can control unlimited outputs // Compiler: Keil C51 MCU: AT89S52 // Demo Code: Understanding the logic below you can adapt this C code // to any target like PIC, ARM, AVR #define DAT P1_0 // Define which MCU pins are connected to board #define CLK P1_1 #define STORE P1_2 #define NUMBER_OF_74595 2 // How many board you have connected? unsigned char outputs[NUMBER_OF_74595]; // Set that many array variables, Each bit=relay // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // -=-=-=-=- Delay x ms -=-=-=-=-=-=-= // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= void delay_ms(int x) // delays x msec (at fosc=11.0592MHz) { int y=0; while(x>=0) { for (y=0; y<100; y++); x--; } } // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // -=-=-=-=- Set Output -=-=-=-=-=-=-= // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= void set_output() { unsigned char i, j, k; CLK = 0; STORE = 0; i=NUMBER_OF_74595; do { i--; k=0x80; for(j=0; j<8; j++) { if(outputs[i] & k) DAT = 1; else DAT = 0; k = k >> 1; CLK = 1; CLK = 0; } } while(i!=0); STORE = 1; delay_ms(10); // hold the STORE pin high for 10ms STORE = 0; } // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // -=-=-=-=- Main Program -=-=-=-=-=-=-= // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= void main() { CLK = 0; // Set Pins Low at power up STORE = 0; // Set Pins Low at power up delay_ms(100); // Power Up delay 100ms // Array storing relay status, Each bit is one relay output outputs[0] = 0xF0; // 1st Board, We want to set first four relay OFF and next four ON outputs[1] = 0xFF; // 2nd Board, Here we are setting all relays high set_output(); // This function shift outs the array and relay are changed accordingly while(1) // Loop Forever { // User Code } }