19781011 发表于 2013-7-8 08:57:24

请教各位大虾,LPC17XX.h文件中的一些结构体变量值的问题

本帖最后由 19781011 于 2013-7-8 08:58 编辑

下面代码是LPC17XX.h头文件的一段,关于定时器的一些结构体变量

/*------------- Timer (TIM) --------------------------------------------------*/
00282 typedef struct
00283 {
00284   __IO uint32_t IR;
00285   __IO uint32_t TCR;
00286   __IO uint32_t TC;
00287   __IO uint32_t PR;
00288   __IO uint32_t PC;
00289   __IO uint32_t MCR;
00290   __IO uint32_t MR0;
00291   __IO uint32_t MR1;
00292   __IO uint32_t MR2;
00293   __IO uint32_t MR3;
00294   __IO uint32_t CCR;
00295   __Iuint32_t CR0;
00296   __Iuint32_t CR1;
00297      uint32_t RESERVED0;
00298   __IO uint32_t EMR;
00299      uint32_t RESERVED1;
00300   __IO uint32_t CTCR;
00301 } LPC_TIM_TypeDef;

请教各位大虾,上述结构体中的,如:IR、TCR、TC。。。等变量的值是多少?又是在何处给其赋值的?

monkerman 发表于 2013-7-8 09:12:55

本帖最后由 monkerman 于 2013-7-9 10:11 编辑

可能是随机值也可能是 0. 这些成员貌似都是配置寄存器啊. 这样的话就得从手册中查看复位时的默认值.
在某个地方应该还有个起始地址.

何处赋值自己找找. 应该是在配置文件或者配置函数里.

Excellence 发表于 2013-7-8 09:17:43

这些都是寄存器名字。手册有复位后默认值。
赋值是你使用它,就赋值,不使用,不管。
他的地址 数据手册有,都是规定好的。

Excellence 发表于 2013-7-8 09:19:25

如果打算用LPC17XX,我的建议:
1,好好把数据手册看看。
2,KEIL,IAR下都有例程,看看。
3,在实验板上自己试试,看看效果。

Excellence 发表于 2013-7-8 09:19:45

推荐网址:WWW.LPCWARE.COM有很多资料。

19781011 发表于 2013-7-9 10:03:23

monkerman 发表于 2013-7-8 09:12 static/image/common/back.gif
可能是随机值也可能是 0. 这些成员貌似都是配置寄存器啊.
在某个地方应该还有个起始地址.



那些结构体变量的确是寄存器,我想知道的是,这些变量的名称就是寄存器的地址,那这个地址手册上有,但是编译器并不知道,肯定有地方给他们赋值(地址)来告诉编译器,只是我没有找到。

又如GPIO的结构体:
union {
   __IO uint32_t   FIODIR
   struct {
      __IO uint16_t   FIODIRL
      __IO uint16_t   FIODIRH
   }   
   struct {
      __IO uint8_t   FIODIR0
      __IO uint8_t   FIODIR1
      __IO uint8_t   FIODIR2
      __IO uint8_t   FIODIR3
   }   
};
......


FIODIR ,FIODIRL ,FIODIRH 都可以使用LPC_GPIOx->来读写,可为什么FIODIR0,FIODIR1,FIODIR2 ,FIODIR3 这四个寄存器却无法操作,我猜想可能就是没有给它们赋值造成的





monkerman 发表于 2013-7-9 10:28:31

本帖最后由 monkerman 于 2013-7-9 10:33 编辑

1. 地址肯定是封装在某个头文件中了.你找找.

2. 无法操作?? 赋值的话可能会有好多中情况, 可以一次性写 FIODIR, 也可以两次 FIODIRH/L, 也可以四次写. 你找找看.
这个估计是控制 IO 方向的. 你得从 IO 寄存器读写才行. 看手册呀. 我没弄过 LPC, 你在板子上试试?
另外, 可以运行下面这个程序, 顺便理解联合体和结构体:#include <stdio.h>

#define    __IO         volatile

union {
        __IO unsigned int   FIODIR ;

        struct {
                __IO unsigned short   FIODIRL ;
                __IO unsigned short   FIODIRH ;
        };

        struct {
                __IO unsigned char   FIODIR0 ;
                __IO unsigned char   FIODIR1 ;
                __IO unsigned char   FIODIR2 ;
                __IO unsigned char   FIODIR3 ;
        };
} IO_DIR;

