38 uint32_t u32ChannelNum,
39 uint32_t u32Frequency,
40 uint32_t u32DutyCycle)
44 uint8_t u8Divider = 1, u8Prescale = 0xFF;
45 uint16_t u16CNR = 0xFFFF;
66 for(; u8Divider < 17; u8Divider <<= 1)
68 i = (u32PWM_Clock / u32Frequency) / u8Divider;
70 if(i > (0x10000 * 0x100))
74 u8Prescale = (i + 0xFFFF)/ 0x10000;
93 i = u32PWM_Clock / (u8Prescale * u8Divider * u16CNR);
100 else if (u8Divider == 2)
102 else if (u8Divider == 4)
104 else if (u8Divider == 8)
111 pwm->
PRES = (pwm->
PRES & ~(
PWM_PRES_CP01_Msk << ((u32ChannelNum >> 1) * 8))) | (u8Prescale << ((u32ChannelNum >> 1) * 8));
115 if(u32DutyCycle == 0)
120 *(__IO uint32_t *) (&pwm->
DUTY0 + 3 * u32ChannelNum) |= ((u32DutyCycle * (u16CNR + 1) / 100 - 1) <<
PWM_DUTY0_CM_Pos);
123 *(__IO uint32_t *) (&pwm->
DUTY0 + 3 * u32ChannelNum) |= u16CNR;
139 uint32_t u32ChannelNum,
140 uint32_t u32UnitTimeNsec,
141 uint32_t u32CaptureEdge)
145 uint8_t u8Divider = 1, u8Prescale = 0xFF;
146 uint16_t u16CNR = 0xFFFF;
151 u32PWM_Clock =
__HXT;
154 u32PWM_Clock =
__LXT;
167 for(; u8Divider < 17; u8Divider <<= 1)
169 i = ((
long long)(u32PWM_Clock / u8Divider) * u32UnitTimeNsec) / 1000000000;
185 i = (
long long) (u8Prescale * u8Divider) * 1000000000 / u32PWM_Clock;
192 else if (u8Divider == 2)
194 else if (u8Divider == 4)
196 else if (u8Divider == 8)
203 pwm->
PRES = (pwm->
PRES & ~(
PWM_PRES_CP01_Msk << ((u32ChannelNum >> 1) * 8))) | (u8Prescale << ((u32ChannelNum >> 1) * 8));
208 *(__IO uint32_t *) (&pwm->
DUTY0 + 3 * u32ChannelNum) |= u16CNR;
223 uint32_t u32Mask = 0;
227 if ( u32ChannelMask & (1 << i))
246 if(u32ChannelMask & (1 << i))
266 if ( u32ChannelMask & (1 << i))
330 uint32_t u32Mask = 0;
334 if ( u32ChannelMask & (1 << i))
353 uint32_t u32CTLMask = 0;
354 uint32_t u32CAPCTLMask = 0;
359 if ( u32ChannelMask & (1 << i))
366 pwm->
CTL &= ~u32CTLMask;
367 pwm->
CAPCTL &= ~u32CAPCTLMask;
380 pwm->
OE |= u32ChannelMask;
392 pwm->
OE &= ~u32ChannelMask;
440 pwm->
CAPINTEN |= (u32Edge << (u32ChannelNum * 8));
456 pwm->
CAPINTEN &= ~(u32Edge << (u32ChannelNum * 8));
472 pwm->
CAPINTSTS = (u32Edge + 1) << (u32ChannelNum * 8);
580 if (u32ChannelNum == 0)
594 if (u32ChannelNum == 0)
#define PWM_INTSTS_TMINT0_Msk
void PWM_EnablePDMA(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32RisingFirst, uint32_t u32Mode)
This function enable capture PDMA of selected channel.
#define PWM_CAPCTL_CAPCH0EN_Msk
void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum)
This function disable selected channel to trigger ADC.
#define PWM_CAPCTL_CH0PDMAEN_Msk
#define PWM_PRES_DZ01_Msk
uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum)
This function get selected channel trigger ADC flag.
void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition)
This function clear selected channel trigger ADC flag.
void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask)
This function stop PWM generation immediately by clear channel enable bit.
void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge)
This function clear capture interrupt flag of selected channel.
#define PWM_INTSTS_Duty0Syncflag_Msk
#define CLK
Pointer to CLK register structure.
#define PWM_CAPCTL_CH0RFORDER_Msk
#define PWM_CAPCTL_PDMACAPMOD0_Msk
uint32_t PWM_ConfigCaptureChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32UnitTimeNsec, uint32_t u32CaptureEdge)
This function config PWM capture and get the nearest unit time.
#define PWM_RISING_FALLING_LATCH_INT_FLAG
#define PWM_CTL_CH0MOD_Msk
#define PWM_INTEN_TMIE0_Msk
uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum)
This function get period interrupt of selected channel.
#define CLK_PWRCTL_HIRC_FSEL_Msk
#define CLK_CLKSEL1_PWM0_CH01_S_Msk
void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask)
This function disables PWM capture of selected channels.
void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition)
This function enable selected channel to trigger ADC.
#define PWM_CTL_DZEN01_Msk
#define PWM_CTL_CH0EN_Msk
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge)
This function disable capture interrupt of selected channel.
void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask)
This function stop PWM module.
void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum)
This function disable Dead zone of selected channel.
void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask)
This function enables PWM capture of selected channels.
uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum)
This function get capture interrupt flag of selected channel.
uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum)
This function get duty interrupt flag of selected channel.
void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge)
This function enable capture interrupt of selected channel.
#define PWM_ADTRGSTS_ADTRG0Flag_Msk
#define PWM_INTSTS_PresSyncFlag_Msk
void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum)
This function disable period interrupt of selected channel.
#define PWM_CLKSEL_CLKSEL0_Msk
void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum)
This function clear period interrupt of selected channel.
void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask)
This function enables PWM output generation of selected channels.
uint32_t PWM_ConfigOutputChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Frequency, uint32_t u32DutyCycle)
This function config PWM generator and get the nearest frequency in edge aligned auto-reload mode.
void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask)
This function start PWM module.
#define PWM_CAPCTL_CH2RFORDER_Msk
void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration)
This function enable Dead zone of selected channel.
#define PWM_PRES_CP01_Msk
#define PWM_ADTRGEN_TRGCH0EN_Msk
#define PWM_CAPCTL_PDMACAPMOD2_Msk
#define CLK_CLKSEL1_PWM0_CH01_S_Pos
void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum)
This function clears duty interrupt flag of selected channel.
void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask)
This function disables PWM output generation of selected channels.
#define PWM_CAPCTL_CH2PDMAEN_Msk
void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType)
This function enable period interrupt of selected channel.
void PWM_DisablePDMA(PWM_T *pwm, uint32_t u32ChannelNum)
This function disable capture PDMA of selected channel.
#define PWM_CAPCTL_CAPCH0PADEN_Msk
#define PWM_PRES_DZ01_Pos