C program to turn on a relay circuit

1 reply [Last post]
ind8105
Offline
Last seen: 10 years 9 weeks ago
Joined: 2009-08-22
bOt Points: 58

Hello,

 

Can anyone design a program in C to send a bit HIGH (1) to the input of a relay circuit to turn on the relay?

 

Thank you

 

Ed

abuanas
Offline
Last seen: 9 years 8 weeks ago
Joined: 2009-08-23
bOt Points: 86
c prog to write high to relay

........................................................

     using the µvision3 software

........................................................

#include <reg51.h>          //header file for the 8051

#define ON 1  // preprocessor defination for ON

#define OFF 0   /*define OFF as low (0)*/

#define switch_pressed (bit) 1

 

sbit relay_input = P1^0;      // the relay input is connected to bit 0 of port 1

sbit switch_input = P3^0;  // a switch is connected to bit 0 of port 3

 

void relay_init()

   {

              .......................................................

                /* funtion to initialize relay */

              .......................................................

               relay_input = OFF;

   }

bit get_input()

   {

               if(switch_input == 0)

                     {

                               while(switch_input == 0);

                               return switch_pressed;

                     }

               else

                     {

                              return 0;

                     }

   }

main()

   {

                  switch_input = OFF;

                  relay_init();

            

                  for(;;)

                      {

                                   get_input();

                                   if(switch_pressed)

                                       {

                                                relay_input = ON;

                                       }  

                                 else

                                       {

                                                 relay_input = OFF;

                                       }

                      }

   }