int main(void)
{
        IO_DIR.FIODIR = 0x12345678;

        printf("%x\n\n", IO_DIR.FIODIR);

        printf("%x\n",   IO_DIR.FIODIRH);
        printf("%x\n\n", IO_DIR.FIODIRL);

        printf("%x\n", IO_DIR.FIODIR0);
        printf("%x\n", IO_DIR.FIODIR1);
        printf("%x\n", IO_DIR.FIODIR2);
        printf("%x\n", IO_DIR.FIODIR3);

        return 0;
}

techbaby 发表于 2013-7-9 10:29:03

/******************************************************************************/
/*                         Peripheral memory map                              */
/******************************************************************************/
/* Base addresses                                                             */
#define LPC_FLASH_BASE      (0x00000000UL)
#define LPC_RAM_BASE          (0x10000000UL)
#define LPC_PERI_RAM_BASE   (0x20000000UL)
#define LPC_APB0_BASE         (0x40000000UL)
#define LPC_APB1_BASE         (0x40080000UL)
#define LPC_AHB_BASE          (0x20080000UL)
#define LPC_CM3_BASE          (0xE0000000UL)

/* APB0 peripherals                                                         */
#define LPC_WDT_BASE          (LPC_APB0_BASE + 0x00000)
#define LPC_TIM0_BASE         (LPC_APB0_BASE + 0x04000)
#define LPC_TIM1_BASE         (LPC_APB0_BASE + 0x08000)
#define LPC_UART0_BASE      (LPC_APB0_BASE + 0x0C000)
#define LPC_UART1_BASE      (LPC_APB0_BASE + 0x10000)
#define LPC_PWM0_BASE         (LPC_APB0_BASE + 0x14000)
#define LPC_PWM1_BASE         (LPC_APB0_BASE + 0x18000)
#define LPC_I2C0_BASE         (LPC_APB0_BASE + 0x1C000)
#define LPC_SPI_BASE          (LPC_APB0_BASE + 0x20000)
#define LPC_RTC_BASE          (LPC_APB0_BASE + 0x24000)
#define LPC_GPIOINT_BASE      (LPC_APB0_BASE + 0x28080)
#define LPC_IOCON_BASE      (LPC_APB0_BASE + 0x2C000)
#define LPC_SSP1_BASE         (LPC_APB0_BASE + 0x30000)
#define LPC_ADC_BASE          (LPC_APB0_BASE + 0x34000)
#define LPC_CANAF_RAM_BASE    (LPC_APB0_BASE + 0x38000)
#define LPC_CANAF_BASE      (LPC_APB0_BASE + 0x3C000)
#define LPC_CANCR_BASE      (LPC_APB0_BASE + 0x40000)
#define LPC_CAN1_BASE         (LPC_APB0_BASE + 0x44000)
#define LPC_CAN2_BASE         (LPC_APB0_BASE + 0x48000)
#define LPC_I2C1_BASE         (LPC_APB0_BASE + 0x5C000)

/* APB1 peripherals                                                         */
#define LPC_SSP0_BASE         (LPC_APB1_BASE + 0x08000)
#define LPC_DAC_BASE          (LPC_APB1_BASE + 0x0C000)
#define LPC_TIM2_BASE         (LPC_APB1_BASE + 0x10000)
#define LPC_TIM3_BASE         (LPC_APB1_BASE + 0x14000)
#define LPC_UART2_BASE      (LPC_APB1_BASE + 0x18000)
#define LPC_UART3_BASE      (LPC_APB1_BASE + 0x1C000)
#define LPC_I2C2_BASE         (LPC_APB1_BASE + 0x20000)
#define LPC_UART4_BASE      (LPC_APB1_BASE + 0x24000)
#define LPC_I2S_BASE          (LPC_APB1_BASE + 0x28000)
#define LPC_SSP2_BASE         (LPC_APB1_BASE + 0x2C000)
#define LPC_RIT_BASE          (LPC_APB1_BASE + 0x30000)
#define LPC_MCPWM_BASE      (LPC_APB1_BASE + 0x38000)
#define LPC_QEI_BASE          (LPC_APB1_BASE + 0x3C000)
#define LPC_MCI_BASE          (LPC_APB1_BASE + 0x40000)
#define LPC_SC_BASE         (LPC_APB1_BASE + 0x7C000)

