0
\$\begingroup\$

I am trying to make sure my motor which is connected to my controller circuit would not just start spinning when you first apply power to it as if the Potentiometer is left high before the battery is connected it could be dangerous. here is my code.... and it works on power up, so if the POT is if left high when you first power up the circuit the motor does not spin you must turn the pot all the way down to zero and than it will start working which is what I want, however, the issue here is for some reason when I Increase the duty cycle using the pot the motor stops completely and I would have to zero the pot again, this happens over and over again unless I remove the while loop which reads the pot value, how can I fix this?

void main(void)
{
    unsigned char start=0;

    while (1)
    {
        while (ADCRead()==0)
        {
            start=1;
        }
        CCPR5L= ADCRead()>>2;                  // Function call for PWM 
        if (start && PORTAbits.RA3)
        {
            commutation180anti();    
        }
        else 
        {
            start=0;
        }
    }        
}
\$\endgroup\$
8
  • 2
    \$\begingroup\$ please clean up your source code: remove all superfluous empty lines, ask your editor to consistently indent it, etc. This is literally 10 lines of code that I need to scroll for - nobody will read it. By the way, void main is accepted by most compilers, but main actually returns an int, even if you don't return anything (C is strange like that). \$\endgroup\$ Mar 17 at 17:09
  • \$\begingroup\$ Tip: "pot" is an abbreviation of "potentiometer". It's not an initialisation so it doesn't get all-caps. \$\endgroup\$
    – Transistor
    Mar 17 at 17:12
  • 1
    \$\begingroup\$ Sounds like EMI is resetting your MCU. \$\endgroup\$
    – Andy aka
    Mar 17 at 18:23
  • \$\begingroup\$ I have put a scope on the MCLR pin but it is fine always stays at 5V and I have a 10k pull up resistor on it \$\endgroup\$ Mar 17 at 18:27
  • 1
    \$\begingroup\$ You have start setting back to 0 under an else condition. Else covers a lot of extra stuff. Only set start back to zero under a very specific explicit set of conditions or not at all. \$\endgroup\$
    – DKNguyen
    Mar 17 at 19:36
0
\$\begingroup\$

Maybe this:

void main(void)
{
   while (ADCRead()!=0)
   {
      // think about putting a short sleep here
   }

   while (1)
   {
      CCPR5L= ADCRead()>>2;                  // Function call for PWM 
     
      if (PORTAbits.RA3)
      {
         commutation180anti();    
      }
   }
}        

This code is supposed to wait for the ADC result to go to zero before doing anything else.

\$\endgroup\$
7
  • \$\begingroup\$ I tried this code and it works but again same issue when I increase the pot the motor comes to a stop and I have to zero the pot again to start the motor is really strange but when I remove the fist while loop all is well !! \$\endgroup\$ Mar 17 at 17:31
  • \$\begingroup\$ It sounds like something is making your MCU reset then, because it should never leave the second while loop. \$\endgroup\$ Mar 17 at 19:03
  • \$\begingroup\$ Might be a brown-out from the motor drawing too much current at full speed, or ground-lift from inadequate grounding. \$\endgroup\$ Mar 17 at 19:08
  • 1
    \$\begingroup\$ I just ran some tests I can confirm that the code is fine it is definitely resetting, I have to now find out why it is doing that. \$\endgroup\$ Mar 18 at 9:55
  • 1
    \$\begingroup\$ I have just noticed that the microcontroller only resets when the current limit of my controller is reached, if I disable the current limit than is fine, this is strange ? \$\endgroup\$ Mar 18 at 11:07
2
\$\begingroup\$

I figured out why my MCU was resetting, this was due to the hall effect sensors to the input of the MCU. When my motor hit the current limit the noise from the motor was induced in to the hall effect sensors which are configured inside the motor, which are than connected to the inputs of my Microcontroller, this was fixed with some decoupling capacitors.

\$\endgroup\$
0
\$\begingroup\$

You build an algorithm and hardware such that either your motor is not powered up until you're ready for it to be, or make sure the control signal on power up is such that the motor is stationary. I suppose another option would be to include a brake that is powerful enough to hold a runaway motor.

There really is no magic here -- it's roll up your sleeves type work.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.