微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 增量式PID的stm32实现,整定过程

增量式PID的stm32实现,整定过程

时间:11-28 来源:互联网 点击:

  1. TIM1_ICInitStructure.TIM_ICFilter = 0x03;//Filter:过滤
  2. TIM_PWMIConfig(TIM1, &TIM1_ICInitStructure);//PWM输入配置
  3. TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);//选择有效输入端
  4. TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);//配置为主从复位模式
  5. TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);//启动定时器的被动触发
  6. // TIM_ITConfig(TIM1, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中断配置
  7. TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE); //通道2 捕获中断打开
  8. //TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
  9. TIM_Cmd(TIM1, ENABLE);
  10. }
  11. void TIM1_CC_IRQHandler(void)
  12. {
  13. {
  14. if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
  15. {
  16. duty_TIM1=TIM_GetCapture1(TIM1); //采集占空比
  17. if(TIM_GetCapture2(TIM1)>600)period_TIM1=TIM_GetCapture2(TIM1);
  18. CollectFlag_TIM1 = 0;
  19. }
  20. }
  21. TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
  22. }
  23. void TIM2_PWMINPUT_INIT(u16 arr,u16 psc)
  24. {
  25. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
  26. NVIC_InitTypeDef NVIC_InitStructure;//中断配置
  27. TIM_ICInitTypeDefTIM2_ICInitStructure;//TIM2PWM配置结构体
  28. GPIO_InitTypeDef GPIO_InitStructure;//IO口配置结构体
  29. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//Open TIM2 clock
  30. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//open gpioB clock
  31. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);//使能GPIO外设和AFIO复用功能模块时钟
  32. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//关闭JTAG
  33. GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer2完全重映射TIM2_CH2->PB3
  34. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//GPIO 3
  35. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//浮空输入 上拉输入
  36. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37. GPIO_Init(GPIOB, &GPIO_InitStructure);
  38. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  39. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  40. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  41. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
  42. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  43. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  44. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  45. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  46. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  47. NVIC_Init(&NVIC_InitStructure);
  48. TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2;
  49. TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  50. TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  51. TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_div1;
  52. TIM2_ICInitStructure.TIM_ICFilter = 0x3;//Filter:过滤
  53. TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure);//PWM输入配置
  54. TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);//选择有效输入端
  55. TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);//配置为主从复位模式
  56. TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//启动定时器的被动触发
  57. TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中断配置
  58. TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
  59. TIM_Cmd(TIM2, ENABLE);
  60. }
  61. void TIM2_IRQHandler(void)
  62. {
  63. {
  64. if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
  65. {
  66. duty_TIM2=TIM_GetCapture1(TIM2); //采集占空比
  67. if(TIM_GetCapture2(TIM2)>600)period_TIM2=TIM_GetCapture2(TIM2);
  68. CollectFlag_TIM2 = 0;
  69. }
  70. }
  71. TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
  72. }
  73. void TIM5_PWMINPUT_INIT(u16 arr,u16 psc)
  74. {
  75. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
  76. NVIC_InitTypeDef NVIC_InitStructure;//中断配置
  77. TIM_ICInitTypeDefTIM5_ICInitStructure;//TIM4PWM配置结构体
  78. GPIO_InitTypeDef GPIO_InitStructure;//IO口配置结构体

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top