2# acgean LOCK位一直是0额。。
下面是调试时用的程序的全部代码(简化了下):
void Preset_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
DMA_InitTypeDef DMA_InitStructure;
//设置TIM2,定时T/P_COUNT
//打开定时器的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//TIMx寄存器重设为缺省值
TIM_DeInit(TIM2);
//时钟源,内部时钟
TIM_InternalClockConfig(TIM2);
//不预分频,72M
TIM_TimeBaseStructure.TIM_Prescaler=1-1;
//初始化时基
TIM_TimeBaseStructure.TIM_Period=SystemCoreClock/P_COUNT/MW.f_set;//自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样分频,不分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//允许更新事件
TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_Trigger,ENABLE);
//清TIM2计数器
TIM2->CNT = 1;
TIM2->SR&=~TIM_FLAG_Update;
//开启时钟
TIM2->CR1 |= TIM_CR1_CEN;
//PWM初始化
pwm_init(&PWM1);
}
void pwm_init(PWM_TypeDef *PWMx)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
u16 ps ;
//预分频值
//开启时钟,下面是简写过的,去掉switch..case等,看起来清楚些
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);