搜索
bottom↓
回复: 13

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

[复制链接]

出0入0汤圆

发表于 2013-7-8 08:57:24 | 显示全部楼层 |阅读模式
本帖最后由 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   __I  uint32_t CR0;
00296   __I  uint32_t CR1;
00297        uint32_t RESERVED0[2];
00298   __IO uint32_t EMR;
00299        uint32_t RESERVED1[12];
00300   __IO uint32_t CTCR;
00301 } LPC_TIM_TypeDef;

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

出0入0汤圆

发表于 2013-7-8 09:12:55 | 显示全部楼层
本帖最后由 monkerman 于 2013-7-9 10:11 编辑

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

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

出0入0汤圆

发表于 2013-7-8 09:17:43 | 显示全部楼层
这些都是寄存器名字。手册有复位后默认值。
赋值是你使用它,就赋值,不使用,不管。
他的地址 数据手册有,都是规定好的。

出0入0汤圆

发表于 2013-7-8 09:19:25 | 显示全部楼层
如果打算用LPC17XX,我的建议:
1,好好把数据手册看看。
2,KEIL,IAR下都有例程,看看。
3,在实验板上自己试试,看看效果。

出0入0汤圆

发表于 2013-7-8 09:19:45 | 显示全部楼层
推荐网址:WWW.LPCWARE.COM有很多资料。

出0入0汤圆

 楼主| 发表于 2013-7-9 10:03:23 | 显示全部楼层
monkerman 发表于 2013-7-8 09:12
可能是随机值也可能是 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 这四个寄存器却无法操作,我猜想可能就是没有给它们赋值造成的





出0入0汤圆

发表于 2013-7-9 10:28:31 | 显示全部楼层
本帖最后由 monkerman 于 2013-7-9 10:33 编辑

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

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

  2. #define    __IO         volatile

  3. union {
  4.         __IO unsigned int   FIODIR ;

  5.         struct {
  6.                 __IO unsigned short   FIODIRL ;
  7.                 __IO unsigned short   FIODIRH ;
  8.         };

  9.         struct {
  10.                 __IO unsigned char   FIODIR0 ;
  11.                 __IO unsigned char   FIODIR1 ;
  12.                 __IO unsigned char   FIODIR2 ;
  13.                 __IO unsigned char   FIODIR3 ;
  14.         };
  15. } IO_DIR;

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

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

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

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

  26.         return 0;
  27. }
复制代码

出20入70汤圆

