Ejemplo n.º 1
0
/*!
  Sets the frequency of PWM waveform according to gear specified and applies the duty cycle.
  
  \param duty: A value between 0-255 
  \param gear: Gear(frequency) that should be used (USE \link GEAR1 \endlink, 
      \link GEAR2 \endlink, \link GEAR3 \endlink, \link GEAR4 \endlink)
      
  \note: This function does not implement any protections while applying the duty and frequency. 
*/
void Motor::applyPWM(int16_t duty, uint16_t gear){
  
  // Set the frequency of PWM according to said gear.
  _gear = gear;
  setPWMFrequency(pinPWM, gear);
  
  // Free Run
  if (abs(duty) > 255) {
    pinMode(pinPWM, INPUT);
    return;
  }
  
  // Braking
  else if (abs(duty) == 0) {
    pinMode(pinPWM, OUTPUT);
    digitalWrite(pinIN1, HIGH);
    digitalWrite(pinIN2, HIGH);
    return;
  }
  
  // Run Mode
  else {
    pinMode(pinPWM, OUTPUT);
    digitalWrite(pinIN1,  (duty > 0));
    digitalWrite(pinIN2, !(duty > 0));
    analogWrite(pinPWM, abs(duty));
  }
  
}
Ejemplo n.º 2
0
/*******************************************************************************//**
* \brief Setup the PWM peripheral frequency and default duty cycle
*
* > This function is called to initialize the PWM peripheral frequency and default
* > duty cycle. The frequency can be set between 1.22kHz and 200kHz with 10Hz resolution,
* > while the duty cycle has a 0.1% resolution
*
* > <BR>
* > **Syntax:**<BR>
* >      setupPWM(module, frequency, dutycycle)
* > <BR><BR>
* > **Parameters:**<BR>
* >     module - PWM module assignment, PWM0, PWM1                          <BR>
* >     frequency - the required PWM frequency in 10Hz resolution           <BR>
* >     dutycycle - the required PWM duty cycle in 0.1% resolution
* > <BR><BR>
* > **Returns:**<BR>
* >     none
* > <BR><BR>
***********************************************************************************/
void setupPWM(enum PWMModules_et ePWM_Module, uint16_t ui16Frequency, uint16_t ui16DutyCycle)
{
	/* Disable PWM Timer Interrupt */
	hal_disablePWMTmrInt();
    /* Set the PWM period */
    setPWMFrequency(ui16Frequency);                 // Common for CCP1 and CCP2
    /* Load the default PWM duty value */
    setPWMDuty(ePWM_Module,ui16DutyCycle);
    /* Configure Timer */
    /* Select Source */
    //mc_PWMClk_Source(CCP_PWM_CLOCK);              // not for PIC16F877A
    /* Clear TMRxIF */
    hal_clrPWMTmrIntFlag() ;                        // not critical
    /* Configure Prescaler value */
    hal_initPWMTimer(ui8PreScalerVal);              // Common for CCP1 and CCP2

    /* Configure CCP to PWM mode */
    if(PWM0 == ePWM_Module)
    {
        hal_configCCP1Mode(CCP_PWM_MODE);
        mc_makeOutput(D2);
    }
    else if(PWM1 == ePWM_Module)
    {
        hal_configCCP2Mode(CCP_PWM_MODE);
        mc_makeOutput(D1);
    }
    else
    {
        /* Invalid PWM Module */
        /* Do Nothing */
    }

    /* Enable Timer */
    hal_enablePWMTmr();                             // Common for CCP1 and CCP2
}