gpt4 book ai didi

c - 在嵌入式领域更好地利用结构

转载 作者:行者123 更新时间:2023-11-30 15:32:00 24 4
gpt4 key购买 nike

我对 C 编程有点陌生,我想学习和使用 C 编程中的结构设施。我从事8位 Controller 的嵌入式编程领域。

我遇到这样的情况

objective:

  1. To set time and date or more things.
  2. To get time and date or more things.

Problem: I have two source files main.c and set_get.c i have a struct varaible in main.

目的:从 pic18 系列 Controller 的寄存器中设置和获取 rtcc 值并创建测试平台。

main()
{
struct data
{
unsigned char hour=10;
unsigned char date=20;
} entry;


entry=set_time_date(entry);

entry=get_time_date();

while(1);

}



and in set_get.c

i have two functions
//here struct parameter will be the input from main.c

struct data
{
unsigned char hour=10;
unsigned char date=20;
};


struct set_time_date(struct x)
{
struct data p1;

p1.hour=x.hour;
p1.date=x.date;
//do set hour register with p.hour value
//do set date register with p.date value

return(p1);
}





struct get_time_date(void)
{
struct data p1;

p1.hour= do read from hour register;
p1.date= do read from day register;

return(p1);
}

我希望得到您的意见,如果我在以下模式中犯了任何错误,请纠正我。我这样做是为了减少全局结构。

我热切地等待您对这段代码的审查。如果我错了,请纠正我

问候

新秀

最佳答案

注意,C89、C99 等中包含丰富的时间函数库。

time_t time (time_t *Current_Calendar_Time);  
clock_t clock (void);
char *ctime (const time_t *Calendar_Time);

仅举几例。但为了与你已经做过的事情的主题保持一致......

首先,此代码段将无法编译。不能在结构定义内部进行赋值:

   struct data
{
unsigned char hour=10;
unsigned char date=20;
};

但是,一旦定义了结构体,您就可以对每个单独的成员进行赋值(请参阅下面的代码示例中的示例),或者您可以进行 block 赋值,如下所示:

//Note, I am using a typedef variation of your original for illustration:
typedef struct data{
unsigned char hour;
unsigned char date;
} data;
//make block assignment here:
// hour date
struct data a = {0x34, 0xA5};

下一步,传递结构体指针有时比传递结构体本身更好。即,当数据量很大时,传递地址(~4 字节)优于传递可能数百个字节。 (对于结构的大小,这确实不是问题)我的示例将使用指针:

为了便于阅读,创建一个类型:

//you originally used unsigned char for member types.  I changed it to 
//accommodate puctuation,as often, timestrings and datestrings use
//puncutation such as : or /
//The unsigned version is below this one...

#define TIME_LEN 20
#define DATE_LEN 20
typedef struct {
char hour[TIME_LEN];
char date[DATE_LEN];
} DATA;

//use DATA to create the other instances you need:

DATA entry, *pEntry;

//Your function prototypes become:

void set_time_date(DATA *x); //no need to return time in set function
DATA * get_time_date(void);


//In main, initialize pointer to struct this way:

int main(void)
{
pEntry = &entry;//initialize pointer pEntry to address of entry

sprintf(pEntry->date , "%s", "12/23/2014");
sprintf(pEntry->hour , "%s", "10:12:13");
set_time_date(pEntry);
pEntry = get_time_date();

return 0;
}

void set_time_date(DATA *x)
{

sprintf(pEntry->date, "%s", x->date);
sprintf(pEntry->hour, "%s", x->hour);
}


DATA * get_time_date(void)
{
sprintf(pEntry->date, "%s", "01/23/2014");
sprintf(pEntry->hour, "%s", "10:10:00");
return pEntry;
}

使用无符号字符

在本节中,进行了更改以适应全局结构的最小化。通过创建结构的 typedef(例如在头文件中),您可以在需要时简单地使用 DATA * 来创建结构的本地实例,并将其作为参数传递...

//Your function prototypes become:  

void set_time_date(DATA *x); //no need to return time in set function
DATA * get_time_date(DATA *x); //Edited to include argument


//In main, initialize pointer to struct this way:

int main(void)
{
//Create local instance of DATA:
DATA entry={0}, *pEntry;
pEntry = &entry;//initialize pointer pEntry to address of entry

pEntry->date = 0x12;
pEntry->hour = 0x23;

set_time_date(pEntry);
pEntry = get_time_date(pEntry);

//print results showing values of both pEntry and entry
printf("pEntry->date: 0x%x\n", pEntry->date);
printf("entry.date: 0x%x\n", entry.date);
printf("pEntry->hour: 0x%x\n", pEntry->hour);
printf("entry.hour: 0x%x\n", entry.hour);

//After the assignment: "pEntry = &entry;" (above)
//pEntry is pointing to the the same location
//in memory as the start of entry. (this is the reason for that assignment)
//Every subsequent assignment you make to pEntry, is also being
//written to entry, without explicitly having to
//write: entry.date = 0x23 etc. (indeed, it is the same location
//in memory you are writing to)

return 0;
}


void set_time_date(DATA *x)
{
x->date = 0xBC;
x->hour = 0x45;
}


DATA * get_time_date(DATA *pX)
{
//Commented following two lines, passed in as argument:
//DATA x, *pX; //now passed in as argument
//pX = &x;//initialize pointer pX to address of x

pX->date = 0x23;
pX->hour = 0x34;
return pX;
}

产生以下输出
enter image description here

关于c - 在嵌入式领域更好地利用结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com