发表于 2013-7-9 10:29:03 | 显示全部楼层
  1. /******************************************************************************/
  2. /*                         Peripheral memory map                              */
  3. /******************************************************************************/
  4. /* Base addresses                                                             */
  5. #define LPC_FLASH_BASE        (0x00000000UL)
  6. #define LPC_RAM_BASE          (0x10000000UL)
  7. #define LPC_PERI_RAM_BASE     (0x20000000UL)
  8. #define LPC_APB0_BASE         (0x40000000UL)
  9. #define LPC_APB1_BASE         (0x40080000UL)
  10. #define LPC_AHB_BASE          (0x20080000UL)
  11. #define LPC_CM3_BASE          (0xE0000000UL)

  12. /* APB0 peripherals                                                           */
  13. #define LPC_WDT_BASE          (LPC_APB0_BASE + 0x00000)
  14. #define LPC_TIM0_BASE         (LPC_APB0_BASE + 0x04000)
  15. #define LPC_TIM1_BASE         (LPC_APB0_BASE + 0x08000)
  16. #define LPC_UART0_BASE        (LPC_APB0_BASE + 0x0C000)
  17. #define LPC_UART1_BASE        (LPC_APB0_BASE + 0x10000)
  18. #define LPC_PWM0_BASE         (LPC_APB0_BASE + 0x14000)
  19. #define LPC_PWM1_BASE         (LPC_APB0_BASE + 0x18000)
  20. #define LPC_I2C0_BASE         (LPC_APB0_BASE + 0x1C000)
  21. #define LPC_SPI_BASE          (LPC_APB0_BASE + 0x20000)
  22. #define LPC_RTC_BASE          (LPC_APB0_BASE + 0x24000)
  23. #define LPC_GPIOINT_BASE      (LPC_APB0_BASE + 0x28080)
  24. #define LPC_IOCON_BASE        (LPC_APB0_BASE + 0x2C000)
  25. #define LPC_SSP1_BASE         (LPC_APB0_BASE + 0x30000)
  26. #define LPC_ADC_BASE          (LPC_APB0_BASE + 0x34000)
  27. #define LPC_CANAF_RAM_BASE    (LPC_APB0_BASE + 0x38000)
  28. #define LPC_CANAF_BASE        (LPC_APB0_BASE + 0x3C000)
  29. #define LPC_CANCR_BASE        (LPC_APB0_BASE + 0x40000)
  30. #define LPC_CAN1_BASE         (LPC_APB0_BASE + 0x44000)
  31. #define LPC_CAN2_BASE         (LPC_APB0_BASE + 0x48000)
  32. #define LPC_I2C1_BASE         (LPC_APB0_BASE + 0x5C000)

  33. /* APB1 peripherals                                                           */
  34. #define LPC_SSP0_BASE         (LPC_APB1_BASE + 0x08000)
  35. #define LPC_DAC_BASE          (LPC_APB1_BASE + 0x0C000)
  36. #define LPC_TIM2_BASE         (LPC_APB1_BASE + 0x10000)
  37. #define LPC_TIM3_BASE         (LPC_APB1_BASE + 0x14000)
  38. #define LPC_UART2_BASE        (LPC_APB1_BASE + 0x18000)
  39. #define LPC_UART3_BASE        (LPC_APB1_BASE + 0x1C000)
  40. #define LPC_I2C2_BASE         (LPC_APB1_BASE + 0x20000)
  41. #define LPC_UART4_BASE        (LPC_APB1_BASE + 0x24000)
  42. #define LPC_I2S_BASE          (LPC_APB1_BASE + 0x28000)
  43. #define LPC_SSP2_BASE         (LPC_APB1_BASE + 0x2C000)
  44. #define LPC_RIT_BASE          (LPC_APB1_BASE + 0x30000)
  45. #define LPC_MCPWM_BASE        (LPC_APB1_BASE + 0x38000)
  46. #define LPC_QEI_BASE          (LPC_APB1_BASE + 0x3C000)
  47. #define LPC_MCI_BASE          (LPC_APB1_BASE + 0x40000)
  48. #define LPC_SC_BASE           (LPC_APB1_BASE + 0x7C000)

  49. /* AHB peripherals                                                            */
  50. #define LPC_GPDMA_BASE        (LPC_AHB_BASE  + 0x00000)
  51. #define LPC_GPDMACH0_BASE     (LPC_AHB_BASE  + 0x00100)
  52. #define LPC_GPDMACH1_BASE     (LPC_AHB_BASE  + 0x00120)
  53. #define LPC_GPDMACH2_BASE     (LPC_AHB_BASE  + 0x00140)
  54. #define LPC_GPDMACH3_BASE     (LPC_AHB_BASE  + 0x00160)
  55. #define LPC_GPDMACH4_BASE     (LPC_AHB_BASE  + 0x00180)
  56. #define LPC_GPDMACH5_BASE     (LPC_AHB_BASE  + 0x001A0)
  57. #define LPC_GPDMACH6_BASE     (LPC_AHB_BASE  + 0x001C0)
  58. #define LPC_GPDMACH7_BASE     (LPC_AHB_BASE  + 0x001E0)
  59. #define LPC_EMAC_BASE         (LPC_AHB_BASE  + 0x04000)
  60. #define LPC_LCD_BASE          (LPC_AHB_BASE  + 0x08000)
  61. #define LPC_USB_BASE          (LPC_AHB_BASE  + 0x0C000)
  62. #define LPC_CRC_BASE          (LPC_AHB_BASE  + 0x10000)
  63. #define LPC_SPIFI_BASE        (LPC_AHB_BASE  + 0x14000)
  64. #define LPC_GPIO0_BASE        (LPC_AHB_BASE  + 0x18000)
  65. #define LPC_GPIO1_BASE        (LPC_AHB_BASE  + 0x18020)
  66. #define LPC_GPIO2_BASE        (LPC_AHB_BASE  + 0x18040)
  67. #define LPC_GPIO3_BASE        (LPC_AHB_BASE  + 0x18060)
  68. #define LPC_GPIO4_BASE        (LPC_AHB_BASE  + 0x18080)
  69. #define LPC_GPIO5_BASE        (LPC_AHB_BASE  + 0x180A0)
  70. #define LPC_EMC_BASE          (LPC_AHB_BASE  + 0x1C000)

  71. #define LPC_EEPROM_BASE       (LPC_FLASH_BASE+ 0x200080)


  72. /******************************************************************************/
  73. /*                         Peripheral declaration                             */
  74. /******************************************************************************/
  75. #define LPC_SC                ((LPC_SC_TypeDef        *) LPC_SC_BASE       )
  76. #define LPC_WDT               ((LPC_WDT_TypeDef       *) LPC_WDT_BASE      )
  77. #define LPC_TIM0              ((LPC_TIM_TypeDef       *) LPC_TIM0_BASE     )
  78. #define LPC_TIM1              ((LPC_TIM_TypeDef       *) LPC_TIM1_BASE     )
  79. #define LPC_TIM2              ((LPC_TIM_TypeDef       *) LPC_TIM2_BASE     )
  80. #define LPC_TIM3              ((LPC_TIM_TypeDef       *) LPC_TIM3_BASE     )
  81. #define LPC_RIT               ((LPC_RIT_TypeDef       *) LPC_RIT_BASE      )
  82. #define LPC_UART0             ((LPC_UART_TypeDef      *) LPC_UART0_BASE    )
  83. #define LPC_UART1             ((LPC_UART1_TypeDef     *) LPC_UART1_BASE    )
  84. #define LPC_UART2             ((LPC_UART_TypeDef      *) LPC_UART2_BASE    )
  85. #define LPC_UART3             ((LPC_UART_TypeDef      *) LPC_UART3_BASE    )
  86. #define LPC_UART4             ((LPC_UART4_TypeDef     *) LPC_UART4_BASE    )
  87. #define LPC_PWM0              ((LPC_PWM_TypeDef       *) LPC_PWM0_BASE     )
  88. #define LPC_PWM1              ((LPC_PWM_TypeDef       *) LPC_PWM1_BASE     )
  89. #define LPC_I2C0              ((LPC_I2C_TypeDef       *) LPC_I2C0_BASE     )
  90. #define LPC_I2C1              ((LPC_I2C_TypeDef       *) LPC_I2C1_BASE     )
  91. #define LPC_I2C2              ((LPC_I2C_TypeDef       *) LPC_I2C2_BASE     )
  92. #define LPC_I2S               ((LPC_I2S_TypeDef       *) LPC_I2S_BASE      )
  93. #define LPC_SPI               ((LPC_SPI_TypeDef       *) LPC_SPI_BASE      )
  94. #define LPC_RTC               ((LPC_RTC_TypeDef       *) LPC_RTC_BASE      )
  95. #define LPC_GPIOINT           ((LPC_GPIOINT_TypeDef   *) LPC_GPIOINT_BASE  )
  96. #define LPC_IOCON             ((LPC_IOCON_TypeDef     *) LPC_IOCON_BASE    )
  97. #define LPC_SSP0              ((LPC_SSP_TypeDef       *) LPC_SSP0_BASE     )
  98. #define LPC_SSP1              ((LPC_SSP_TypeDef       *) LPC_SSP1_BASE     )
  99. #define LPC_SSP2              ((LPC_SSP_TypeDef       *) LPC_SSP2_BASE     )
  100. #define LPC_ADC               ((LPC_ADC_TypeDef       *) LPC_ADC_BASE      )
  101. #define LPC_DAC               ((LPC_DAC_TypeDef       *) LPC_DAC_BASE      )
  102. #define LPC_CANAF_RAM         ((LPC_CANAF_RAM_TypeDef *) LPC_CANAF_RAM_BASE)
  103. #define LPC_CANAF             ((LPC_CANAF_TypeDef     *) LPC_CANAF_BASE    )
  104. #define LPC_CANCR             ((LPC_CANCR_TypeDef     *) LPC_CANCR_BASE    )
  105. #define LPC_CAN1              ((LPC_CAN_TypeDef       *) LPC_CAN1_BASE     )
  106. #define LPC_CAN2              ((LPC_CAN_TypeDef       *) LPC_CAN2_BASE     )
  107. #define LPC_MCPWM             ((LPC_MCPWM_TypeDef     *) LPC_MCPWM_BASE    )
  108. #define LPC_QEI               ((LPC_QEI_TypeDef       *) LPC_QEI_BASE      )
  109. #define LPC_MCI               ((LPC_MCI_TypeDef       *) LPC_MCI_BASE      )
  110. #define LPC_GPDMA             ((LPC_GPDMA_TypeDef     *) LPC_GPDMA_BASE    )
  111. #define LPC_GPDMACH0          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH0_BASE )
  112. #define LPC_GPDMACH1          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH1_BASE )
  113. #define LPC_GPDMACH2          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH2_BASE )
  114. #define LPC_GPDMACH3          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH3_BASE )
  115. #define LPC_GPDMACH4          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH4_BASE )
  116. #define LPC_GPDMACH5          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH5_BASE )
  117. #define LPC_GPDMACH6          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH6_BASE )
  118. #define LPC_GPDMACH7          ((LPC_GPDMACH_TypeDef   *) LPC_GPDMACH7_BASE )
  119. #define LPC_EMAC              ((LPC_EMAC_TypeDef      *) LPC_EMAC_BASE     )
  120. #define LPC_LCD               ((LPC_LCD_TypeDef       *) LPC_LCD_BASE      )
  121. #define LPC_USB               ((LPC_USB_TypeDef       *) LPC_USB_BASE      )
  122. #define LPC_GPIO0             ((LPC_GPIO_TypeDef      *) LPC_GPIO0_BASE    )
  123. #define LPC_GPIO1             ((LPC_GPIO_TypeDef      *) LPC_GPIO1_BASE    )
  124. #define LPC_GPIO2             ((LPC_GPIO_TypeDef      *) LPC_GPIO2_BASE    )
  125. #define LPC_GPIO3             ((LPC_GPIO_TypeDef      *) LPC_GPIO3_BASE    )
  126. #define LPC_GPIO4             ((LPC_GPIO_TypeDef      *) LPC_GPIO4_BASE    )
  127. #define LPC_GPIO5             ((LPC_GPIO_TypeDef      *) LPC_GPIO5_BASE    )
  128. #define LPC_EMC               ((LPC_EMC_TypeDef       *) LPC_EMC_BASE      )
  129. #define LPC_CRC               ((LPC_CRC_TypeDef       *) LPC_CRC_BASE      )
  130. #define LPC_SPIFI             ((LPC_SPIFI_TypeDef     *) LPC_SPIFI_BASE    )
  131. #define LPC_EEPROM            ((LPC_EEPROM_TypeDef    *) LPC_EEPROM_BASE   )
