结构体指针加宏定义访问结构体变量

本文介绍了嵌入式编程中结构体指针的使用,通过宏定义`#define SP((int*)b)`将整型变量b转换为指向整型的指针,并展示了如何通过宏IOM访问特定地址的结构体成员。示例中,IOM结构体包含了一系列的硬件寄存器,用于设备控制。最终,通过IOM->CLC.U = 0x00000000;修改了相关寄存器的值。
摘要由CSDN通过智能技术生成

#define C ((A *)B)
含义:把定义((A *) B)为宏C
((A )B) 含义是把B强制准换为A型的指针
例如:

#define SP ((int*)b)
void main(void){
int b=4;
int *p=SP;
printf("%d\n",p);
}

输出结果是4。这说明用把b这个int数据变量强制成了一个int *型指针,并最后输出了。
这里用最简单的int替换了a,实际上a应该是自建的复杂类型;
如结构体类型;

typedef volatile struct _Ifx_IOM
{
    Ifx_IOM_CLC CLC;                        /**< \brief 0, IOM Clock Control Register */
    unsigned char reserved_4[4];            /**< \brief 4, \internal Reserved */
    Ifx_IOM_ID ID;                          /**< \brief 8, IOM Identification Register */
    unsigned char reserved_C[16];           /**< \brief C, \internal Reserved */
    Ifx_IOM_KRSTCLR KRSTCLR;                /**< \brief 1C, IOM Kernel Reset Status Clear Register */
    Ifx_IOM_KRST1 KRST1;                    /**< \brief 20, IOM Kernel Reset Register 1 */
    Ifx_IOM_KRST0 KRST0;                    /**< \brief 24, IOM Kernel Reset Register 0 */
    Ifx_IOM_ACCEN1 ACCEN1;                  /**< \brief 28, IOM Access Enable Register 1 */
    Ifx_IOM_ACCEN0 ACCEN0;                  /**< \brief 2C, IOM Access Enable Register 0 */
    Ifx_IOM_ECMCCFG ECMCCFG;                /**< \brief 30, IOM Event Combiner Module Counter Configuration Register */
    Ifx_IOM_ECMSELR ECMSELR;                /**< \brief 34, IOM Event Combiner Module Global Event Selection Register */
    Ifx_IOM_ECMETH0 ECMETH0;                /**< \brief 38, IOM Event Combiner Module Event Trigger History Register 0 */
    Ifx_IOM_ECMETH1 ECMETH1;                /**< \brief 3C, IOM Event Combiner Module Event Trigger History Register 1 */
    Ifx_IOM_GTMEXR GTMEXR;                  /**< \brief 40, IOM GTM Input EXOR Combiner Selection Register */
    unsigned char reserved_44[52];          /**< \brief 44, \internal Reserved */
    Ifx_IOM_FPCESR FPCESR;                  /**< \brief 78, IOM Filter and Prescaler Cells Rising & Falling Edge Status Register */
    unsigned char reserved_7C[4];           /**< \brief 7C, \internal Reserved */
    Ifx_IOM_FPCCTR FPCCTR[16];              /**< \brief 80, IOM Filter and Prescaler Cell Control Register */
    Ifx_IOM_FPCTIM FPCTIM[16];              /**< \brief C0, IOM Filter and Prescaler Cell Timer Register */
    Ifx_IOM_LAMEWC LAMEWC[16];              /**< \brief 100, IOM Logic Analyzer Module Event Window Count Status Register */
    unsigned char reserved_140[64];         /**< \brief 140, \internal Reserved */
    Ifx_IOM_LAMCFG LAMCFG[16];              /**< \brief 180, IOM Logic Analyzer Module Configuration Register */
    Ifx_IOM_LAMEWS LAMEWS[16];              /**< \brief 1C0, IOM Logic Analyzer Module Event Window Configuration Register */
} Ifx_IOM;

#define IOM /*lint --e(923)*/ ((Ifx_IOM*)0xF0035000u)
IOM->CLC.U = 0x00000000;

技术交流请关注公众号:
“教你嵌入式面试求职”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值