static void pwm_2_init(void) { uint8_t i = 0; /* Setup Board specific output pin */ /* Enable SWM clock before altering SWM */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM); /* Connect SCT outputs. */ for(i = 2; i < 4; i++) { Chip_SWM_MovablePortPinAssign(pwm_config[i].pin_mov, pwm_config[i].port, pwm_config[i].pin); } /* Disable SWM clock after altering SWM */ Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM); Chip_SCTPWM_Init(LPC_SCT2); Chip_SCTPWM_SetRate(LPC_SCT2, PWM_2_RATE); for(i = 2; i < 4; i++) { /* Use SCT2_OUTx pin. */ Chip_SCTPWM_SetOutPin(pwm_config[i].sct, pwm_config[i].index, i - 2); /* Start with 0% duty cycle */ Chip_SCTPWM_SetDutyCycle(pwm_config[i].sct, pwm_config[i].index, 0); } Chip_SCTPWM_Start(LPC_SCT2); return; }
/** \brief Main function * * This is the main entry point of the software. * * \returns 0 * * \remarks This function never returns. Return value is only to avoid compiler * warnings or errors. */ void InicializarPWM(void){ /*Funcion inicializa PWM */ Chip_SCTPWM_Init(LPC_SCT); }
/** * Initialize the PWM. * * This function will initialize the PWM. * It will set the global frequency. * And it will enable any enabled pwm channel * * Note: In higher level functions we will use pwm channel 0 - 5, but in fact these are routed to logic channels 1 - 6. * This number conversion has been taken care of in all pwm functions. * We need to do this, because pwm logic channel 0 cannot be used. (See datasheet) * We fix this mapping in this init function, because we do not need to remap afterwards. * @param p_pwm p_pwm device * @param p_pwmConfig configuration data * @return status_ok if succeeded (otherwise check status.h for details). */ status_t PWM_Init(pwm_t* p_pwm, pwm_config_t *p_pwmConfig) { status_t status = status_ok; uint8_t i; /* populate the pwm struct */ p_pwm->frequency = p_pwmConfig->frequency; for(i = 0; i < PWM_AMOUNTOFCHANNELS; i++) { p_pwm->dutycycle[i] = p_pwmConfig->dutycycle[i]; p_pwm->usechannel[i] = p_pwmConfig->usechannel[i]; } /* Initialize the SCT as PWM and set frequency */ Chip_SCTPWM_Init(PWM_SCT); Chip_SCTPWM_SetRate(PWM_SCT, p_pwm->frequency); /* then enable the outputchannel, we here fix pins to logic channel + 1 (because channel 0 should not be used) */ for(i = 0; i < PWM_AMOUNTOFCHANNELS; i++) { if(p_pwm->usechannel[i]) { Chip_SCTPWM_SetOutPin(PWM_SCT, ctout1 + i, ctout0 + i); } } /* then set the dutycycles */ for(i = 0; i < PWM_AMOUNTOFCHANNELS; i++) { if(status == status_ok) { if(p_pwm->usechannel[i]) { status = PWM_setdutycycle(p_pwm, i, p_pwm->dutycycle[i]); } } } if(status == status_ok) { /* start the pwm */ Chip_SCTPWM_Start(PWM_SCT); } return status; }
/* Example entry point */ int main(void) { uint32_t cnt1 = 0, cnt2 = 0; int led_dp = 0, led_step = 1, out_dp = 0; /* Generic Initialization */ SystemCoreClockUpdate(); Board_Init(); /* Initialize the SCT as PWM and set frequency */ Chip_SCTPWM_Init(SCT_PWM); Chip_SCTPWM_SetRate(SCT_PWM, SCT_PWM_RATE); /* Setup Board specific output pin */ app_setup_pin(); /* Use SCT0_OUT1 pin */ Chip_SCTPWM_SetOutPin(SCT_PWM, SCT_PWM_OUT, SCT_PWM_PIN_OUT); Chip_SCTPWM_SetOutPin(SCT_PWM, SCT_PWM_LED, SCT_PWM_PIN_LED); /* Start with 0% duty cycle */ Chip_SCTPWM_SetDutyCycle(SCT_PWM, SCT_PWM_OUT, Chip_SCTPWM_GetTicksPerCycle(SCT_PWM) / 2); Chip_SCTPWM_SetDutyCycle(SCT_PWM, SCT_PWM_LED, 0); Chip_SCTPWM_Start(SCT_PWM); /* Enable SysTick Timer */ SysTick_Config(SystemCoreClock / TICKRATE_HZ); while (1) { cnt1++; cnt2++; if (cnt1 >= OUT_STEP_CNT) { out_dp += 10; if (out_dp > 100) { out_dp = 0; } /* Increase dutycycle by 10% every second */ Chip_SCTPWM_SetDutyCycle(SCT_PWM, SCT_PWM_OUT, Chip_SCTPWM_PercentageToTicks(SCT_PWM, out_dp)); cnt1 = 0; } if (cnt2 >= LED_STEP_CNT) { led_dp += led_step; if (led_dp < 0) { led_dp = 0; led_step = 1; } if (led_dp > 200) { led_dp = 200; led_step = -1; } /* Increment or Decrement Dutycycle by 0.5% every 10ms */ Chip_SCTPWM_SetDutyCycle(SCT_PWM, SCT_PWM_LED, Chip_SCTPWM_PercentageToTicks(SCT_PWM, led_dp) / 2); cnt2 = 0; } __WFI(); } }
void Board_PWM_SetFrequency(uint32_t freq) { Chip_SCTPWM_Init(LPC_SCT); Chip_SCTPWM_SetRate(LPC_SCT, freq); Chip_SCTPWM_Start(LPC_SCT); }
/** * @brief Initializes the SCT(State Configurable Timer) peripheral * * @param[in] frequency Frequency of all PWM channels */ void SctInit(uint16_t frequency){ Chip_SCTPWM_Init(LPC_SCT); Chip_SCTPWM_SetRate(LPC_SCT, frequency); }