/******************************************************************************
;  *   	@MCU				 : MC51F003A4
;  *   	@Create Date         : 2021.10.11
;  *   	@Author/Corporation  : Martin/SinoMCU
;  *   	@晟矽微技术支持QQ 	 : 2048615934
;  *    @晟矽微官网		     : http://www.sinomcu.com/
;  *   	@Copyright           : 2020 SINOMCU Corporation,All Rights Reserved.
;  *----------------------Abstract Description---------------------------------
;  *			          定义                              		
******************************************************************************/
#include "PWM.h"

/************************************************
;  *    @Function Name       : PWM1_Init
;  *    @Description         : PWM1配置
;  *    @IN_Parameter      	 :
;  *    @Return parameter    :
;  ***********************************************/
void PWM1_Init(void)
{

	SLK_SFR1();				  //切换SFR1
	PWM1CR0 |= DEF_SET_BIT7;  //使能PWM1
	PWM1CR1 &= ~DEF_SET_BIT7; //PWM1时钟FSYS
	//在PWM1 输出中,请勿随意修改频率,否则会影响 PWM1 的输出结果;
	//建议按为先关闭PWM1,修改后再打开PWM1的顺序操作
	PWM1CR1 &= 0xf8;
	PWM1CR1 |= 0x04;																					  //PWM1CLK=16分频
	PWM1IER |= DEF_SET_BIT0;																			  //使能刹车保护
	PWM1IER |= DEF_SET_BIT1;																			  //下降沿	P2.6
	PWM1CR0 |= (DEF_SET_BIT0 | DEF_SET_BIT1 | DEF_SET_BIT2 | DEF_SET_BIT3 | DEF_SET_BIT4 | DEF_SET_BIT5); //6个通道使能
	PWM1PH = 1000 >> 8;
	PWM1PL = 1000 % 256;																				  //周期
	PWM1CR2 |= DEF_SET_BIT7;																			  //溢出自动重载
	PWM1CR2 &= ~(DEF_SET_BIT0 | DEF_SET_BIT1 | DEF_SET_BIT2);											  //占空比 高电平
	PWM1CR3 |= (DEF_SET_BIT0 | DEF_SET_BIT1 | DEF_SET_BIT2 | DEF_SET_BIT3 | DEF_SET_BIT4 | DEF_SET_BIT5); //6个通道比较使能
	PWM1CR3 |= DEF_SET_BIT7;																			  //比较总使能

#if (PWM1_Mode == 1)										  //互补模式
	PWM1CR1 &= ~(DEF_SET_BIT4 | DEF_SET_BIT5 | DEF_SET_BIT6); //互补模式
	PWM1CR2 |= (DEF_SET_BIT4 | DEF_SET_BIT5 | DEF_SET_BIT6);  //死区时间使能
	PWM10DH = 600 >> 8;
	PWM10DL = 600 % 256; //占空比60%
	PWM101F = 30;		 //死区时间	PWM101F/FSYS/PWM1CLK=30/16M/16=30us
	PWM12DH = 500 >> 8;
	PWM12DL = 500 % 256; //占空比50%
	PWM123F = 20;		 //死区时间	20us
	PWM14DH = 400 >> 8;
	PWM14DL = 400 % 256; //占空比40%
	PWM145F = 10;		 //死区时间	10us
#endif
#if (PWM1_Mode == 2)										 //独立模式
	PWM1CR1 |= (DEF_SET_BIT4 | DEF_SET_BIT5 | DEF_SET_BIT6); //独立模式
	PWM10DH = 1000 >> 8;
	PWM10DL = 1000 % 256; //占空比100%
	PWM11DH = 800 >> 8;
	PWM11DL = 800 % 256; //占空比80%
	PWM12DH = 600 >> 8;
	PWM12DL = 600 % 256; //占空比60%
	PWM13DH = 400 >> 8;
	PWM13DL = 400 % 256; //占空比40%
	PWM14DH = 200 >> 8;
	PWM14DL = 200 % 256; //占空比20%
	PWM15DH = 0 >> 8;
	PWM15DL = 0 % 256; //占空比0%
#endif
	IE1 |= (DEF_SET_BIT2 | DEF_SET_BIT5); //使能溢出、刹车中断根据情况选择
	SLK_SFR0();							  //切换SFR0
}

void PWM1_ISR(void) interrupt PWM1_IRQn //溢出中断
{
	SFR_INTERRUPT_BUFF_BIT_PWM1 = SFRSL0;		//进中断缓存SFR
	
	SLK_SFR1();		//切换到1页操作寄存器
	PWM1IER &= ~DEF_SET_BIT7; //清除中断标志
	SLK_SFR0();		//切换到0页操作寄存器
	LED1 = !LED1; //溢出 IO翻转
	
	SFRSL0 = SFR_INTERRUPT_BUFF_BIT_PWM1;		//退出中断还原SFR
	
}
void PWM1_FB_ISR(void) interrupt PWM1_FB_IRQn //刹车中断
{
	SFR_INTERRUPT_BUFF_BIT_PWM1_FB = SFRSL0;		//进中断缓存SFR
	
	SLK_SFR0(); //切换SFR0
	if (KEY1 == 0) //清除刹车
	{
		SLK_SFR1();		//切换到1页操作寄存器
		PWM1IER &= ~DEF_SET_BIT6; //清除中断标志
		SLK_SFR0();		//切换到0页操作寄存器
		LED2 = 0;
	}
	else
	{
		LED2 = !LED2;
	}
	
	SFRSL0 = SFR_INTERRUPT_BUFF_BIT_PWM1_FB;		//退出中断还原SFR
}

/**************************** end of file *********************************************/
