diff --git a/TrainCamp_zhangcheng_PLSR/app/src/plsr.c b/TrainCamp_zhangcheng_PLSR/app/src/plsr.c index b29e131..a6748ad 100644 --- a/TrainCamp_zhangcheng_PLSR/app/src/plsr.c +++ b/TrainCamp_zhangcheng_PLSR/app/src/plsr.c @@ -33,6 +33,7 @@ static OS_STK PlsrModuleStack[PLSR_MODULE_STACK_SIZE]; static void PlsrModuleTask(void * pArg); static void TimCallBack(void *pArg); +static void Tim2CntCallBack(void *pArg); static void ReadRegisterParam(PLSR *plsr); static void configPulsePort(PLSR *plsr); static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart); @@ -88,6 +89,8 @@ RESUIL PLSRInit(void) * @retval MODBUS_FALSE 失败 */ TIM_OBJ tim2; +int32_t tim2OverCnt = 0; +int32_t CumulativePulseCount = 0; TIM_OBJ tim10; int32_t accSpeedSlope = 0; int32_t decSpeedSlope = 0; @@ -109,7 +112,7 @@ SEGMENT_PART currentSeg = { .currentPart = 1 }; PLSR plsr = { - .segmentAllNum = 3, + .segmentAllNum = 4, .defaultSpeed = 1000, .defaultAccSpeedTime = 5, .defaultDecSpeedTime = 10, @@ -122,9 +125,9 @@ PLSR plsr = { .segment[2].pulseFreq = 6000, .segment[2].pulseNumber = 2000, .segment[2].jumpNumber = 0, -// .segment[3].pulseFreq = 9000, -// .segment[3].pulseNumber = 2000, -// .segment[3].jumpNumber = 0, + .segment[3].pulseFreq = 9000, + .segment[3].pulseNumber = 100000, + .segment[3].jumpNumber = 0, .startRunSegment = 0 }; @@ -140,8 +143,10 @@ void PlsrModuleTask(void *pArg) res = GetTimObj("tim2", &tim2); res = GetTimObj("tim10", &tim10); - tim2.init(&tim2, 0, 65535); + tim2.init(&tim2, 0, 50000 - 1); tim2.open(&tim2); + tim2.registerIrq(&tim2, Tim2CntCallBack, NULL); + tim2.openIrq(&tim2); tim10.init(&tim10, 168 - 1, 65535); tim10.registerIrq(&tim10, TimCallBack, ¤tSeg); @@ -162,6 +167,8 @@ void PlsrModuleTask(void *pArg) continue; } + CumulativePulseCount = tim2OverCnt * (TIM2->ARR + 1) + TIM2->CNT; + if (plsr.segment[currentSegment].pulseFreq <= 1000) pulseFreq = 2625 - 1; else if ((plsr.segment[currentSegment].pulseFreq > 1000) @@ -224,26 +231,28 @@ void PlsrModuleTask(void *pArg) //定时器9 内部输入时钟源ITx主从模式定时器10 //定时器12 内部输入时钟源ITx主从模式定时器13 -void TimCallBack(void *pArg) +static void Tim2CntCallBack(void *pArg) +{ + tim2OverCnt++; +} + +static void TimCallBack(void *pArg) { uint16_t reloadValue = 0; static int32_t pulseCount = 0; static int32_t allcount = 0; - pulseCount++; - allcount++; + if (currentSeg.currentPart == 1) { switch (currentSeg.currentState) { case 2: ///< 加速 { - if ((currentSpeed + currentSeg.inc) >= plsr.segment[currentSegment].pulseFreq - || (pulseCount > currentSeg.firstPartNum - 1)) + if ((currentSpeed + currentSeg.inc) >= plsr.segment[currentSegment].pulseFreq) { currentSpeed = plsr.segment[currentSegment].pulseFreq; currentSeg.currentPart = 2; - pulseCount = 0; } else currentSpeed += currentSeg.inc; @@ -255,7 +264,13 @@ void TimCallBack(void *pArg) reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); tim10.setPer(&tim10, reloadValue - 1); - tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); + tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); + allcount++; + pulseCount++; + + if (pulseCount > currentSeg.firstPartNum - 1) + pulseCount = 0; + break; } default: @@ -285,20 +300,23 @@ void TimCallBack(void *pArg) else currentSegment = plsr.segment[currentSegment].jumpNumber; } + allcount++; + pulseCount++; } else if (currentSeg.currentPart == 3) { if (((currentSpeed - currentSeg.dec) <= 0) - || (pulseCount > currentSeg.thirdPartNum - 1)) + || (pulseCount > currentSeg.thirdPartNum - 2)) { + currentSpeed = 0; pulseCount = 0; currentSeg.currentPart = 1; tim10.setPer(&tim10, 100); tim10.pwmSetPulse(&tim10, 65535); tim10.closeIrq(&tim10); - - tim10.registerIrq(&tim10, TimCallBack, ¤tSeg.currentPart); + + allcount++; return; } currentSpeed -= currentSeg.dec; @@ -311,6 +329,8 @@ void TimCallBack(void *pArg) reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); tim10.setPer(&tim10, reloadValue - 1); tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); + allcount++; + pulseCount++; } } diff --git a/TrainCamp_zhangcheng_PLSR/bsp/src/time.c b/TrainCamp_zhangcheng_PLSR/bsp/src/time.c index 4df429b..db1ad86 100644 --- a/TrainCamp_zhangcheng_PLSR/bsp/src/time.c +++ b/TrainCamp_zhangcheng_PLSR/bsp/src/time.c @@ -83,7 +83,7 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per) GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2); TIM_SelectInputTrigger(tim->TIM, TIM_TS_ETRF); - TIM_ETRClockMode2Config(tim->TIM, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0); + TIM_ETRClockMode2Config(tim->TIM, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); } else if (tim->TIM == TIM10) @@ -166,8 +166,10 @@ void TimDeInit(TIM_OBJ *tim) */ void TimSetPsc(TIM_OBJ *tim, uint16_t psc) { + TIM_ITConfig(tim->TIM, TIM_IT_Update, DISABLE); TIM_PrescalerConfig(tim->TIM, psc, TIM_PSCReloadMode_Immediate); TIM_ClearITPendingBit(tim->TIM, TIM_IT_Update); + TIM_ITConfig(tim->TIM, TIM_IT_Update, ENABLE); } @@ -361,7 +363,7 @@ void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2,TIM_IT_Update) == SET) { - if (ArgList[0] != NULL) + if (CallBackFuncList[0] != NULL) CallBackFuncList[0](ArgList[0]); TIM_ClearITPendingBit(TIM2, TIM_IT_Update); }