更好地利用嵌入式领域的结构

时间:2014-06-29 13:47:06

标签: c function embedded structure microcontroller

我对c编程有点新意,我想在C编程中学习和使用结构工具。 我在8位控制器的嵌入式编程领域工作。

我有一种情况

  

目标:

     
      
  1. 设置时间和日期或更多内容。
  2.   
  3. 获取时间和日期或更多内容。
  4.         

    问题:我有两个源文件main.c和set_get.c我有一个结构   主要是可变的。

目标:从pic18系列控制器中的寄存器设置和获取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);
     }

如果我在以下模式中犯了任何错误,我想对此有所了解并纠正我。我已经在这种方法中做了以减少全局结构。

我正在等待你对这段代码的审核。如果我错了,请更正我

此致

Arookie

1 个答案:

答案 0 :(得分:1)

请注意,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;
   };   

但是,一旦定义了结构,就可以为每个成员进行赋值(参见下面代码示例中的示例)或者您可以进行块赋值,如下所示:

//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};

Next ,将指针传递给struct有时比传递struct本身更好。即,当数据量很大时,传递地址(~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;
}

使用unsigned char

在本节中,已进行了更改以适应全局结构的最小化。通过创建结构的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