/**********************************************************
* @brief: 使用SPI读取LDC1000中的数据
* @param: none
* @return: none
***********************************************************/
void LDCRead(void)
{
// LDC_CSN_LOW();
ProximityData = 0;
FrequencyData = 0;
while(LDC_Read_IRQ()!=0)
printf(“read 1 failed!”);
DataRcv[0] = SPI_LDC_ReadReg(LDC1000_CMD_PROXLSB); //写入将要读取的Proximity Data LSB寄存器地址(0x21)
//printf(“0 %drn”,DataRcv[0]);
//SPI_Read(&DataRcv[0]); //读取上述寄存器中的值,并存入DataRcv[0]
ProximityData|= DataRcv[0] ;
while(LDC_Read_IRQ()!=0)
printf(“read 2 failed!”);
DataRcv[1] = SPI_LDC_ReadReg(LDC1000_CMD_PROXMSB); //写入将要读取的Proximity Data MSB寄存器地址(0x22)
//printf(“1 %drn”,DataRcv[1]);
//SPI_Read(&DataRcv[1]); //读取上述寄存器中的值,并存入DataRcv[1]
ProximityData|= (DataRcv[1]<<8) ; //组合成ProximityData
while(LDC_Read_IRQ()!=0)
printf(“read 3 failed!”);
DataRcv[2] = SPI_LDC_ReadReg(LDC1000_CMD_FREQCTRLSB); //写入将要读取的Frequency Counter Data LSB寄存器地址(0x23)
//printf(“2 %drn”,DataRcv[2]);
//SPI_Read(&DataRcv[2]); //读取上述寄存器中的值,并存入DataRcv[2]
FrequencyData|= DataRcv[2] ;
while(LDC_Read_IRQ()!=0)
printf(“read 4 failed!”);
DataRcv[3] = SPI_LDC_ReadReg(LDC1000_CMD_FREQCTRMID); //写入将要读取的Frequency Counter Data Mid-Byte寄存器地址(0x24)
//printf(“3 %drn”,DataRcv[3]);
//SPI_Read(&DataRcv[3]); //读取上述寄存器中的值,并存入DataRcv[3]
FrequencyData|= (DataRcv[3]<<8) ;
while(LDC_Read_IRQ()!=0);
DataRcv[4] = SPI_LDC_ReadReg(LDC1000_CMD_FREQCTRMSB); //写入将要读取的Frequency Counter Data MSB寄存器地址(0x25)
//printf(“4 %drnnn”,DataRcv[4]);
//SPI_Read(&DataRcv[4]); //读取上述寄存器中的值,并存入DataRcv[4]
FrequencyData|= (DataRcv[4]<<16) ; //组合成FrequencyData
// LDC_CSN_HIGH();
}
//——————————————————//
//———————-Rp 计算————————-//
//————-unsigned long CountRp() —————–//
//——————————————————//
float CountRp(void)
{
float Y;
unsigned Code;
float Rp;
Code =ProximityData;
Y = ((float)Code/32768.0);
printf(“Y value: %frn”,Y);
Rp = (float)(((float)TEST_RPMAX_MAX*(float)TEST_RPMAX_MIN) / ((float)TEST_RPMAX_MIN*(1-Y)+(float)TEST_RPMAX_MAX*(float)Y));
return Rp;
}
//——————————————————//
//——————-LC震荡频率求值——————–//
//—–unsigned long Fsensor(unsigned long lFcount)—–//、
//输入参数:lFcount: Fre Code的值
//——————————————————//
unsigned long Fsensor(unsigned long lFcount)
{
unsigned long lFsensor;
unsigned long Fcount;
Fcount = lFcount;
lFsensor = (1*Fext*ResponseTime)/(3*Fcount);
return lFsensor;
}
//——————————————————//
//———————L电感求值———————-//
//–unsigned long CountInductor(unsigned long lFsensor)-//
//输入参数:lFsensor: LC谐振频率
//——————————————————//
unsigned long CountInductor(unsigned long lFsensor)
{
unsigned long Fsensor;
unsigned long L;
Fsensor = lFsensor;
L = (unsigned long)(pow(10,12)* 1/(CapVal * pow(2*3.14*Fsensor,2)) ); //uH
return L;
}
extern void SPI_LDC_Init(void)
{
TIM3_Init();
MY_SPI_Init();
LDC1000_init();
flag=1;
}
/*
* 函数名:TIM3_NVIC_Configuration
* 描述 :TIM3中断优先级配置
* 输入 :无
* 输出 :无
*/
void TIM3_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); }
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM信号电平跳变值 */
u16 TIM_CCR3_Val = 2;
/* -----------------------------------------------------------------------
TIME3 可以输出 4 路 PWM 波形:
TIME3_CH1 ------ PA6
TIME3_CH2 ------ PA7
TIME3_CH3 ------ PB0
TIME3_CH4 ------ PB1
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 3; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频:预分频72,即为1KHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update); /* 清除溢出中断标志 */
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_CCR3_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_CCR3_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //使能通道1
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使用CCR3寄存器的影子寄存器(直到产生更新事件才发生更改)
//TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重载寄存器ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //使能定时器3
}
void TIM3_Init(void)
{
//TIM3_NVIC_Configuration();
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
include “pid.h”
include “sys.h”
include “motor.h”
include “delay.h”
extern unsigned long ProximityData,red_ProximityData,bule_ProximityData;
extern unsigned long jichu_red,jichu_bule;
extern u16 CCR3_Val,CCR4_Val;
void pid_pwm_red()//
{
int chazhi_z = 0, pid_val_z = 0, weifen_z=0;
// float pid_p_z = 1.05,pid_i_z = 0.1, pid_d_z = 2.5;
static int jifen_z = 0, l_chazhi_z= 0, l_val_z= 0;
float pid_p_z = 1.8,pid_i_z = 0, pid_d_z = 0;
chazhi_z = red_ProximityData - jichu_red;
jifen_z += chazhi_z;
if(jifen_z> 400)
{
jifen_z= 400;
}
else if(jifen_z < -400)
{
jifen_z = -400;
}
weifen_z = chazhi_z - l_chazhi_z;
if(weifen_z > 10)
{
weifen_z = 10;
}
if(weifen_z < -10)
{
weifen_z = -10;
}
pid_val_z = chazhi_z*pid_p_z+jifen_z*pid_i_z+weifen_z*pid_d_z;
if(pid_val_z>0)
{
TIM_SetCompare3(TIM5, CCR3_Val+pid_val_z);
TIM_SetCompare4(TIM5, CCR4_Val-pid_val_z);
}
else
{
TIM_SetCompare3(TIM5, CCR3_Val-pid_val_z);
TIM_SetCompare4(TIM5, CCR4_Val+pid_val_z);
}
l_chazhi_z = chazhi_z;
}
void pid_pwm_blue(void)
{
int chazhi_y = 0, pid_val_y = 0, weifen_y=0;
// float pid_p_y = 1.9, pid_i_y = 1, pid_d_y = 3;
static int jifen_y = 0, l_chazhi_y= 0, l_val_= 0;
float pid_p_y = 1.2, pid_i_y = 0.1, pid_d_y =0.05; //1.5 0.05
chazhi_y = bule_ProximityData-jichu_bule;
jifen_y+= chazhi_y;
if(jifen_y> 200)
{
jifen_y= 200;
}
else if(jifen_y < -200)
{
jifen_y = -200;
}
weifen_y = chazhi_y - l_chazhi_y;
if(weifen_y > 10)
{
weifen_y = 10;
}
if(weifen_y < -10)
{
weifen_y = -10;
}
pid_val_y = chazhi_y*pid_p_y+jifen_y*pid_i_y+weifen_y*pid_d_y;
if(pid_val_y>0)
{
TIM_SetCompare3(TIM5, CCR3_Val-pid_val_y);
TIM_SetCompare4(TIM5, CCR4_Val+pid_val_y);
}
else
{
TIM_SetCompare3(TIM5, CCR3_Val+pid_val_y);
TIM_SetCompare4(TIM5, CCR4_Val+pid_val_y);
}
l_chazhi_y = chazhi_y;
}
/**********************************************************
* @brief: 使用SPI读取LDC1000中的数据
* @param: none
* @return: none
***********************************************************/
void LDCRead(void)
{
// LDC_CSN_LOW();
ProximityData = 0;
FrequencyData = 0;
while(LDC_Read_IRQ()!=0)
printf(“read 1 failed!”);
DataRcv[0] = SPI_LDC_ReadReg(LDC1000_CMD_PROXLSB); //写入将要读取的Proximity Data LSB寄存器地址(0x21)
//printf(“0 %drn”,DataRcv[0]);
//SPI_Read(&DataRcv[0]); //读取上述寄存器中的值,并存入DataRcv[0]
ProximityData|= DataRcv[0] ;
while(LDC_Read_IRQ()!=0)
printf(“read 2 failed!”);
DataRcv[1] = SPI_LDC_ReadReg(LDC1000_CMD_PROXMSB); //写入将要读取的Proximity Data MSB寄存器地址(0x22)
//printf(“1 %drn”,DataRcv[1]);
//SPI_Read(&DataRcv[1]); //读取上述寄存器中的值,并存入DataRcv[1]
ProximityData|= (DataRcv[1]<<8) ; //组合成ProximityData
while(LDC_Read_IRQ()!=0)
printf(“read 3 failed!”);
DataRcv[2] = SPI_LDC_ReadReg(LDC1000_CMD_FREQCTRLSB); //写入将要读取的Frequency Counter Data LSB寄存器地址(0x23)
//printf(“2 %drn”,DataRcv[2]);
//SPI_Read(&DataRcv[2]); //读取上述寄存器中的值,并存入DataRcv[2]
FrequencyData|= DataRcv[2] ;
while(LDC_Read_IRQ()!=0)
printf(“read 4 failed!”);
DataRcv[3] = SPI_LDC_ReadReg(LDC1000_CMD_FREQCTRMID); //写入将要读取的Frequency Counter Data Mid-Byte寄存器地址(0x24)
//printf(“3 %drn”,DataRcv[3]);
//SPI_Read(&DataRcv[3]); //读取上述寄存器中的值,并存入DataRcv[3]
FrequencyData|= (DataRcv[3]<<8) ;
while(LDC_Read_IRQ()!=0);
DataRcv[4] = SPI_LDC_ReadReg(LDC1000_CMD_FREQCTRMSB); //写入将要读取的Frequency Counter Data MSB寄存器地址(0x25)
//printf(“4 %drnnn”,DataRcv[4]);
//SPI_Read(&DataRcv[4]); //读取上述寄存器中的值,并存入DataRcv[4]
FrequencyData|= (DataRcv[4]<<16) ; //组合成FrequencyData
// LDC_CSN_HIGH();
}
//——————————————————//
//———————-Rp 计算————————-//
//————-unsigned long CountRp() —————–//
//——————————————————//
float CountRp(void)
{
float Y;
unsigned Code;
float Rp;
Code =ProximityData;
Y = ((float)Code/32768.0);
printf(“Y value: %frn”,Y);
Rp = (float)(((float)TEST_RPMAX_MAX*(float)TEST_RPMAX_MIN) / ((float)TEST_RPMAX_MIN*(1-Y)+(float)TEST_RPMAX_MAX*(float)Y));
return Rp;
}
//——————————————————//
//——————-LC震荡频率求值——————–//
//—–unsigned long Fsensor(unsigned long lFcount)—–//、
//输入参数:lFcount: Fre Code的值
//——————————————————//
unsigned long Fsensor(unsigned long lFcount)
{
unsigned long lFsensor;
unsigned long Fcount;
Fcount = lFcount;
lFsensor = (1*Fext*ResponseTime)/(3*Fcount);
return lFsensor;
}
//——————————————————//
//———————L电感求值———————-//
//–unsigned long CountInductor(unsigned long lFsensor)-//
//输入参数:lFsensor: LC谐振频率
//——————————————————//
unsigned long CountInductor(unsigned long lFsensor)
{
unsigned long Fsensor;
unsigned long L;
Fsensor = lFsensor;
L = (unsigned long)(pow(10,12)* 1/(CapVal * pow(2*3.14*Fsensor,2)) ); //uH
return L;
}
extern void SPI_LDC_Init(void)
{
TIM3_Init();
MY_SPI_Init();
LDC1000_init();
flag=1;
}
/*
* 函数名:TIM3_NVIC_Configuration
* 描述 :TIM3中断优先级配置
* 输入 :无
* 输出 :无
*/
void TIM3_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); }
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM信号电平跳变值 */
u16 TIM_CCR3_Val = 2;
/* -----------------------------------------------------------------------
TIME3 可以输出 4 路 PWM 波形:
TIME3_CH1 ------ PA6
TIME3_CH2 ------ PA7
TIME3_CH3 ------ PB0
TIME3_CH4 ------ PB1
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 3; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频:预分频72,即为1KHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update); /* 清除溢出中断标志 */
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_CCR3_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_CCR3_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //使能通道1
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使用CCR3寄存器的影子寄存器(直到产生更新事件才发生更改)
//TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重载寄存器ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //使能定时器3
}
void TIM3_Init(void)
{
//TIM3_NVIC_Configuration();
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
include “pid.h”
include “sys.h”
include “motor.h”
include “delay.h”
extern unsigned long ProximityData,red_ProximityData,bule_ProximityData;
extern unsigned long jichu_red,jichu_bule;
extern u16 CCR3_Val,CCR4_Val;
void pid_pwm_red()//
{
int chazhi_z = 0, pid_val_z = 0, weifen_z=0;
// float pid_p_z = 1.05,pid_i_z = 0.1, pid_d_z = 2.5;
static int jifen_z = 0, l_chazhi_z= 0, l_val_z= 0;
float pid_p_z = 1.8,pid_i_z = 0, pid_d_z = 0;
chazhi_z = red_ProximityData - jichu_red;
jifen_z += chazhi_z;
if(jifen_z> 400)
{
jifen_z= 400;
}
else if(jifen_z < -400)
{
jifen_z = -400;
}
weifen_z = chazhi_z - l_chazhi_z;
if(weifen_z > 10)
{
weifen_z = 10;
}
if(weifen_z < -10)
{
weifen_z = -10;
}
pid_val_z = chazhi_z*pid_p_z+jifen_z*pid_i_z+weifen_z*pid_d_z;
if(pid_val_z>0)
{
TIM_SetCompare3(TIM5, CCR3_Val+pid_val_z);
TIM_SetCompare4(TIM5, CCR4_Val-pid_val_z);
}
else
{
TIM_SetCompare3(TIM5, CCR3_Val-pid_val_z);
TIM_SetCompare4(TIM5, CCR4_Val+pid_val_z);
}
l_chazhi_z = chazhi_z;
}
void pid_pwm_blue(void)
{
int chazhi_y = 0, pid_val_y = 0, weifen_y=0;
// float pid_p_y = 1.9, pid_i_y = 1, pid_d_y = 3;
static int jifen_y = 0, l_chazhi_y= 0, l_val_= 0;
float pid_p_y = 1.2, pid_i_y = 0.1, pid_d_y =0.05; //1.5 0.05
chazhi_y = bule_ProximityData-jichu_bule;
jifen_y+= chazhi_y;
if(jifen_y> 200)
{
jifen_y= 200;
}
else if(jifen_y < -200)
{
jifen_y = -200;
}
weifen_y = chazhi_y - l_chazhi_y;
if(weifen_y > 10)
{
weifen_y = 10;
}
if(weifen_y < -10)
{
weifen_y = -10;
}
pid_val_y = chazhi_y*pid_p_y+jifen_y*pid_i_y+weifen_y*pid_d_y;
if(pid_val_y>0)
{
TIM_SetCompare3(TIM5, CCR3_Val-pid_val_y);
TIM_SetCompare4(TIM5, CCR4_Val+pid_val_y);
}
else
{
TIM_SetCompare3(TIM5, CCR3_Val+pid_val_y);
TIM_SetCompare4(TIM5, CCR4_Val+pid_val_y);
}
l_chazhi_y = chazhi_y;
}
举报