复制代码

出20入70汤圆

发表于 2013-7-9 10:31:37 | 显示全部楼层
上面代码。

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

出0入0汤圆

发表于 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)

出0入0汤圆

发表于 2013-7-9 10:36:03 | 显示全部楼层
头文件的名字可以随便叫。
但是地址要对。

出0入0汤圆

 楼主| 发表于 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_t  FIODIR0;
00203       __IO uint8_t  FIODIR1;
00204       __IO uint8_t  FIODIR2;
00205       __IO uint8_t  FIODIR3;
00206     };
00207   };
00208   uint32_t RESERVED0[3];
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_t  FIOMASK0;
00217       __IO uint8_t  FIOMASK1;
00218       __IO uint8_t  FIOMASK2;
00219       __IO uint8_t  FIOMASK3;
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_t  FIOPIN0;
00230       __IO uint8_t  FIOPIN1;
00231       __IO uint8_t  FIOPIN2;
00232       __IO uint8_t  FIOPIN3;
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_t  FIOSET0;
00243       __IO uint8_t  FIOSET1;
00244       __IO uint8_t  FIOSET2;
00245       __IO uint8_t  FIOSET3;
00246     };
00247   };

00248   union {
00249     __O  uint32_t FIOCLR;
00250     struct {
00251       __O  uint16_t FIOCLRL;
00252       __O  uint16_t FIOCLRH;
00253     };
00254     struct {
00255       __O  uint8_t  FIOCLR0;
00256       __O  uint8_t  FIOCLR1;
00257       __O  uint8_t  FIOCLR2;
00258       __O  uint8_t  FIOCLR3;
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,这四句却不能用

出0入0汤圆

 楼主| 发表于 2013-7-13 08:20:37 | 显示全部楼层
木有人帮忙回答吗

出0入8汤圆

发表于 2013-7-13 09:21:34 | 显示全部楼层
请使用 Source Insight 来查看库函数或者程序,你就不会问这个问题了。直接鼠标一点就出来原始位置了。你还是没有找对地方。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-10 21:14

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表