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

📄 pwm.c

📁 此程序为DP256的PWM实验程序,已经通过实验证明
💻 C
字号:
#define PWM_GLOBALS
#include "LIN_Header.h"

void Init_PWM(void)
{
	//DISABLE ALL PWM CHANNEL
	PWME=0;

	//PWMCAE REGISTER
	#if PWM_CAE==0
		PWMCAE=0;
	#else
		PWMCAE=0xff;
	#endif
	
	//CLKSA (SB) INIT
	
	PWMPRCLK = CLKAB_DIV;//SET CLK A(B) PRESCLAR VALUE
	
	//PWM CLOCK SELECT
	#if PWM_HIFREQ == 1
		PWMCLK=0;//SELECT CLK A(B) AS PWM CLK
	#else
		PWMSCLA=CLKSAB_DIV;//SET CLK SA SCLAR VALUE
		PWMSCLB=CLKSAB_DIV;//SET CLK SB SCLAR VALUE
		PWMCLK=0xff;//SELECT CLK SA(SB) AS PWM CLK
	#endif
	
	//PWM PERIOD 
	PWMPER0=PWM_PERIOD;
	PWMPER1=PWM_PERIOD;
	PWMPER2=PWM_PERIOD;
	PWMPER3=PWM_PERIOD;
	PWMPER4=PWM_PERIOD;
	PWMPER5=PWM_PERIOD;
	PWMPER6=PWM_PERIOD;
	PWMPER7=PWM_PERIOD;

	//PWM DUTY 
	PWMDTY0=0;
	PWMDTY1=0;
	PWMDTY2=0;
	PWMDTY3=0;
	PWMDTY4=0;
	PWMDTY5=0;
	PWMDTY6=0;
	PWMDTY7=0;

	//PWM POLARITY INIT
	#if PWM_POL == 1
		PWMPOL=0xff;
	#else
		PWMPOL=0;
	#endif
}

//per: 1% step
void PWM0_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY0=PWMPER0;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY0=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY0=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY0=dty;
		}
	#endif	
}

//per: 1% step
void PWM1_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY1=PWMPER1;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY1=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY1=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY1=dty;
		}
	#endif	
}

//per: 1% step
void PWM2_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY2=PWMPER2;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY2=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY2=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY2=dty;
		}
	#endif	
}

//per: 1% step
void PWM3_Duty(unsigned char per)
{
	unsigned char dty;

	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY3=PWMPER3;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY3=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY3=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY3=dty;
		}
	#endif	
}

//per: 1% step
void PWM4_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY4=PWMPER4;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY4=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY4=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY4=dty;
		}
	#endif	
}

//per: 1% step
void PWM5_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY5=PWMPER5;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY5=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY5=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY5=dty;
		}
	#endif	
}

//per: 1% step
void PWM6_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY6=PWMPER6;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY6=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY6=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY6=dty;
		}
	#endif	
}

//per: 1% step
void PWM7_Duty(unsigned char per)
{
	unsigned char dty;
	#if PWM_POL==1
		if(per>=PER_DIV) PWMDTY7=PWMPER7;
		else
		{
			dty=PWM_PERCENT*per;
			PWMDTY7=dty;
		}
	#else
		if(per>=PER_DIV) PWMDTY7=0;
		else
		{
			dty=(PWM_PERIOD-PWM_PERCENT*per);
			PWMDTY7=dty;
		}
	#endif	
}

⌨️ 快捷键说明

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