From eede0ad12f0b5f3bcd5e75c34064ff1e5371c4f1 Mon Sep 17 00:00:00 2001 From: 1923777848 <1923777848@qq.com> Date: Wed, 13 Aug 2025 08:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=84=89=E5=86=B2=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TrainCamp_zhangcheng_PLSR/app/src/plsr.c | 39 ++++++++++++++++-------- TrainCamp_zhangcheng_PLSR/bsp/src/time.c | 18 ++++++++--- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/TrainCamp_zhangcheng_PLSR/app/src/plsr.c b/TrainCamp_zhangcheng_PLSR/app/src/plsr.c index 52a36c4..b29e131 100644 --- a/TrainCamp_zhangcheng_PLSR/app/src/plsr.c +++ b/TrainCamp_zhangcheng_PLSR/app/src/plsr.c @@ -109,7 +109,7 @@ SEGMENT_PART currentSeg = { .currentPart = 1 }; PLSR plsr = { - .segmentAllNum = 4, + .segmentAllNum = 3, .defaultSpeed = 1000, .defaultAccSpeedTime = 5, .defaultDecSpeedTime = 10, @@ -122,9 +122,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 = 2000, +// .segment[3].jumpNumber = 0, .startRunSegment = 0 }; @@ -139,11 +139,12 @@ void PlsrModuleTask(void *pArg) res = GetTimObj("tim2", &tim2); res = GetTimObj("tim10", &tim10); - + + tim2.init(&tim2, 0, 65535); + tim2.open(&tim2); + tim10.init(&tim10, 168 - 1, 65535); -// tim10.open(&tim10); tim10.registerIrq(&tim10, TimCallBack, ¤tSeg); -// tim10.openIrq(&tim10); tim10.pwmInit(&tim10); tim10.pwmSetPulse(&tim10, TIM10->ARR); @@ -227,8 +228,9 @@ 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) @@ -245,7 +247,12 @@ void TimCallBack(void *pArg) } else currentSpeed += currentSeg.inc; - + + if (currentSpeed <= 1000) + tim10.setPsc(&tim10, 2625 - 1); + else + tim10.setPsc(&tim10, 3 - 1); + reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); tim10.setPer(&tim10, reloadValue - 1); tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); @@ -259,7 +266,7 @@ void TimCallBack(void *pArg) } else if (currentSeg.currentPart == 2) { - if (pulseCount > currentSeg.secondPartNum - 4) + if (pulseCount > currentSeg.secondPartNum - 1) { if ((currentSegment == (plsr.segmentAllNum -1)) && (plsr.segment[currentSegment].jumpNumber == 0)) @@ -282,7 +289,7 @@ void TimCallBack(void *pArg) else if (currentSeg.currentPart == 3) { if (((currentSpeed - currentSeg.dec) <= 0) - || (pulseCount > currentSeg.thirdPartNum)) + || (pulseCount > currentSeg.thirdPartNum - 1)) { pulseCount = 0; currentSeg.currentPart = 1; @@ -295,6 +302,12 @@ void TimCallBack(void *pArg) return; } currentSpeed -= currentSeg.dec; + + if (currentSpeed <= 1000) + tim10.setPsc(&tim10, 2625 - 1); + else + tim10.setPsc(&tim10, 3 - 1); + reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); tim10.setPer(&tim10, reloadValue - 1); tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); @@ -433,13 +446,13 @@ static void FirstSegmentProc(SEGMENT_PART *segmentPart) segmentPart->inc = segmentPart->inc - diffValue; } incAll += segmentPart->inc; - if (plsr.segment[currentSegment].pulseFreq <= 5000) + if (currentSpeed <= 1000) { tim10.setPsc(&tim10, 2625 - 1); } else { - tim10.setPsc(&tim10, 168 - 1); + tim10.setPsc(&tim10, 3 - 1); } if (currentSpeed != 0) tim10.setPer(&tim10, 168000000 / (TIM10->PSC + 1) / currentSpeed); diff --git a/TrainCamp_zhangcheng_PLSR/bsp/src/time.c b/TrainCamp_zhangcheng_PLSR/bsp/src/time.c index 17e8e1f..4df429b 100644 --- a/TrainCamp_zhangcheng_PLSR/bsp/src/time.c +++ b/TrainCamp_zhangcheng_PLSR/bsp/src/time.c @@ -70,7 +70,20 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per) if (tim->TIM == TIM2) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOA, &GPIO_InitStructure); + + 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); } else if (tim->TIM == TIM10) @@ -99,10 +112,7 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per) else return -1; - if (tim->TIM == TIM2) - //TIM_TIxExternalClockConfig(tim->TIM, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 10); - TIM_InternalClockConfig(tim->TIM); - else + if (tim->TIM != TIM2) TIM_InternalClockConfig(tim->TIM); TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;