/* AHB peripherals                                                            */
#define LPC_GPDMA_BASE      (LPC_AHB_BASE+ 0x00000)
#define LPC_GPDMACH0_BASE   (LPC_AHB_BASE+ 0x00100)
#define LPC_GPDMACH1_BASE   (LPC_AHB_BASE+ 0x00120)
#define LPC_GPDMACH2_BASE   (LPC_AHB_BASE+ 0x00140)
#define LPC_GPDMACH3_BASE   (LPC_AHB_BASE+ 0x00160)
#define LPC_GPDMACH4_BASE   (LPC_AHB_BASE+ 0x00180)
#define LPC_GPDMACH5_BASE   (LPC_AHB_BASE+ 0x001A0)
#define LPC_GPDMACH6_BASE   (LPC_AHB_BASE+ 0x001C0)
#define LPC_GPDMACH7_BASE   (LPC_AHB_BASE+ 0x001E0)
#define LPC_EMAC_BASE         (LPC_AHB_BASE+ 0x04000)
#define LPC_LCD_BASE          (LPC_AHB_BASE+ 0x08000)
#define LPC_USB_BASE          (LPC_AHB_BASE+ 0x0C000)
#define LPC_CRC_BASE          (LPC_AHB_BASE+ 0x10000)
#define LPC_SPIFI_BASE      (LPC_AHB_BASE+ 0x14000)
#define LPC_GPIO0_BASE      (LPC_AHB_BASE+ 0x18000)
#define LPC_GPIO1_BASE      (LPC_AHB_BASE+ 0x18020)
#define LPC_GPIO2_BASE      (LPC_AHB_BASE+ 0x18040)
#define LPC_GPIO3_BASE      (LPC_AHB_BASE+ 0x18060)
#define LPC_GPIO4_BASE      (LPC_AHB_BASE+ 0x18080)
#define LPC_GPIO5_BASE      (LPC_AHB_BASE+ 0x180A0)
#define LPC_EMC_BASE          (LPC_AHB_BASE+ 0x1C000)

#define LPC_EEPROM_BASE       (LPC_FLASH_BASE+ 0x200080)


