由于毕业在即,整理档案,打印表格,时间仓促,急急忙忙的用了两个晚上调试了tim1的pwm。以后工作时间充裕了,会多上传调试代码!
1、首先时钟配置
- *-----------------------------------------------------------------------------
- * System Clock source | PLL (HSE)
- *-----------------------------------------------------------------------------
- * SYSCLK(Hz) | 32000000
- *-----------------------------------------------------------------------------
- * HCLK(Hz) | 32000000
- *-----------------------------------------------------------------------------
- * AHB Prescaler | 1
- *-----------------------------------------------------------------------------
- * APB1 Prescaler | 2
- *-----------------------------------------------------------------------------
- * APB2 Prescaler | 1
- *-----------------------------------------------------------------------------
- * HSE Frequency(Hz) | 6000000
- *-----------------------------------------------------------------------------
复制代码外部6M晶振,内部倍频到32MHz。
2、选取定时器 TIM1 ,4通道输出,分别为PA8 PA9 PA10 PA11
2.1 完成IO配置
- void timer_io_do(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure2;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
- GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_8|GPIO_Pin_9;
- GPIO_Init(GPIOA, &GPIO_InitStructure2);
-
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1); //Connect TIM1 pins to AF1
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_TIM1); //Connect TIM1 pins to AF1
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_TIM1);
-
- }
复制代码2.2 完成定时器 统一功能初始化
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- /* Channel 1, 2,3 and 4 Configuration in PWM mode */
- TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //
- TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;//
- TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
复制代码2.3 ch1通道配置 同时按照同一方法配置ch2 ch3 ch4
- void TIM1_conf_1(unsigned int arr,unsigned int psc,unsigned int ccr)
- {
- uint16_t CCR1_Va1 = ccr;
- /* Time Base configuration */
- TIM_TimeBaseStructure.TIM_Prescaler = psc;
- TIM_TimeBaseStructure.TIM_Period = arr-1;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- TIM1_OCInitStructure.TIM_Pulse = CCR1_Va1;
- }
- void TIM1_1_EN(void)
- {
- TIM_OC1Init(TIM1, &TIM1_OCInitStructure);//TIM1-CH1
- }
复制代码3 主函数
main()
{
- timer_io_do();
- TIM1_Init();
- TIM1_conf_1(32000000/32000-1,0,(32000000/32000-1)/2);
- TIM1_conf_2(32000000/32000-1,0,(32000000/32000-1)/2);
- TIM1_conf_3(32000000/32000-1,0,(32000000/32000-1)/2);
- TIM1_conf_4(32000000/32000-1,0,(32000000/32000-1)/2);
- TIM1_4_EN();
- TIM1_3_EN();
- TIM1_2_EN();
- TIM1_1_EN();
- open_time1();
- while(1);
复制代码}
完成输出,都是输出 32K 的 占空比1/2 = 50% 的PWM。
至此,完成PWM的配置。
另外对网上的帖子表示感谢,现贴出学习帖子
http://www.cnblogs.com/zyqgold/archive/2013/05/26/3100377.html