/************************ 定时器配置 ****************************/
void Timer_config(void)
{
TIM_InitTypeDef TIM_InitStructure; //结构定义
TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
TIM_InitStructure.TIM_Polity = Polity_0; //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3
TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE
TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
TIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLE
TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000); //中断频率, 1000次/秒
TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
}
/*************** 串口初始化函数 *****************/
void PWM_config(void)
{
PWMx_InitDefine PWMx_InitStructure;
PWMx_InitStructure.PWM1_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
PWMx_InitStructure.PWM2_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
PWMx_InitStructure.PWM3_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
PWMx_InitStructure.PWM4_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
PWMx_InitStructure.PWM1_SetPolity = Polity_0; //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3
PWMx_InitStructure.PWM2_SetPolity = Polity_0; //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3
PWMx_InitStructure.PWM3_SetPolity = Polity_0; //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3
PWMx_InitStructure.PWM4_SetPolity = Polity_0; //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3
PWMx_InitStructure.PWM_Period = 1024; //周期时间, 0~65535
PWMx_InitStructure.PWM1_Duty = PWMA_Duty.PWM1_Duty; //PWM1占空比时间, 0~Period
PWMx_InitStructure.PWM2_Duty = PWMA_Duty.PWM2_Duty; //PWM2占空比时间, 0~Period
PWMx_InitStructure.PWM3_Duty = PWMA_Duty.PWM3_Duty; //PWM3占空比时间, 0~Period
PWMx_InitStructure.PWM4_Duty = PWMA_Duty.PWM4_Duty; //PWM4占空比时间, 0~Period
PWMx_InitStructure.PWM_DeadTime = 0; //死区发生器设置, 0~255
PWMx_InitStructure.PWM_EnoSelect = ENO1P | ENO1N ; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
PWMx_InitStructure.PWM_PS_SW = PWM1_SW_P10_P11 ; //切换端口, PWM1_SW_P10_P11,PWM1_SW_P20_P21,PWM1_SW_P60_P61
// PWM2_SW_P12_P13,PWM2_SW_P22_P23,PWM2_SW_P62_P63
// PWM3_SW_P14_P15,PWM3_SW_P24_P25,PWM3_SW_P64_P65
// PWM4_SW_P16_P17,PWM4_SW_P26_P27,PWM4_SW_P66_P67,PWM4_SW_P34_P33
PWMx_InitStructure.PWM_CC1Enable = ENABLE; //开启PWM1P输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC1NEnable = ENABLE; //开启PWM1N输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC2Enable = ENABLE; //开启PWM2P输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC2NEnable = ENABLE; //开启PWM2N输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC3Enable = ENABLE; //开启PWM3P输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC3NEnable = ENABLE; //开启PWM3N输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC4Enable = ENABLE; //开启PWM4P输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_CC4NEnable = ENABLE; //开启PWM4N输入捕获/比较输出, ENABLE,DISABLE
PWMx_InitStructure.PWM_MainOutEnable= ENABLE; //主输出使能, ENABLE,DISABLE
PWMx_InitStructure.PWM_CEN_Enable = ENABLE; //使能计数器, ENABLE,DISABLE
PWM_Configuration(PWMA, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
}
定时器中断里面是这样写的
if(!PWM1_Flag)
{
PWMA_Duty.PWM1_Duty++;
if(PWMA_Duty.PWM1_Duty >= 2047) PWM1_Flag = 1;
}
else
{
PWMA_Duty.PWM1_Duty--;
if(PWMA_Duty.PWM1_Duty <= 0) PWM1_Flag = 0;
}
if(!PWM2_Flag)
{
PWMA_Duty.PWM2_Duty++;
if(PWMA_Duty.PWM2_Duty >= 2047) PWM2_Flag = 1;
}
else
{
PWMA_Duty.PWM2_Duty--;
if(PWMA_Duty.PWM2_Duty <= 0) PWM2_Flag = 0;
}
if(!PWM3_Flag)
{
PWMA_Duty.PWM3_Duty++;
if(PWMA_Duty.PWM3_Duty >= 2047) PWM3_Flag = 1;
}
else
{
PWMA_Duty.PWM3_Duty--;
if(PWMA_Duty.PWM3_Duty <= 0) PWM3_Flag = 0;
}
if(!PWM4_Flag)
{
PWMA_Duty.PWM4_Duty++;
if(PWMA_Duty.PWM4_Duty >= 2047) PWM4_Flag = 1;
}
else
{
PWMA_Duty.PWM4_Duty--;
if(PWMA_Duty.PWM4_Duty <= 0) PWM4_Flag = 0;
}
UpdatePwm(PWMA, &PWMA_Duty);
|