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





/*_____ I N C L U D E S _________________________________*/
#define __XRAM_SFR_H__
#include <SN8F5754.h>
#include "pwm.h"
/*_____ F U N C T I O N S _______________________________*/

void PWM_InitializePwm10(uint16_t pwm10_period,uint8_t pwm10_percentage);
void PWM_InitializePwm11(uint16_t pwm11_period,uint8_t pwm11_percentage);
void PWM_InitializePwm12(uint16_t pwm12_period,uint8_t pwm12_percentage);
void PWM_InitializePwm13(uint16_t pwm13_period,uint8_t pwm13_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 &= 0xFF;
#define    IO_PORT2_OUTPUT_LOW        P2M |= 0xFF;    P2 &= 0x00;
#define    IO_PORT3_OUTPUT_LOW        P3M |= 0xFF;    P3 &= 0x00;

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

void main(void)
{
	/*
   *	System initial:
	 *	(1) GPIO
	 *  Pwm App Fuction:
	 *	(2) PWM10		(Period[n*100us],duty percentage)
	 *	(3) PWM11		(Period[n*100us],duty percentage)
	 *	(4) PWM12		(Period[n*100us],duty percentage)
	 *	(5) PWM13		(Period[n*100us],duty percentage)
	 */	
		GPIO_Initialization();																									/*	(1)	*/
    PWM_InitializePwm10(10,75); 	       																		/*	(2)	*/
		PWM_InitializePwm11(100,100);       																		/*	(3)	*/
		PWM_InitializePwm12(20,33);       																			/*	(4)	*/
		PWM_InitializePwm13(25,75);       																			/*	(5)	*/

		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
		IO_PORT3_OUTPUT_LOW
}

/**
 *	@Brief		PWM_ExecutePwm10Init: initial PWM10 Function
 *	@param		pwm10_period,pwm10_percentage
 *	@return		None
 */
void PWM_InitializePwm10(uint16_t pwm10_period,uint8_t pwm10_percentage)
{	
	/*	
	 *	(1)	Set PWM1 Period 
	 *	(2)	Set PWM1 Period
	 *	(3)	Set PWM10 Duty
	 *	(4)	Set PWM10 Duty 
	 *	(5)	Set PWM1 Freq Division
	 *	(6)	Set PWM10 As PWM	Channel
	 *	(7)	PWM1 Enable	
	 *	 
	 */
		PW1YH	=	((pwm10_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW1YL	=	((pwm10_period*25)&0xFF);																				/*	(2)	*/	
		PW10DH	=	(uint16_t) (pwm10_period*pwm10_percentage)>>10;								/*	(3)	*/
		PW10DL	=	(uint16_t) (pwm10_period*pwm10_percentage)>>2;								/*	(4)	*/
		PW1M	|=	mskPW1M_PWM1_DIV128;																					/*	(5)	*/
		PW1CH	=	mskPW1CH_PW1CH0;																								/*	(6)	*/
		PW1M	|=	mskPW1M_PW1EN;																								/*	(7)	*/
}


/**
 *	@Brief		PWM_ExecutePwm11Init: initial PWM11 Function
 *	@param		pwm11_period,pwm11_percentage
 *	@return		None
 */
void PWM_InitializePwm11(uint16_t pwm11_period,uint8_t pwm11_percentage)
{
	/*	
	 *	(1)	Set PWM1 Period 
	 *	(2)	Set PWM1 Period
	 *	(3)	Set PWM11 Duty
	 *	(4)	Set PWM11 Duty 
	 *	(5)	Set PWM1 Freq Division
	 *	(6)	Set PWM11 As PWM	Channel
	 *	(7)	PWM1 Enable	
	 *	 
	 */	
		PW1YH	=	((pwm11_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW1YL	=	((pwm11_period*25)&0xFF);																				/*	(2)	*/	
		PW11DH	=	(uint16_t) (pwm11_period*pwm11_percentage)>>10;								/*	(3)	*/
		PW11DL	=	(uint16_t) (pwm11_period*pwm11_percentage)>>2;								/*	(4)	*/
		PW1M	|=	mskPW1M_PWM1_DIV128;																					/*	(5)	*/
		PW1CH	=	mskPW1CH_PW1CH1;																								/*	(6)	*/
		PW1M	|=	mskPW1M_PW1EN;																								/*	(7)	*/		
}

/**
 *	@Brief		PWM_ExecutePwm12Init: initial PWM12 Function
 *	@param		pwm12_period,pwm12_percentage
 *	@return		None
 */
void PWM_InitializePwm12(uint16_t pwm12_period,uint8_t pwm12_percentage)
{
	/*	
	 *	(1)	Set PWM1 Period 
	 *	(2)	Set PWM1 Period
	 *	(3)	Set PWM12 Duty
	 *	(4)	Set PWM12 Duty 
	 *	(5)	Set PWM1 Freq Division
	 *	(6)	Set PWM12 As PWM	Channel
	 *	(7)	PWM1 Enable	
	 *	 
	 */		
		PW1YH	=	((pwm12_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW1YL	=	((pwm12_period*25)&0xFF);																				/*	(2)	*/	
		PW12DH	=	(uint16_t) (pwm12_period*pwm12_percentage)>>10;								/*	(3)	*/
		PW12DL	=	(uint16_t) (pwm12_period*pwm12_percentage)>>2;								/*	(4)	*/
		PW1M	|=	mskPW1M_PWM1_DIV128;																					/*	(5)	*/
		PW1CH	=	mskPW1CH_PW1CH2;																								/*	(6)	*/
		PW1M	|=	mskPW1M_PW1EN;																								/*	(7)	*/		
}

/**
 *	@Brief		PWM_ExecutePwm13Init: initial PWM13 Function
 *	@param		pwm13_period,pwm13_percentage
 *	@return		None
 */
void PWM_InitializePwm13(uint16_t pwm13_period,uint8_t pwm13_percentage)
{
	/*	
	 *	(1)	Set PWM1 Period 
	 *	(2)	Set PWM1 Period
	 *	(3)	Set PWM13 Duty
	 *	(4)	Set PWM13 Duty 
	 *	(5)	Set PWM1 Freq Division
	 *	(6)	Set PWM13 As PWM	Channel
	 *	(7)	PWM1 Enable	
	 *	 
	 */		
		PW1YH	=	((pwm13_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW1YL	=	((pwm13_period*25)&0xFF);																				/*	(2)	*/
		PW13DH	=	(uint16_t) (pwm13_period*pwm13_percentage)>>10;								/*	(3)	*/
		PW13DL	=	(uint16_t) (pwm13_period*pwm13_percentage)>>2;								/*	(4)	*/
		PW1M	|=	mskPW1M_PWM1_DIV128;																					/*	(5)	*/
		PW1CH	=	mskPW1CH_PW1CH3;																								/*	(6)	*/
		PW1M	|=	mskPW1M_PW1EN;																								/*	(7)	*/		
}

