Chinaunix首页 | 论坛 | 博客
  • 博客访问: 831427
  • 博文数量: 281
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:45
个人简介

邮箱:zhuimengcanyang@163.com 痴爱嵌入式技术的蜗牛

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: 嵌入式

2017-03-07 16:14:07

Timer1属于高级定时器,在设置PWM模式的时候,必须加这一句话:使能PWM输出
TIM_CtrlPWMOutputs(TIM_1, ENABLE);

点击(此处)折叠或打开

  1. /*****************************************************************************
  2.  * Function : timer_GpioInit
  3.  * Description : gpio initialize for timer module.
  4. * A8, A9 - TIM1_CH1, TIM1_CH2
  5.                          A6, A7 - TIM3_CH1, TIM3_CH2
  6.                          B0, B1 - TIM3_CH3, TIM3_CH4
  7.  
  8.  * Input : void
  9.  * Output : None
  10.  * Return :
  11.  * Others :
  12.  * Record
  13.  * 1.Date : 20170307
  14.  * Author : grant.hu
  15.  * Modification: Created function

  16. *****************************************************************************/
  17. static void timer_GpioInit(void)
  18. {
  19.     GPIO_InitTypeDef GPIO_InitStructure;

  20.     /* Enable clock for timer1, timer3 module */
  21.     //RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  22.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  23.     /* GPIOB clock enable, Enable AFIO function */
  24.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

  25.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* enable alternative function of GPIO */
  26.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.     
  28.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 |GPIO_Pin_9;
  29.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  30.     
  31.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  32.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  33. }


  34. /*****************************************************************************
  35.  * Function : timer_SetFreDty
  36.  * Description : set input frequency and dutycycle for different channel
  37.                             of different timer module.
  38.  * Input : TIM_TypeDef *TIM_TYPE
  39.                         uint16_t * pu16HZ
  40.                         uint8_t *u8Dty
  41.  * Output : None
  42.  * Return :
  43.  * Others :
  44.  * Record
  45.  * 1.Date : 20170307
  46.  * Author : grant.hu
  47.  * Modification: Created function

  48. *****************************************************************************/
  49. static int timer_SetFreDty(TIM_TypeDef *TIM_TYPE, PT_tmrOper ptTmrOper)
  50. {
  51.     uint16_t u16HzTemp;
  52.     uint16_t u16Period;
  53.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  54.     TIM_OCInitTypeDef TIM_OCInitStructure;

  55.     if(TIM_TYPE != TIM1 && TIM_TYPE != TIM3)
  56.         return -1;
  57.         
  58.     u16HzTemp = (TIM_TYPE == TIM1)? (ptTmrOper->u16Fre[0]) : (ptTmrOper->u16Fre[1]);
  59.     printf("timer HZ: %d\n", u16HzTemp);

  60.     TIM_InternalClockConfig(TIM_TYPE);
  61.     /* TIMxCLK = 72MHZ, timer module input clock = 72 MHZ/ (71 + 1) = 1Mhz */
  62.     TIM_TimeBaseStructure.TIM_Prescaler = 71;

  63.     /* set timer frequency */
  64.     u16Period = (uint16_t)((long)1000000 / u16HzTemp - 1);
  65.     
  66.     TIM_TimeBaseStructure.TIM_Period = u16Period;
  67.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
  68.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  69.     TIM_TimeBaseInit(TIM_TYPE, &TIM_TimeBaseStructure);

  70.     /* timer mode setting. */
  71.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  72.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  73.     
  74.     if(TIM_TYPE == TIM1)
  75.     {
  76.         /* TIM1, channel1 */
  77.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  78.         TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[0] * u16Period )/100;
  79.         TIM_OC1Init(TIM_TYPE, &TIM_OCInitStructure);
  80.         TIM_OC1PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);

  81.         /* TIM1, channel2 */
  82.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  83.         TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[1] * u16Period )/100;
  84.         TIM_OC2Init(TIM_TYPE, &TIM_OCInitStructure);
  85.         TIM_OC2PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);

  86.         // TODO: this code must be add when config TIM1, TIM8, different with configure general timer
  87.         TIM_CtrlPWMOutputs(TIM_TYPE, ENABLE);
  88.     }
  89.     else
  90.     {
  91.         /* TIM3, channel1 */
  92.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  93.         TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[2] * u16Period )/100;
  94.         TIM_OC1Init(TIM_TYPE, &TIM_OCInitStructure);
  95.         TIM_OC1PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);

  96.         /* TIM3, channel2 */
  97.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  98.         TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[3] * u16Period )/100;
  99.         TIM_OC2Init(TIM_TYPE, &TIM_OCInitStructure);
  100.         TIM_OC2PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);

  101.         /* TIM3, channel3 */
  102.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  103.         TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[4] * u16Period )/100;
  104.         TIM_OC3Init(TIM_TYPE, &TIM_OCInitStructure);
  105.         TIM_OC3PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
  106.         
  107.         /* TIM3, channel4 */
  108.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  109.         TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[5] * u16Period )/100;
  110.         TIM_OC4Init(TIM_TYPE, &TIM_OCInitStructure);
  111.         TIM_OC4PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
  112.     }
  113.                      
  114.     TIM_ARRPreloadConfig(TIM_TYPE, ENABLE);
  115.     TIM_Cmd(TIM_TYPE, ENABLE); /* TIM4 enable counter */

  116.     return 0;
  117. }


阅读(2706) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~