Chinaunix首页 | 论坛 | 博客
  • 博客访问: 94968
  • 博文数量: 17
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-24 17:41
文章分类

全部博文(17)

文章存档

2008年(17)

我的朋友

分类: LINUX

2008-12-04 17:05:17

一、结构体定义举例
 
(1).先声明结构体类型再定义结构体变量

struct timer_list key_timer[KEY_NUM];

(2).直接定义结构体变量
 
typedef struct
{
 unsigned int keyStatus[KEY_NUM]; //4个按键的按键状态
 KEY_RET buf[MAX_KEY_BUF]; //按键缓冲区
 unsigned int head,tail;  //按键缓冲区头和尾
 wait_queue_head_t wq;  //等待队列
 struct cdev cdev;   //cdev结构体
}KEY_DEV;  //结构体变量
 
(3).定义结构体类型同时定义了类型变量
 
static struct key_info
{
 int irq_no;  //中断号
 unsigned int gpio_port;  //GPIO端口
 int key_no;  //键值
}key_info_tab[4]={
 {
 IRQ_EINT0,GPIO_F0,1
 }
 ,
 {
 IRQ_EINT2,GPIO_F2,2
 }
 ,
 {
 IRQ_EINT11,GPIO_G3,3
 }
 ,
 {
 IRQ_EINT19,GPIO_G11,4
 }
 ,
};
 
二、union 举例

    在使用瑞萨单片机过程中,遇到用结构体、联合体定义I/O 口,举例如下

/********************************************************
*  declare SFR bit                                      *
********************************************************/
struct bit_def {
 char b0:1;
 char b1:1;
 char b2:1;
 char b3:1;
 char b4:1;
 char b5:1;
 char b6:1;
 char b7:1;
};
union byte_def{
 struct bit_def bit;
 char byte;
};


/********************************************************
*  declare SFR addresses                                *
********************************************************/
#pragma ADDRESS  p0_addr   00E0H /* Port P0 register */
#pragma ADDRESS  p1_addr   00E1H /* Port P1 register */
…… ……
 
/*------------------------------------------------------
  Port P0 register
------------------------------------------------------*/
union byte_def p0_addr;
#define  p0    p0_addr.byte
#define  p0_0  p0_addr.bit.b0   /* Port P00 bit */
#define  p0_1  p0_addr.bit.b1   /* Port P01 bit */
#define  p0_2  p0_addr.bit.b2   /* Port P02 bit */
#define  p0_3  p0_addr.bit.b3   /* Port P03 bit */
#define  p0_4  p0_addr.bit.b4   /* Port P04 bit */
#define  p0_5  p0_addr.bit.b5   /* Port P05 bit */
#define  p0_6  p0_addr.bit.b6   /* Port P06 bit */
#define  p0_7  p0_addr.bit.b7   /* Port P07 bit */
 
/*------------------------------------------------------
  Port P1 register
------------------------------------------------------*/
union byte_def p1_addr;
#define  p1   p1_addr.byte
#define  p1_0  p1_addr.bit.b0   /* Port P10 bit */
#define  p1_1  p1_addr.bit.b1   /* Port P11 bit */
#define  p1_2  p1_addr.bit.b2   /* Port P12 bit */
#define  p1_3  p1_addr.bit.b3   /* Port P13 bit */
#define  p1_4  p1_addr.bit.b4   /* Port P14 bit */
#define  p1_5  p1_addr.bit.b5   /* Port P15 bit */
#define  p1_6  p1_addr.bit.b6   /* Port P16 bit */
#define  p1_7  p1_addr.bit.b7   /* Port P17 bit */
…………

说明:

1.结构体所占内存长度是各成员所占内存长度之和,每个成员分别占有自己的内存单元;共用体(联合体)变量所占的内存长度等于最长的成员长度(共用一段内存)。

2.在结构体 bit_def 中,用到了结构体的高级特性,那就是在基本成员变量的后面添加:
: 数据位数
组成新的结构体:
struct xxx
{
成员1 类型成员1 : 成员1 位数;
成员2 类型成员2 : 成员2 位数;
成员3 类型成员3 : 成员3 位数;
};
基本的成员变量就会被拆分,bit_def 的8个成员共占一个char 的空间。

3.#pragma ADDRESS directive 的说明

  • On setting the address value of an object containing a structure, array or union using the #pragma ADDRESS directive


  1. Product Concerned
    M3T-CC32R V.4.30 Release 00


  2. Description
    Consider that a #pragma ADDRESS directive is used for defining the address of an object that is an array or structure; or a union that has members of type array or structure.
    If a member or element placed at any address except the beginning address of the object is accessed (read or written) or referenced by an address operator (&), incorrect code is generated using the address of the member or element placed at the beginning of the object.

    2.1 Conditions
    This problem occurs if the following conditions are satisfied:
    (1) Any of the following three objects is declared outside of a function:
    (a) A structure
    (b) An array
    (c) A union having members of type array or structure
    (2) A #pragma ADDRESS directive is used for defining the object in (1).
    (3) For a member or element placed at any address except the beginning address of the object, either of the following is performed:
    (a) Accessing (reading or writing)
    (b) Referencing by an address operator (&)

    2.2 Examples
    Source file: sample1.c 
    ----------------------------------------------------------------------
    #pragma ADDRESS data1 0x10000 /* Condition (2) */
    struct stg1 {
    int a;
    int b;
    } data1; /* Condition (1)-(a) */

    void func1(void)
    {
    data1.b = 3; /* Condition (3)-(a) */
    }
    ----------------------------------------------------------------------

    Source file: sample2.c
    ----------------------------------------------------------------------
    #pragma ADDRESS data2 0x20000 /* Condition (2) */
    short data2[10]; /* Condition (1)-(b) */
    short *d2;

    void func2(void)
    {
    d2 = &data2[3]; /* Condition (3)-(b) */
    }
    ----------------------------------------------------------------------

    Source file: sample3.c
    ----------------------------------------------------------------------
    union utg3 {
    double a[10];
    struct {
    int b;
    double c[5];
    } d;
    } data3; /* Condition (1)-(c) */

    #pragma ADDRESS data3 0x30000 /* Condition (2) */

    void func3(int i)
    {
    data3.d.c[i] = /* Condition (3)-(a) */
    data3.a[4]; /* Condition (3)-(a) */
    }
    ----------------------------------------------------------------------

阅读(957) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~