
/******************** (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_InitializePwm20(uint16_t pwm20_period,uint8_t pwm20_percentage);
void PWM_InitializePwm21(uint16_t pwm21_period,uint8_t pwm21_percentage);
void PWM_InitializePwm22(uint16_t pwm22_period,uint8_t pwm22_percentage);
void PWM_InitializePwm23(uint16_t pwm23_period,uint8_t pwm23_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;
#define    IO_PORT3_OUTPUT_LOW        P3M |= 0xFF;    P3 &= 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) PWM21		(Period[n*100us],duty percentage)
	 *	(4) PWM22		(Period[n*100us],duty percentage)
	 *	(5) PWM23		(Period[n*100us],duty percentage)
	 */	
		GPIO_Initialization();																									/*	(1)	*/
    PWM_InitializePwm20(10,75);        																			/*	(2)	*/
		PWM_InitializePwm21(100,25);       																			/*	(3)	*/
		PWM_InitializePwm22(20,33);       																			/*	(4)	*/
		PWM_InitializePwm23(25,66);       																			/*	(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_ExecutePwm20Init:	PWM20 App Function
 *	@param		pwm20_period,pwm20_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
	 *	(6)	Set PWM20 As PWM	Channel
	 *	(7)	PWM2 Enable	
	 *	 
	 */
		PW2YH	=	((pwm20_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW2YL	=	((pwm20_period*25)&0xFF);																				/*	(2)	*/	
		PW20DH	=	(uint16_t) (pwm20_period*pwm20_percentage)>>10;								/*	(3)	*/
		PW20DL	=	(uint16_t) (pwm20_period*pwm20_percentage)>>2;								/*	(4)	*/
		PW2M	|=	mskPW2M_PWM2_DIV128;																					/*	(5)	*/
		PW2CH	=	mskPW2CH_PW2CH0;																								/*	(6)	*/
		PW2M	|=	mskPW2M_PW2EN;																								/*	(7)	*/
}

/**
 *	@Brief		PWM_ExecutePwm21Init:	PWM21 App Function
 *	@param		pwm21_period,pwm21_percentage
 *	@return		None
 */
void PWM_InitializePwm21(uint16_t pwm21_period,uint8_t pwm21_percentage)
{
	/*	
	 *	(1)	Set PWM2 Period 
	 *	(2)	Set PWM2 Period
	 *	(3)	Set PWM21 Duty
	 *	(4)	Set PWM21 Duty 
	 *	(5)	Set PWM2 Freq Division
	 *	(6)	Set PWM21 As PWM	Channel
	 *	(7)	PWM2 Enable	
	 *	 
	 */	
		PW2YH	=	((pwm21_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW2YL	=	((pwm21_period*25)&0xFF);																				/*	(2)	*/	
		PW21DH	=	(uint16_t) (pwm21_period*pwm21_percentage)>>10;								/*	(3)	*/
		PW21DL	=	(uint16_t) (pwm21_period*pwm21_percentage)>>2;;								/*	(4)	*/
		PW2M	|=	mskPW2M_PWM2_DIV128;																					/*	(5)	*/
		PW2CH	=	mskPW2CH_PW2CH1;																								/*	(6)	*/
		PW2M	|=	mskPW2M_PW2EN;																								/*	(7)	*/		
}


/**
 *	@Brief		PWM_ExecutePwm22Init:	PWM22 App Function
 *	@param		pwm22_period,pwm22_percentage
 *	@return		None
 */
void PWM_InitializePwm22(uint16_t pwm22_period,uint8_t pwm22_percentage)
{
	/*	
	 *	(1)	Set PWM2 Period 
	 *	(2)	Set PWM2 Period
	 *	(3)	Set PWM22 Duty
	 *	(4)	Set PWM22 Duty 
	 *	(5)	Set PWM2 Freq Division
	 *	(6)	Set PWM22 As PWM	Channel
	 *	(7)	PWM2 Enable	
	 *	 
	 */		
		PW2YH	=	((pwm22_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW2YL	=	((pwm22_period*25)&0xFF);																				/*	(2)	*/	
		PW22DH	=	(uint16_t) (pwm22_period*pwm22_percentage)>>10;								/*	(3)	*/
		PW22DL	=	(uint16_t) (pwm22_period*pwm22_percentage)>>2;;								/*	(4)	*/
		PW2M	|=	mskPW2M_PWM2_DIV128;																					/*	(5)	*/
		PW2CH	=	mskPW2CH_PW2CH2;																								/*	(6)	*/
		PW2M	|=	mskPW2M_PW2EN;																								/*	(7)	*/		
}


/**
 *	@Brief		PWM_ExecutePwm23Init:	PWM23 App Function
 *	@param		pwm23_period,pwm23_percentage
 *	@return		None
 */
void PWM_InitializePwm23(uint16_t pwm23_period,uint8_t pwm23_percentage)
{
	/*	
	 *	(1)	Set PWM2 Period 
	 *	(2)	Set PWM2 Period
	 *	(3)	Set PWM23 Duty
	 *	(4)	Set PWM23 Duty 
	 *	(5)	Set PWM2 Freq Division
	 *	(6)	Set PWM23 As PWM	Channel
	 *	(7)	PWM2 Enable	
	 *	 
	 */		
		PW2YH	=	((pwm23_period*25)&0xFF00)>>8;																	/*	(1)	*/	
		PW2YL	=	((pwm23_period*25)&0xFF);																				/*	(2)	*/
		PW23DH	=	(uint16_t) (pwm23_period*pwm23_percentage)>>10;								/*	(3)	*/
		PW23DL	=	(uint16_t) (pwm23_period*pwm23_percentage)>>2;;								/*	(4)	*/
		PW2M	|=	mskPW2M_PWM2_DIV128;																					/*	(5)	*/
		PW2CH	=	mskPW2CH_PW2CH3;																								/*	(6)	*/
		PW2M	|=	mskPW2M_PW2EN;																								/*	(7)	*/		
}





