⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm.c

📁 摩托罗拉MMC2107在ucosII的移植代码
💻 C
字号:
#include "pwm.h"

/*********************************************************************
   Macro Definitions                                               
/*********************************************************************/

/*********************************************************************
   Variable Definitions                                               
/*********************************************************************/
UINT32 MaxPulseWidth[ TIM_A_CHANNELS ];

/*
*********************************************************************************************************
*                                           INIT PWM
* Description: This function is called to initialize Pulse-Width Modulation mechanism.
*              The mechanism includes 4 Output Compare Channels.
*
* Arguments  : none
*********************************************************************************************************
*/
PWM_ReturnCode_t PWM_Init( void )
{
    TIM_A_ReturnCode_t status;
    
    /***********************************************************
    Enable Timer 1 with 1:1 prescaler based on System Clock
    ************************************************************/
    status = TIM_A_Init( (pTIM_A_t)__MMC2107_TIM1,
                    TIM_A_FUNCTION_ENABLED,
                    TIM_A_FAST_CLEAR_DISABLED,
                    TIM_A_INTERRUPT_DISABLED,
                    TIM_A_PULLUP_INPUT_DISABLED,
                    TIM_A_DRV_REDUCED_OUTPUT_DISABLED,
                    TIM_A_RESET_COMPARE_3_DISABLED,
                    TIM1_PRE
    );
    
    HANDLE_ERROR( status );
    
    return PWM_ERR_NONE;
}

PWM_ReturnCode_t PWM_ConfigChannel(  TIM_A_Channel_t           Ch,
                                     TIM_A_OutputAction_t      OutputAction,
                                     UINT32                    MaxWidth )
{
    TIM_A_ReturnCode_t status;
    
    MaxPulseWidth[ Ch ] = MaxWidth;
    /***********************************************************
    Set requested channel of timer 1 so that:
    1) its output will toggle on timer overflow (0xFFFF).
    2) it will be cleared upon reaching its compare value.
    !!! Initial compare value should be 0x1 (see function
    "PWM_SetPulseWidth" for the explanation).
    ************************************************************/
    status = TIM_A_ConfigChannelOuputCompare_f( (pTIM_A_t)__MMC2107_TIM1,
                    Ch,
                    TIM_A_OUTPUT_COMPARE_3_MASK_DISABLED,
                    TIM_A_TOGGLE_ON_OVERFLOW_ENABLED,
                    TIM_A_OUTPUT_ACTION_CLEAR,
                    TIM_A_INTERRUPT_DISABLED,
                    0x0001 /* initial compare value */
    );
    
    HANDLE_ERROR( status );
    
    return PWM_ERR_NONE;
}

PWM_ReturnCode_t PWM_SetPulseWidth(  TIM_A_Channel_t           Ch,
                                     UINT32                    Width )
{
    TIM_A_ReturnCode_t status;
    
    if( Width > MaxPulseWidth[ Ch ] )
        return PWM_ERR_WIDTH_OVERFLOW;
    
    /***********************************************************
    Convert pulse width from user-defined units to compare value
    of requested Channel in Timer 1 dedicated to PWM.
    ************************************************************/
    Width *= TIM_A_MAX_COMPARE_VALUE;
    Width /= MaxPulseWidth[ Ch ];
    
    /***********************************************************
    Be aware! Compare value 0x0000 has no effect when channel is set
    to be toggled on timer overflow. The reason for this is that
    both of them happen when counter rolls over from 0xFFFF to
    0x0000. As a result, two events create a collision, trying
    to change a signal in opposite directions. Timer overflow takes
    over Output Compare.
    Constant toggling on timer overflow will create the dynamic
    in which half of timer periods will be full-scaled (remember,
    initially 0 was written into Compare Value).
    To prevent this, we set compare value to 1 so Output Compare
    could be executed. From user point of view, the difference
    between 0 and 1 on 65535 scale is practically unnoticeable.
    ************************************************************/
    if( Width == 0x0000 ) Width = 0x0001;
    
    status = TIM_A_SetCompareValue_f( (pTIM_A_t)__MMC2107_TIM1,
                        Ch,
                        Width
    );
    
    HANDLE_ERROR( status );
    
    return (PWM_ERR_NONE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -