/* 16F676 Configuration
O = Output, I = Input
_________
Vdd | 1 14 | Vss
(I) SW1 --> RA5 | 2 13 | AN0 <-- ADC_Value (I)
(I) SW2 --> RA4 | 3 12 | RA1 --> RL4 (O)
MCLR | 4 11 | RA2 --> RL5 (O)
(O) L1 <-- RC5 | 5 10 | RC0 --> RL1 (O)
(O) L1 <-- RC4 | 6 9 | RC1 --> RL2 (O)
(O) BZR <-- RC3 | 7 8 | RC2 --> RL3 (O)
---------
*/
/*................................................................................
Delay Defines
.................................................................................*/
#define BLINK_DELAY 1 // Change to "1000" for one SEC for real card
#define ADC_DELAY 100 // Change to "1" for 1 ms for real card
/*................................................................................
NORMAL_VOLTAGE / HIGH_VOLTAGE / LOW_VOLTAGE Defines
.................................................................................*/
#define NORMAL_VOLTAGE 0 // Return Value indicates Normal Voltage is present
#define HIGH_VOLTAGE 1 // Return Value indicates High_Voltage is present
#define LOW_VOLTAGE 2 // Return Value indicates Low_Voltage is present
/*................................................. ...............................
Global Variables
.................................................. ...............................*/
unsigned int ADC_Value;
long ch , tlong;
int voltage_type;
/*................................................. ...............................
Define relay selection port pins
.................................................. ...............................*/
sbit RL1 at RC0_bit;
sbit RL2 at RC1_bit;
sbit RL3 at RC2_bit;
sbit RL4 at RA1_bit;
sbit RL5 at RA2_bit;
/*................................................. ...............................
Define LED/ bzr selection port pins
.................................................. ...............................*/
sbit L1 at RC5_bit; //PIN 05 hi\lo cut LED
sbit L2 at RC4_bit; //PIN 06 mains normal LED
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
/*................................................................................
Function prototypes
.................................................................................*/
int ProcessMains(void);
void BlinkLED1(void);
void BlinkLED2(void);
void main()
{
TRISA = 0b00110001; //AN0, RA4, RA5 Input, all others output
TRISC = 0b00000000; //All output
// Configure ADCON0 for channel AN0
ADCON1=0x00; // FOsc/2
ANSEL = 0x01; // RA0 analog input as AN0, all others digital
CMCON = 0x07 ;
while(1)
{
voltage_type = ProcessMains();
BlinkLED1();
BlinkLED2();
/* Below orginal code */
//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.
}
} // End main()
/* Function ProcessMains
Reads ADC Value and process */
int ProcessMains(void)
{
ADCON0=0b00000001; //Left justified, Channel AN0 selected, ADON On
Delay_ms(ADC_DELAY) ; //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
{
}
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;
RL1 = 1;
RL2 = 1;
RL3 = 1;
RL4 = 1;
}
else
{
//PORTC =0b0111;
RL1 = 1;
RL2 = 1;
RL3 = 1;
RL4 = 0;
}
}
else
{
//PORTC =0b1011;
RL1 = 1;
RL2 = 1;
RL3 = 0;
RL4 = 1;
}
}
else
{
//PORTC =0b0011;
RL1 = 1;
RL2 = 1;
RL3 = 0;
RL4 = 0;
}
}
else
{
//PORTC =0b1001;
RL1 = 1;
RL2 = 0;
RL3 = 0;
RL4 = 1;
}
}
else
{
//PORTC =0b0001;
RL1 = 1;
RL2 = 0;
RL3 = 0;
RL4 = 0;
}
}
else
{
//PORTC =0b1000;
RL1 = 0;
RL2 = 0;
RL3 = 0;
RL4 = 1;
}
}
else
{
//PORTC =0b0000;
RL1 = 0;
RL2 = 0;
RL3 = 0;
RL4 = 0;
}
/* Check this code, is this supposed to be RL4? */
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
return(HIGH_VOLTAGE); // Exit function // 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
return(LOW_VOLTAGE); // Exit function // goto loop_01;
}
return(NORMAL_VOLTAGE);
}
/*................................................. ...............................
Blink Mains LED -- Normal Mode
.................................................. ...............................*/
void BlinkLED1(void)
{
int x;
for(x=0; x<=9; x++) // Repeat 10 times, count from 0 to 9
{
L1 = 1; // Turn on LED
delay_ms(BLINK_DELAY);
L1 = 0; // Turn off LED
}
}
void BlinkLED2(void)
{
int x;
for(x=0; x<=9; x++) // Repeat 10 times, count from 0 to 9
{
L2 = 1; // Turn on LED
delay_ms(BLINK_DELAY);
L2 = 0; // Turn off LED
}
}