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

📄 75x_pwm.c

📁 嵌入式实验源码。包括:电源管理、复位、时钟管理
💻 C
📖 第 1 页 / 共 3 页
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 75x_pwm.c
* Author             : MCD Application Team
* Date First Issued  : 03/10/2006 : V0.1
* Description        : This file provides all the PWM software functions.
********************************************************************************
* History:
* 03/10/2006 : V0.1
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "75x_pwm.h"
#include "75x_mrcc.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* PWM interrupt masks */
#define PWM_IT_Clear_Mask    0x7FFF
#define PWM_IT_Enable_Mask   0xEFFF

/* PWM_CR Masks bit */
#define PWM_CounterMode_Mask           0xFF8F
#define PWM_DBASE_Mask                 0x077F
#define PWM_MasterModeSelection_Mask   0xFC7F

/* PWM Update flag selection Set/Reset value */
#define PWM_UFS_Reset 0xFFFE
#define PWM_UFS_Set   0x0001

/* PWM Counter value */
#define PWM_COUNTER_Reset  0x0002
#define PWM_COUNTER_Start  0x0004
#define PWM_COUNTER_Stop   0xFFFB

/* PWM Debug Mode Set/Reset value */
#define PWM_DBGC_Set    0x0400
#define PWM_DBGC_Reset  0xFBFF

/* PWM Output Compare Polarity Set/Reset value */
#define PWM_OC1P_Set    0x0020
#define PWM_OC1P_Reset  0xFFDF

#define PWM_OC1NP_Set    0x0080
#define PWM_OC1NP_Reset  0xFF7F

#define PWM_OC2P_Set    0x2000
#define PWM_OC2P_Reset  0xDFFF

#define PWM_OC2NP_Set    0x8000
#define PWM_OC2NP_Reset  0x7FFF

#define PWM_OC3P_Set     0x0020
#define PWM_OC3P_Reset   0xFFDF

#define PWM_OC3NP_Set    0x0080
#define PWM_OC3NP_Reset  0xFF7F

/* PWM Output Compare control mode constant */
#define PWM_OCControl_PWM         0x000C
#define PWM_OCControl_OCToggle    0x0006
#define PWM_OCControl_OCInactive  0x0004
#define PWM_OCControl_OCActive    0x0002
#define PWM_OCControl_OCTiming    0x0000

/* PWM Output Compare mode Enable value */
#define PWM_OC1_Enable  0x0010
#define PWM_OC2_Enable  0x1000
#define PWM_OC3_Enable  0x0010

#define PWM_OC1_Disable  0xFFEF
#define PWM_OC2_Disable  0xEFFF
#define PWM_OC3_Disable  0xFFEF

#define PWM_OC1N_Enable  0x0040
#define PWM_OC2N_Enable  0x4000
#define PWM_OC3N_Enable  0x0040

#define PWM_OC1N_Disable  0xFFBF
#define PWM_OC2N_Disable  0xBFFF
#define PWM_OC3N_Disable  0xFFBF

/* PWM Output Compare mode Mask value */
#define PWM_OC1C_Mask  0xFFF1
#define PWM_OC2C_Mask  0xF1FF
#define PWM_OC3C_Mask  0xFFF1

/* PWM Preload bit Set/Reset value */
#define PWM_PLD1_Set    0x0001
#define PWM_PLD2_Set   0x0100
#define PWM_PLD3_Set   0x0001

/* PWM OCRM Set/Reset value */
#define PWM_OCMR_Set    0x0080
#define PWM_OCMR_Reset  0xFF7F

/* PWM_DTR bit Masks value */
#define PWM_DTR_Mask   0xFC00
#define PWM_LOCK_Mask  0xF3FF

/* PWM MOE Set value */
#define PWM_MOE_Set    0x8000
#define PWM_MOE_Reset  0x7FFF

/* PWM OSSR bit Set/Reset value */
#define PWM_OSSR_Set    0x4000
#define PWM_OSSR_Reset  0xBFFF

/* Reset Register Masks */
#define PWM_Prescaler_Reset_Mask          0x0000
#define PWM_Pulse1_Reset_Mask             0x0000
#define PWM_Pulse2_Reset_Mask             0x0000
#define PWM_Pulse3_Reset_Mask             0x0000
#define PWM_Period_Reset_Mask             0xFFFF
#define PWM_RepetitionCounter_Reset_Mask  0x0000
#define PWM_DeadTime_Reset_Mask           0x0000

/* Private function prototypes -----------------------------------------------*/
static void OCM_ModuleConfig(PWM_InitTypeDef* PWM_InitStruct);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
* Function Name  : PWM_DeInit
* Description    : Deinitializes PWM peripheral registers to their default reset
*                  values.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_DeInit(void)
{
  /* Enters and exits the PWM peripheral to and from reset */
  MRCC_PeripheralSWResetConfig(MRCC_Peripheral_PWM,ENABLE);
  MRCC_PeripheralSWResetConfig(MRCC_Peripheral_PWM,DISABLE);
}

