赛元微 SC92F8003 PWM固件函数库使用示范

赛元微 SC92F8003 PWM固件函数库使用示范

#include "sc92f8003_pwm.h"
#include "sc92f8003_gpio.h"

void PWM_INIT(void)
{
	PWM_Init(PWM_PRESSEL_FOSC_D2, 599);					//PWM时钟源2分频,周期为(599+1)*2/FOSC
	PWM_OutputStateConfig(PWM0, PWM_OUTPUTSTATE_ENABLE);//使能PWM0
	PWM_OutputStateConfig(PWM3, PWM_OUTPUTSTATE_ENABLE);//使能PWM3
	PWM_PolarityConfig(PWM0, PWM_POLARITY_NON_INVERT);	//设置PWM0不反向
	PWM_PolarityConfig(PWM3, PWM_POLARITY_INVERT);		//设置PWM3反向
	PWM_ComplementaryModeConfig(PWM0PWM3, 300);			//PWM0PWM3互补设置,占空比设置300/600=1/2
	PWM_DeadTimeConfig(15,15);							//PWM0上升沿死区时间设置15*2/FOSC,PWM3下降沿死区时间设置15*2/FOSC
	PWM_Cmd(ENABLE);									//打开PWM总开关
	PWM_ITConfig(ENABLE, LOW);			   				//打开PWM中断
}
void main(void)
{
	GPIO_Init(GPIO2,GPIO_PIN_0,GPIO_MODE_OUT_PP);		//P20配置为强推挽模式
	GPIO_WriteHigh(GPIO2,GPIO_PIN_0);					//P20 = 1

	PWM_INIT();

	enableInterrupts();									//开总中断
	while(1);
    
}
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCY5202015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值