增量式PID的stm32實現(xiàn),整定過程 作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對面交流海量資料庫查詢 收藏 void TIM5_PWMINPUT_INIT(u16 arr,u16 psc){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化結(jié)構(gòu)體NVIC_InitTypeDef NVIC_InitStructure;//中斷配置TIM_ICInitTypeDefTIM5_ICInitStructure;//TIM4PWM配置結(jié)構(gòu)體GPIO_InitTypeDef GPIO_InitStructure;//IO口配置結(jié)構(gòu)體RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);//Open TIM4 clockRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//open gpioB clockGPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//GPIO 1GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//浮空輸入 上拉輸入GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時鐘分割:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上計數(shù)模式TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2;TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;TIM5_ICInitStructure.TIM_ICFilter = 0x3;//Filter:過濾TIM_PWMIConfig(TIM5, &TIM5_ICInitStructure);//PWM輸入配置TIM_SelectInputTrigger(TIM5, TIM_TS_TI2FP2);//選擇有效輸入端TIM_SelectSlaveMode(TIM5, TIM_SlaveMode_Reset);//配置為主從復(fù)位模式TIM_SelectMasterSlaveMode(TIM5, TIM_MasterSlaveMode_Enable);//啟動定時器的被動觸發(fā)TIM_ITConfig(TIM5, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中斷配置TIM_ClearITPendingBit(TIM5, TIM_IT_CC2|TIM_IT_Update); //清除中斷標志位TIM_Cmd(TIM5, ENABLE);}void TIM5_IRQHandler(void){{if (TIM_GetITStatus(TIM5, TIM_IT_CC2) != RESET)//捕獲1發(fā)生捕獲事件{duty_TIM5=TIM_GetCapture1(TIM5); //采集占空比if(TIM_GetCapture2(TIM5)>600)period_TIM5=TIM_GetCapture2(TIM5);CollectFlag_TIM5 = 0;}}TIM_ClearITPendingBit(TIM5, TIM_IT_CC2|TIM_IT_Update); //清除中斷標志位}復(fù)制代碼
評論