Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

return case

Status
Not open for further replies.

sahu

Member
here the of my code . which work as when power on of pic 16f676 .
1- green led toggle up to 3 min.ofter 3 min green led on continuous.
2- during 3 min adc work also. it is check adc value & return as adc case. the case are as
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();
}

its work normally .but have 1 problem when adc return(HIGH_VOLTAGE) or return(LOW_VOLTAGE) to return(NORMAL_VOLTAGE) . its return suddenly . i want Agnes work 3 min timer which i discraive previous
 
i think compiler is tripped by 'time_elasped' ;)
just kidding
 
Where you call this "ProcessMains" Don't it have to run that part before you can pick the "select case" Its run after
 
Where you call this "ProcessMains" Don't it have to run that part before you can pick the "select case" Its run after
He calls it at the beginning of the while statement.

Code:
voltage_type = ProcessMains();

Shivendra..... You'll need to explain more... The last time we were here, I couldn't understand what it was you actually wanted.... Can you do a flow chart of what YOU want it to do...
 
Shivendra..... You'll need to explain more... The last time we were here, I couldn't understand what it was you actually wanted.... Can you do a flow chart of what YOU want it to do...

Ya, I'd like to help, but I don't understand what the problem is, or what this is actually supposed to do. Can you not debug this by stepping through the code and setting debug variables?
 
Where you call this "ProcessMains" Don't it have to run that part before you can pick the "select case" Its run after

if posibak can u give 1 exmapal ?

in eving . I will here flow chart
 
Last edited:
He is saying it returns to fast it should wait for a set time .

But His switch case statement have If statements in them which is what a switch case is for

case A A is the same as IF A then
do this
case B same for B
Then you get here
case C and he nest a IF then in it And that may just keep the loop here

But I really think we need more of your code you didn't post it all can't see what is happening for sure with what is posted.
 
Last edited:
But I really think we need more of your code you didn't post it all can't see what is happening for sure with what is posted.

Believe me... You don't.... I have a copy, Its like reading a Chinese menu...

We NEED a flow chart, with the timers he needs to set / unset.. I think hes making a voltage controller to stabilize the very unstable mains where he lives..
 
Well you may be right. It would be best to show a block diagram of what he wants to happen.

But his switch case could get lost la-la land or simply not do what you want it to do.
 
Status
Not open for further replies.

Latest threads

Back
Top