/*******************************************************************************
* Function Name  : PWM_Init
* Description    : Initializes the PWM peripheral according to the specified
*                  parameters in the PWM_InitStruct .
* Input          : PWM_InitStruct: pointer to a PWM_InitTypeDef structure that
*                  contains the configuration information for the PWM peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_Init(PWM_InitTypeDef* PWM_InitStruct)
{
  /* Sets the prescaler value */
  PWM->PSC = PWM_InitStruct->PWM_Prescaler;

  /* Selects the counter mode */
  PWM->CR &= PWM_CounterMode_Mask;
  PWM->CR |= PWM_InitStruct->PWM_CounterMode;

  /* Sets the period value */
  PWM->ARR = PWM_InitStruct->PWM_Period;
  
  /* Sets the repetition counter */
  PWM->RCR &= PWM_RepetitionCounter_Reset_Mask;
  PWM->RCR |= PWM_InitStruct->PWM_RepetitionCounter;
  
  /* Configures the PWM according to the PWM_InitTypeDef structure parameters */
  OCM_ModuleConfig(PWM_InitStruct);
}

/*******************************************************************************
* Function Name  : PWM_StructInit
* Description    : Fills each PWM_InitStruct member with its default value.
* Input          : PWM_InitStruct : pointer to a PWM_InitTypeDef structure which
*                  will be initialized.
* Output         : None                        
* Return         : None.
*******************************************************************************/
void PWM_StructInit(PWM_InitTypeDef *PWM_InitStruct)
{
  /* Sets the default configuration */
  PWM_InitStruct->PWM_Mode = PWM_Mode_OCTiming;
  PWM_InitStruct->PWM_Prescaler = PWM_Prescaler_Reset_Mask;
  PWM_InitStruct->PWM_CounterMode = PWM_CounterMode_Up;
  PWM_InitStruct->PWM_Period = PWM_Period_Reset_Mask;
  PWM_InitStruct->PWM_Complementary = PWM_Complementary_Disable;
  PWM_InitStruct->PWM_OCState = PWM_OCState_Disable;
  PWM_InitStruct->PWM_OCNState = PWM_OCNState_Disable;
  PWM_InitStruct->PWM_Channel = PWM_Channel_1;
  PWM_InitStruct->PWM_Pulse1 = PWM_Pulse1_Reset_Mask;
  PWM_InitStruct->PWM_Pulse2 = PWM_Pulse2_Reset_Mask;
  PWM_InitStruct->PWM_Pulse3 = PWM_Pulse3_Reset_Mask;
  PWM_InitStruct->PWM_Polarity1 = PWM_Polarity1_High;
  PWM_InitStruct->PWM_Polarity2 = PWM_Polarity2_High;
  PWM_InitStruct->PWM_Polarity3 = PWM_Polarity3_High;
  PWM_InitStruct->PWM_Polarity1N = PWM_Polarity1N_High;
  PWM_InitStruct->PWM_Polarity2N = PWM_Polarity2N_High;
  PWM_InitStruct->PWM_Polarity3N = PWM_Polarity3N_High;
  PWM_InitStruct->PWM_DTRAccess = PWM_DTRAccess_Disable;
  PWM_InitStruct->PWM_DeadTime = PWM_DeadTime_Reset_Mask;
  PWM_InitStruct->PWM_Emergency = PWM_Emergency_Disable;
  PWM_InitStruct->PWM_LOCKLevel = PWM_LOCKLevel_OFF;
  PWM_InitStruct->PWM_OSSIState = PWM_OSSIState_Disable;
  PWM_InitStruct->PWM_RepetitionCounter = PWM_RepetitionCounter_Reset_Mask;
}

