
/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2021/12/30
* AUTHOR:		SONiX
* IC:			SN8F5762
*____________________________________________________________________________
* REVISION		Date				User		Description
* 1.0					2021/12/30	SONiX		1. First release
*____________________________________________________________________________
*****************************************************************************/

/*_____ I N C L U D E S _________________________________*/
#include <SN8F5762.h>
#include "pwm.h"

/*_____ F U N C T I O N S _______________________________*/
void PWM_InitializePwm20(uint16_t pwm20_period,uint8_t pwm20_percentage);
void PWM_InitializePwm21NV(uint16_t pwm21NV_period,uint8_t pwm21NV_percentage);
void GPIO_Initialization(void);
/*_____ D E F I N I T I O N S ___________________________*/


/*_____ D E C L A R A T I O N S _________________________*/



/*_____ M A C R O S _____________________________________*/
#define    IO_PORT0_OUTPUT_LOW        P0M |= 0xFF;    P0 &= 0x00;
#define    IO_PORT1_OUTPUT_LOW        P1M |= 0xFF;    P1 &= 0x00;
#define    IO_PORT2_OUTPUT_LOW        P2M |= 0xFF;    P2 &= 0x00;


/**
 *	@Brief		main: PWM2 Demo Function
 *	@param		None
 *	@return		None
 */

void main(void)
{
	/*
   *	System initial:
	 *	(1) GPIO
	 *  Pwm App Fuction:
	 *	(2) PWM20	 	    (Period[n*100us],duty percentage)
	 *	(3) PWM21NV	 	  (Period[n*100us],duty percentage)
	 *	(4) Interrupt enable
	 */	
		GPIO_Initialization();																									/*	(1)	*/
   PWM_InitializePwm20(100,50);        																		  /*	(2)	*/
//	  PWM_InitializePwm21NV(10,75);        																		/*	(3)	*/

	 IEN0 |= mskINT_EN;                                                       /*	(4)	*/
	 IEN2 |= mskPWM2INT_EN;
	
		while(1)
		{
	/*	
	 *	(1)	Clear Watchdog Timer
	 *	Use Oscilloscope To Check the PWM Duty
	 *	 
	 */			
			WDTR = 0x5A;																													/*	(1)	*/
		}
}

/**
 *	@Brief		initial GPIO status
 *	@param		None
 *	@return		None
 */
void GPIO_Initialization(void)
{			
		IO_PORT0_OUTPUT_LOW
		IO_PORT1_OUTPUT_LOW
		IO_PORT2_OUTPUT_LOW
}

/**
 *	@Brief		PWM_ExecutePwm20DInit:	PWM20 App Function
 *	@param		pwm20D_period,pwm20D_percentage
 *	@return		None
 */
void PWM_InitializePwm20(uint16_t pwm20_period,uint8_t pwm20_percentage)
{	
	/*	
	 *	(1)	Set PWM2 Period 
	 *	(2)	Set PWM2 Period
	 *	(3)	Set PWM20 Duty
	 *	(4)	Set PWM20 Duty 
	 *	(5)	Set PWM2 Freq Division and enable PWM2
	 *	(6)	Set PWM20 As PWM	Channel
	 *	(7) PWM IO select bit
	 *	(8) PWM Counter select bit
	 */
		PW2YH	=	((pwm20_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW2YL	=	((pwm20_period*25)&0xFF);																				/*	(2)	*/	
		PW2DH =	(uint16_t) (pwm20_period*pwm20_percentage)>>10;							  	/*	(3)	*/
		PW2DL	=	(uint16_t) (pwm20_period*pwm20_percentage)>>2;								  /*	(4)	*/
		PW2M  |= mskPW2M_PW2EN | mskPW2M_PWM2_DIV32;														/*	(5)	*/
		PW2CH	|= mskPW2CH_PW2CH0;																							  /*	(6)	*/
	  PW2M	|= mskPW2M_PW2MX0;																								/*	(7)	*/
	  PW2M  |= mskPW2M_EDGE_ALIGNED;                                          /*	(8)	*/
}

/**
 *	@Brief		PWM_ExecutePwm20DInit:	PWM20 App Function
 *	@param		pwm20D_period,pwm20D_percentage
 *	@return		None
 */
void PWM_InitializePwm21NV(uint16_t pwm21NV_period,uint8_t pwm21NV_percentage)
{	
	/*	
	 *	(1)	Set PWM2 Period 
	 *	(2)	Set PWM2 Period
	 *	(3)	Set PWM20 Duty
	 *	(4)	Set PWM20 Duty 
	 *	(5)	Set PWM2 Freq Division and enable PWM2
	 *	(6)	Set PWM20 As PWM	Channel
	 *	(7) PWM IO select bit
	 *	(8) PWM Inverse
	 */
	
		PW2YH	=	((pwm21NV_period*25)&0xFF00)>>8;																/*	(1)	*/	
		PW2YL	=	((pwm21NV_period*25)&0xFF);																			/*	(2)	*/	
		PW2DH =	(uint16_t) (pwm21NV_period*pwm21NV_percentage)>>10;						  /*	(3)	*/
		PW2DL	=	(uint16_t) (pwm21NV_period*pwm21NV_percentage)>>2;							/*	(4)	*/
		PW2M	|=	mskPW2M_PW2EN | mskPW2M_PWM2_DIV32;														/*	(5)	*/
		PW2CH	|=	mskPW2CH_PW2CH1;																							/*	(6)	*/
	  PW2M	|=	mskPW2M_PW2MX0;																								/*	(7)	*/
   	PW2NV |= mskPW2NV_PW21NV;                                               /*	(8)	*/
}


void ISR_PW2(void) interrupt ISRPwm2
{
  P00 = 1;
	P00 = 0;
}
