/**
* [url=home.php?mod=space&uid=247401]@brief[/url] This function config PWM generator
* @param[in] u8ChannelNum PWM channel number. Valid values are between 0~5
* @param[in] u8OPMode PWM run mode select from Independent, Complementary or Synchronized mode.
* @param[in] u8PwmType select Edge-Aligned Type or Center-Aligned Type
* @param[in] u32PWM0Frequency Target generator frequency, note the actually PWM period is 16bit value. from 0x0000 ~ 0xFFFF
* @param[in] u16DutyCycle Target generator duty cycle percentage. Valid range are between 0 ~ 100. 10 means 10%, 20 means 20%...
* [url=home.php?mod=space&uid=266161]@return[/url] none
* [url=home.php?mod=space&uid=536309]@NOTE[/url] none
* [url=home.php?mod=space&uid=389923]@example[/url] PWM0_ConfigOutputChannel(0,Independent,EdgeAligned,0x6FF,10);
*/
void PWM0_ConfigOutputChannel(unsigned char u8PWM0ChannelNum,
unsigned char u8PWM0OPMode,
unsigned char u8PWM0PwmType,
unsigned long u32PWM0Frequency,
unsigned int u16PWM0DutyCycle)
{
SFRS = 0x01;
switch (u8PWM0OPMode)
{
case Independent: PWM0CON1&=0x3F;break;
case Complementary: PWM0CON1&=0x3F;PWM0CON1|=0x40;break;
case Synchronous: PWM0CON1&=0x3F;PWM0CON1|=0x80;break;
}
switch (u8PWM0PwmType)
{
case EdgeAligned: PWM0CON1&=0xEF;break;
case CenterAligned: PWM0CON1|=0x10;break;
}
switch (u8PWM0ChannelNum)
{
case 0: PWM0C1H=(u32PWM0Frequency*u16PWM0DutyCycle/100)>>8;PWM0C0L=(u32PWM0Frequency*u16PWM0DutyCycle/100);break;
case 1: PWM0C1H=(u32PWM0Frequency*u16PWM0DutyCycle/100)>>8;PWM0C1L=(u32PWM0Frequency*u16PWM0DutyCycle/100);break;
case 2: PWM0C2H=(u32PWM0Frequency*u16PWM0DutyCycle/100)>>8;PWM0C2L=(u32PWM0Frequency*u16PWM0DutyCycle/100);break;
case 3: PWM0C3H=(u32PWM0Frequency*u16PWM0DutyCycle/100)>>8;PWM0C3L=(u32PWM0Frequency*u16PWM0DutyCycle/100);break;
case 4: PWM0C4H=(u32PWM0Frequency*u16PWM0DutyCycle/100)>>8;PWM0C4L=(u32PWM0Frequency*u16PWM0DutyCycle/100);break;
case 5: PWM0C5H=(u32PWM0Frequency*u16PWM0DutyCycle/100)>>8;PWM0C5L=(u32PWM0Frequency*u16PWM0DutyCycle/100);break;
}
PWM0PH = u32PWM0Frequency>>8;
PWM0PL = u32PWM0Frequency;
}
大家看看这个例子,一共五个参数,第四个是PWM频率,而函数内,直接把这个频率值付给了PWM0PH和PWM0PL,这个值
应该就是分母上这个值吧,这个值可不是频率吧,是一个PWM周期对应的PWM时钟数-1啊。
|