/*******************************************************************************
* Function Name  : PWM_Cmd
* Description    : Enables or disables the PWM peripheral.
* Input          : Newstate: new state of the PWM peripheral.
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_Cmd(FunctionalState Newstate)
{
 if(Newstate == ENABLE)
  {
    PWM->CR |= PWM_COUNTER_Start;
  }
  else
  {
    PWM->CR &= PWM_COUNTER_Stop;
  }
}

/*******************************************************************************
* Function Name  : PWM_CtrlPWMOutputs
* Description    : Enables or disables PWM peripheral Main Outputs.
* Input          : Newstate: new state of the PWM peripheral Main Outputs. 
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_CtrlPWMOutputs(FunctionalState Newstate)
{
 if(Newstate == ENABLE)
  {
    PWM->DTR |= PWM_MOE_Set;
  }
  else
  {
    PWM->DTR &= PWM_MOE_Reset;
  }
}

/*******************************************************************************
* Function Name  : PWM_ITConfig
* Description    : Enables or disables the PWM interrupts.
* Input          : - PWM_IT: specifies the PWM interrupts sources to be enabled
*                    or disabled.
*                    This parameter can be any combination of the following values:
*                         - PWM_IT_OC1: PWM Output Compare 1 Interrupt source
*                         - PWM_IT_OC2: PWM Output Compare 2 Interrupt source
*                         - PWM_IT_OC3: PWM Output Compare 3 Interrupt source
*                         - PWM_IT_Update: PWM update Interrupt source
*                         - PWM_IT_Emergency: PWM Emergency interrupt source
*                         - PWM_IT_GlobalUpdate: PWM global update Interrupt
*                           source
*                  - Newstate: new state of PWM interrupts.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_ITConfig(u16 PWM_IT, FunctionalState Newstate)
{ 
  u16 PWM_IT_Enable = 0;

  PWM_IT_Enable = PWM_IT & PWM_IT_Enable_Mask;

  if(Newstate == ENABLE)
  {
    /* Update interrupt global source: overflow/undeflow, counter reset operation
    or slave mode controller in reset mode */
    if ((PWM_IT & PWM_IT_GlobalUpdate) == PWM_IT_GlobalUpdate)
    {
      PWM->CR &= PWM_UFS_Reset;
    }
    /* Update interrupt source: counter overflow/underflow */
    else if ((PWM_IT & PWM_IT_Update) == PWM_IT_Update)
    {
      PWM->CR |= PWM_UFS_Set;
    }
    /* Select and enable the interrupts requests */
    PWM->RSR |= PWM_IT_Enable;
    PWM->RER |= PWM_IT_Enable;
  }
  /* Disable the interrupts requests */
  else
  {
    PWM->RSR &= ~PWM_IT_Enable;
    PWM->RER &= ~PWM_IT_Enable;
  }
}

/*******************************************************************************
* Function Name  : PWM_DMAConfig
* Description    : Configures the PWM抯 DMA interface.
* Input          : - PWM_DMASources: specifies the DMA Request sources.
*                    This parameter can be any combination of the following values:
*                         - PWM_DMASource_OC1: PWM Output Compare 1 DMA source
*                         - PWM_DMASource_OC2: PWM Output Compare 2 DMA source
*                         - PWM_DMASource_OC3: PWM Output Compare 3 DMA source
*                         - PWM_DMASource_Update: PWM Update DMA source
*                  - PWM_OCRMState: the state of output compare request mode.
*                    This parameter can be one of the following values:
*                         - PWM_OCRMState_Enable 
*                         - PWM_OCRMState_Disable 
*                  - PWM_DMABase:DMA Base address.
*                    This parameter can be one of the following values:
*                    PWM_DMABase_CR, PWM_DMABase_SCR, PWM_DMABase_OMR1, 
*                    PWM_DMABase_OMR2, PWM_DMABase_RSR, PWM_DMABase_RER, 
*                    PWM_DMABase_ISR, PWM_DMABase_CNT, PWM_DMABase_PSC,
*                    PWM_DMABase_RCR, PWM_DMABase_ARR, PWM_DMABase_OCR1,
*                    PWM_DMABase_OCR2, PWM_DMABase_OCR3 ,PWM_DMABase_DTR.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_DMAConfig(u16 PWM_DMASources, u16 PWM_OCRMState, u16 PWM_DMABase)
{
  /* Select the DMA requests */
  PWM->RSR &= ~PWM_DMASources;
  
  /* Sets the OCRM state */
  if(PWM_OCRMState == PWM_OCRMState_Enable)
  {
    PWM->RSR |= PWM_OCMR_Set;
  }
  else
  {
    PWM->RSR &= PWM_OCMR_Reset;
  }

  /* Sets the DMA Base address */
  PWM->CR &= PWM_DBASE_Mask;
  PWM->CR |= PWM_DMABase;
}

/*******************************************************************************
* Function Name  : PWM_DMACmd
* Description    : Enables or disables the PWM抯 DMA interface.
* Input          : - PWM_DMASources: specifies the DMA Request sources.
*                    This parameter can be any combination of the following values:
*                         - PWM_DMASource_OC1: PWM Output Compare 1 DMA source
*                         - PWM_DMASource_OC2: PWM Output Compare 2 DMA source
*                         - PWM_DMASource_OC3: PWM Output Compare 3 DMA source
*                         - PWM_DMASource_Update: PWM Update DMA source
*                  - Newstate: new state of the DMA Request sources.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_DMACmd(u16 PWM_DMASources, FunctionalState Newstate)
{
  if(Newstate == ENABLE)
  {
    PWM->RER |= PWM_DMASources;
  }
  else
  {
    PWM->RER &= ~PWM_DMASources;
  }
}

/*******************************************************************************
* Function Name  : PWM_SetPrescaler
* Description    : Sets the PWM prescaler value.
* Input          : Prescaler: PWM prescaler new value.
* Output         : None
* Return         : None
*******************************************************************************/
void PWM_SetPrescaler(u16 Prescaler)
{
  PWM->PSC = Prescaler;
}

/*******************************************************************************
* Function Name  : PWM_SetPeriod
* Description    : Sets the PWM period value.
* Input          : Period: PWM period new value.
* Output         : None

⌨️ 快捷键说明

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