Browse Source

增加脉冲输出计数

feature/PLSR功能
1923777848 21 hours ago
parent
commit
eede0ad12f
2 changed files with 40 additions and 17 deletions
  1. +26
    -13
      TrainCamp_zhangcheng_PLSR/app/src/plsr.c
  2. +14
    -4
      TrainCamp_zhangcheng_PLSR/bsp/src/time.c

+ 26
- 13
TrainCamp_zhangcheng_PLSR/app/src/plsr.c View File

@@ -109,7 +109,7 @@ SEGMENT_PART currentSeg = {
.currentPart = 1 .currentPart = 1
}; };
PLSR plsr = { PLSR plsr = {
.segmentAllNum = 4,
.segmentAllNum = 3,
.defaultSpeed = 1000, .defaultSpeed = 1000,
.defaultAccSpeedTime = 5, .defaultAccSpeedTime = 5,
.defaultDecSpeedTime = 10, .defaultDecSpeedTime = 10,
@@ -122,9 +122,9 @@ PLSR plsr = {
.segment[2].pulseFreq = 6000, .segment[2].pulseFreq = 6000,
.segment[2].pulseNumber = 2000, .segment[2].pulseNumber = 2000,
.segment[2].jumpNumber = 0, .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 .startRunSegment = 0
}; };
@@ -139,11 +139,12 @@ void PlsrModuleTask(void *pArg)


res = GetTimObj("tim2", &tim2); res = GetTimObj("tim2", &tim2);
res = GetTimObj("tim10", &tim10); res = GetTimObj("tim10", &tim10);
tim2.init(&tim2, 0, 65535);
tim2.open(&tim2);
tim10.init(&tim10, 168 - 1, 65535); tim10.init(&tim10, 168 - 1, 65535);
// tim10.open(&tim10);
tim10.registerIrq(&tim10, TimCallBack, &currentSeg); tim10.registerIrq(&tim10, TimCallBack, &currentSeg);
// tim10.openIrq(&tim10);
tim10.pwmInit(&tim10); tim10.pwmInit(&tim10);
tim10.pwmSetPulse(&tim10, TIM10->ARR); tim10.pwmSetPulse(&tim10, TIM10->ARR);


@@ -227,8 +228,9 @@ void TimCallBack(void *pArg)
{ {
uint16_t reloadValue = 0; uint16_t reloadValue = 0;
static int32_t pulseCount = 0; static int32_t pulseCount = 0;
static int32_t allcount = 0;
pulseCount++; pulseCount++;
allcount++;
if (currentSeg.currentPart == 1) if (currentSeg.currentPart == 1)
{ {
switch (currentSeg.currentState) switch (currentSeg.currentState)
@@ -245,7 +247,12 @@ void TimCallBack(void *pArg)
} }
else else
currentSpeed += currentSeg.inc; currentSpeed += currentSeg.inc;
if (currentSpeed <= 1000)
tim10.setPsc(&tim10, 2625 - 1);
else
tim10.setPsc(&tim10, 3 - 1);
reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed);
tim10.setPer(&tim10, reloadValue - 1); tim10.setPer(&tim10, reloadValue - 1);
tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1);
@@ -259,7 +266,7 @@ void TimCallBack(void *pArg)
} }
else if (currentSeg.currentPart == 2) else if (currentSeg.currentPart == 2)
{ {
if (pulseCount > currentSeg.secondPartNum - 4)
if (pulseCount > currentSeg.secondPartNum - 1)
{ {
if ((currentSegment == (plsr.segmentAllNum -1)) if ((currentSegment == (plsr.segmentAllNum -1))
&& (plsr.segment[currentSegment].jumpNumber == 0)) && (plsr.segment[currentSegment].jumpNumber == 0))
@@ -282,7 +289,7 @@ void TimCallBack(void *pArg)
else if (currentSeg.currentPart == 3) else if (currentSeg.currentPart == 3)
{ {
if (((currentSpeed - currentSeg.dec) <= 0) if (((currentSpeed - currentSeg.dec) <= 0)
|| (pulseCount > currentSeg.thirdPartNum))
|| (pulseCount > currentSeg.thirdPartNum - 1))
{ {
pulseCount = 0; pulseCount = 0;
currentSeg.currentPart = 1; currentSeg.currentPart = 1;
@@ -295,6 +302,12 @@ void TimCallBack(void *pArg)
return; return;
} }
currentSpeed -= currentSeg.dec; currentSpeed -= currentSeg.dec;
if (currentSpeed <= 1000)
tim10.setPsc(&tim10, 2625 - 1);
else
tim10.setPsc(&tim10, 3 - 1);

reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed);
tim10.setPer(&tim10, reloadValue - 1); tim10.setPer(&tim10, reloadValue - 1);
tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1);
@@ -433,13 +446,13 @@ static void FirstSegmentProc(SEGMENT_PART *segmentPart)
segmentPart->inc = segmentPart->inc - diffValue; segmentPart->inc = segmentPart->inc - diffValue;
} }
incAll += segmentPart->inc; incAll += segmentPart->inc;
if (plsr.segment[currentSegment].pulseFreq <= 5000)
if (currentSpeed <= 1000)
{ {
tim10.setPsc(&tim10, 2625 - 1); tim10.setPsc(&tim10, 2625 - 1);
} }
else else
{ {
tim10.setPsc(&tim10, 168 - 1);
tim10.setPsc(&tim10, 3 - 1);
} }
if (currentSpeed != 0) if (currentSpeed != 0)
tim10.setPer(&tim10, 168000000 / (TIM10->PSC + 1) / currentSpeed); tim10.setPer(&tim10, 168000000 / (TIM10->PSC + 1) / currentSpeed);


+ 14
- 4
TrainCamp_zhangcheng_PLSR/bsp/src/time.c View File

@@ -70,7 +70,20 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per)
if (tim->TIM == TIM2) if (tim->TIM == TIM2)
{ {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 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) else if (tim->TIM == TIM10)
@@ -99,10 +112,7 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per)
else else
return -1; 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_InternalClockConfig(tim->TIM);


TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;


Loading…
Cancel
Save