/******************************************************************************/
/*                         Peripheral declaration                           */
/******************************************************************************/
#define LPC_SC                ((LPC_SC_TypeDef      *) LPC_SC_BASE       )
#define LPC_WDT               ((LPC_WDT_TypeDef       *) LPC_WDT_BASE      )
#define LPC_TIM0            ((LPC_TIM_TypeDef       *) LPC_TIM0_BASE   )
#define LPC_TIM1            ((LPC_TIM_TypeDef       *) LPC_TIM1_BASE   )
#define LPC_TIM2            ((LPC_TIM_TypeDef       *) LPC_TIM2_BASE   )
#define LPC_TIM3            ((LPC_TIM_TypeDef       *) LPC_TIM3_BASE   )
#define LPC_RIT               ((LPC_RIT_TypeDef       *) LPC_RIT_BASE      )
#define LPC_UART0             ((LPC_UART_TypeDef      *) LPC_UART0_BASE    )
#define LPC_UART1             ((LPC_UART1_TypeDef   *) LPC_UART1_BASE    )
#define LPC_UART2             ((LPC_UART_TypeDef      *) LPC_UART2_BASE    )
#define LPC_UART3             ((LPC_UART_TypeDef      *) LPC_UART3_BASE    )
#define LPC_UART4             ((LPC_UART4_TypeDef   *) LPC_UART4_BASE    )
#define LPC_PWM0            ((LPC_PWM_TypeDef       *) LPC_PWM0_BASE   )
#define LPC_PWM1            ((LPC_PWM_TypeDef       *) LPC_PWM1_BASE   )
#define LPC_I2C0            ((LPC_I2C_TypeDef       *) LPC_I2C0_BASE   )
#define LPC_I2C1            ((LPC_I2C_TypeDef       *) LPC_I2C1_BASE   )
#define LPC_I2C2            ((LPC_I2C_TypeDef       *) LPC_I2C2_BASE   )
#define LPC_I2S               ((LPC_I2S_TypeDef       *) LPC_I2S_BASE      )
#define LPC_SPI               ((LPC_SPI_TypeDef       *) LPC_SPI_BASE      )
#define LPC_RTC               ((LPC_RTC_TypeDef       *) LPC_RTC_BASE      )
#define LPC_GPIOINT         ((LPC_GPIOINT_TypeDef   *) LPC_GPIOINT_BASE)
#define LPC_IOCON             ((LPC_IOCON_TypeDef   *) LPC_IOCON_BASE    )
#define LPC_SSP0            ((LPC_SSP_TypeDef       *) LPC_SSP0_BASE   )
#define LPC_SSP1            ((LPC_SSP_TypeDef       *) LPC_SSP1_BASE   )
#define LPC_SSP2            ((LPC_SSP_TypeDef       *) LPC_SSP2_BASE   )
#define LPC_ADC               ((LPC_ADC_TypeDef       *) LPC_ADC_BASE      )
#define LPC_DAC               ((LPC_DAC_TypeDef       *) LPC_DAC_BASE      )
#define LPC_CANAF_RAM         ((LPC_CANAF_RAM_TypeDef *) LPC_CANAF_RAM_BASE)
#define LPC_CANAF             ((LPC_CANAF_TypeDef   *) LPC_CANAF_BASE    )
#define LPC_CANCR             ((LPC_CANCR_TypeDef   *) LPC_CANCR_BASE    )
#define LPC_CAN1            ((LPC_CAN_TypeDef       *) LPC_CAN1_BASE   )
#define LPC_CAN2            ((LPC_CAN_TypeDef       *) LPC_CAN2_BASE   )
#define LPC_MCPWM             ((LPC_MCPWM_TypeDef   *) LPC_MCPWM_BASE    )
#define LPC_QEI               ((LPC_QEI_TypeDef       *) LPC_QEI_BASE      )
#define LPC_MCI               ((LPC_MCI_TypeDef       *) LPC_MCI_BASE      )
#define LPC_GPDMA             ((LPC_GPDMA_TypeDef   *) LPC_GPDMA_BASE    )
#define LPC_GPDMACH0          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH0_BASE )
#define LPC_GPDMACH1          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH1_BASE )
#define LPC_GPDMACH2          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH2_BASE )
#define LPC_GPDMACH3          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH3_BASE )
#define LPC_GPDMACH4          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH4_BASE )
#define LPC_GPDMACH5          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH5_BASE )
#define LPC_GPDMACH6          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH6_BASE )
#define LPC_GPDMACH7          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH7_BASE )
#define LPC_EMAC            ((LPC_EMAC_TypeDef      *) LPC_EMAC_BASE   )
#define LPC_LCD               ((LPC_LCD_TypeDef       *) LPC_LCD_BASE      )
#define LPC_USB               ((LPC_USB_TypeDef       *) LPC_USB_BASE      )
#define LPC_GPIO0             ((LPC_GPIO_TypeDef      *) LPC_GPIO0_BASE    )
#define LPC_GPIO1             ((LPC_GPIO_TypeDef      *) LPC_GPIO1_BASE    )
#define LPC_GPIO2             ((LPC_GPIO_TypeDef      *) LPC_GPIO2_BASE    )
#define LPC_GPIO3             ((LPC_GPIO_TypeDef      *) LPC_GPIO3_BASE    )
#define LPC_GPIO4             ((LPC_GPIO_TypeDef      *) LPC_GPIO4_BASE    )
#define LPC_GPIO5             ((LPC_GPIO_TypeDef      *) LPC_GPIO5_BASE    )
#define LPC_EMC               ((LPC_EMC_TypeDef       *) LPC_EMC_BASE      )
#define LPC_CRC               ((LPC_CRC_TypeDef       *) LPC_CRC_BASE      )
#define LPC_SPIFI             ((LPC_SPIFI_TypeDef   *) LPC_SPIFI_BASE    )
#define LPC_EEPROM            ((LPC_EEPROM_TypeDef    *) LPC_EEPROM_BASE   )

techbaby 发表于 2013-7-9 10:31:37

上面代码。

每个功能模块的地址都先用宏定义好了。然后再用宏将地址强制类型转换为相应结构体的指针。

Excellence 发表于 2013-7-9 10:35:39

所有你看到的英文名字,最后都要转化为地址。。

#define LPC_FLASH_BASE      (0x00000000UL)
#define LPC_RAM_BASE          (0x10000000UL)
#define LPC_PERI_RAM_BASE   (0x20000000UL)
#define LPC_APB0_BASE         (0x40000000UL)
#define LPC_APB1_BASE         (0x40080000UL)
#define LPC_AHB_BASE          (0x20080000UL)
#define LPC_CM3_BASE          (0xE0000000UL)

Excellence 发表于 2013-7-9 10:36:03

