栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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)); 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号