今天刚刚看了看ke02zepwm的例程并用示波器做了测试,但现在有一些问题想问一下各位大神
现附上例程/******************************************************************************
*
* Freescale Semiconductor Inc.
* (c) Copyright 2013 Freescale Semiconductor, Inc.
* ALL RIGHTS RESERVED.
*
***************************************************************************
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
***************************************************************************//*!
*
* @file FTM_EPWM_demo.c
*
* @author Freescale
*
* @version 0.0.1
*
* @date Jun. 25, 2013
*
* @brief Demonstrate EPWM, output pwm from FTM2 Ch1,
LED RED on FRDM-KE02 board will blink as pwm pulse width changing..
*
*******************************************************************************/
#include "common.h"
#include "ics.h"
#include "ftm.h"
#include "uart.h"
#include "sysinit.h"
/******************************************************************************
* Global variables
******************************************************************************/
/******************************************************************************
* Constants and macros
******************************************************************************/
/******************************************************************************
* Local types
******************************************************************************/
/******************************************************************************
* Local function prototypes
******************************************************************************/
/******************************************************************************
* Local variables
******************************************************************************/
/******************************************************************************
* Local functions
******************************************************************************/;
void FTM2_Task(void);
/******************************************************************************
* Global functions
******************************************************************************/
/********************************************************************/
int main (void)
{
/* Perform processor initialization */
sysinit();
SIM_RemapFTM2CH1Pin();
SIM_RemapFTM2CH0Pin();
/* FTM2 is set as edge aligned pwm mode, high true pulse */
FTM_PWMInit(FTM2, FTM_PWMMODE_EDGEALLIGNED, FTM_PWM_HIGHTRUEPULSE);
/* FTMEN enable */
FTM_SetFTMEnhanced(FTM2);
/* update MOD value */
FTM_SetModValue(FTM2,4999);
/* set clock source, start counter */
FTM_ClockSet(FTM2, FTM_CLOCK_SYSTEMCLOCK, FTM_CLOCK_PS_DIV1);
/* enable FTM2 interrupt in NVIC */
NVIC_EnableIRQ(FTM2_IRQn);
/* setup call back function for interrupt */
FTM_SetCallback(FTM2, FTM2_Task);
/* enable FTM2 overflow interrupt */
FTM_EnableOverflowInt(FTM2);
while(1)
{}
}
/*****************************************************************************//*!
*
* @brief callback routine of FTM2 driver. FTM2 CH5 will change the duty cycle
* periodically.
*
* @param none.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
uint16_t u16ChV_old, u16ChV_new;
void FTM2_Task(void)
{
static uint16_t u16count;
static uint8_t u8DirMark=1;
/* clear the flag */
FTM_ClrOverFlowFlag(FTM2);
if(50 == u16count)
{
u16count = 0;
u16ChV_old = FTM2->CONTROLS[1].CnV;
if(!u8DirMark)
{
u16ChV_new = u16ChV_old + 200;
if(u16ChV_new >= FTM2->MOD)
{
u16ChV_new = FTM2->MOD - 200;
u8DirMark = 1;
}
else
{
}
}
else
{
u16ChV_new = u16ChV_old - 200;
if(u16ChV_new < 200)
{
u16ChV_new = 200;
u8DirMark = 0;
}
else
{
}
}
/* update the channel value */
FTM_SetChannelValue(FTM2, FTM_CHANNEL_CHANNEL1, u16ChV_new);
}
else
{
u16count++;
}
}
/********************************************************************/
先说现象
例程烧录后板子上的灯会有呼吸灯的效果同时ptc2 ptc3 ptb5 pth0四个管脚会有PWM输出
现在有三个问题
1、板子上的灯有呼吸灯的效果,但是灯的亮暗本身由什么决定?是PWM的占空比吗?如果是 那么是哪个管脚的?
2、根据FTM_SetChannelValue()的原型,我理解的是第三个参数应该是决定cnv的值,我将其改为定值200后呼吸灯的效果小时,但是四个管脚的pwm占空比却没有变化,如果不做修改,灯会有呼吸灯的效果但是四个管脚输出的pwm的占空比一直是一个值,不随灯的亮度改变(如PTB5一直是15%左右)这是为什么?
3、同样根据FTM_SetChannelValue()我理解第二个参数是决定用那个通道输出pwm波,但是为什么中断里只配值了1通道但是会有四个脚有pwm波?还有就是FTM2 CH1所对应的PTC1并没有PWM波输出 这是为什么?
是我的理解有误吗?如果我想用一个特定的管脚(如PTC1)来输出特定占空比的PWM波(如20%)那么我应该怎么改程序?
先附上FTM_SetChannelValue()的原型
/*****************************************************************************//*!
*
* @brief set the ftm channel value.
*
* @param[in] pFTM pointer to one of three FTM base register address.
* @param[in] u16ChannelValue the CnV value required to set.
* @param[in] u8FTM_Channel ftm channel number.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see FTM_SetModValue.
*
*****************************************************************************/
__STATIC_INLINE void FTM_SetChannelValue(FTM_Type *pFTM, uint8_t u8FTM_Channel, uint16_t u16ChannelValue)
{
pFTM->CONTROLS[u8FTM_Channel].CnV = u16ChannelValue;
if(FTM2 == pFTM)
{
if(pFTM->MODE & FTM_MODE_FTMEN_MASK)
{
if(u8FTM_Channel < 2)
{
pFTM->COMBINE |= FTM_COMBINE_SYNCEN0_MASK; /* enable the sync function */
}
else if (u8FTM_Channel < 4)
{
pFTM->COMBINE |= FTM_COMBINE_SYNCEN1_MASK;
}
else
{
pFTM->COMBINE |= FTM_COMBINE_SYNCEN2_MASK;
}
pFTM->PWMLOAD |= FTM_PWMLOAD_LDOK_MASK;
}
else
{
}
}
else
{
}
}
|