为有效电平;在向下计数时,一旦TIM1_CNT>TIM1_CCR1时通道1为有效电平,否则为无效
电平
11.控制寄存器 1(TIMx_CR1)
复位值0x00,这个寄存器对所有定时器适用。
12.刹车寄存器(TIM1_BKR)
二,程序:
1、TIM1_CH1_PWM互补+死区
void PWM2_INIT()
{
CLK_PCKENR2 |= 0x80;//开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_CR1 = 0x00; //B7(0)可以直接写入 B65(00)边缘对齐模式B4(0)向上计数B3(0)计数器不停止发生更新事件
TIM1_RCR = 0x00;
TIM1_PSCRH =0; //设定预分频为,16分频 16M
TIM1_PSCRL =0x00; //PWM的时钟 影响周期
TIM1_CCER1 = 0x0F; //CC2ER1开启1,2,高电平有效
TIM1_CCMR1 = 0x60; //PWM模式1,CC1配置入输出
TIM1_ARRH = 0x03; //设定重装载值
TIM1_ARRL = 0xe7; //PWM的周期
TIM1_CCR1H = 0x01;
TIM1_CCR1L = 0xf4; // 占空比值
TIM1_CR1 |= 0x01; //使能TIM1计数器
TIM1_DTR = 0x50; // Deadtime generator
TIM1_BKR |= 0x80;
}
2、TIM1_CH2_PWM互补+死区
void PWM2_INIT()
{
CLK_PCKENR1 |= 0x80;//开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_CR1 = 0x00; //B7(0)可以直接写入 B65(00)边缘对齐模式B4(0)向上计数B3(0)计数器不停止发生更新事件
TIM1_RCR = 0x00;
TIM1_PSCRH =0; //设定预分频为,16分频 16M
TIM1_PSCRL =0x00; //PWM的时钟 影响周期
TIM1_CCER1 = 0xf0; //CC2ER1开启1,2,高电平有效
TIM1_CCMR2 = 0x60; //PWM模式1,CC1配置入输出
TIM1_ARRH = 0x03; //设定重装载值
TIM1_ARRL = 0xe7; //PWM的周期
TIM1_CCR2H = 0x01;
TIM1_CCR2L = 0xf4; // 占空比值
TIM1_CR1 |= 0x01; //使能TIM1计数器
TIM1_DTR = 0x50; // Deadtime generator
TIM1_BKR |= 0x80;
}
3、TIM1_CH1_PWM+TIM1_CH2_PWM互补+死区
void PWM2_INIT()
{
CLK_PCKENR1 |= 0x80;//开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_CR1 = 0x00; //B7(0)可以直接写入 B65(00)边缘对齐模式B4(0)向上计数B3(0)计数器不停止发生更新事件
TIM1_RCR = 0x00;
TIM1_PSCRH =0; //设定预分频为,16分频 16M
TIM1_PSCRL =0x00; //PWM的时钟 影响周期
TIM1_CCER1 = 0xff; //CC2ER1开启1,2,高电平有效
TIM1_CCMR1 = 0x60; //PWM模式1,CC1配置入输出
TIM1_CCMR2 = 0x60; //PWM模式1,CC1配置入输出
TIM1_ARRH = 0x03; //设定重装载值
TIM1_ARRL = 0xe7; //PWM的周期
TIM1_CCR1H = 0x01;
TIM1_CCR1L = 0xf4; // 占空比值
TIM1_CCR2H = 0x01;
TIM1_CCR2L = 0xf4; // 占空比值
TIM1_CR1 |= 0x01; //使能TIM1计数器
TIM1_DTR = 0x50; // Deadtime generator
TIM1_BKR |= 0x80;
}
三、采用IAR仿真需设置:选择IAR菜单ST-Link,选择Option Bytes,配置如图
程序如下:亲测可以使用。
#include
/*
//TIM1_CH1_PWM互补+死区
void PWM2_INIT()
{
CLK_PCKENR2 |= 0x80;//开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_CR1 = 0x00; //B7(0)可以直接写入 B65(00)边缘对齐模式B4(0)向上计数B3(0)计数器不停止发生更新事件
TIM1_RCR = 0x00;
TIM1_PSCRH =0; //设定预分频为,16分频 16M
TIM1_PSCRL =0x00; //PWM的时钟 影响周期
TIM1_CCER1 = 0x0F; //CC2ER1开启1,2,高电平有效
TIM1_CCMR1 = 0x60; //PWM模式1,CC1配置入输出
TIM1_ARRH = 0x03; //设定重装载值
TIM1_ARRL = 0xe7; //PWM的周期
TIM1_CCR1H = 0x01;
TIM1_CCR1L = 0xf4; // 占空比值
TIM1_CR1 |= 0x01; //使能TIM1计数器
TIM1_DTR = 0x50; // Deadtime generator
TIM1_BKR |= 0x80;
}
*/
/*
//TIM1_CH2_PWM互补+死区
void PWM2_INIT()
{
CLK_PCKENR1 |= 0x80;//开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_CR1 = 0x00; //B7(0)可以直接写入 B65(00)边缘对齐模式B4(0)向上计数B3(0)计数器不停止发生更新事件
TIM1_RCR = 0x00;
TIM1_PSCRH =0; //设定预分频为,16分频 16M
TIM1_PSCRL =0x00; //PWM的时钟 影响周期
TIM1_CCER1 = 0xf0; //CC2ER1开启1,2,高电平有效
TIM1_CCMR2 = 0x60; //PWM模式1,CC1配置入输出
TIM1_ARRH = 0x03; //设定重装载值
TIM1_ARRL = 0xe7; //PWM的周期
TIM1_CCR2H = 0x01;
TIM1_CCR2L = 0xf4; // 占空比值
TIM1_CR1 |= 0x01; //使能TIM1计数器
TIM1_DTR = 0x50; // Deadtime generator
TIM1_BKR |= 0x80;
}
*/
//TIM1_CH1_PWM+TIM1_CH2_PWM互补+死区
void PWM2_INIT()
{
CLK_PCKENR1 |= 0x80;//开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_CR1 = 0x00; //B7(0)可以直接写入 B65(00)边缘对齐模式B4(0)向上计数B3(0)计数器不停止发生更新事件
TIM1_RCR = 0x00;
TIM1_PSCRH =0; //设定预分频为,16分频 16M
TIM1_PSCRL =0x00; //PWM的时钟 影响周期
TIM1_CCER1 = 0xff; //CC2ER1开启1,2,高电平有效
TIM1_CCMR1 = 0x60; //PWM模式1,CC1配置入输出
TIM1_CCMR2 = 0x60; //PWM模式1,CC1配置入输出
TIM1_ARRH = 0x03; //设定重装载值
TIM1_ARRL = 0xe7; //PWM的周期
TIM1_CCR1H = 0x01;
TIM1_CCR1L = 0xf4; // 占空比值
TIM1_CCR2H = 0x01;
TIM1_CCR2L = 0xf4; // 占空比值
TIM1_CR1 |= 0x01; //使能TIM1计数器
TIM1_DTR = 0x50; // Deadtime generator
TIM1_BKR |= 0x80;
}
int main(void)
{
/* Infinite loop */
CLK_CKDIVR=0x00;/*设置时钟为内部16M高速时钟*/
PWM_INIT();
//PWM2_INIT();
while(1)
{
}
}