【C语言】结构体数组的妙用

1. 什么场合会用到结构体数组

举个例子,我控制13个电机,电机参数申明一个结构体。正常结构体类型实例化要实例化13个结构体变量。因为一个结构体变量只能存储一组信息,再挨个赋值很麻烦。由此引出了结构体数组。

2. 结构体数组申明方法

2.1 先定义结构体类型,后定义结构体数组

 struct UploadFormat
{
 uint8 head[2];
 uint16 eco2;
 uint8 checksum;
}; 
struct UploadFormat line[23];

2.2 在定义结构体类型的同时定义结构体数组

 struct UploadFormat
{
 uint8 head[2];
 uint16 eco2;
 uint8 checksum;
}line[23]; 

2.3 直接定义结构体数组

 struct
{
 uint8 head[2];
 uint16 eco2;
 uint8 checksum;
}line[23]; 

3. 传统的实现方法

typedef struct{
    __IO uint16_t  SetTemp;     //设定目标 Desired Value
    __IO uint16_t  SumError;    //误差累计
    __IO uint16_t  Proportion;  //比例常数 Proportional Const
    __IO uint16_t  Integral;    //积分常数 Integral Const
    __IO uint16_t  Derivative;  //微分常数 Derivative Const  
    __IO float  Proportion_Current;  //比例常数 Proportional Const
    __IO float  Integral_Current;    //积分常数 Integral Const
    __IO float  Derivative_Current;  //微分常数 Derivative Const 
    __IO float  LastError;   //Error[-1]
    __IO float  PrevError;   //Error[-2]     
}PID_TypeDef; 

3.1 对结构体进行初始化:

PID_TypeDef PidChannel_0 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative = 80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1  
};
PID_TypeDef PidChannel_1 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_2 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_3 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_4 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const  
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_5 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const  
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_6 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_7 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const   
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1  
};

PID_TypeDef PidChannel_8 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_9 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_10 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_11 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_12 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_13 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const 
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_14 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const
    .Proportion_Current =50,
    .Integral_Current = 0,
    .Derivative_Current = 1      
};
PID_TypeDef PidChannel_15 = {
    .SetTemp = 50,     //设定目标 Desired Value
    .LastError = 0,   //Error[-1]
    .PrevError = 0,   //Error[-2] 
    .Proportion = 80,  //比例常数 Proportional Const
    .Integral = 0,    //积分常数 Integral Const
    .Derivative =80,  //微分常数 Derivative Const
    .Proportion_Current = 50,
    .Integral_Current = 0,
    .Derivative_Current = 1  
};

3.2 对结构体进行引用

建立一个函数,该函数传入数据的形参就是结构体类型参数。

int32_t PIDCalc_IRQHandler(PID_TypeDef *PidChannel,VirPwmTypeDef *VirPwmDef,float CurrentTemp)
{
    float iError,iIncpid;
    if(VirPwmDef->Status == 1)
    {
           iError = PidChannel->SetTemp - CurrentTemp;
    }

    return(iIncpid);    
}
VirPwmChannel_0.DutyCycle = PIDCalc_IRQHandler(&PidChannel_0,&VirPwmChannel_0,*(adctmparray+12)); 

4. 结构体数组实现方法

利用结构体数组初始化我可以这样写。这样类型申明和初始化的工作就做完了。

typedef struct PID_TypeDef{
    __IO uint16_t  SetTemp;     //设定目标 Desired Value
    __IO uint16_t  SumError;    //误差累计
    __IO uint16_t  Proportion;  //比例常数 Proportional Const
    __IO uint16_t  Integral;    //积分常数 Integral Const
    __IO uint16_t  Derivative;  //微分常数 Derivative Const  
    __IO float  Proportion_Current;  //比例常数 Proportional Const
    __IO float  Integral_Current;    //积分常数 Integral Const
    __IO float  Derivative_Current;  //微分常数 Derivative Const 
    __IO float  LastError;   //Error[-1]
    __IO float  PrevError;   //Error[-2]     
}; 

struct PID_TypeDef pid_type[13] = { {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1}, 
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                    {50,0,0,80,0,80,50,0,1},
                                   };

结构体数组引用。

int32_t PIDCalc_IRQHandler(PID_TypeDef *PidChannel,VirPwmTypeDef *VirPwmDef,float CurrentTemp)
{
    float iError,iIncpid;
    if(VirPwmDef->Status == 1)
    {
           iError = PidChannel->SetTemp - CurrentTemp;
    }

    return(iIncpid);    
}
VirPwmChannel_0.DutyCycle = PIDCalc_IRQHandler(&pid_type[1],&VirPwmChannel[1],*(adctmparray+12)); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米杰的声音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值