DC Relay C Demo
This demonstration program turns each relay on in sequence and then turns them off in sequence. Next it turns all of the relays on, and then turns them all off. Use this demo as a starting point for your own instrumentation project.
// ********************************************************************* // FILE NAME: dcrelay.c // --------------------------------------------------------------------- // AUTHOR: DAVID J. SIU // BEN MORSE // DATE: 5/6/2009 // VERSION: 1.1 // --------------------------------------------------------------------- // This is the driver code for the DC Relay Module. // This code: // -Initializes the DC Relay Module. // -Turns a relay on or off. // -Returns the state of the relays. // --------------------------------------------------------------------- // Important user words: // Init_DC_Relay: Initializes the DC Relay Module. // Control_DC_Relay: Turns the specified relay on or off. // Read_DC_Relay_Status: Returns the state of the relays. // WDCM_MODULE_NUM this constant MUST match hardware jumper settings! // --------------------------------------------------------------------- // Notes: // RELAYS ARE ACTIVE LOW! WRITING A 1 TO THE RELAY TURNS IT OFF WHILE // WRITING A 0 TO THE RELAY TURNS IT ON! // // Disclaimer: THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT // ANY WARRANTIES OR REPRESENTATIONS EXPRESS OR IMPLIED, // INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES // OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. // // ********************************************************************* #include <mosaic\allqed.h> // Include QED header files #ifndef __GNUC__ #include "../../libraries/include/io_access.h" #include "../../libraries/include/io_access.c" #endif // NOTE: YOU MUST MAKE SURE THAT THIS CONSTANT CORRESPONDS TO YOUR MODULE SELECT JUMPERS! // For example, to access the Wildcard at address 4: // remove both module select jumper caps and mount the card on Wildcard Module Bus 1 #define WDCM_MODULE_NUM 4 // Note: if you are using a QScreen or Handheld, accessing a wildcard at module address 0 // is not allowed; this module address is reserved for the GUI on the QScreen and Handheld. // Relays are active low (i.e. writing a 0 to the relay turns it on). #define RELAY_ON 0 #define RELAY_OFF 1 #define RELAY_CONTROL_LINES 1 #define DIRECTION_OFFSET 0x05 #define RELAY_CONTROL_OFFSET 0x00 #define ALL_RELAYS 0xF _Q void Init_DC_Relay ( uchar module_number ) // Valid module numbers are 0-7 // Initializes the DC Relay Module by configuring the DC relay control lines // of the CPLD to outputs. The module number depends on the module select // jumpers. See Table 1 for the jumper settings and associated addresses. { // Turn all relays off before initializing control lines to outputs. // Relays are active low (i.e. writing a 0 to the relay turns it on). IOStoreChar( ALL_RELAYS, RELAY_CONTROL_OFFSET, module_number ); IOStoreChar( RELAY_CONTROL_LINES, DIRECTION_OFFSET, module_number ); } _Q void Control_DC_Relay ( uchar module_number, uchar relay_num, uchar state ) // Sets the relay number to the appropriate state (on or off). // Valid relay numbers are 0-2. Valid module numbers are 0-7. { if(state) // turn relay off { state = state << relay_num; IOSetBits( state, RELAY_CONTROL_OFFSET, module_number ); } else // turn relay on { state = 1 << relay_num; IOClearBits ( state, RELAY_CONTROL_OFFSET, module_number ); } } _Q uchar Read_DC_Relay_Status ( uchar module_number ) // Reads the current state of the opto isolated DC Relays. Valid module numbers are 0-7. // Returns a character whose three least significant bits represents the // three relays. For example, if 1 is returned (001 in binary), then Relay 0 // is off and the other relays are on. If 6 is returned (110 in binary), // then relays 1 and 2 are off and 0 is on. The five most significant bits // do not matter. { char dc_relay_status; dc_relay_status = IOFetchChar( RELAY_CONTROL_OFFSET, module_number ); return( dc_relay_status ); } // main() function for C example showing control of // Crydom Solid State Relays (SSRS) int main ( void ) { Init_DC_Relay( WDCM_MODULE_NUM ); printf("\npress enter to turn on the first relay.\n"); Key(); Control_DC_Relay( WDCM_MODULE_NUM, 0, RELAY_ON ); // turn on 1st relay printf("press enter to turn on the second relay.\n"); Key(); Control_DC_Relay( WDCM_MODULE_NUM, 1, RELAY_ON ); // turn on 2nd relay printf("press enter to turn on the third relay.\n"); Key(); Control_DC_Relay( WDCM_MODULE_NUM, 2, RELAY_ON ); // turn on 3rd relay printf("press enter to turn off the first relay.\n"); Key(); Control_DC_Relay( WDCM_MODULE_NUM, 0, RELAY_OFF ); // turn off 1st relay printf("press enter to turn off the second relay.\n"); Key(); Control_DC_Relay( WDCM_MODULE_NUM, 1, RELAY_OFF ); // turn off 2nd relay printf("press enter to turn off the third relay.\n"); Key(); Control_DC_Relay( WDCM_MODULE_NUM, 2, RELAY_OFF ); // turn off 3rd relay printf("press enter to turn on all three relays.\n"); Key(); IOClearBits( ALL_RELAYS, RELAY_CONTROL_OFFSET, WDCM_MODULE_NUM ); printf("press enter to turn off all three relays.\n"); Key(); IOSetBits( ALL_RELAYS, RELAY_CONTROL_OFFSET, WDCM_MODULE_NUM ); return 0; }
For more usage examples, see the Forth version of this program.
See also → DC Relay Forth Demo
DC Relay Wildcard Users Guide
This page is about: DC Relay Example C Program, Crydom Relay Control – Simple C example program shows DC relay control with large loads.