原理图:
1.TIM1 初始化
void TIM1_Init(void)
{
TIM1_PSCRH = 0;
TIM1_PSCRL = 15; //(15+1)分频为1M
TIM1_ARRH = 0x03;
TIM1_ARRL = 0xE8; //每1000us中断一次
TIM1_CCMR2_OC2M = 6; //PWM2模式1
TIM1_CCER1_CC2E = 1; //CC2引脚使能
TIM1_CCMR3_OC3M = 6; //PWM3模式1
TIM1_CCER2_CC3E = 1; //CC3引脚使能
TIM1_CCR2H = 0X00; //DUTY
TIM1_CCR2L = 0x00;
TIM1_CCR3H = 0X00; //DUTY
TIM1_CCR3L = 0x00;
TIM1_BKR |= 0x80; //设置刹车寄存器
TIM1_IER = 0x01; //允许更新中断
TIM1_CR1 = 0x01; //计数器使能,开始计数
}
2.设置占空比
void pwm1_duty(u16 duty)
{
TIM1_CCR2H=duty>>8;
TIM1_CCR2L=(u8)(duty);
TIM1_CCR3H=duty>>8;
TIM1_CCR3L=(u8)(duty);
}
3.呼吸
while(1)
{
if(key_value>=1000) dir=0;
if(key_value<=1) dir=1;
if(dir) key_value++;
else key_value--;
pwm1_duty(key_value);
delay_ms(3);
}
4.实验现象: