/********************************************************************//** * @brief Initializes the MCPWMx Peripheral according to the specified * parameters. * @param[in] MCPWMx MCPWM peripheral selected, should be: * Should be: LPC_MCPWM * @return None *********************************************************************/ void MCPWM_Config (void) { /* Init MCPWM peripheral */ MCPWM_Init(LPC_MCPWM); /* MCPWM Channel Config */ MCPWM_Channel_Config(LPC_MCPWM, MCPWM_MR0, None, MCPWM_INT_DISABLE); //MCPWM_Channel_Config(LPC_MCPWM, MCPWM_CR0, None, MCPWM_INT_ENABLE); /*Start PWM Channels */ MCPWM_Start(LPC_MCPWM, ENABLE, DISABLE, DISABLE); }
/*********************************************************************//** * @brief c_entry: Main MCPWM program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // MCPWM Channel configuration data MCPWM_CHANNEL_CFG_Type channelsetup[3]; uint32_t i; PINSEL_CFG_Type PinCfg; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); _DBG_("Hello MCPWM ..."); /* Pin configuration for MCPWM function: * Assign: - P1.19 as MCOA0 - Motor Control Channel 0 Output A * - P1.22 as MCOB0 - Motor Control Channel 0 Output B * - P1.25 as MCOA1 - Motor Control Channel 1 Output A * - P1.26 as MCOB1 - Motor Control Channel 1 Output B * - P1.28 as MCOA2 - Motor Control Channel 2 Output A * - P1.29 as MCOB2 - Motor Control Channel 2 Output B * - P1.20 as MCI0 - Motor Control Feed Back Channel 0 * Warning: According to Errata.lpc1768-18.March.2010: Input pin (MIC0-2) * on the Motor Control PWM peripheral are not functional */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 19; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 22; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 25; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 26; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 29; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 20; PINSEL_ConfigPin(&PinCfg); /* Disable interrupt for MCPWM */ NVIC_DisableIRQ(MCPWM_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(MCPWM_IRQn, ((0x01<<3)|0x01)); /* Init MCPWM peripheral */ MCPWM_Init(LPC_MCPWM); channelsetup[0].channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup[0].channelPolarity = MCPWM_CHANNEL_PASSIVE_LO; channelsetup[0].channelDeadtimeEnable = DISABLE; channelsetup[0].channelDeadtimeValue = 0; channelsetup[0].channelUpdateEnable = ENABLE; channelsetup[0].channelTimercounterValue = 0; channelsetup[0].channelPeriodValue = 300; channelsetup[0].channelPulsewidthValue = 0; channelsetup[1].channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup[1].channelPolarity = MCPWM_CHANNEL_PASSIVE_LO; channelsetup[1].channelDeadtimeEnable = DISABLE; channelsetup[1].channelDeadtimeValue = 0; channelsetup[1].channelUpdateEnable = ENABLE; channelsetup[1].channelTimercounterValue = 0; channelsetup[1].channelPeriodValue = 300; channelsetup[1].channelPulsewidthValue = 100; channelsetup[2].channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup[2].channelPolarity = MCPWM_CHANNEL_PASSIVE_LO; channelsetup[2].channelDeadtimeEnable = DISABLE; channelsetup[2].channelDeadtimeValue = 0; channelsetup[2].channelUpdateEnable = ENABLE; channelsetup[2].channelTimercounterValue = 0; channelsetup[2].channelPeriodValue = 300; channelsetup[2].channelPulsewidthValue = 200; MCPWM_ConfigChannel(LPC_MCPWM, 0, &channelsetup[0]); MCPWM_ConfigChannel(LPC_MCPWM, 1, &channelsetup[1]); MCPWM_ConfigChannel(LPC_MCPWM, 2, &channelsetup[2]); #if DC_MODE_TEST /* * - DC mode enabled. * - Invert Output enabled * - A0 and A1 output pin is internally routed to A0 signal */ MCPWM_DCMode(LPC_MCPWM, ENABLE, ENABLE, (MCPWM_PATENT_A0|MCPWM_PATENT_A1)); #endif #if AC_MODE_TEST /* * - AC mode is enabled. */ MCPWM_ACMode(LPC_MCPWM, ENABLE); #endif #if CAPTURE_MODE_TEST /* * Capture mode in this case is used to detect the falling edge on MCO0B output pin. * The MCFB0 input pin therefore must be connected to MCO0B. (P1.20 - P1.22) * - Capture Channel 0. * - Capture falling edge on MCFB0 input pin. * - Interrupt enabled on capture event. */ captureCfg.captureChannel = 0; captureCfg.captureFalling = ENABLE; captureCfg.captureRising = DISABLE; captureCfg.hnfEnable = DISABLE; captureCfg.timerReset = DISABLE; MCPWM_ConfigCapture(LPC_MCPWM, 0, &captureCfg); // Reset flag for the first time CapFlag = RESET; // Enable interrupt for capture event on MCI0 (MCFB0) MCPWM_IntConfig(LPC_MCPWM, MCPWM_INTFLAG_CAP0, ENABLE); /* Enable interrupt for MCPWM */ NVIC_EnableIRQ(MCPWM_IRQn); #endif MCPWM_Start(LPC_MCPWM, ENABLE, ENABLE, ENABLE); // Main loop while (1) { // Timer_Wait(LPC_TIM0, 1000); //delay for(i=0; i<100000; i++); channelsetup[0].channelPulsewidthValue = (channelsetup[0].channelPulsewidthValue >= 300) ? 0 : channelsetup[0].channelPulsewidthValue + 20; channelsetup[1].channelPulsewidthValue = (channelsetup[1].channelPulsewidthValue >= 300) ? 0 : channelsetup[1].channelPulsewidthValue + 20; channelsetup[2].channelPulsewidthValue = (channelsetup[2].channelPulsewidthValue >= 300) ? 0 : channelsetup[2].channelPulsewidthValue + 20; _DBG_("Update!"); MCPWM_WriteToShadow(LPC_MCPWM, 0, &channelsetup[0]); MCPWM_WriteToShadow(LPC_MCPWM, 1, &channelsetup[1]); MCPWM_WriteToShadow(LPC_MCPWM, 2, &channelsetup[2]); #if CAPTURE_MODE_TEST // Check capture flag is set or not if (CapFlag) { // Print out the value _DBG("Capture Value: "); _DBD32(CapVal); _DBG_(""); // Setup a new capture event MCPWM_ConfigCapture(LPC_MCPWM, 0, &captureCfg); // Re-Enable interrupt for capture event on MCI0 (MCFB0) MCPWM_IntConfig(LPC_MCPWM, MCPWM_INTFLAG_CAP0, ENABLE); // Reset flag CapFlag = RESET; } #endif } /* Loop forever */ return 1; }
int main (void) { MCPWM_CHANNEL_SETUP_T channelsetup[3]; /* SystemClockUpdate() updates the SystemFrequency variable */ SystemClockUpdate(); MCPWM_Init(); channelsetup[0].channelType = 0; channelsetup[0].channelPolarity = 0; channelsetup[0].channelDeadtimeEnable = 0; channelsetup[0].channelDeadtimeValue = 0; #if SHADOW_UPDATE channelsetup[0].channelUpdateEnable = 1; #else channelsetup[0].channelUpdateEnable = 0; #endif channelsetup[0].channelTimercounterValue = 0; channelsetup[0].channelPeriodValue = 300; channelsetup[0].channelPulsewidthValue = 150; channelsetup[1].channelType = 0; channelsetup[1].channelPolarity = 0; channelsetup[1].channelDeadtimeEnable = 0; channelsetup[1].channelDeadtimeValue = 0; #if SHADOW_UPDATE channelsetup[1].channelUpdateEnable = 1; #else channelsetup[1].channelUpdateEnable = 0; #endif channelsetup[1].channelTimercounterValue = 0; channelsetup[1].channelPeriodValue = 300; channelsetup[1].channelPulsewidthValue = 150; channelsetup[2].channelType = 0; channelsetup[2].channelPolarity = 0; channelsetup[2].channelDeadtimeEnable = 0; channelsetup[2].channelDeadtimeValue = 0; #if SHADOW_UPDATE channelsetup[2].channelUpdateEnable = 1; #else channelsetup[2].channelUpdateEnable = 0; #endif channelsetup[2].channelTimercounterValue = 0; channelsetup[2].channelPeriodValue = 300; channelsetup[2].channelPulsewidthValue = 150; if (!MCPWM_Config(0, &channelsetup[0])) { return 0; } if (!MCPWM_Config(1, &channelsetup[1])) { return 0; } if (!MCPWM_Config(2, &channelsetup[2])) { return 0; } #if CHANNEL_0_CAPTURE MCPWM_CaptureEvent(0, 0x01, 1, 0); #endif #if CHANNEL_1_CAPTURE MCPWM_CaptureEvent(1, 0x04, 1, 0); #endif #if CHANNEL_2_CAPTURE MCPWM_CaptureEvent(2, 0x10, 1, 0); #endif #if AC_MODE MCPWM_acMode(1); #endif #if DC_MODE MCPWM_dcMode(1, 1, 0x3F); #endif LPC_GPIO3->FIODIR = 0x02000000; LPC_GPIO3->FIOSET = 0x02000000; delayMs(0, 20); LPC_GPIO3->FIOCLR = 0x02000000; MCPWM_Start(1,1,1); for (;;) { #if SHADOW_UPDATE delayMs(0, 10000); channelsetup[0].channelPeriodValue = 150; channelsetup[0].channelPulsewidthValue = 75; channelsetup[1].channelPeriodValue = 150; channelsetup[1].channelPulsewidthValue = 75; channelsetup[2].channelPeriodValue = 150; channelsetup[2].channelPulsewidthValue = 75; if (!MCPWM_WriteToShadow(0, &channelsetup[0])) { return 0; } if (!MCPWM_WriteToShadow(1, &channelsetup[1])) { return 0; } if (!MCPWM_WriteToShadow(2, &channelsetup[2])) { return 0; } delayMs(0, 10000); channelsetup[0].channelPeriodValue = 300; channelsetup[0].channelPulsewidthValue = 150; channelsetup[1].channelPeriodValue = 300; channelsetup[1].channelPulsewidthValue = 150; channelsetup[2].channelPeriodValue = 300; channelsetup[2].channelPulsewidthValue = 150; if (!MCPWM_WriteToShadow(0, &channelsetup[0])) { return 0; } if (!MCPWM_WriteToShadow(1, &channelsetup[1])) { return 0; } if (!MCPWM_WriteToShadow(2, &channelsetup[2])) { return 0; } #endif } }
/*********************************************************************//** * @brief c_entry: Main MCPWM program body * @param[in] None * @return None **********************************************************************/ void c_entry(void) { // MCPWM Channel configuration data MCPWM_CHANNEL_CFG_Type channelsetup[3]; uint32_t i; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); _DBG_(menu); /* Pin configuration for MCPWM function: * Assign: - P1.19 as MCOA0 - Motor Control Channel 0 Output A * - P1.22 as MCOB0 - Motor Control Channel 0 Output B * - P1.25 as MCOA1 - Motor Control Channel 1 Output A * - P1.26 as MCOB1 - Motor Control Channel 1 Output B * - P1.28 as MCOA2 - Motor Control Channel 2 Output A * - P1.29 as MCOB2 - Motor Control Channel 2 Output B * - P1.20 as MCI0 - Motor Control Feed Back Channel 0 * Warning: According to Errata.lpc1768-18.March.2010: Input pin (MIC0-2) * on the Motor Control PWM peripheral are not functional */ //MCOA0 — Motor control PWM channel 0, output A PINSEL_ConfigPin (1, 19, 4); //MCI0 — Motor control PWM channel 0 input PINSEL_ConfigPin (1, 20, 4); //MCOB0 — Motor control PWM channel 0, output B PINSEL_ConfigPin (1, 22, 4); // Motor control PWM channel 1, output A. PINSEL_ConfigPin (1, 25, 4); //MCOB1 — Motor control PWM channel 1, output B. PINSEL_ConfigPin (1, 26, 4); //MCOA2 — Motor control PWM channel 2, output A. PINSEL_ConfigPin (1, 28, 4); //MCOB2 — Motor control PWM channel 2, output B. PINSEL_ConfigPin (1, 29, 1); /* Disable interrupt for MCPWM */ NVIC_DisableIRQ(MCPWM_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(MCPWM_IRQn, ((0x01<<3)|0x01)); /* Init MCPWM peripheral */ MCPWM_Init(LPC_MCPWM); channelsetup[0].channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup[0].channelPolarity = MCPWM_CHANNEL_PASSIVE_LO; channelsetup[0].channelDeadtimeEnable = DISABLE; channelsetup[0].channelDeadtimeValue = 0; channelsetup[0].channelUpdateEnable = ENABLE; channelsetup[0].channelTimercounterValue = 0; channelsetup[0].channelPeriodValue = 300; channelsetup[0].channelPulsewidthValue = 0; channelsetup[1].channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup[1].channelPolarity = MCPWM_CHANNEL_PASSIVE_LO; channelsetup[1].channelDeadtimeEnable = DISABLE; channelsetup[1].channelDeadtimeValue = 0; channelsetup[1].channelUpdateEnable = ENABLE; channelsetup[1].channelTimercounterValue = 0; channelsetup[1].channelPeriodValue = 300; channelsetup[1].channelPulsewidthValue = 100; channelsetup[2].channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup[2].channelPolarity = MCPWM_CHANNEL_PASSIVE_LO; channelsetup[2].channelDeadtimeEnable = DISABLE; channelsetup[2].channelDeadtimeValue = 0; channelsetup[2].channelUpdateEnable = ENABLE; channelsetup[2].channelTimercounterValue = 0; channelsetup[2].channelPeriodValue = 300; channelsetup[2].channelPulsewidthValue = 200; MCPWM_ConfigChannel(LPC_MCPWM, MCPWM_CHANNEL_0, &channelsetup[0]); MCPWM_ConfigChannel(LPC_MCPWM, MCPWM_CHANNEL_1, &channelsetup[1]); MCPWM_ConfigChannel(LPC_MCPWM, MCPWM_CHANNEL_2, &channelsetup[2]); #if (MCPWM_WORKING_MODE == DC_MODE_TEST) /* * - DC mode enabled. * - Invert Output enabled * - A0 and A1 output pin is internally routed to A0 signal */ MCPWM_DCMode(LPC_MCPWM, ENABLE, ENABLE, (MCPWM_PATENT_A0|MCPWM_PATENT_A1)); #elif (MCPWM_WORKING_MODE == AC_MODE_TEST) // AC mode is enabled. MCPWM_ACMode(LPC_MCPWM, ENABLE); #endif #if CAPTURE_MODE_TEST /* * Capture mode in this case is used to detect the falling edge on MCO0B output pin. * The MCFB0 input pin therefore must be connected to MCO0B. (P1.20 - P1.22) * - Capture Channel 0. * - Capture falling edge on MCFB0 input pin. * - Interrupt enabled on capture event. */ captureCfg.captureChannel = MCPWM_CHANNEL_0; captureCfg.captureFalling = ENABLE; captureCfg.captureRising = DISABLE; captureCfg.hnfEnable = DISABLE; captureCfg.timerReset = DISABLE; MCPWM_ConfigCapture(LPC_MCPWM, MCPWM_CHANNEL_0, &captureCfg); // Reset flag for the first time CapFlag = RESET; // Enable interrupt for capture event on MCI0 (MCFB0) MCPWM_IntConfig(LPC_MCPWM, MCPWM_INTFLAG_CAP0, ENABLE); /* Enable interrupt for MCPWM */ NVIC_EnableIRQ(MCPWM_IRQn); #endif MCPWM_Start(LPC_MCPWM, ENABLE, ENABLE, ENABLE); // Main loop while (1) { //delay for(i = 0; i < 100000; i++); channelsetup[0].channelPulsewidthValue = (channelsetup[0].channelPulsewidthValue >= 300) ? 0 : channelsetup[0].channelPulsewidthValue + 20; channelsetup[1].channelPulsewidthValue = (channelsetup[1].channelPulsewidthValue >= 300) ? 0 : channelsetup[1].channelPulsewidthValue + 20; channelsetup[2].channelPulsewidthValue = (channelsetup[2].channelPulsewidthValue >= 300) ? 0 : channelsetup[2].channelPulsewidthValue + 20; _DBG_("Update!"); MCPWM_WriteToShadow(LPC_MCPWM, MCPWM_CHANNEL_0, &channelsetup[0]); MCPWM_WriteToShadow(LPC_MCPWM, MCPWM_CHANNEL_1, &channelsetup[1]); MCPWM_WriteToShadow(LPC_MCPWM, MCPWM_CHANNEL_2, &channelsetup[2]); #if CAPTURE_MODE_TEST // Check capture flag is set or not if (CapFlag) { // Print out the value _DBG("Capture Value: "); _DBD32(CapVal); _DBG_(""); // Must be re-configure the Capture Feature as below for the next capturing, //unless, it will not capture anything more. // Setup a new capture event MCPWM_ConfigCapture(LPC_MCPWM, MCPWM_CHANNEL_0, &captureCfg); // Re-Enable interrupt for capture event on MCI0 (MCFB0) MCPWM_IntConfig(LPC_MCPWM, MCPWM_INTFLAG_CAP0, ENABLE); // Reset flag CapFlag = RESET; } #endif } }