头文件的名字可以随便叫。
但是地址要对。

19781011 发表于 2013-7-9 15:05:29

本帖最后由 19781011 于 2013-7-9 15:49 编辑

感谢楼上几位的回复。
你们提供的各功能模块的地址宏定义我在LPC17xx.h中也看到了。
还是拿GPIO来举例,
LPC17xx.h中对GPIO0的地址宏定义如下:
#define LPC_GPIO_BASE         (0x2009C000UL)
#define LPC_GPIO1_BASE       (LPC_GPIO_BASE + 0x00020)
#define LPC_GPIO1                ((LPC_GPIO_TypeDef*) LPC_GPIO1_BASE)
从上面三句代码可以看出,GPIO1的起始地址为0x2009C020,这与手册上的GPIO1的FIO0DIR,也就是GPIO1的起始地址是一致的。
但是如果使用LPC_GPIO1->FIOSET这句代码,那么编译器是如何将LPC_GPIO1->FIOSET这句代码指向地址为0x0x2009C038这个地址的呢?
虽然在LPC17xx.h里有对GPIO模块定义的共用体、结构体中有对FIOSET进行定义,如下面代码:
00191 /*------------- General Purpose Input/Output (GPIO) --------------------------*/
00193 typedef struct
00194 {
00195   union {
00196   __IO uint32_t FIODIR;
00197   struct {
00198       __IO uint16_t FIODIRL;
00199       __IO uint16_t FIODIRH;
00200   };
00201   struct {
00202       __IO uint8_tFIODIR0;
00203       __IO uint8_tFIODIR1;
00204       __IO uint8_tFIODIR2;
00205       __IO uint8_tFIODIR3;
00206   };
00207   };
00208   uint32_t RESERVED0;
00209   union {
00210   __IO uint32_t FIOMASK;
00211   struct {
00212       __IO uint16_t FIOMASKL;
00213       __IO uint16_t FIOMASKH;
00214   };
00215   struct {
00216       __IO uint8_tFIOMASK0;
00217       __IO uint8_tFIOMASK1;
00218       __IO uint8_tFIOMASK2;
00219       __IO uint8_tFIOMASK3;
00220   };
00221   };
00222   union {
00223   __IO uint32_t FIOPIN;
00224   struct {
00225       __IO uint16_t FIOPINL;
00226       __IO uint16_t FIOPINH;
00227   };
00228   struct {
00229       __IO uint8_tFIOPIN0;
00230       __IO uint8_tFIOPIN1;
00231       __IO uint8_tFIOPIN2;
00232       __IO uint8_tFIOPIN3;
00233   };
00234   };
00235   union {
00236   __IO uint32_t FIOSET;
00237   struct {
00238       __IO uint16_t FIOSETL;
00239       __IO uint16_t FIOSETH;
00240   };
00241   struct {
00242       __IO uint8_tFIOSET0;
00243       __IO uint8_tFIOSET1;
00244       __IO uint8_tFIOSET2;
00245       __IO uint8_tFIOSET3;
00246   };
00247   };
00248   union {
00249   __Ouint32_t FIOCLR;
00250   struct {
00251       __Ouint16_t FIOCLRL;
00252       __Ouint16_t FIOCLRH;
00253   };
00254   struct {
00255       __Ouint8_tFIOCLR0;
00256       __Ouint8_tFIOCLR1;
00257       __Ouint8_tFIOCLR2;
00258       __Ouint8_tFIOCLR3;
00259   };
00260   };
00261 } LPC_GPIO_TypeDef;

但并没有指定FIOSET这个结构体变量的值(地址),实在不清楚编译器是如何将LPC_GPIO1->FIOSET这句话转为0x0x2009C038这个地址的。
更何况实际使用中LPC_GPIO1->FIOSET,LPC_GPIO1->FIOSETL,LPC_GPIO1->FIOSETH这三句代码可以使用,也就是说,编译器指定了实际的地址,而LPC_GPIO1->FIOSET0......LPC_GPIO1->FIOSET3,这四句却不能用

19781011 发表于 2013-7-13 08:20:37

木有人帮忙回答吗

kebaojun305 发表于 2013-7-13 09:21:34

请使用 Source Insight 来查看库函数或者程序,你就不会问这个问题了。直接鼠标一点就出来原始位置了。你还是没有找对地方。
页: [1]
查看完整版本: 请教各位大虾,LPC17XX.h文件中的一些结构体变量值的问题