//************************************************************ // Copyright (c) 深圳市赛元微电子有限公司 // 文件名称 : sc92f8003_pwm.c // 作者 : // 模块功能 : PWM固件库函数C文件 // 局部函数列表: // 最后更正日期: 2018/9/4 // 版本 : V1.0 // 说明 : //************************************************************* #include "sc92f8003_pwm.h" /************************************************** *函数名称:void PWM_DeInit(void) *函数功能:PWM相关寄存器复位至缺省值 *入口参数:void *出口参数:void **************************************************/ void PWM_DeInit(void) { PWMCFG = 0X00; PWMCON0 = 0X00; PWMPRD = 0X00; PWMDTYA = 0X00; PWMDTY0 = 0X00; PWMDTY1 = 0X00; PWMDTY2 = 0X00; PWMCON1 = 0X00; PWMDTYB = 0X00; PWMDTY3 = 0X00; PWMDTY4 = 0X00; PWMDTY5 = 0X00; PWMDTY6 = 0X00; IE1 &= 0XFD; IP1 &= 0XFD; } /************************************************** *函数名称:void PWM_Init(PWM_PresSel_TypeDef PWM_PresSel, uint16_t PWM_Period) *函数功能:PWM初始化配置函数 *入口参数:PWM_PresSel 预分频选择 PWM_Period PWM周期配置 *出口参数:void **************************************************/ void PWM_Init(PWM_PresSel_TypeDef PWM_PresSel, uint16_t PWM_Period) { PWMCON0 = (PWMCON0 & 0XCC) | PWM_PresSel | (uint8_t)(PWM_Period & 0X0003); //预分频及周期的低2位 PWMPRD = (uint8_t)(PWM_Period >> 2); //周期高八位 } /************************************************** *函数名称:void PWM_OutputStateConfig(uint8_t PWM_OutputPin, PWM_OutputState_TypeDef PWM_OutputState) *函数功能:PWMx输出使能/失能配置函数 *入口参数:PWM_OutputPin PWMx选择 PWM_OutputState PWM输出状态配置 *出口参数:void **************************************************/ void PWM_OutputStateConfig(uint8_t PWM_OutputPin, PWM_OutputState_TypeDef PWM_OutputState) { if(PWM_OutputState == PWM_OUTPUTSTATE_ENABLE) { PWMCON1 |= PWM_OutputPin; } else { PWMCON1 &= (~PWM_OutputPin); } } /************************************************** *函数名称:void PWM_PolarityConfig(uint8_t PWM_OutputPin, PWM_Polarity_TypeDef PWM_Polarity) *函数功能:PWMx正/反向输出配置函数 *入口参数:PWM_OutputPin PWMx选择 PWM_Polarity PWM输出正/反向配置 *出口参数:void **************************************************/ void PWM_PolarityConfig(uint8_t PWM_OutputPin, PWM_Polarity_TypeDef PWM_Polarity) { if(PWM_Polarity == PWM_POLARITY_INVERT) { PWMCFG |= PWM_OutputPin; } else { PWMCFG &= (~PWM_OutputPin); } } /************************************************** *函数名称:void PWM_IndependentModeConfig(PWM_OutputPin_TypeDef PWM_OutputPin, uint16_t PWM_DutyCycle) *函数功能:PWMx独立工作模式配置函数 *入口参数:PWM_OutputPin PWMx独立通道选择 PWM_DutyCycle PWM占空比配置 *出口参数:void **************************************************/ void PWM_IndependentModeConfig(PWM_OutputPin_TypeDef PWM_OutputPin, uint16_t PWM_DutyCycle) { PWMCON1 &= 0X7F; //设置PWM为独立模式 switch(PWM_OutputPin) //设置占空比 { case PWM0: PWMDTYA = PWMDTYA & 0XFC | (PWM_DutyCycle % 4); PWMDTY0 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM1: PWMDTYA = PWMDTYA & 0XF3 | ((PWM_DutyCycle % 4) << 2); PWMDTY1 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM2: PWMDTYA = PWMDTYA & 0XCF | ((PWM_DutyCycle % 4) << 4); PWMDTY2 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM3: PWMDTYA = PWMDTYA & 0X3F | ((PWM_DutyCycle % 4) << 6); PWMDTY3 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM4: PWMDTYB = PWMDTYB & 0XFC | (PWM_DutyCycle % 4); PWMDTY4 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM5: PWMDTYB = PWMDTYB & 0XF3 | ((PWM_DutyCycle % 4) << 2); PWMDTY5 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM6: PWMDTYB = PWMDTYB & 0XCF | ((PWM_DutyCycle % 4) << 4); PWMDTY6 = (uint8_t)(PWM_DutyCycle >> 2); break; default: break; } } /************************************************** *函数名称:void PWM_ComplementaryModeConfig(PWM_ComplementaryOutputPin_TypeDef PWM_ComplementaryOutputPin, uint16_t PWM_DutyCycle) *函数功能:PWMxPWMy互补工作模式配置函数 *入口参数:PWM_ComplementaryOutputPin PWMxPWMy互补通道选择 PWM_DutyCycle PWM占空比配置 *出口参数:void **************************************************/ void PWM_ComplementaryModeConfig(PWM_ComplementaryOutputPin_TypeDef PWM_ComplementaryOutputPin, uint16_t PWM_DutyCycle) { PWMCON1 |= 0X80; //设置PWM为互补模式 switch(PWM_ComplementaryOutputPin) //设置占空比 { case PWM0PWM3: PWMDTYA = PWMDTYA & 0XFC | (PWM_DutyCycle % 4); PWMDTY0 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM1PWM4: PWMDTYA = PWMDTYA & 0XF3 | ((PWM_DutyCycle % 4) << 2); PWMDTY1 = (uint8_t)(PWM_DutyCycle >> 2); break; case PWM2PWM5: PWMDTYA = PWMDTYA & 0XCF | ((PWM_DutyCycle % 4) << 4); PWMDTY2 = (uint8_t)(PWM_DutyCycle >> 2); break; default: break; } } /************************************************** *函数名称:void PWM_DeadTimeConfig(uint8_t PWM012_RisingDeadTime, uint8_t PWM345_fallingDeadTime) *函数功能:PWM互补工作模式下死区时间配置函数 *入口参数:PWM012_RisingDeadTime PWM死区上升时间 PWM345_fallingDeadTime PWM死区下降时间 *出口参数:void **************************************************/ void PWM_DeadTimeConfig(uint8_t PWM012_RisingDeadTime, uint8_t PWM345_fallingDeadTime) { PWMDTY3 = (PWM012_RisingDeadTime | (PWM345_fallingDeadTime << 4)); } /***************************************************** *函数名称:void PWM_Cmd(FunctionalState NewState) *函数功能:PWM功能开关函数 *入口参数:FunctionalState NewState 功能启动/关闭选择 *出口参数:void *****************************************************/ void PWM_Cmd(FunctionalState NewState) { if (NewState != DISABLE) { PWMCON0 |= 0X80; } else { PWMCON0 &= ~0X80; } } /***************************************************** *函数名称:void PWM_ITConfig(FunctionalState NewState, PriorityStatus Priority) *函数功能:PWM中断初始化 *入口参数:FunctionalState NewState 中断使能/关闭选择 PriorityStatus Priority 中断优先级选择 *出口参数:void *****************************************************/ void PWM_ITConfig(FunctionalState NewState, PriorityStatus Priority) { if (NewState != DISABLE) { IE1 |= 0X02; } else { IE1 &= 0XFD; } if(Priority == LOW) { IP1 |= 0X02; } else { IP1 &= 0XFD; } } /***************************************************** *函数名称:FlagStatus PWM_GetFlagStatus(void) *函数功能:获得PWM中断标志状态 *入口参数:void *出口参数:FlagStatus PWM中断标志状态 *****************************************************/ FlagStatus PWM_GetFlagStatus(void) { return (FlagStatus)(PWMCON0 & 0X40); } /***************************************************** *函数名称:void PWM_ClearFlag(void) *函数功能:清除PWM中断标志状态 *入口参数:void *出口参数:void *****************************************************/ void PWM_ClearFlag(void) { PWMCON0 &= 0XBF; } /******************* (C) COPYRIGHT 2018 SinOne Microelectronics *****END OF FILE****/