本帖最后由 309976659 于 2014-5-5 15:48 编辑
我想要用TIM1输出4路PWM,通道3和4都没有问题,可是1,2却死活不行,才接触stm单片机,求高手指点。代码如下:
#ifndef BIT
#define BIT(n) (1<<(n))
#endif
void setGpio(void)
{
PC_DDR = 0XF8; //PC7-PC3设输出
PC_CR1 = 0XF8; // PC7-PC3推挽输出
PC_CR2 = 0X0;
}
void PWM_INIT(void)
{
TIM1_CR1 &= ~BIT(0); //关闭TIM1
//
TIM1_PSCRH = 0;
TIM1_PSCRL = 0; //不分频2MHz
//
TIM1_ARRH = 0X07;
TIM1_ARRL = 0XCF; //2MHz/(1999+1) = 1KHz
//
TIM1_CR1 |= BIT(7); //使能ARP,边沿对齐,向上计数
TIM1_EGR |= BIT(0); //更新TIM1,使PSC有效
TIM1_EGR|=0x20;//重新初始化TIM1
TIM1_CCR1H = 0X03;
TIM1_CCR1L = 0XE6; //占空比50%
TIM1_CCMR1 = 0X68; //配置TIM1_CH1为PWM1模式输出
TIM1_CCR2H = 0X03;
TIM1_CCR2L = 0XE6; //占空比50%
TIM1_CCMR2 = 0X68; //配置TIM1_CH2为PWM1模式输出
TIM1_CCR3H = 0X03;
TIM1_CCR3L = 0XE6; //占空比50%
TIM1_CCMR3 = 0X68; //配置TIM1_CH3为PWM1模式输出
TIM1_CCR4H = 0X03;
TIM1_CCR4L = 0XE6; //占空比50%
TIM1_CCMR4 = 0X68; //配置TIM1_CH4为PWM1模式输出
//
TIM1_CCER1 |= BIT(0); //Enable TIM1_CH1 channel
TIM1_CCER1 |= BIT(4); //Enable TIM1_CH2 channel
TIM1_CCER2 |= BIT(0); //使能TIM1_CH3通道
TIM1_CCER2 |= BIT(4); //Enable TIM1_CH4 channel
//
TIM1_BKR |= BIT(7); //
TIM1_CR1 |= BIT(0); //使能TIM1
}
|