sahu77
Joined: 08 Sep 2011 Posts: 202
|
return case |
Posted: Thu Nov 01, 2012 11:50 am |
|
|
Here is my code, which work as when power on/off pic 16f676.
1- green led toggle up to 3 minutes. After 3 min, green led on continuous.
2- during 3 minutes adc work also. It checks adc value & return as adc case. The cases are:
NORMAL_VOLTAGE
HIGH_VOLTAGE
LOW_VOLTAGE
green led = L1
Code: |
/*****************************************************************************
Timer1 Interrupt, executed every 10 ms
****************************************************************************/
#INT_TIMER1
void TIMER1_isr(void)
{
// Increment time_elasped to keep track of ms
time_elasped++;
// Increment seconds variable if 1000 ms have passed
if (time_elasped == 100) //NORMAL_VOLTAGE led blink freqency @ 1000 ms
{ seconds++;
BlinkLED1_flag=~BlinkLED1_flag;
time_elasped = 0;
}
// If Quick Start switch is set between start up time, set QuickStart_flag
if ((input(SW1)==0) && (seconds <STARTUP_SEC) && (seconds == 5) ) //STARTUP_SEC=180
{ QuickStart_flag = 1;
}
// On start up, blink LED1 for 3 mins
//led1 blink mains normal ,led1 blink & Reads ADC
// both are work in same time. not one by one.
//if led1 blink duration complete then it no blink agen.
//Has three mins passed?
if (seconds > STARTUP_SEC) // STARTUP_SEC = 180
{
BlinkMains_flag=1;
}
//Reset Timer 1
set_timer1(60545);
clear_interrupt(INT_TIMER1);
}
/*****************************************************************************
Main Program
****************************************************************************/
void main()
{
disable_interrupts(GLOBAL);
// Initilize RAM Variables
BlinkMains_flag = 0;
QuickStart_flag = 0;
ErrorConditionExists_flag = 0;
time_elasped = 0;
seconds = 0;
//Timer1 Init
setup_timer_1(T1_INTERNAL | T1_DIV_BY_2); // Use Internal Timer, Prescaler = 1 : 2
set_timer1(60545); // Set Timer1 register to 1 ms @ 4 Mhz
//ADC Init
setup_adc_ports(sAN0 );
setup_adc(ADC_CLOCK_DIV_8);
ADFM = 1; //AD Result Right Justified
setup_comparator(NC_NC);
// Setup PORTA pull ups
port_a_pullups(0b00110000); // Turn on Pullups for RA5 and RA4
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while (TRUE)
{
if (BlinkMains_flag==0)
QuickStartTest();
voltage_type = ProcessMains();
// Quick start here if SW1 is on any time 1 sec
// time_elasped set in interrupt
// Quick start here if SW1 is on during start up
if ( ((QuickStart_flag == 1) || (seconds == 5)) && (voltage_type != HIGH_VOLTAGE) )
voltage_type = NORMAL_VOLTAGE;
switch(voltage_type)
{
case HIGH_VOLTAGE:
output_low(RL5); // Relay Off
ErrorConditionExists_flag = 1;
BlinkLED1();
break;
case LOW_VOLTAGE:
output_low(RL5); // Relay Off
ErrorConditionExists_flag = 1;
BlinkLED1();
break;
case NORMAL_VOLTAGE:
ErrorConditionExists_flag = 0;
if ((BlinkMains_flag==1 || QuickStart_flag == 1 )) // any one active
{
output_high(L1);
output_high(RL5);
}
output_low(L2);
break;
default:
//We shouldn't get here
break;
}
// Delay to make sure a minimum of 2 TADs have gone by before we do another measurement.
delay_ms(5); //1=5
} // Loop Forever
} // End Main
/*****************************************************************************
Function ProcessMains
Reads Form AN1 ADC_Value and returns NORMAL_VOLTAGE, HIGH_VOLTAGE, LOW_VOLTAGE
****************************************************************************/
int ProcessMains(void)
{ ch_1 = getchreading(0); // Extract volts (thousands of millivolts
Process_ch_1(); // Do something with ch_1
if (ch_1>1000)
return(HIGH_VOLTAGE);
// Low Voltage protection is done here
if ((ch_1<=465) && ((input(SW2)==0) || (QuickStart_flag == 1))) //Low Voltage protection enable if SW2 is always on(conect with GND).
return(LOW_VOLTAGE); //if SW2 is always off(no conect with GND ) Low Voltage protection disable
//We have got here, so voltage should be normal
return(NORMAL_VOLTAGE);
}
/*****************************************************************************
Function BlinkLED1
Blink Low LED
****************************************************************************/
void BlinkLED1(void)
{
if (BlinkLED1_flag==0)
{
output_high(L1);
output_low(RL5);
}
else
{
output_low(L1);
output_low(RL5);
}
}
/***************************************************************************
Function QuickStartTest
Quick Start Test LED
****************************************************************************/
void QuickStartTest(void)
{
if (QuickStart_flag==1)
output_high(L1);
else
if (ErrorConditionExists_flag == 0)
BlinkLED1();
}
|
It works normally, but have one problem when adc return(HIGH_VOLTAGE) or return(LOW_VOLTAGE) to return(NORMAL_VOLTAGE). It returns suddenly, but I want Again work 3 min timer which I describe previous. _________________ sahu |
|