//float ch;
unsigned int ADC_Value;
long ch , tlong;
/*................................................. ...............................
Define relay selection port pins
.................................................. ...............................*/
sbit RL1 at RC0_bit;
sbit RL2 at RC1_bit;
sbit RL3 at RC2_bit;
sbit RL4 at RC3_bit;
sbit RL5 at RC4_bit;
//defined RL1 at PORTA,1;//PIN 12
//sbit RL2 at RC0_bit;//PIN 10
//sbit RL3 at RC1_bit;//PIN 09
//sbit RL4 at RC2_bit;//PIN 08
//sbit RL5 at PORTA,2;//PIN 11
/*................................................. ...............................
Define LED/ bzr selection port pins
.................................................. ...............................*/
//sbit L1 at Rc5_bit;//PIN 05 hi\lo cut
//sbit L2 at Rc4_bit;//PIN 06 mains normal
//sbit BZR at Rc3_bit;//PIN 07 SOUND
/*................................................. ...............................
Define LED/ bzr selection port pins
.................................................. ...............................*/
sbit SW1 at RA5_bit;//PIN 03 hi\lo cut enabol
sbit SW2 at RA4_bit;//PIN 02 QEK SATART
void main()
{
TRISA = 0x01; // setting RA0 analog input & RA3 -RA5 digital i\p
//TRISC = 0x00; // setting port C as output.
TRISC.RC0=0;
TRISC.RC1=0;
TRISC.RC2=0;
TRISC.RC3=0;
// Configure ADCON0 for channel AN0
ADCON1=0x00;
// ANSEL = 0x01; // RA0 analog input
CMCON = 0x07 ;
while(1)
{
ADCON0=0b00000001; //Left justified, Channel AN0 selected, ADON On
Delay_ms(100) ; //Allow 1ms for holding cap to charge
GO_DONE_bit=1; //Start A/D process
while (GO_DONE_bit==1) //Wait for A/D to finish
{
}
//loop_01:
// L2 = 1;// mains normal on
// L1 = 0;// hi\lo cut off
//BZR = 0;// SOUND off
//RL4 = 0;// Output relay off R4
/*................................................. ...............................
Delay mode selection for user friendly...
.................................................. ...............................*/
// if(SW2)
// {
// Delay_ms(5);
//}
//else
//{
// Delay_ms(1);
//}
/* .................................................. .............................
Main working starts here
.................................................. ................................*/
// L1 = 0; // delay indicator off.
while(1)
{
ADC_Value = ADC_Read(0); // A/D conversion. Pin RA0 is an input.
tlong =(long)ADC_Value*5000; // Convert the result in millivolts
tlong = tlong/1023; // 0..1023 -> 0-5000mV
ch = tlong; // Extract volts (thousands of millivolts)
/*................................................. ...............................
Main stabilizing is done here
.................................................. ...............................*/
if((ch<=4000)&&(ch>=0675))
{
if(ch>1035) //115v
{
if(ch>1260) //140v
{
if(ch>1503) //167 v
{
if(ch>1800) //200v
{
if(ch>2160) //240v
{
if(ch>2592) //= 288vac
{
if(ch>=3115) //= 346vac
{
PORTC =0b1111;
}
else
PORTC =0b0111;
}
else
PORTC =0b1011;
}
else
PORTC =0b0011;
}
else
PORTC =0b1001;
}
else
PORTC =0b0001;
}
else
PORTC =0b1000;
}
else
PORTC =0b0000;
//RL4 = 1;// output relay on.
// L2 = 1; // mains indicator on
// L1 = 0; // high //low voltage indicator off
//BZR = 0; // high //low voltage indicator off
}
/*................................................. ...............................
High voltage protection for load is done here
.................................................. ...............................*/
//if(ch > 2100)
// {
// L1 = 1; // high // low voltage indicator on
//BZR = 1; // cret sound
// RL4 = 0; // relay off
// L2 = 0; // Mains normal indicator off
// goto loop_01;
// }
/*................................................. ...............................
Low voltage protection is done here
.................................................. ...............................*/
//if(ch <= 1300)
//{
// L1 = 1; // high // low voltage indicator on
// BZR = 1; // cret sound
// RL4 = 0; // relay off
// L2 = 0; // Mains normal indicator off
// goto loop_01;
}
}
} // End main()