修复中文注释乱码:22个GBK源文件统一转换为UTF-8,并更新Keil调试配置

This commit is contained in:
2026-09-04 15:03:52 +08:00
parent 8200e7b8aa
commit 41021cf95c
24 changed files with 1058 additions and 1044 deletions
+191 -191
View File
@@ -5,10 +5,10 @@
// 全局变量
// 全局变量
volatile MotorState g_motorState = MOTOR_STOPPED;
volatile MotorCommand g_motorCmd = MOTOR_CMD_STOP;
volatile MotorDirection g_motorDirection = DIR_STOPPED; // 新增运行方向标志位
volatile MotorDirection g_motorDirection = DIR_STOPPED; // 新增运行方向标志位
volatile uint16_t g_motorSpeed = 0;
volatile uint16_t g_accelTimer = 0;
@@ -32,7 +32,7 @@ void DCMOTORDBGOnOff(uint8_t OnOff)
}
#if(MCU == HC32F460JETA)
#if(0) //旧版本PCB
#if(0) //旧版本PCB
/* Timer4 CNT */
#define TIMER4_UNIT (M4_TMR41)
#define TIMER4_CNT_CYCLE_VAL (500u) /* Timer4 counter cycle value */
@@ -50,7 +50,7 @@ void DCMOTORDBGOnOff(uint8_t OnOff)
#define TIMER4_PWM_L_PIN (Pin13)
#endif
#if(1) //V2.1版本PCB
#if(1) //V2.1版本PCB
/* Timer4 CNT */
#define TIMER4_UNIT (M4_TMR41)
#define TIMER4_CNT_CYCLE_VAL (500u) /* Timer4 counter cycle value */
@@ -76,22 +76,22 @@ uint16_t init_adc_val;
uint16_t AdcInitCnt = 0;
#define Adc 0.80566 //3.3/4096=0.00080566 //ADC分辨率
#define Adc 0.80566 //3.3/4096=0.00080566 //ADC分辨率
uint16_t g_adc_val[ADC_CH_NUM];
extern uint16_t m_au16Adc1SaValue[ADC_CH_NUM * ADC_COLL];
/************************************* 第二部分 电压电流温度采集 **********************************************/
/************************************* 第二部分 电压电流温度采集 **********************************************/
/*
Rt = Rp *exp(B*(1/T1-1/T2))
Rt 是热敏电阻在T1温度下的阻值;
Rp是热敏电阻在T2常温下的标称阻值;
exp是e的n次方,e是自然常数,就是自然对数的底数,近似等于 2.7182818;
B值是热敏电阻的重要参数,教程中用到的热敏电阻B值为3380;
这里T1和T2指的是开尔文温度,T2是常温25℃,即(273.15+25)K
T1就是所求的温度
Rt 是热敏电阻在T1温度下的阻值;
Rp是热敏电阻在T2常温下的标称阻值;
exp是e的n次方,e是自然常数,就是自然对数的底数,近似等于 2.7182818;
B值是热敏电阻的重要参数,教程中用到的热敏电阻B值为3380;
这里T1和T2指的是开尔文温度,T2是常温25℃,即(273.15+25)K
T1就是所求的温度
*/
const float Rp = 10000.0f; /* 10K */
@@ -100,12 +100,12 @@ const float Bx = 3380.0f; /* B */
const float Ka = 273.15f;
/**
* @brief 计算温度值
* @param para: 温度采集对应ADC通道的值(已滤波)
* @note 计算温度分为两步:
1.根据ADC采集到的值计算当前对应的Rt
2.根据Rt计算对应的温度值
* @retval 温度值
* @brief 计算温度值
* @param para: 温度采集对应ADC通道的值(已滤波)
* @note 计算温度分为两步:
1.根据ADC采集到的值计算当前对应的Rt
2.根据Rt计算对应的温度值
* @retval 温度值
*/
float get_temp(uint16_t para)
{
@@ -113,97 +113,97 @@ float get_temp(uint16_t para)
float temp;
/*
第一步:
Rt = 3.3 * 4700 / VTEMP - 4700 ,其中VTEMP就是温度检测通道采集回来的电压值,VTEMP = ADC值* 3.3/4096
由此我们可以计算出当前Rt的值:Rt = 3.3f * 4700.0f / (para * 3.3f / 4096.0f ) - 4700.0f;
第一步:
Rt = 3.3 * 4700 / VTEMP - 4700 ,其中VTEMP就是温度检测通道采集回来的电压值,VTEMP = ADC值* 3.3/4096
由此我们可以计算出当前Rt的值:Rt = 3.3f * 4700.0f / (para * 3.3f / 4096.0f ) - 4700.0f;
*/
Rt = 3.3f * 4700.0f / (para * 3.3f / 4096.0f ) - 4700.0f; /* 根据当前ADC值计算出Rt的值 */
Rt = 3.3f * 4700.0f / (para * 3.3f / 4096.0f ) - 4700.0f; /* 根据当前ADC值计算出Rt的值 */
/*
第二步:
根据当前Rt的值来计算对应温度值:Rt = Rp *exp(B*(1/T1-1/T2))
第二步:
根据当前Rt的值来计算对应温度值:Rt = Rp *exp(B*(1/T1-1/T2))
*/
temp = Rt / Rp; /* 解出exp(B*(1/T1-1/T2)) ,即temp = exp(B*(1/T1-1/T2)) */
temp = log(temp); /* 解出B*(1/T1-1/T2) ,即temp = B*(1/T1-1/T2) */
temp /= Bx; /* 解出1/T1-1/T2 ,即temp = 1/T1-1/T2 */
temp += (1.0f / T2); /* 解出1/T1 ,即temp = 1/T1 */
temp = 1.0f / (temp); /* 解出T1 ,即temp = T1 */
temp -= Ka; /* 计算T1对应的摄氏度 */
return temp; /* 返回温度值 */
temp = Rt / Rp; /* 解出exp(B*(1/T1-1/T2)) ,即temp = exp(B*(1/T1-1/T2)) */
temp = log(temp); /* 解出B*(1/T1-1/T2) ,即temp = B*(1/T1-1/T2) */
temp /= Bx; /* 解出1/T1-1/T2 ,即temp = 1/T1-1/T2 */
temp += (1.0f / T2); /* 解出1/T1 ,即temp = 1/T1 */
temp = 1.0f / (temp); /* 解出T1 ,即temp = T1 */
temp -= Ka; /* 计算T1对应的摄氏度 */
return temp; /* 返回温度值 */
}
/**
* @brief 计算ADC的平均值(滤波)
* @param * p :存放ADC值的指针地址
* @note 此函数对电压、温度、电流对应的ADC值进行滤波,
* p[0]-p[2]对应的分别是电压、温度和电流
* @retval 无
* @brief 计算ADC的平均值(滤波)
* @param * p :存放ADC值的指针地址
* @note 此函数对电压、温度、电流对应的ADC值进行滤波,
* p[0]-p[2]对应的分别是电压、温度和电流
* @retval 无
*/
void calc_adc_val(uint16_t * p)
{
uint32_t temp[3] = {0,0,0};
int i;
for(i=0;i<ADC_COLL;i++) /* 循环ADC_COLL次取值,累加 */
for(i=0;i<ADC_COLL;i++) /* 循环ADC_COLL次取值,累加 */
{
temp[0] += m_au16Adc1SaValue[0+i*ADC_CH_NUM];
temp[1] += m_au16Adc1SaValue[1+i*ADC_CH_NUM];
temp[2] += m_au16Adc1SaValue[2+i*ADC_CH_NUM];
}
temp[0] /= ADC_COLL; /* 取平均值 */
temp[0] /= ADC_COLL; /* 取平均值 */
temp[1] /= ADC_COLL;
temp[2] /= ADC_COLL;
p[0] = temp[0]; /* 存入电压ADC通道平均值 */
p[1] = temp[1]; /* 存入温度ADC通道平均值 */
p[2] = temp[2]; /* 存入电流ADC通道平均值 */
p[0] = temp[0]; /* 存入电压ADC通道平均值 */
p[1] = temp[1]; /* 存入温度ADC通道平均值 */
p[2] = temp[2]; /* 存入电流ADC通道平均值 */
}
/************************************* 第三部分 编码器测速 ****************************************************/
/************************************* 第三部分 编码器测速 ****************************************************/
Motor_TypeDef g_motor_data; /*电机参数变量*/
ENCODE_TypeDef g_encode; /*编码器参数变量*/
Motor_TypeDef g_motor_data; /*电机参数变量*/
ENCODE_TypeDef g_encode; /*编码器参数变量*/
/**
* @brief 电机速度计算
* @param encode_now:当前编码器总的计数值
* ms:计算速度的间隔,中断1ms进入一次,例如ms = 5即5ms计算一次速度
* @retval 无
* @brief 电机速度计算
* @param encode_now:当前编码器总的计数值
* ms:计算速度的间隔,中断1ms进入一次,例如ms = 5即5ms计算一次速度
* @retval 无
*/
void speed_computer(int32_t encode_now, uint8_t ms)
{
uint8_t i = 0, j = 0;
float temp = 0.0;
static uint8_t sp_count = 0, k = 0;
static float speed_arr[10] = {0.0}; /* 存储速度进行滤波运算 */
static float speed_arr[10] = {0.0}; /* 存储速度进行滤波运算 */
if (sp_count == ms) /* 计算一次速度 */
if (sp_count == ms) /* 计算一次速度 */
{
/* 计算电机转速
第一步 :计算ms毫秒内计数变化量
第二步 ;计算1min内计数变化量:g_encode.speed * ((1000 / ms) * 60
第三步 :除以编码器旋转一圈的计数次数(倍频倍数 * 编码器分辨率)
第四步 :除以减速比即可得出电机转速
/* 计算电机转速
第一步 :计算ms毫秒内计数变化量
第二步 ;计算1min内计数变化量:g_encode.speed * ((1000 / ms) * 60
第三步 :除以编码器旋转一圈的计数次数(倍频倍数 * 编码器分辨率)
第四步 :除以减速比即可得出电机转速
*/
g_encode.encode_now = encode_now; /* 取出编码器当前计数值 */
g_encode.speed = (g_encode.encode_now - g_encode.encode_old); /* 计算编码器计数值的变化量 */
g_encode.encode_now = encode_now; /* 取出编码器当前计数值 */
g_encode.speed = (g_encode.encode_now - g_encode.encode_old); /* 计算编码器计数值的变化量 */
speed_arr[k++] = (float)(g_encode.speed * ((1000 / ms) * 60.0) / REDUCTION_RATIO / ROTO_RATIO ); /* 保存电机转速 */
speed_arr[k++] = (float)(g_encode.speed * ((1000 / ms) * 60.0) / REDUCTION_RATIO / ROTO_RATIO ); /* 保存电机转速 */
g_encode.encode_old = g_encode.encode_now; /* 保存当前编码器的值 */
g_encode.encode_old = g_encode.encode_now; /* 保存当前编码器的值 */
/* 累计10次速度值,后续进行滤波*/
/* 累计10次速度值,后续进行滤波*/
if (k == 10)
{
for (i = 10; i >= 1; i--) /* 冒泡排序*/
for (i = 10; i >= 1; i--) /* 冒泡排序*/
{
for (j = 0; j < (i - 1); j++)
{
if (speed_arr[j] > speed_arr[j + 1]) /* 数值比较 */
if (speed_arr[j] > speed_arr[j + 1]) /* 数值比较 */
{
temp = speed_arr[j]; /* 数值换位 */
temp = speed_arr[j]; /* 数值换位 */
speed_arr[j] = speed_arr[j + 1];
speed_arr[j + 1] = temp;
}
@@ -212,17 +212,17 @@ void speed_computer(int32_t encode_now, uint8_t ms)
temp = 0.0;
for (i = 2; i < 8; i++) /* 去除两边高低数据 */
for (i = 2; i < 8; i++) /* 去除两边高低数据 */
{
temp += speed_arr[i]; /* 将中间数值累加 */
temp += speed_arr[i]; /* 将中间数值累加 */
}
temp = (float)(temp / 6); /*求速度平均值*/
temp = (float)(temp / 6); /*求速度平均值*/
/* 一阶低通滤波
* 公式为:Y(n)= qX(n) + (1-q)Y(n-1)
* 其中X(n)为本次采样值;Y(n-1)为上次滤波输出值;Y(n)为本次滤波输出值,q为滤波系数
* q值越小则上一次输出对本次输出影响越大,整体曲线越平稳,但是对于速度变化的响应也会越慢
/* 一阶低通滤波
* 公式为:Y(n)= qX(n) + (1-q)Y(n-1)
* 其中X(n)为本次采样值;Y(n-1)为上次滤波输出值;Y(n)为本次滤波输出值,q为滤波系数
* q值越小则上一次输出对本次输出影响越大,整体曲线越平稳,但是对于速度变化的响应也会越慢
*/
g_motor_data.speed = (float)( ((float)0.48 * temp) + (g_motor_data.speed * (float)0.52) );
k = 0;
@@ -233,70 +233,70 @@ void speed_computer(int32_t encode_now, uint8_t ms)
}
/*****************************************************************************************
* 函数名称: get_current
* 功能描述: 获取工作电流函数
* 参 数: 无
* 返 回 值: 电流值
* 函数名称: get_current
* 功能描述: 获取工作电流函数
* 参 数: 无
* 返 回 值: 电流值
*****************************************************************************************/
float get_current(void)
{
return g_motor_data.current; /* 返回工作电流值 */
return g_motor_data.current; /* 返回工作电流值 */
}
/*****************************************************************************************
* 函数名称: get_volatage
* 功能描述: 获取工作电压函数
* 参 数: 无
* 返 回 值: 电压值
* 函数名称: get_volatage
* 功能描述: 获取工作电压函数
* 参 数: 无
* 返 回 值: 电压值
*****************************************************************************************/
float get_volatage(void)
{
return g_motor_data.volatage; /* 返回工作电压值 */
return g_motor_data.volatage; /* 返回工作电压值 */
}
/*****************************************************************************************
* 函数名称: get_Temperature
* 功能描述: 获取工作温度函数
* 参 数: 无
* 返 回 值: 温度值
* 函数名称: get_Temperature
* 功能描述: 获取工作温度函数
* 参 数: 无
* 返 回 值: 温度值
*****************************************************************************************/
float get_Temperature(void)
{
return g_motor_data.current; /* 返回工作温度值 */
return g_motor_data.current; /* 返回工作温度值 */
}
/*****************************************************************************************
* 函数名称: get_speed
* 功能描述: 获取电机实际转速值函数
* 参 数: 无
* 返 回 值: 电压值
* 函数名称: get_speed
* 功能描述: 获取电机实际转速值函数
* 参 数: 无
* 返 回 值: 电压值
*****************************************************************************************/
float get_speed(void)
{
return g_motor_data.speed; /* 返回电机实际转速值 */
return g_motor_data.speed; /* 返回电机实际转速值 */
}
/*****************************************************************************************
* 函数名称: get_location
* 功能描述: 获取电机实际位置函数
* 参 数: 无
* 返 回 值: 电压值
* 函数名称: get_location
* 功能描述: 获取电机实际位置函数
* 参 数: 无
* 返 回 值: 电压值
*****************************************************************************************/
float get_location(void)
{
return g_motor_data.speed; /* 返回电机实际位置 */
return g_motor_data.speed; /* 返回电机实际位置 */
}
/*****************************************************************************************
* 函数名称: Adc1mSRoutine
* 功能描述: Adc 1毫秒服务函数
* 参 数: 无
* 返 回 值: 无
* 函数名称: Adc1mSRoutine
* 功能描述: Adc 1毫秒服务函数
* 参 数: 无
* 返 回 值: 无
*****************************************************************************************/
bool AdcInitStatus = false; //ADC初始化状态标志位
volatile bool AdcInitSta = false; //ADC电流电压采集初始化标志位
uint16_t AdcDly = 0; //ADC采样定时计数
bool AdcInitStatus = false; //ADC初始化状态标志位
volatile bool AdcInitSta = false; //ADC电流电压采集初始化标志位
uint16_t AdcDly = 0; //ADC采样定时计数
void Adc1mSRoutine(void)
{
if(AdcInitStatus == false)
@@ -311,17 +311,17 @@ void Adc1mSRoutine(void)
/*****************************************************************************************
* 函数名称: AdcLoopHander
* 功能描述: Adc主循环函数
* 参 数: 无
* 返 回 值: 无
* 函数名称: AdcLoopHander
* 功能描述: Adc主循环函数
* 参 数: 无
* 返 回 值: 无
*****************************************************************************************/
void AdcLoopHander(void)
{
uint32_t temp = 0;
int i;
float temp_c = 0.0;
//开机等待电流数据稳定1秒,采集稳定电流值为初始值
//开机等待电流数据稳定1秒,采集稳定电流值为初始值
if((AdcInitStatus == true) && (AdcInitSta == false))
{
ADC_Start();
@@ -335,19 +335,19 @@ void AdcLoopHander(void)
else
{
while(DMA_GetChFlag(M4_DMA1, DmaCh0Sta) != DmaTransferComplete);
for(i=0;i<ADC_COLL;i++) /* 循环ADC_COLL次取值,累加 */
for(i=0;i<ADC_COLL;i++) /* 循环ADC_COLL次取值,累加 */
{
init_adc_val += m_au16Adc1SaValue[2+i*ADC_CH_NUM];
if(i > 0)
{
init_adc_val /= 2; /* 取平均值 */
init_adc_val /= 2; /* 取平均值 */
}
}
DCMOTOR_DBG_LOG("init_adc_val %d.\n", init_adc_val);
AdcInitSta = true;
}
}
//计算ADC采样值并滤波
//计算ADC采样值并滤波
if(AdcDly == 0)
{
float BatVoltage = 0;
@@ -356,7 +356,7 @@ void AdcLoopHander(void)
{
while(DMA_GetChFlag(M4_DMA1, DmaCh0Sta) != DmaTransferComplete);
DMA_Cmd(M4_DMA1, Disable);
calc_adc_val(g_adc_val); /* 计算ADC的平均值 */
calc_adc_val(g_adc_val); /* 计算ADC的平均值 */
// DCMOTOR_DBG_LOG("ADC1_IN1 value %d.\t Temp:%.1fC \r\n", g_adc_val[0u], get_temp((float)g_adc_val[0]*Adc));
// DCMOTOR_DBG_LOG("ADC2_IN2 value %d.\t Valtage:%.1fV \r\n", g_adc_val[1u], (float)g_adc_val[1]*ADC2VBUS);
@@ -365,8 +365,8 @@ void AdcLoopHander(void)
// g_motor_data.current = temp_c;
g_motor_data.current = (float)((g_motor_data.current * (float)0.60) + ((float)0.40 * temp_c)); /* 一阶低通滤波 */
if (abs(g_motor_data.current) <= 20) /* 过滤掉微弱浮动电流 */
g_motor_data.current = (float)((g_motor_data.current * (float)0.60) + ((float)0.40 * temp_c)); /* 一阶低通滤波 */
if (abs(g_motor_data.current) <= 20) /* 过滤掉微弱浮动电流 */
{
g_motor_data.current = 0.0;
}
@@ -429,7 +429,7 @@ void Timer41_Init(uint8_t dir)
/* Timer4 OCO : Initialize OCO configuration structure */
stcOcoInit.enOccrBufMode = OccrBufTrsfByCntZero;
stcOcoInit.enOcmrBufMode = OcmrBufTrsfByCntZero;
stcOcoInit.enPortLevel = E_OC_Port; /*输出低电平到OC端口*/
stcOcoInit.enPortLevel = E_OC_Port; /*输出低电平到OC端口*/
stcOcoInit.enOcoIntCmd = Disable;
TIMER4_OCO_Init(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, &stcOcoInit); /* Initialize OCO high channel */
TIMER4_OCO_Init(TIMER4_UNIT, enOcoLowCh, &stcOcoInit); /* Initialize OCO low channel */
@@ -504,14 +504,14 @@ void Timer41_Init(uint8_t dir)
PORT_SetFunc(TIMER4_PWM_L_PORT, TIMER4_PWM_L_PIN, Func_Tim4, Disable);
/* Timer4 PWM: Initialize PWM configuration structure */
//重载定时器中断
//重载定时器中断
stcPwmInit.enRtIntMaskCmd = Enable;
//计数时钟分频
stcPwmInit.enClkDiv = PwmPlckDiv1; /*滤波计数器和死区计数器的计数时钟分频*/
//PWM输出极性控制
stcPwmInit.enOutputState = E_PWM_OS; /*输出PWMH信号不改变电平,输出PWML信号反转*/
//PWM输出模式
stcPwmInit.enMode = PwmThroughMode; /*直通模式*/
//计数时钟分频
stcPwmInit.enClkDiv = PwmPlckDiv1; /*滤波计数器和死区计数器的计数时钟分频*/
//PWM输出极性控制
stcPwmInit.enOutputState = E_PWM_OS; /*输出PWMH信号不改变电平,输出PWML信号反转*/
//PWM输出模式
stcPwmInit.enMode = PwmThroughMode; /*直通模式*/
TIMER4_PWM_Init(TIMER4_UNIT, TIMER4_PWM_CH, &stcPwmInit); /* Initialize timer4 pwm */
/* Clear && Start CNT */
@@ -549,8 +549,8 @@ void Timer41_Init(void)
stcCntInit.enClk = Timer4CntPclk;
stcCntInit.enClkDiv = Timer4CntPclkDiv4; /* CNT clock divide */
stcCntInit.u16Cycle = TIMER4_CNT_CYCLE_VAL;
// stcCntInit.enCntMode = Timer4CntSawtoothWave; //锯齿波模式
stcCntInit.enCntMode = Timer4CntTriangularWave; //三角波模式Timer4CntTriangularWave
// stcCntInit.enCntMode = Timer4CntSawtoothWave; //锯齿波模式
stcCntInit.enCntMode = Timer4CntTriangularWave; //三角波模式Timer4CntTriangularWave
stcCntInit.enZeroIntCmd = Disable;
stcCntInit.enPeakIntCmd = Disable;
stcCntInit.enZeroIntMsk = Timer4CntIntMask0;
@@ -560,7 +560,7 @@ void Timer41_Init(void)
/* Timer4 OCO : Initialize OCO configuration structure */
stcOcoInit.enOccrBufMode = OccrBufDisable;
stcOcoInit.enOcmrBufMode = OcmrBufDisable;
stcOcoInit.enPortLevel = OcPortLevelHigh; /*输出高电平到OC端口*/
stcOcoInit.enPortLevel = OcPortLevelHigh; /*输出高电平到OC端口*/
stcOcoInit.enOcoIntCmd = Disable;
TIMER4_OCO_Init(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, &stcOcoInit); /* Initialize OCO high channel */
TIMER4_OCO_Init(TIMER4_UNIT, enOcoLowCh, &stcOcoInit); /* Initialize OCO low channel */
@@ -629,14 +629,14 @@ void Timer41_Init(void)
PORT_SetFunc(TIMER4_PWM_L_PORT, TIMER4_PWM_L_PIN, Func_Tim4, Disable);
/* Timer4 PWM: Initialize PWM configuration structure */
//重载定时器中断
//重载定时器中断
stcPwmInit.enRtIntMaskCmd = Enable;
//计数时钟分频
stcPwmInit.enClkDiv = PwmPlckDiv1; /*滤波计数器和死区计数器的计数时钟分频*/
//PWM输出极性控制
stcPwmInit.enOutputState = PwmHPwmLReverse; /*输出PWMH信号不改变电平,输出PWML信号反转*/
//PWM输出模式
stcPwmInit.enMode = PwmThroughMode; /*直通模式*/
//计数时钟分频
stcPwmInit.enClkDiv = PwmPlckDiv1; /*滤波计数器和死区计数器的计数时钟分频*/
//PWM输出极性控制
stcPwmInit.enOutputState = PwmHPwmLReverse; /*输出PWMH信号不改变电平,输出PWML信号反转*/
//PWM输出模式
stcPwmInit.enMode = PwmThroughMode; /*直通模式*/
TIMER4_PWM_Init(TIMER4_UNIT, TIMER4_PWM_CH, &stcPwmInit); /* Initialize timer4 pwm */
/* Clear && Start CNT */
@@ -795,9 +795,9 @@ void Timer61B_Init(void)
/**
* @brief 电机初始化
* @param 无
* @retval 无
* @brief 电机初始化
* @param 无
* @retval 无
*/
void dcmotor_init(void)
{
@@ -814,30 +814,30 @@ void dcmotor_init(void)
// PORT_Init(PortA, Pin03, &stcPortInit);
// PORT_SetBits(PortA, Pin03);
// /* SD拉低,关闭输出 */
// /* SD拉低,关闭输出 */
//
Timer41_Init();
dcmotor_stop(); /* 停止电机 */
dcmotor_dir(0); /* 设置正转 */
dcmotor_speed(0); /* 速度设置为0 */
dcmotor_start(); /* 开启电机 */
dcmotor_stop(); /* 停止电机 */
dcmotor_dir(0); /* 设置正转 */
dcmotor_speed(0); /* 速度设置为0 */
dcmotor_start(); /* 开启电机 */
}
/**
* @brief 电机开启
* @param 无
* @retval 无
* @brief 电机开启
* @param 无
* @retval 无
*/
void dcmotor_start(void)
{
ENABLE_MOTOR; /* 拉高SD引脚,开启电机 */
ENABLE_MOTOR; /* 拉高SD引脚,开启电机 */
}
/**
* @brief 电机停止
* @param 无
* @retval 无
* @brief 电机停止
* @param 无
* @retval 无
*/
void dcmotor_stop(void)
{
@@ -854,10 +854,10 @@ void dcmotor_stop(void)
}
/**
* @brief 电机旋转方向设置
* @param para:方向 0正转,1反转
* @note 以电机正面,顺时针方向旋转为正转
* @retval 无
* @brief 电机旋转方向设置
* @param para:方向 0正转,1反转
* @note 以电机正面,顺时针方向旋转为正转
* @retval 无
*/
void dcmotor_dir(uint8_t para)
{
@@ -865,14 +865,14 @@ void dcmotor_dir(uint8_t para)
Timer6_StopCount(M4_TMR61);
Timer6_DeInit(M4_TMR61);
if (para == 0) /* 正转 */
if (para == 0) /* 正转 */
{
Timer61A_Init();
}
else if (para == 1) /* 反转 */
else if (para == 1) /* 反转 */
{
Timer61B_Init();
}
@@ -883,12 +883,12 @@ void dcmotor_dir(uint8_t para)
#if(TIMER == TIMER4)
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, 0);
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, 0u);
if (para == 0) /* 正转 */
if (para == 0) /* 正转 */
{
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, 0);
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, 0u);
}
else if (para == 1) /* 反转 */
else if (para == 1) /* 反转 */
{
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, 0);
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, 0u);
@@ -898,9 +898,9 @@ void dcmotor_dir(uint8_t para)
}
/**
* @brief 电机速度设置
* @param para:比较寄存器值
* @retval 无
* @brief 电机速度设置
* @param para:比较寄存器值
* @retval 无
*/
void dcmotor_speed(uint16_t para)
{
@@ -914,14 +914,14 @@ void dcmotor_speed(uint16_t para)
#endif
#if(TIMER == TIMER4)
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, g_motorSpeed); //正转
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, g_motorSpeed); //反转
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, g_motorSpeed); //正转
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, g_motorSpeed); //反转
#endif
dcmotor_stop();
return;
}
if(Motor_GetDirection() == DIR_REVERSE) //反转
if(Motor_GetDirection() == DIR_REVERSE) //反转
{
#if(TIMER == TIMER6)
Timer6_SetGeneralCmpValue(M4_TMR61, Timer6GenCompareA, g_motorSpeed); //Set General Compare RegisterA Value
@@ -929,12 +929,12 @@ void dcmotor_speed(uint16_t para)
#endif
#if(TIMER == TIMER4)
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, g_motorSpeed); //反转
TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, g_motorSpeed); //反转
#endif
DCMOTOR_DBG_LOG("REVERSE\tnewSpeed = %d\r\n", g_motorSpeed);
}
else if(Motor_GetDirection() == DIR_FORWARD) //正转
else if(Motor_GetDirection() == DIR_FORWARD) //正转
{
#if(TIMER == TIMER6)
Timer6_SetGeneralCmpValue(M4_TMR61, Timer6GenCompareA, 10000-g_motorSpeed); //Set General Compare RegisterA Value
@@ -942,7 +942,7 @@ void dcmotor_speed(uint16_t para)
#endif
#if(TIMER == TIMER4)
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, g_motorSpeed); //正转
TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, g_motorSpeed); //正转
#endif
DCMOTOR_DBG_LOG("FORWARD\tnewSpeed = %d\r\n", g_motorSpeed);
}
@@ -951,10 +951,10 @@ void dcmotor_speed(uint16_t para)
#endif
/**
* @brief 电机控制
* @param para: pwm比较值 ,正数电机为正转,负数为反转
* @note 根据传入的参数控制电机的转向和速度
* @retval 无
* @brief 电机控制
* @param para: pwm比较值 ,正数电机为正转,负数为反转
* @note 根据传入的参数控制电机的转向和速度
* @retval 无
*/
void motor_pwm_set(float para)
{
@@ -969,7 +969,7 @@ void motor_pwm_set(float para)
if (val > 0)
{
g_motorDirection = DIR_FORWARD;
dcmotor_dir(0); /* 正转 */
dcmotor_dir(0); /* 正转 */
#if(TIMER == TIMER6)
dcmotor_speed(10000-val);
#endif
@@ -981,7 +981,7 @@ void motor_pwm_set(float para)
else
{
g_motorDirection = DIR_REVERSE;
dcmotor_dir(1); /* 反转 */
dcmotor_dir(1); /* 反转 */
#if(TIMER == TIMER6)
dcmotor_speed(-val);
#endif
@@ -996,9 +996,9 @@ void motor_pwm_set(float para)
// 设置电机方向
// 设置电机方向
void Motor_SetDirection(uint8_t dir) {
// 更新方向标志位
// 更新方向标志位
if (dir == 0) {
g_motorDirection = DIR_FORWARD;
// Up();
@@ -1012,32 +1012,32 @@ void Motor_SetDirection(uint8_t dir) {
}
// 电机控制接口
// 电机控制接口
void Motor_Control(MotorCommand cmd) {
g_motorCmd = cmd;
}
// 获取电机当前方向
// 获取电机当前方向
MotorDirection Motor_GetDirection(void) {
return g_motorDirection;
}
// 设置电机速度
// 设置电机速度
void Motor_SetSpeed(uint16_t speed) {
g_motorSpeed = (speed > MAX_SPEED) ? MAX_SPEED : speed;
// 当速度为零时,更新方向状态为停止
// 当速度为零时,更新方向状态为停止
if (g_motorSpeed == 0) {
g_motorDirection = DIR_STOPPED;
}
if(Motor_GetDirection() == DIR_REVERSE) //反转
if(Motor_GetDirection() == DIR_REVERSE) //反转
{
// Adt_SetCompareValue(M0P_ADTIM4, AdtCompareC, PWM_PERIOD - g_motorSpeed);
// DBG_LOG("REVERSE\tnewSpeed = %d\r\n", g_motorSpeed);
}
else if(Motor_GetDirection() == DIR_FORWARD) //正转
else if(Motor_GetDirection() == DIR_FORWARD) //正转
{
// Adt_SetCompareValue(M0P_ADTIM5, AdtCompareC, g_motorSpeed);
// DBG_LOG("FORWARD\tnewSpeed = %d\r\n", g_motorSpeed);
@@ -1145,27 +1145,27 @@ void DcmotorLoopHandler(void)
{
Dcmotor1msStatus = false;
// 电机状态机处理
// 电机状态机处理
switch (g_motorState) {
case MOTOR_STOPPED:
// Motor_SetSpeed(0);
// motor_pwm_set(0);
dcmotor_speed(0);
// 检测新命令
// 检测新命令
if (g_motorCmd == MOTOR_CMD_FORWARD) {
Motor_SetDirection(0); // 设置正转方向
Motor_SetDirection(0); // 设置正转方向
g_motorState = MOTOR_ACCELERATING;
accelCounter = 0;
// Motor_SetSpeed(ACCEL_STEP); // 初始速度
// Motor_SetSpeed(ACCEL_STEP); // 初始速度
// motor_pwm_set(ACCEL_STEP);
dcmotor_speed(ACCEL_STEP);
}
else if (g_motorCmd == MOTOR_CMD_REVERSE) {
Motor_SetDirection(1); // 设置反转方向
Motor_SetDirection(1); // 设置反转方向
g_motorState = MOTOR_ACCELERATING;
accelCounter = 0;
// Motor_SetSpeed(ACCEL_STEP); // 初始速度
// Motor_SetSpeed(ACCEL_STEP); // 初始速度
// motor_pwm_set(ACCEL_STEP);
dcmotor_speed(ACCEL_STEP);
TIMER4_CNT_ClearCountVal(TIMER4_UNIT);
@@ -1174,40 +1174,40 @@ void DcmotorLoopHandler(void)
break;
case MOTOR_ACCELERATING:
// 加速过程
// 加速过程
if (++accelCounter >= ACCEL_INTERVAL) {
accelCounter = 0;
uint16_t newSpeed = g_motorSpeed + ACCEL_STEP;
if (newSpeed >= MAX_SPEED) {
newSpeed = MAX_SPEED;
g_motorState = MOTOR_RUNNING; // 达到最大速度,进入匀速
g_motorState = MOTOR_RUNNING; // 达到最大速度,进入匀速
}
// Motor_SetSpeed(newSpeed);
// motor_pwm_set(newSpeed);
dcmotor_speed(newSpeed);
}
// 检查停止命令
// 检查停止命令
if (g_motorCmd == MOTOR_CMD_STOP) {
g_motorState = MOTOR_DECELERATING;
}
break;
case MOTOR_RUNNING:
// 检查停止命令
// 检查停止命令
if (g_motorCmd == MOTOR_CMD_STOP) {
g_motorState = MOTOR_DECELERATING;
}
// 检查方向改变命令
// 检查方向改变命令
else if ((g_motorCmd == MOTOR_CMD_FORWARD && g_motorDirection != DIR_FORWARD) ||
(g_motorCmd == MOTOR_CMD_REVERSE && g_motorDirection != DIR_REVERSE)) {
g_motorState = MOTOR_DECELERATING; // 需要先减速停止
g_motorState = MOTOR_DECELERATING; // 需要先减速停止
}
break;
case MOTOR_DECELERATING:
// 减速过程
// 减速过程
if (++accelCounter >= ACCEL_INTERVAL) {
accelCounter = 0;
// if (g_motorSpeed > DECEL_STEP) {
@@ -1219,7 +1219,7 @@ void DcmotorLoopHandler(void)
// Motor_SetSpeed(0);
// motor_pwm_set(0);
dcmotor_speed(0);
g_motorState = MOTOR_STOPPED; // 减速完成,进入停止状态
g_motorState = MOTOR_STOPPED; // 减速完成,进入停止状态
}
}
break;
@@ -2,9 +2,9 @@
#ifndef __DCMOTOR_H
#define __DCMOTOR_H
#define ADC_CH_NUM 3 /* 需要转换的通道数目 */
#define ADC_COLL 100 /* 单采集次数 */
#define ADC_SUM ADC_CH_NUM * ADC_COLL /* 总采集次数 */
#define ADC_CH_NUM 3 /* 需要转换的通道数目 */
#define ADC_COLL 100 /* 单采集次数 */
#define ADC_SUM ADC_CH_NUM * ADC_COLL /* 总采集次数 */
@@ -20,26 +20,26 @@
#define TIMER4 0
#define TIMER6 1
#define TIMER TIMER4 //HC32F460单片机PWM输出控制定时器选择
#define TIMER TIMER4 //HC32F460单片机PWM输出控制定时器选择
#endif
// 电机控制参数
#define PWM_PERIOD TIM_PERIOD // PWM周期值 (ARR)
#define MAX_SPEED 480 // 最大速度(PWM占空比)
#define ACCEL_STEP 10 // 加速步进值
#define DECEL_STEP 10 // 减速步进值
#define ACCEL_INTERVAL 10 // 加减速间隔(ms)
// 电机控制参数
#define PWM_PERIOD TIM_PERIOD // PWM周期值 (ARR)
#define MAX_SPEED 480 // 最大速度(PWM占空比)
#define ACCEL_STEP 10 // 加速步进值
#define DECEL_STEP 10 // 减速步进值
#define ACCEL_INTERVAL 10 // 加减速间隔(ms)
// 电机控制命令
// 电机控制命令
typedef enum {
MOTOR_CMD_STOP,
MOTOR_CMD_FORWARD,
MOTOR_CMD_REVERSE
} MotorCommand;
// 电机运行状态机
// 电机运行状态机
typedef enum {
MOTOR_STOPPED,
MOTOR_ACCELERATING,
@@ -48,7 +48,7 @@ typedef enum {
} MotorState;
// 电机转动方向
// 电机转动方向
typedef enum {
DIR_STOPPED,
DIR_FORWARD,
@@ -57,19 +57,19 @@ typedef enum {
/************************************* 第一部分 电压电流温度采集 ******************************/
/************************************* 第一部分 电压电流温度采集 ******************************/
/* 电流计算公式:
* I=(最终输出电压-初始参考电压)/(6*0.02)A
* ADC值转换为电压值:电压=ADC值*3.3/4096,这里电压单位为V,我们换算成mV,4096/1000=4.096,后面就直接算出为mA
* 整合公式可以得出电流 I= (当前ADC值-初始参考ADC值)* 3.3 / 4.096 / 0.12
/* 电流计算公式:
* I=(最终输出电压-初始参考电压)/(6*0.02)A
* ADC值转换为电压值:电压=ADC值*3.3/4096,这里电压单位为V,我们换算成mV,4096/1000=4.096,后面就直接算出为mA
* 整合公式可以得出电流 I= (当前ADC值-初始参考ADC值)* 3.3 / 4.096 / 0.12
*/
#define ADC2CURT (float)(3.3f / 4.096f / 0.12f)
/* 电压计算公式:
/* 电压计算公式:
* V_POWER = V_BUS * 25
* ADC值转换为电压值:电压=ADC值*3.3/4096
* 整合公式可以得出电压V_POWER= ADC值 *3.3f * 25 / 4096
* ADC值转换为电压值:电压=ADC值*3.3/4096
* 整合公式可以得出电压V_POWER= ADC值 *3.3f * 25 / 4096
*/
#define ADC2VBUS (float)(3.3f * 25 / 4096)
@@ -80,63 +80,63 @@ typedef enum {
/* 停止引脚操作宏定义
* 此引脚控制H桥是否生效以达到开启和关闭电机的效果
/* 停止引脚操作宏定义
* 此引脚控制H桥是否生效以达到开启和关闭电机的效果
*/
#define SHUTDOWN1_Pin Pin07
#define SHUTDOWN1_GPIO_Port PortB
//#define SHUTDOWN2_Pin GPIO_PIN_2
//#define SHUTDOWN2_GPIO_Port GPIOF
#define SHUTDOWN_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOF_CLK_ENABLE(); }while(0) /* PF口时钟使能 */
#define SHUTDOWN_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOF_CLK_ENABLE(); }while(0) /* PF口时钟使能 */
#if(MCU == HC32F460JETA)
/* 电机停止引脚定义 这里默认是接口1 */
/* 电机停止引脚定义 这里默认是接口1 */
#define ENABLE_MOTOR PORT_SetBits(SHUTDOWN1_GPIO_Port,SHUTDOWN1_Pin)
#define DISABLE_MOTOR PORT_ResetBits(SHUTDOWN1_GPIO_Port,SHUTDOWN1_Pin)
#endif
/******************************************************************************************/
/* 编码器参数结构体 */
/* 编码器参数结构体 */
typedef struct
{
int encode_old; /* 上一次计数值 */
int encode_now; /* 当前计数值 */
float speed; /* 编码器速度 */
int encode_old; /* 上一次计数值 */
int encode_now; /* 当前计数值 */
float speed; /* 编码器速度 */
} ENCODE_TypeDef;
extern ENCODE_TypeDef g_encode; /* 编码器参数变量 */
extern ENCODE_TypeDef g_encode; /* 编码器参数变量 */
/************************************* 第三部分 编码器测速 ****************************************************/
/************************************* 第三部分 编码器测速 ****************************************************/
#define ROTO_RATIO 44 /* 线数*倍频系数,即11*4=44 */
#define REDUCTION_RATIO 30 /* 减速比30:1 */
#define ROTO_RATIO 44 /* 线数*倍频系数,即11*4=44 */
#define REDUCTION_RATIO 30 /* 减速比30:1 */
/* 电机参数结构体 */
/* 电机参数结构体 */
typedef struct
{
uint8_t state; /*电机状态*/
float current; /*电机电流*/
float volatage; /*电机电压*/
float power; /*电机功率*/
float speed; /*电机实际速度*/
float location; /*电机位置*/
int32_t motor_pwm; /*设置比较值大小 */
uint8_t state; /*电机状态*/
float current; /*电机电流*/
float volatage; /*电机电压*/
float power; /*电机功率*/
float speed; /*电机实际速度*/
float location; /*电机位置*/
int32_t motor_pwm; /*设置比较值大小 */
} Motor_TypeDef;
extern Motor_TypeDef g_motor_data; /*电机参数变量*/
extern Motor_TypeDef g_motor_data; /*电机参数变量*/
/*********************************************************************************************************************/
void dcmotor_init(void); /* 直流有刷电机初始化 */
void dcmotor_start(void); /* 开启电机 */
void dcmotor_stop(void); /* 关闭电机 */
void dcmotor_dir(uint8_t para); /* 设置电机方向 */
void dcmotor_speed(uint16_t para); /* 设置电机速度 */
void motor_pwm_set(float para); /* 电机控制 */
void dcmotor_init(void); /* 直流有刷电机初始化 */
void dcmotor_start(void); /* 开启电机 */
void dcmotor_stop(void); /* 关闭电机 */
void dcmotor_dir(uint8_t para); /* 设置电机方向 */
void dcmotor_speed(uint16_t para); /* 设置电机速度 */
void motor_pwm_set(float para); /* 电机控制 */
float get_temp(uint16_t para);
@@ -147,17 +147,17 @@ void calc_adc_val(uint16_t * p);
void Encoder (void);
//外部调用接口函数
MotorDirection Motor_GetDirection(void);/* 获取电机转动方向 */
void Motor_Control(MotorCommand cmd); /* 电机控制 */
//外部调用接口函数
MotorDirection Motor_GetDirection(void);/* 获取电机转动方向 */
void Motor_Control(MotorCommand cmd); /* 电机控制 */
void Dcmotor1msRoutine(void);
void DcmotorLoopHandler(void);
float get_current(void); /* 获取工作电流 */
float get_volatage(void); /* 获取工作电压 */
float get_Temperature(void); /* 获取工作温度 */
float get_speed(void); /* 获取电机转速 */
float get_location(void); /* 获取电机位置 */
float get_current(void); /* 获取工作电流 */
float get_volatage(void); /* 获取工作电压 */
float get_Temperature(void); /* 获取工作温度 */
float get_speed(void); /* 获取电机转速 */
float get_location(void); /* 获取电机位置 */
#endif