Can't manage to get if or while or conditionals working

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
bytraper
Posts: 29
Joined: 13 Sep 2012 13:34

Can't manage to get if or while or conditionals working

#1 Post by bytraper » 10 Aug 2015 12:09

Hi People!

I am having nightmares over this stupid code. No matter what I do, I can't make it work, because for some reason I don't understand it seems to skip over the conditionals I have set, regardless if they are true or false.

The pssa is a trigger, on the first run (before the forever loop it is set to 1. Its job is the grab the active value from the potentiometer (read_adc(3) and store those values in PA or PB
the one time run routine takes the values and checks which one is highest, and then ramps up the value until it reaches the original set value or pa or pb (pwm output)
But for some reason I can't understand its not taking any notice of things. I'm more of a basic guy, so I'm really stuck!

Code: Select all

   
   pssa = 0x01;           // *** this tells the controller that this is the first power on. It will be changed to 0 once the soft start ramp up is complete.
   ssb = 11;              // this is the counter for the soft start increment

   // End of Initialization code
  
   
   while (1) {
      //Compute Desired Command Values
      command = read_adc(3);
      // Get command value
      command = command - 512;
      // Scale Value
      command = command / 2;

//   after this has processed, we have our input and the number left in command is either 0-254 or negative 0-254
//   this part of code has been bug checked and works OK


  

  // ****************************
  // ** This part of the code either sets the forward mode (PA) or reverse mode (PB) to active depending on the result of command from the previous routine
  // ** this part of code is bug checked and works OK.

         if(command > 0) { // Forward mode
                          pa = command;
                          pb = 0x00;
                          }

         else             { //Reverse mode
                          pa = 0x00;
                          pb = -command;
                          }
         // end of if loop # 1
         
         
         
       // ************************
       // here is where my problems start.
       // firstly, I check is pssa = 1, this is set prior to this routine in the init code
       // if pssa = 1 then the controller has just been switched on OR the brake has been applied and there needs to be a soft start
       // so we need a new variable that we can count up to either PA or PB (dependant on which is currently active)
       // that variable is SSB. We set it to 11 so that it doesn't clash with the validation code later on.
         
         
         

      // start of new problem loop. Only ever one of these will be counting up, either PA or PB. Never both at the same time.
      
      if ((pssa == 1) && (pa>=12) || (pb >= 12))                   // If the switch is on, then we check if the PA or PB outputs are high enough for us to count to

                     {                                               // if the conditions above are correct, then we have to ramp up ssb to equal whatever PA or PB is currently set to (soft start)
                   Delay_us(10000);                                // if we are here, we have a delay, which stops the soft start ramping up too fast!
                        if (ssb >= pb)                             // Here, I check if SSB is greater than or equal to PB (which is the count up to PB)
                              {                                    // IF SSB has counted up to PB then our soft start ramp up is complete.
                              pssa = 0 ;                           // this turns off the switch
                              ssb = 11 ;                           // this resets ssb back to 11 (so if needed again it can begin the next count up)
                              pb=ssb;                              // this sets pb to the last state of ssb
                              pa=0;                                // sets the other output channel to OFF
                              break;                               // breaks out of the if loop.
                              }
                        if (ssb <= pb)                             // if ssb still hasn't counted up to PB then we need to set pb to ssb and keep counting upwards!
                              {
                              pb = (char)ssb ;
                              pa = 0;
                              ssb++   ;
                              break;
                              }
                                                                         
							                                       // i think there is a problem here (logic)
							  
                          if (ssb >= pa)                           // Here, I check if SSB is greater than or equal to Pa (which is the count up to Pa)
                              {                                    // IF SSB has counted up to Pa then our soft start ramp up is complete.
                              pssa = 0 ;                           // this turns off the switch
                              ssb = 11 ;                           // this resets ssb back to 11 (so if needed again it can begin the next count up)
                              pa=ssb;                              // this sets pa to the last state of ssb
                              pb=0;                                // sets the other output channel to OFF
                              break;                               // breaks out of the if loop.
                              }
                        if (ssb <= pa)                             // if ssb still hasn't counted up to Pa then we need to set pa to ssb and keep counting upwards!
                              {
                              pa = (char)ssb ;
                              pb = 0;
                              ssb++   ;
                              break;
                              }
     
     
                       else                                      // if none of the above is true then we make sure everything is set up for the next time this routine is called
                             {
                              pssa = 0;
                              ssb = 11;
                              break;
                             }
     
   
                     }
     
  

     
             
      
   
   //Check to make sure output value is valid. The reason we have these at 10 and 253 is that we need some middle ground in the potentiometer
   // without this, there is no centre off position on the potentiometer! this gives a small window from 0-10 where there is an off position on either side.
      if (pa > 253) pa = 253;
      if (pa < 10)   pa = 0;
      if (pb > 253) pb = 253;
      if (pb < 10)   pb = 0;

   
   
      //****************************************
      //Set Output, these outputs will always be between 10 and 253 on either one of the channels (pa or pb)
      OCR0A = (char)pa;
      OCR0B = (char)pb;
      //*******************************************
   }
    }

User avatar
viktor.milovanovic
Posts: 240
Joined: 08 Jun 2015 10:09

Re: Can't manage to get if or while or conditionals working

#2 Post by viktor.milovanovic » 11 Aug 2015 11:31

Hello,

Have you tried going through the debugger, and watching the values of your variables?

Best regards,

Viktor Milovanovic

Post Reply

Return to “mikroC PRO for AVR General”