/*
Program for Wireless Control Relay (Macro C compiler)
Designed and developed by Lim.laboratory
Contact at lim.laboratory@yahoo.com,Thailand
*/
//---------------------------------------------------------------------------
#include "c:\mc\8051io.h"
#include "c:\mc\8051reg.h"
#include "c:\mc\8051bit.h"
#define data0 P1.0
#define data1 P1.1
#define data2 P1.2
#define data3 P1.3
#define te P1.4
//===========================================================================================
main()
{
        char gmain,main1,main2,main3;
        serinit(9600);
        clear_display();
        title_wcr();
        delay(3000);
        while(1)
        {
                clear_display();
                putstr("\nPress 1 for WCR 1,Press 2 for WCR 2,Press 3 for WCR 3");
                putstr("\n");
                gmain=getch();
                switch(gmain)
                {
                        case('1'):
                        wcr1();
                        command_relay();
                        putstr("\nPress r for run,Press n for New Program,Press h for Help");
                        putstr("\n");
                        main1=getch();
                                switch(main1)
                                {
                                        case('r'):
                                        command_run();
                                        break;
                                        case('n'):
                                        command_new();
                                        break;
                                        case('h'):
                                        command_help();
                                        break;
                                        default:
                                        command_error();
                                }
                        break;
                        case('2'):
                        wcr2();
                        command_relay();
                        putstr("\nPress r for run,Press n for New Prongram,Press h for Help");
                        putstr("\n");
                        main2=getch();
                                switch(main2)
                                {
                                        case('r'):
                                        command_run();
                                        break;
                                        case('n'):
                                        command_new();
                                        break;
                                        case('h'):
                                        command_help();
                                        break;
                                        default:
                                        command_error();
                                }
                        break;
                        case('3'):
                        wcr3();
                        command_relay();
                        putstr("\nPress r for run,Press n for New Program,Press h for Help");
                        putstr("\n");
                        main3=getch();
                                switch(main3)
                                {
                                        case('r'):
                                        command_run();
                                        break;
                                        case('n'):
                                        command_new();
                                        break;
                                        case('h'):
                                        command_help();
                                        break;
                                        default:
                                        command_error();
                                }
                        break;
                        default:
                        command_error();
                }
                
        }
}
//============================================================================
wcr1()
{      
	P2=0x80;
        putstr("\rWCR 1 READY");
}
//---------------------------------------------------------------------------
wcr2()
{
        P2=0x0c0;
        putstr("\rWCR 2 READY");
}
//---------------------------------------------------------------------------
wcr3()
{
        P2=0x0e0;
        putstr("\rWCR 3 READY");
}
//---------------------------------------------------------------------------
command_run()
{
        transmit_value();
        putstr("\rRunprogram complete");
        delay(3000);
}
//---------------------------------------------------------------------------
command_new()
{
        putstr("\rNew Program");
        delay(3000);
}
//---------------------------------------------------------------------------
command_help()
{
        char hel;
        clear_display();
        putstr("\n\t\tWireless Control Relay");
        putstr("\n");
        putstr("\nWCR is instrument that use for control electrical up to 250V/10A AC");
        putstr("\nWCR include two device,transmitter and receiver control.transmitter"); 
        putstr("\nconnect to computer by seriel port call com1 and using hyperterminl"); 
        putstr("\nprogram for communication with human and get value from keyboard.And");
        putstr("\nreceiver control use to control electrical some where in the building.");
        putstr("\nIt far from transmitter so can control by RF 434MHz");
        putstr("\n");
        putstr("\n\tHow to use");
        putstr("\n");
        putstr("\nConnect transmitter to serial port and open hyperterminal program. And");
        putstr("\nwhen see the instuction on sceen select function and follow the sceen");
        putstr("\nnotice the receiver control latch status after get the command so when");
        putstr("\nshut down computer or disconnect.The receiver control not change status");
        putstr("\nPress Anykey for out and New Program");
        putstr("\n");
        hel=getch();
}
//---------------------------------------------------------------------------
command_error()
{
        putstr("\rError Command");
        delay(2000);
        putstr("\nNew Program");
        delay(3000);
}
//---------------------------------------------------------------------------
command_relay()
{
        putstr("\nPress o for turn-on relay,Press f for turn-off relay");
        command_relay0();
        command_relay1();
        command_relay2();
        command_relay3();
}
//---------------------------------------------------------------------------
command_relay0()
{
        char a;
        putstr("\nRelay 0");
        a=getch();
        if(a=='f')
        {
                space_bar();
                putstr("OFF");
                clrbit(data0)
        }
        else
        {
                switch(a)
                {
                        case('o'):
                        space_bar();
                        putstr("ON");
                        setbit(data0)
                        break;
                        default:
                        space_bar();
                        putstr("Incorrect command this Relay not work");
                }
        }
}
//---------------------------------------------------------------------------
command_relay1()
{
        char b;
        putstr("\nRelay 1");
        b=getch();
        if(b=='f')
        {
                space_bar();
                putstr("OFF");
                clrbit(data1)
        }
        else
        {
                switch(b)
                {
                        case('o'):
                        space_bar();
                        putstr("ON");
                        setbit(data1)
                        break;
                        default:
                        space_bar();
                        putstr("Incorrect command this Relay not work");
                }
        }
}
//---------------------------------------------------------------------------
command_relay2()
{
        char c;
        putstr("\nRelay 2");
        c=getch();
        if(c=='f')
        {
                space_bar();
                putstr("OFF");
                clrbit(data2)
        }
        else
        {
                switch(c)
                {
                        case('o'):
                        space_bar();
                        putstr("ON");
                        setbit(data2)
                        break;
                        default:
                        space_bar();
                        putstr("Incorrect command this Relay not work");
                }
        }
}
//---------------------------------------------------------------------------
command_relay3()
{
        char d;
        putstr("\nRelay 3");
        d=getch();
        if(d=='f')
        {
                space_bar();
                putstr("OFF");
                clrbit(data3)
        }
        else
        {
                switch(d)
                {
                        case('o'):
                        space_bar();
                        putstr("ON");
                        setbit(data3)
                        break;
                        default:
                        space_bar();
                        putstr("Incorrect command this Relay not work");
                }
        }
}
//---------------------------------------------------------------------------
transmit_value()
{
        clrbit(te)
        delay(200);
        setbit(te)
}
//---------------------------------------------------------------------------
space_bar()
{
        putstr("\b");
        putstr("    ");
}
//---------------------------------------------------------------------------
clear_display()
{
        char dis;
        for(dis=0;dis<30;dis++)
        putstr("\n");
}
//---------------------------------------------------------------------------
title_wcr()
{
        putstr("\n                     000  0  000   00000   0000000    ");
        putstr("\n                     000 000 000  00000000 000 0000   ");
        putstr("\n                      000000000  0000  000 000 0000   ");
        putstr("\n                      000000000  000       0000000    ");
        putstr("\n                       0000000   000       00000      ");
        putstr("\n                       000 000   0000  000 00 000     ");
        putstr("\n                        00 00     00000000 00  000    ");
        putstr("\n                        0   0      00000   00   000   ");
        putstr("\n");
        putstr("\n                          WIRELESS CONTROL RELAY      ");
        putstr("\n");
}
//---------------------------------------------------------------------------
