|
- #include "PLSR.h"
- #include "PSCARR.h"
- #include "tim.h"
- #include "sram.h"
- #include <string.h>
- #include "modbus.h"
- #include "bitset.h"
-
- Pulse PulseOutput[10]; // 十段脉冲的配置
- Option Options;
-
-
- uint8_t NowPulse; /* 当前处于第几段脉冲 */
- uint8_t PrePulse; /* 之前处于第几段脉冲 */
- uint8_t PulseNum; /* 一共有多少段脉冲 */
- int32_t MAX_Pulse; /* 最多的脉冲数 */
- int32_t AccCount; /* 加速脉冲数 */
- int32_t NowMAXPulse; /* 当前要达到的总脉冲 */
-
- int32_t PulseCount = 0;
-
- /**
- * @brief 根据脉冲的频率设置对应的PSC和ARR
- * @param[in] SentPost 脉冲通道(目前只有通道Y0)
- * @param[in] Ferquency 脉冲的频率
- * @return 无
- */
- void SetFrequency(uint8_t SentPost, uint32_t Frequency)
- {
- Frequency_Change_PSC_And_ARR(Frequency, &psc, &arr);
- switch(SentPost)
- {
- case 0: TIM10->ARR = arr, TIM10->PSC = psc, TIM10->CCR1 = arr / 2; break;
- }
-
- }
-
- /**
- * @brief 设置脉冲基本参数
- * @param[in] Pulse 第几段脉冲
- * @param[in] Ferquency 脉冲的频率
- * @param[in] Count 脉冲的数量
- * @param[in] EXT EXT信号
- * @param[in] NextPulse 下一段脉冲
- * @return 无
- */
- void SetPulse(uint8_t Pulse, uint32_t Ferquency, int32_t Count, uint8_t EXT, int8_t NextPulse)
- {
- PulseOutput[Pulse].Frequency = Ferquency;
- PulseOutput[Pulse].PulseCount = Count;
- PulseOutput[Pulse].EXT = EXT;
- PulseOutput[Pulse].NextPulse = NextPulse;
- }
-
- /**
- * @brief 脉冲初始化
- * @param[in] StartPulse 起始脉冲段
- * @param[in] PulseMod 绝对1/相对0
- * @return 无
- */
- void PulseBaseInit(uint8_t StartPulse, uint8_t PulseMod, int32_t MAX_Pulse)
- {
- Options.StartPulse = StartPulse;
- Options.RunMod = PulseMod;
- NowPulse = StartPulse;
- MAX_Pulse = MAX_Pulse;
- }
-
- void PulseStart(void)
- {
- SetFrequency(Options.DirPost, PulseOutput[NowPulse].Frequency);
- TIM2->ARR = PulseOutput[NowPulse].PulseCount;
- HAL_TIM_PWM_Start_IT(&htim10, TIM_CHANNEL_1);
- }
- /**
- * @brief 获取加速度
- * @param[in] Start 起始脉冲频率
- * @param[in] End 目标脉冲频率
- * @param[in] AccCount 需要多少个脉冲完成加速
- * @return 加速度
- */
- float GetAcc(int32_t Start, int32_t End, int32_t AccCount)
- {
- return ((float)End - (float)Start) / (float)AccCount;
- }
-
- /**
- * @brief 添加新的脉冲(只允许按顺序添加)
- * @param[in] Pulse 第几段脉冲
- * @param[in] Ferquency 脉冲的频率
- * @param[in] Count 脉冲的数量
- * @param[in] NextPulse 下一段脉冲
- * @return 无
- */
- void AddPulse(uint8_t Pulse, uint32_t Ferquency, int32_t Count, int8_t NextPulse)
- {
- if(Pulse == Options.AllPulse)
- {
- if(Count > 0)
- {
- SetPulse(Pulse, Ferquency, Count, 0, NextPulse);
- }
- else
- {
- SetPulse(Pulse, Ferquency, -Count, 1, NextPulse);
- }
-
- Options.AllPulse ++;
- }
-
- }
-
- /**
- * @brief 脉冲初始化
- * @param[in] Pulse 第几段脉冲
- * @param[in] Ferquency 脉冲的频率
- * @param[in] Count 脉冲的数量
- * @param[in] Direction 脉冲的方向
- * @param[in] NextPulse 下一段脉冲
- * @return 无
- */
- void PulseInit(uint8_t Pulse, uint32_t Ferquency, int32_t Count, int8_t NextPulse)
- {
- if(Count > 0)
- {
- SetPulse(Pulse, Ferquency, Count, 0, NextPulse);
- }
- else
- {
- SetPulse(Pulse, Ferquency, -Count, 1, NextPulse);
- }
- Options.AllPulse = 1;
- }
-
- /**
- * @brief 设置结果脉冲数
- * @param[in] PulseCount 要设定的脉冲数
- * @return 无
- */
- void MAXPulseSet(int32_t PulseCount)
- {
- if(NowPulse == 0)
- {
- NowMAXPulse = PulseOutput[NowPulse].PulseCount;
- }
- else
- {
- NowMAXPulse += PulseOutput[NowPulse].PulseCount;
- }
- }
-
- /**
- * @brief 设置下一次进入中断的脉冲数
- * @return 无
- */
- void SetNextPulse(void)
- {
- MAXPulseSet(PulseOutput[NowPulse].PulseCount); /* 更改结果脉冲数 */
-
- if(Options.RunMod == 0) /* 如果是相对模式 */
- {
- TIM2->ARR = PulseOutput[NowPulse].PulseCount;
- }
- else /* 如果是绝对模式 */
- {
- if(NowMAXPulse >= MAX_Pulse) /* 如果超出了上限 */
- {
- TIM2->ARR = - (MAX_Pulse - NowMAXPulse);
- }
- else
- {
- TIM2->ARR = PulseOutput[NowPulse].PulseCount;
- }
- }
- }
-
- /**
- * @brief 获取与CNT相加的基础值
- * @param[in] NowPulseNUM 当前的脉冲段数
- * @return 基础值
- */
- uint32_t GetBase(uint8_t NowPulseNUM)
- {
- uint32_t temp = 0;
- if(NowPulseNUM == 0)
- {
- temp = 0;
- }
- else
- {
- for(int i = 0; i < NowPulseNUM; i++)
- {
- temp += PulseOutput[i].PulseCount;
- }
- }
- return temp;
- }
-
- /**
- * @brief 从寄存器读取设置参数
- * @return 无
- */
- void PLSROptionLoad(void)
- {
- /* 数据转换 */
- uint32_t InitSpeed;
- uint16_t tempH, tempL;
- Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
- Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
- Set16_32(&InitSpeed, &tempH, &tempL);
-
- /* 数据读取 */
- Options.SentPost = Register_L[0x1000];
- Options.DirPost = Register_L[0x1001];
- Options.EXT = Register_L[0x1002];
- Options.DirDelay = Register_L[0x1003]; //可能更大。
- Options.Dir = Register_L[0x1004];
- Options.AccMod = Register_L[0x1005];
- Options.RunMod = Register_L[0x1006];
- Options.AllPulse = Register_L[0x1007];
- Options.StartPulse = Register_L[0x1008];
- Options.InitSpeed = InitSpeed;
- Options.AccUpTime = Register_L[0x100B];
- Options.AccDownTime = Register_L[0x100C];
- }
-
- void PLSRPluseLoad(void)
- {
- uint32_t temp_Frequency, temp_PulseCount;
- uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;
- for (int i = 0; i < 10; i++)
- {
- Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
- , &Register_L[0x1100 + 0x10 * i]);
- Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
- , &Register_L[0x1101 + 0x10 * i]);
- Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
-
- Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
- , &Register_L[0x1102 + 0x10 * i]);
- Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
- , &Register_L[0x1103 + 0x10 * i]);
- Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);
-
- PulseOutput[i].Frequency = temp_Frequency;
- PulseOutput[i].PulseCount = temp_PulseCount;
- PulseOutput[i].EXT = Register_L[0x1104 + 0x10 * i];
- PulseOutput[i].NextPulse = Register_L[0x1105 + 0x10 * i];
-
- }
- }
-
- void CountSave(void)
- {
- int32_t temp_Count = 0;
- uint16_t DatH, DatL;
-
-
- memcpy(&temp_Count, &AllPulseCNT, 4);
- DatH = temp_Count / 0xffff;
- DatL = temp_Count % 0xffff;
- Register_H[0x2001] = DatH / 0xff;
- Register_L[0x2001] = DatH % 0xff;
- Register_H[0x2000] = DatL / 0xff;
- Register_L[0x2000] = DatL % 0xff;
-
- }
|