一个很简单的PWM生成程序,用的是Keil (MDK-ARM Version 3.20),程序运行一切正常,在PORTA的PIN3生成50%占空比的方波,程序如下: RCC_HSICmd(ENABLE); FLASH_SetLatency(FLASH_Latency_2); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9); RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08) { } RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div4); RCC_GetClocksFreq(&RCC_ClockFreq); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1800; TIM_TimeBaseStructure.TIM_Prescaler = 0x00; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Channel = TIM_Channel_4; //******** TIM_OCInitStructure.TIM_Pulse = 900; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInit(TIM2, &TIM_OCInitStructure); //******** TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM2, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE);
升级Keil(MDK-ARM Version 3.20),编译报错,信息是: “error: #136: struct "<unnamed>" has no field "TIM_Channel"” 检查STM的参考手册,发现STM改了库文件;TIM_OCInit不再存在,增加了TIM_OC1Init,TIM_OC2Init,TIM_OC3Init,TIM_OC4Init;TIM_OCInitStructure结构中不再有TIM_Channel;于是把程序中标有“//********”,改成如下: ........ // TIM_OCInitStructure.TIM_Channel = TIM_Channel_4; ....... TIM_OC4Init(TIM2, &TIM_OCInitStructure); 编译通过,测试后发现计数器运行正常,但是在PORTA的PIN3没有方波生成。问题是程序没有什么改动,怎么会有这种结果呢? 花了好长时间都没有搞清楚原因,是Keil的问题还是STM的问题? 或者是在新的库文件里,还需要进行其它的设置? |