|
你的程序有问题,下面的程序是我自己写的,给你作参考:
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** TIM1初始化, 更新时间为40uS(PWM输出周期/25MHz)
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void TIM1_Init(void)
{
CLK_PCKENR1 |= 0x80; //开启定时器1外设时钟
TIM1_EGR |= 0x01; //重新初始化TIM1
TIM1_ARRH = (UCHAR)(TIM1_Period >> 8); //设定重装载值
TIM1_ARRL = (UCHAR)(TIM1_Period);
TIM1_PSCRH = 0x00; //设定预分频为1 = 16M
TIM1_PSCRL = 0x00;
TIM1_CR1 = 0x00; //边沿对齐,向上计数
TIM1_RCR = 0x00;
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** TIM1_CC2 PWM输出初始化
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void TIM1_OC2Init(void)
{
TIM1_CCER1 = 0x30; //CC2E开启,高电平有效
TIM1_CCMR2 = 0x70; //PWM模式1,CC1配置入输出
TIM1_OISR &= ~0x03;
TIM1_CCR2H = (UCHAR)(TIM1_Pulse >> 8);
TIM1_CCR2L = (UCHAR)(TIM1_Pulse);
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** TIM1使能或禁止
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void TIM1_Cmd(FunctionalState NewState)
{
if(NewState != DISABLE)
{
TIM1_CR1 |= 0x01; //使能计数器
}
else
{
TIM1_CR1 &= ~0x01; //禁止计数器
}
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** TIM1_CC2 PWM输出控制
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void TIM1_CtrlPWMOutPuts(FunctionalState NewState)
{
if(NewState != DISABLE)
{
TIM1_BKR |= 0x80;
}
else
{
TIM1_BKR &= ~0x80;
}
} |
|