#define TIM1_DIV1 (uint16)(1-1)
#define TIM1_DIV2 (uint16)(2-1)
#define TIM1_DIV4 (uint16)(4-1)
#define TIM1_DIV8 (uint16)(8-1)
#define TIM1_DIV16 (uint16)(16-1)
#define TIM1_DIV32 (uint16)(32-1)
//timer1 PWM 输出到管脚使能
_Bool PWMAOUT_PINENBLE @TIM1_CCER1:0 ;//TIM1_CH1 -- PC6
_Bool PWMBOUT_PINENBLE @TIM1_CCER1:4 ;//TIM1_CH2 -- PC7
_Bool PWMCOUT_PINENBLE @TIM1_CCER2:0 ;//TIM1_CH3 -- PC3
// PWM 频率设定
#define V_PWMFRE 1000
#define ToCMPxH(CMP,Value) (CMP = (uint8)((Value >> 8 ) & 0xFF))
#define ToCMPxL(CMP,Value) (CMP = (uint8)(Value & 0xFF))
//*************************************
// 函数名称:Init_Timer1_PWM
// 函数功能:定时器1作PWM输出时初始化
// 入口参数:PWM等级 每级0.625U (1000*0.625 = 62.5U = 8K)
// 出口参数:无
// 此初始化设置应用在BLDC 三相PWM输出
//***************************************
void Init_Timer1_PWM(uint16 Tcon,uint16 Pscr)
{
//16M系统时钟经预分频f=fck/(PSCR+1)
TIM1_PSCRH = (Pscr >> 8) & 0xff ;
TIM1_PSCRL = Pscr & 0xff ;
//设定重装载时的寄存器值,255是最大值
TIM1_ARRH = (Tcon >> 8) & 0xff ;
TIM1_ARRL = Tcon & 0xff ;
//设置刹车寄存器
TIM1_BKR|=0X80 ;
//PWM1模式,TIM1_CNT<TIM1_CCR1时有效
TIM1_CCMR1 =0x6C ;
//PWM1模式,TIM1_CNT<TIM1_CCR1时有效
TIM1_CCMR2 =0x6C ;
//PWM1模式,TIM1_CNT<TIM1_CCR1时有效
TIM1_CCMR3 =0x6C ;
//冻结模式,TIM1_CNT<TIM1_CCR1时有效
TIM1_CCMR4 =0x08 ;
//PWM 占空比 清0
TIM1_CCR1H = 0;
TIM1_CCR1L = 0;
TIM1_CCR2H = 0;
TIM1_CCR2L = 0;
TIM1_CCR3H = 0;
TIM1_CCR3L = 0;
TIM1_EGR = 0X01 ; //UG = 1 ;初始化计数器 预装载载入影子寄存器中
TIM1_CNTRH = 0 ; //计数器清0
TIM1_CNTRL = 0 ;
TIM1_CR1 |= 0X01; // 计数器使能,开始计数
}
//*************************************
// 函数名称:TOPWMVALUE
// 函数功能:设置PWM占空比
// 入口参数:占空比值
// 出口参数:无
//***************************************
void TOPWMVALUE(uint16 OUT_PWM)
{
ToCMPxH(TIM1_CCR1H,OUT_PWM);
ToCMPxL(TIM1_CCR1L,OUT_PWM);
ToCMPxH(TIM1_CCR2H,OUT_PWM);
ToCMPxL(TIM1_CCR2L,OUT_PWM);
ToCMPxH(TIM1_CCR3H,OUT_PWM);
ToCMPxL(TIM1_CCR3L,OUT_PWM);
}
//----实例应用------
//定时器1初始化
Init_Timer1_PWM(V_PWMFRE,TIM1_DIV2) ;//16M/2=8M 设置8K PWM输出