IET EC PWM开发
IT5570 PWM模块
1.IT5570有8路PWM,这8路PWM的时钟源可以从Group 0-Group3中任意选择。
2.其中EC Clock为9.2M,每个Group的时钟都可以根据6MSELx&PCFSx两个寄存器的设置从6.144M、9.2M、32.768K、中选择,也就是说只能同时输出4种不同频率的PWM波。选择了相同Group的PWM通道只能输出相同频率的PWM波,但是占空比可以不同。
IT5570 PWM实例
typedef enum
{
CLOCK_32_768K =0,
CLOCK_EC , //9.2M
CLOCK_6_14M , //2/3 EC clock
}CLOCK_SOURCE_X;
typedef enum
{
PWM_Channel_0=0,
PWM_Channel_1 ,
PWM_Channel_2 ,
PWM_Channel_3 ,
PWM_Channel_4 ,
PWM_Channel_5 ,
PWM_Channel_6 ,
PWM_Channel_7 ,
}PWM_CHANNEL_X; //eight pwm channel index
typedef enum
{
GROUP1=0,
GROUP2 ,
GROUP3 ,
GROUP4 ,
//note spec is GROUP0-GROUP3 and there is GROUP1-GROUP4
}SCALER_GROUP_X; //four scaler group index
typedef enum
{
DISABLE =0,
ENABLE =1,
}PWM_SWITCH_X;
void DISABLE_PWM_OUT() //disable pwm clock out
{
CLEAR_MASK(ZTIER, BIT1);
}
void ENABLE_PWM_OUT() //enable pwm clock out
{
SET_MASK(ZTIER, BIT1);
}
void pwm_channelx_gpio_cfg(PWM_SWITCH_X pwm_switch,PWM_CHANNEL_X pwm_channel)// enable or disable chenanel x
{
if(pwm_switch)
{
switch(pwm_channel)
{
case PWM_Channel_0:
GPCRA0=ALT;
break;
case PWM_Channel_1:
GPCRA1=ALT;
break;
case PWM_Channel_2:
GPCRA2=ALT;
break;
case PWM_Channel_3:
GPCRA3=ALT;
break;
case PWM_Channel_4:
GPCRA4=ALT;
break;
case PWM_Channel_5:
GPCRA5=ALT;
break;
case PWM_Channel_6:
GPCRA6=ALT;
break;
case PWM_Channel_7:
GPCRA7=ALT;
break;
}
}
else
{
switch(pwm_channel)
{
case PWM_Channel_0:
GPCRA0=INPUT;
break;
case PWM_Channel_1:
GPCRA1=INPUT;
break;
case PWM_Channel_2:
GPCRA2=INPUT;
break;
case PWM_Channel_3:
GPCRA3=INPUT;
break;
case PWM_Channel_4:
GPCRA4=INPUT;
break;
case PWM_Channel_5:
GPCRA5=INPUT;
break;
case PWM_Channel_6:
GPCRA6=INPUT;
break;
case PWM_Channel_7:
GPCRA7=INPUT;
break;
}
}
}
/********************************************
**function: configure pwm channel x and output
**
**parameter:clock_source :one of the three clock source
pwm_channel :one of the eight pwm channel
scaler_group :one of the four group CxCPRS
period :count period 0-255
duty :duty<period
scaler_div :freq div
pwm freq=clock_source/((scaler_div+1)*(period+1))
pwm duty=duty/(period+1)
**return : Null
*********************************************/
void pwm_channelx_cfg(CLOCK_SOURCE_X clock_source,PWM_CHANNEL_X pwm_channel,SCALER_GROUP_X scaler_group,
unsigned char period,unsigned char duty,unsigned int scaler_div)
{
unsigned char GROUP_Select = 0; //This variable is used to configure PCSSGL or PCSSGH (use bit0,1)
DISABLE_PWM_OUT();
pwm_channelx_gpio_cfg(DISABLE,pwm_channel);
switch(scaler_group)
{
case GROUP1: C0CPRS = (unsigned char)scaler_div;
CTR=period;
GROUP_Select =0x00; //this channel select C0PRS
break;
case GROUP2: C4CPRS = (unsigned char)scaler_div;
C4MCPRS= (unsigned char)(scaler_div>>8);
CTR1=period;
GROUP_Select =0x01; //this channel select C4PRS
break;
case GROUP3: C6CPRS = (unsigned char)scaler_div;
C6MCPRS= (unsigned char)(scaler_div>>8);
CTR2=period;
GROUP_Select =0x02; //this channel select C6PRS
break;
case GROUP4: C7CPRS = (unsigned char)scaler_div;
C7MCPRS= (unsigned char)(scaler_div>>8);
CTR3=period;
GROUP_Select =0x03; //this channel select C7PRS
break;
}
switch(clock_source) //diferent group select diferent clock
{
case CLOCK_32_768K: switch(scaler_group)
{
case GROUP1: CLEAR_MASK(PCFSR, BIT0);
CLEAR_MASK(CLK6MSEL, BIT0); //GROUP1 clock set to 32.768K
break;
case GROUP2: CLEAR_MASK(PCFSR, BIT1);
CLEAR_MASK(CLK6MSEL, BIT1);
break;
case GROUP3: CLEAR_MASK(PCFSR, BIT2);
CLEAR_MASK(CLK6MSEL, BIT2);
break;
case GROUP4: CLEAR_MASK(PCFSR, BIT3);
CLEAR_MASK(CLK6MSEL, BIT3);
break;
}
break;
case CLOCK_EC: switch(scaler_group)
{
case GROUP1: SET_MASK (PCFSR, BIT0);
CLEAR_MASK(CLK6MSEL, BIT0); //GROUP1 clock set to EC clock (9.2M)
break;
case GROUP2: SET_MASK (PCFSR, BIT1);
CLEAR_MASK(CLK6MSEL, BIT1);
break;
case GROUP3: SET_MASK (PCFSR, BIT2);
CLEAR_MASK(CLK6MSEL, BIT2);
break;
case GROUP4: SET_MASK (PCFSR, BIT3);
CLEAR_MASK(CLK6MSEL, BIT3);
break;
}
break;
case CLOCK_6_14M: switch(scaler_group)
{
case GROUP1: CLEAR_MASK(PCFSR, BIT0);
SET_MASK (CLK6MSEL, BIT0); //GROUP1 clock set to 2/3 EC clock (6.14M)
break;
case GROUP2: CLEAR_MASK(PCFSR, BIT1);
SET_MASK (CLK6MSEL, BIT1);
break;
case GROUP3: CLEAR_MASK(PCFSR, BIT2);
SET_MASK (CLK6MSEL, BIT2);
break;
case GROUP4: CLEAR_MASK(PCFSR, BIT3);
SET_MASK (CLK6MSEL, BIT3);
break;
}
break;
}
switch(pwm_channel)
{
case PWM_Channel_0: CLEAR_MASK(PCSSGL, BIT0+BIT1);
SET_MASK (PCSSGL, GROUP_Select);
DCR0 = duty;
break;
case PWM_Channel_1: CLEAR_MASK(PCSSGL, BIT2+BIT3);
SET_MASK (PCSSGL, GROUP_Select<<2);
DCR1 = duty;
break;
case PWM_Channel_2: CLEAR_MASK(PCSSGL, BIT4+BIT5);
SET_MASK (PCSSGL, GROUP_Select<<4);
DCR2 = duty;
break;
case PWM_Channel_3: CLEAR_MASK(PCSSGL, BIT6+BIT7);
SET_MASK (PCSSGL, GROUP_Select<<6);
DCR3 = duty;
break;
case PWM_Channel_4: CLEAR_MASK(PCSSGH, BIT0+BIT1);
SET_MASK (PCSSGH, GROUP_Select);
DCR4 = duty;
break;
case PWM_Channel_5: CLEAR_MASK(PCSSGH, BIT2+BIT3);
SET_MASK (PCSSGH, GROUP_Select<<2);
DCR5 = duty;
break;
case PWM_Channel_6: CLEAR_MASK(PCSSGH, BIT4+BIT5);
SET_MASK (PCSSGH, GROUP_Select<<4);
DCR6 = duty;
break;
case PWM_Channel_7: CLEAR_MASK(PCSSGH, BIT6+BIT7);
SET_MASK (PCSSGH, GROUP_Select<<6);
DCR7 = duty;
break;
}
pwm_channelx_gpio_cfg(ENABLE,pwm_channel);
ENABLE_PWM_OUT();
}
1.有三个枚举类型CLOCK_SOURCE_X,PWM_CHANNEL_X,SCALER_GROUP_X
分别用于选择时钟源,选择哪一路PWM通道,以及要配置的Group。
2.ENABLE_PWM_OUT()
,以及DISABLE_PWM_OUT()
是PWM的总开关。
3.void pwm_channelx_gpio_cfg(PWM_SWITCH_X pwm_switch,PWM_CHANNEL_X pwm_channel)
是配置通道的GPIO模式;
4.void pwm_channelx_cfg(CLOCK_SOURCE_X clock_source,PWM_CHANNEL_X pwm_channel,SCALER_GROUP_X scaler_group, unsigned char period,unsigned char duty,unsigned int scaler_div)
用于配置通道的Group选择,占空比,Group的时钟及分频,PWM周期等
5.使用示例:
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_0,GROUP1,100-1,50,92-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_1,GROUP2,50-1,25,92-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_2,GROUP3,100-1,50,23-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_3,GROUP4,50-1,25,23-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_4,GROUP1,100-1,60,92-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_5,GROUP2,50-1,30,92-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_6,GROUP3,100-1,60,23-1);
pwm_channelx_cfg(CLOCK_EC,PWM_Channel_7,GROUP4,50-1,30,23-1);
以上配置CH0和CH4的PWM频率为1K、CH1和CH5为2K、CH2和CH6为4K、CH3和CH7为8K,前四个通道的占空比为50%,后四个为60%,八个通道同时输出。如CH0的频率计算方式为:9.2M/(100*92)=1K。