Link here

AC 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. This demonstration program can serve as a starting point for the instrumentation of AC controlled devices.

AC Relay C Demo

// *********************************************************************
// FILE NAME:   acrelay.c
// ---------------------------------------------------------------------
// AUTHOR:      DAVID J. SIU
//              BEN MORSE
// DATE:        5/4/2009
// VERSION:     1.1
// ---------------------------------------------------------------------
// This is the driver code for the AC Relay Module.
// This code:
//     -Initializes the AC Relay Module.
//     -Turns a relay on or off.
//     -Returns the state of the relays.
// ---------------------------------------------------------------------
// Important user words:
// Init_AC_Relay:         Initializes the AC Relay Module.
// Control_AC_Relay:      Turns the specified relay on or off.
// Read_AC_Relay_Status:  Returns the state of the relays.
// WACM_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 WACM_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_AC_Relay ( uchar module_number ) // Valid module numbers are 0-7
// Initializes the AC Relay Module by configuring the AC 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_AC_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-3  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_AC_Relay_Status ( uchar module_number )
// Reads the current state of the Crydom AC Relays.  Valid module numbers are 0-7.
// Returns a character whose four 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 four most significant bits
// do not matter.
{
  char ac_relay_status;
 
  // status of opto isolated relays (SSRS)
  ac_relay_status = IOFetchChar( RELAY_CONTROL_OFFSET, module_number );
 
  return( ac_relay_status );
}
 
// solid state relay main() function
int main ( void )
{
  Init_AC_Relay( WACM_MODULE_NUM );
 
  printf("\npress enter to turn on the first relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 0, RELAY_ON );    // turn on 1st relay
  printf("press enter to turn on the second relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 1, RELAY_ON );    // turn on 2nd relay
  printf("press enter to turn on the third relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 2, RELAY_ON );    // turn on 3rd relay
  printf("press enter to turn on the fourth relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 3, RELAY_ON );    // turn on 4th relay
 
  printf("press enter to turn off the first relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 0, RELAY_OFF );    // turn off 1st relay
  printf("press enter to turn off the second relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 1, RELAY_OFF );    // turn off 2nd relay
  printf("press enter to turn off the third relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 2, RELAY_OFF );    // turn off 3rd relay
  printf("press enter to turn off the fourth relay.\n");
  Key();
  Control_AC_Relay( WACM_MODULE_NUM, 3, RELAY_OFF );    // turn off 4th relay
 
  printf("press enter to turn on all four relays.\n");
  Key();
  IOClearBits( ALL_RELAYS, RELAY_CONTROL_OFFSET, WACM_MODULE_NUM );
  printf("press enter to turn off all four relays.\n");
  Key();
  IOSetBits( ALL_RELAYS,  RELAY_CONTROL_OFFSET, WACM_MODULE_NUM );
 
  return 0;
}



See also → AC Relay Forth Demo
AC Relay Wildcard Users Guide

 
This page is about: C Example for 5 Amp Solid State Relay Control, AC Relay Module – C language program shows simple example of cycling the states of 4 embedded AC Relays. AC Relay, Relay Control, Software, C language, simple example, character, least significant bits, turn on relay
 
 
Navigation