/*********************************************************************//** * @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 } }
/*********************************************************************//** * @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; }
/********************************************************************//** * @brief Initializes the MCPWMx channels according to the specified * parameters. * @param[in] MCPWMx MCPWM peripheral selected, should be: * Should be: LPC_MCPWM * @return None *********************************************************************/ void MCPWM_Channel_Config(LPC_MCPWM_TypeDef *MCPWMx, MCPWM_PCFG_TYPE PCfg, MCPWM_MODE_TYPE ModeCfg, MCPWM_INT_TYPE IntCfg) { // MCPWM Channel configuration data MCPWM_CHANNEL_CFG_Type channelsetup; /** Capture configuration data */ MCPWM_CAPTURE_CFG_Type captureCfg; // Pin configuration for MCPWM PINSEL_CFG_Type PinCfg; if(MCPWMx == LPC_MCPWM) { PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; switch (PCfg) { case MCPWM_MR0: // Configure P1.19 as MCOA0 PinCfg.Pinnum = 19; PINSEL_ConfigPin(&PinCfg); // Configure P1.22 as MCOB0 PinCfg.Pinnum = 22; PINSEL_ConfigPin(&PinCfg); channelsetup.channelType = MCPWM_CHANNEL_EDGE_MODE; channelsetup.channelPolarity = MCPWM_CHANNEL_PASSIVE_HI; channelsetup.channelDeadtimeEnable = ENABLE; channelsetup.channelDeadtimeValue = 50; channelsetup.channelUpdateEnable = ENABLE; channelsetup.channelTimercounterValue = 0; channelsetup.channelPeriodValue = 500000; channelsetup.channelPulsewidthValue = 300000; MCPWM_ConfigChannel(MCPWMx, 0, &channelsetup); break; case MCPWM_MR1: // Pins not available break; case MCPWM_MR2: // Pins not available break; case MCPWM_CR0: // Configure P1.20 as MCI0 PinCfg.Pinnum = 20; PINSEL_ConfigPin(&PinCfg); /* * 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(MCPWMx, 0, &captureCfg); // Reset flag for the first time CapFlag = RESET; // Enable interrupt for capture event on MCI0 (MCFB0) MCPWM_IntConfig(MCPWMx, MCPWM_INTFLAG_CAP0, ENABLE); break; case MCPWM_CR1: // Configure P1.23 as MCI1 PinCfg.Pinnum = 23; PINSEL_ConfigPin(&PinCfg); break; case MCPWM_CR2: // Configure P1.24 as MCI2 PinCfg.Pinnum = 24; PINSEL_ConfigPin(&PinCfg); break; default: //Error match value //Error loop while(1); } if (IntCfg == MCPWM_INT_ENABLE) { /* Disable interrupt for MCPWM */ NVIC_DisableIRQ(MCPWM_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(MCPWM_IRQn, 4); } } if (ModeCfg == MCPWM_DC) { /* * - DC mode enabled. * - Invert Output enabled * - A0 and A1 output pin is internally routed to A0 signal */ MCPWM_DCMode(MCPWMx, ENABLE, ENABLE, (MCPWM_PATENT_A0|MCPWM_PATENT_A1)); } else if(ModeCfg == MCPWM_AC) { /* * - AC mode is enabled. */ MCPWM_ACMode(MCPWMx, ENABLE); } if (IntCfg == MCPWM_INT_ENABLE) { /* Enable interrupt for MCPWM */ NVIC_EnableIRQ(MCPWM_IRQn); } }