wlzxzq的个人空间 https://bbs.21ic.com/?849035 [收藏] [复制] [RSS]

日志

为期两天的stm32f207 Tim1 的PWM输出搞定了,现分享下

已有 1740 次阅读2014-6-25 16:49 |个人分类:STM32F207学习|系统分类:ARM| stm32f207, Tim1, PWM

由于毕业在即,整理档案,打印表格,时间仓促,急急忙忙的用了两个晚上调试了tim1的pwm。以后工作时间充裕了,会多上传调试代码!
1、首先时钟配置
       
  1. *-----------------------------------------------------------------------------
  2.   *        System Clock source                    | PLL (HSE)
  3.   *-----------------------------------------------------------------------------
  4.   *        SYSCLK(Hz)                             | 32000000
  5.   *-----------------------------------------------------------------------------
  6.   *        HCLK(Hz)                               | 32000000
  7.   *-----------------------------------------------------------------------------
  8.   *        AHB Prescaler                          | 1
  9.   *-----------------------------------------------------------------------------
  10.   *        APB1 Prescaler                         | 2
  11.   *-----------------------------------------------------------------------------
  12.   *        APB2 Prescaler                         | 1
  13.   *-----------------------------------------------------------------------------
  14.   *        HSE Frequency(Hz)                      | 6000000
  15.   *-----------------------------------------------------------------------------
复制代码
外部6M晶振,内部倍频到32MHz。
2、选取定时器 TIM1 ,4通道输出,分别为PA8 PA9 PA10 PA11
2.1 完成IO配置
       
  1. void timer_io_do(void)
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure2;
  4.                RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); 

  5.         GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP;
  6.         GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP; 
  7.         GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF;
  8.               GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;

  9.         GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_8|GPIO_Pin_9;
  10.               GPIO_Init(GPIOA, &GPIO_InitStructure2); 
  11.            
  12.               GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);  //Connect TIM1 pins to AF1 
  13.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1);

  14.               GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_TIM1);  //Connect TIM1 pins to AF1 
  15.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_TIM1); 
  16.              
  17. }
复制代码
2.2 完成定时器 统一功能初始化
       
  1.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
  2.           TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  3.     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  4.           TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
  5.     /* Channel 1, 2,3 and 4 Configuration in PWM mode */
  6.     TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;    //
  7.     TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  8.     TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;//
  9.     TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  10.     TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
复制代码
2.3 ch1通道配置  同时按照同一方法配置ch2 ch3 ch4
       
  1. void TIM1_conf_1(unsigned int arr,unsigned int psc,unsigned int ccr)
  2. {
  3.     uint16_t CCR1_Va1 = ccr;

  4.   /* Time Base configuration */

  5.     TIM_TimeBaseStructure.TIM_Prescaler = psc; 

  6.     TIM_TimeBaseStructure.TIM_Period = arr-1;  

  7.     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  8.     TIM1_OCInitStructure.TIM_Pulse = CCR1_Va1; 
  9. }
  10. void TIM1_1_EN(void)
  11. {
  12.             TIM_OC1Init(TIM1, &TIM1_OCInitStructure);//TIM1-CH1
  13. }
复制代码
3 主函数
  main()
{
       
  1. timer_io_do();
  2.         TIM1_Init();
  3.         TIM1_conf_1(32000000/32000-1,0,(32000000/32000-1)/2);
  4.         TIM1_conf_2(32000000/32000-1,0,(32000000/32000-1)/2);
  5.         TIM1_conf_3(32000000/32000-1,0,(32000000/32000-1)/2);
  6.         TIM1_conf_4(32000000/32000-1,0,(32000000/32000-1)/2);
  7.         TIM1_4_EN();
  8.         TIM1_3_EN();
  9.         TIM1_2_EN();
  10.         TIM1_1_EN();
  11.         open_time1();
  12. while(1);
复制代码
}
完成输出,都是输出  32K 的 占空比1/2 = 50% 的PWM。
至此,完成PWM的配置。



另外对网上的帖子表示感谢,现贴出学习帖子
http://www.cnblogs.com/zyqgold/archive/2013/05/26/3100377.html

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)