Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] If else statement not working with PIC16F886

Status
Not open for further replies.

freemanantony

Member level 4
Joined
May 19, 2010
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chennai, India
Activity points
2,005
hai everybody,
i have done code for current measurement in Pic 16F886 and after measuring current need to switch off output if the current measured is more than the maximum current, but when comparing if the current is less than maximum current it is not hitting the else condition. i am attaching my code here
Code:
 R_Phase_curr: ADCON0 = 0xA9 ;      //0x69  //0xA9          // reading R_ phase current// Changed ADC conversion clock to Fosc/32 from Fosc/8
    delay(10);
    adc_do();
       current_R = adc_val;
       if(current_R > MAX_CURRENT)                                                                                                                                                                                                                       if(current_R > MAX_CURRENT)
       {
          
            R_Phase_ol = 1;
        
            ALERT_FR = 1;
       }
       else if (current_R < MAX_CURRENT)
       {
            R_Phase_ol = 0;
           ALERT_FR = 0;
               
       }
adc_val, curent_R and MAX_CURENT are unsigned long variables
can anyone help me on this ?
 

Hi,

I wonder: why there is twice the same "if(current_R > MAX_CURRENT)" immediately after the other?

******
Maybe this happens:
Everything works fine, until the current is over the limit
Then it is switched OFF
Then because it is switched OFF obviously the current goes to zero
Then the current is measured again (loop)
Because it now it is zero...the load is switched ON
For sure the current rises
It is measured again
Maybe it now again it is over the limit
Therfore it is switched OFF again
..then ON again
..then OFF again
And so on..

Klaus
 

Code:
 R_Phase_curr: ADCON0 = 0xA9 ;      //0x69  //0xA9          // reading R_ phase current// Changed ADC conversion clock to Fosc/32 from Fosc/8
    delay(10);
    adc_do();
       current_R = adc_val;
       if(current_R > MAX_CURRENT)                                                                                                                                                                                                                       if(current_R > MAX_CURRENT)
       {
          
            R_Phase_ol = 1;
        
            ALERT_FR = 1;
       }
       else
       {
            R_Phase_ol = 0;
           ALERT_FR = 0;
               
       }

Try this
 
one condition is (current_R > MAX_CURRENT) and other is (current_R < MAX_CURRENT), even when current is within limits it is not hitting the else if condition

- - - Updated - - -

easyrider i tired that too it hitting the if condition when that condition is true but
not hitting the else condition when that condition is false
 

Hi,

Copied from your code:
Code:
       current_R = adc_val;
     [COLOR="#FF0000"]  if(current_R > MAX_CURRENT)                                                                                                                                                                                                                       if(current_R > MAX_CURRENT)[/COLOR]
       {

Klaus
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top