|
- #include "PLSR.h"
- #include "PSCARR.h"
- #include "tim.h"
- #include "sram.h"
- #include <string.h>
- #include "modbus.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 PLSRSramSave(void)
- {
- uint32_t temp[80] = {0}; //一共4 * 80 = 320
-
- Write_Backup_SRAM(temp, 80);
-
- }
-
- /**
- * @brief 数据上电读取
- * @return 无
- */
- void PLSRSramRead(void)
- {
- uint32_t temp[80] = {0};
- Read_Backup_SRAM(temp, 80);
-
- }
-
- /**
- * @brief 从寄存器读取设置参数
- * @return 无
- */
- void PLSROptionLoad(void)
- {
- 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 = Register_L[0x1009]; //需要修改。
- Options.AccUpTime = Register_L[0x100B];
- Options.AddDownTime = Register_L[0x100C];
- }
|