在线时间96 小时
UID3167208
注册时间2015-10-21
NXP金币0
TA的每日心情 | 开心 2019-3-22 20:15 |
---|
签到天数: 21 天 连续签到: 1 天 [LV.4]偶尔看看III
高级会员

- 积分
- 909
- 最后登录
- 2019-4-7
|
- void LED_Init(void)//LED初始化
- {
- //LED_R P0-15
- //LED_G P0-16
- //LED_B P0-17
- PORT0_IOCON15 = 0;
- PORT0_GPIO_DIR |= 1UL << 15;
- PORT0_OUT(15) = 1;
-
- PORT0_IOCON16 = 0;
- PORT0_GPIO_DIR |= 1UL << 16;
- PORT0_OUT(16) = 1;
-
- PORT0_IOCON17 = 0;
- PORT0_GPIO_DIR |= 1UL << 17;
- PORT0_OUT(17) = 1;
- }
复制代码- //LED
- #define LED_R PORT0_OUT(15)
- #define LED_G PORT0_OUT(16)
- #define LED_B PORT0_OUT(17)
复制代码
当你看到这个程序时,你肯定会觉得这寄存器配置还是蛮简单的,可是你却不知道这其中的寄存器是怎样编写出来的,下面我就给大家讲解LED灯是如何点亮的!!
1.关于最底层的文件为什么没有再软件篇讲呢,原因是这样的,为了方便大家快速开发寄存器,更多的是教会大家如何自己配置功能,以及如何使用寄存器来开发一些项目。
2.下面介绍一下外设寄存器的底层寄存器编写:
- /* ----------------------------------------------------------------------------
- -- I/O pin configuration (IOCON)
- ---------------------------------------------------------------------------- */
- //LPC5410x I/O pin configuration (IOCON) P87
- //Register overview: I/O configuration (base address 0x4004 4000)
- //地址
- #define PORT0_IOCON_PTR ((IOCON_MemMapPtr)0x40044000UL)
- //结构体
- typedef struct IOCON_MemMap {
- uint32_t PIO[31];
- } volatile *IOCON_MemMapPtr;
- //控制配置
- #define PORT_IOCON_PIO_REG(base,index) ((base)->PIO[index])
- //PORT0_IOCON
- #define PORT0_IOCON0 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x11)
- #define PORT0_IOCON1 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x0B)
- #define PORT0_IOCON2 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x06)
- #define PORT0_IOCON3 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x05)
- #define PORT0_IOCON4 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x04)
- #define PORT0_IOCON5 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x03)
- #define PORT0_IOCON6 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x10)
- #define PORT0_IOCON7 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x0F)
- #define PORT0_IOCON8 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x0E)
- #define PORT0_IOCON9 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x0D)
- #define PORT0_IOCON10 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x08)
- #define PORT0_IOCON11 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x07)
- #define PORT0_IOCON12 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x02)
- #define PORT0_IOCON13 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x01)
- #define PORT0_IOCON14 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x12)
- #define PORT0_IOCON15 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x0A)
- #define PORT0_IOCON16 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x09)
- #define PORT0_IOCON17 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x00)
- #define PORT0_IOCON18 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x1E)
- #define PORT0_IOCON19 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x1D)
- #define PORT0_IOCON20 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x1C)
- #define PORT0_IOCON21 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x1B)
- #define PORT0_IOCON22 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x1A)
- #define PORT0_IOCON23 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x19)
- #define PORT0_IOCON24 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x18)
- #define PORT0_IOCON25 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x17)
- #define PORT0_IOCON26 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x16)
- #define PORT0_IOCON27 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x15)
- #define PORT0_IOCON28 PORT_IOCON_PIO_REG(PORT0_IOCON_PTR,0x14)
- /* ----------------------------------------------------------------------------
- -- General Purpose I/O (GPIO)
- ---------------------------------------------------------------------------- */
- // LPC82x General Purpose I/O (GPIO) P123
- // Register overview: GPIO port (base address 0xA000 0000)
- //地址
- #define PORT_GPIO_PTR ((LPC_GPIO_MemMapPtr)0xA0000000UL)
- //结构体
- typedef struct LPC_GPIO_MemMap{/*!< GPIO_PORT Structure */
- __IO uint8_t B[128][32]; /*!< Offset 0x0000: Byte pin registers ports 0 to n; pins PIOn_0 to PIOn_31 */
- __IO uint32_t W[32][32]; /*!< Offset 0x1000: Word pin registers port 0 to n */
- __IO uint32_t DIR[32]; /*!< Offset 0x2000: Direction registers port n */
- __IO uint32_t MASK[32]; /*!< Offset 0x2080: Mask register port n */
- __IO uint32_t PIN[32]; /*!< Offset 0x2100: Portpin register port n */
- __IO uint32_t MPIN[32]; /*!< Offset 0x2180: Masked port register port n */
- __IO uint32_t SET[32]; /*!< Offset 0x2200: Write: Set register for port n Read: output bits for port n */
- __O uint32_t CLR[32]; /*!< Offset 0x2280: Clear port n */
- __O uint32_t NOT[32]; /*!< Offset 0x2300: Toggle port n */
- __O uint32_t DIRSET[32]; /*!< Offset 0x2380: Set Direction */
- __O uint32_t DIRCLR[32]; /*!< Offset 0x2400: Clear Direction */
- __O uint32_t DIRNOT[32]; /*!< Offset 0x2480: Toggle Dirction */
- } volatile *LPC_GPIO_MemMapPtr;
- //控制配置
- #define PORT_GPIO_B_REG(base,index,port) ((base)->B[index][port])
- #define PORT_GPIO_DIR_REG(base,index) ((base)->DIR[index])
- #define PORT_GPIO_MASK_REG(base,index) ((base)->MASK[index])
- #define PORT_GPIO_PIN_REG(base,index) ((base)->PIN[index])
- #define PORT_GPIO_MPIN_REG(base,index) ((base)->MPIN[index])
- #define PORT_GPIO_SET_REG(base,index) ((base)->SET[index])
- #define PORT_GPIO_CLR_REG(base,index) ((base)->CLR[index])
- #define PORT_GPIO_NOT_REG(base,index) ((base)->NOT[index])
- //GPIO_B
- #define PORT0_OUT(n) PORT_GPIO_B_REG(PORT_GPIO_PTR,0,n) //输出 n<32
- #define PORT0_IN(n) PORT_GPIO_B_REG(PORT_GPIO_PTR,0,n) //输入
- //GPIO_DIR
- #define PORT0_GPIO_DIR PORT_GPIO_DIR_REG(PORT_GPIO_PTR,0)
- //GPIO_MASK
- #define PORT0_GPIO_MASK PORT_GPIO_MASK_REG(PORT_GPIO_PTR,0)
- //GPIO_PIN
- #define PORT0_GPIO_PIN PORT_GPIO_PIN_REG(PORT_GPIO_PTR,0)
- //GPIO_MPIN
- #define PORT0_GPIO_MPIN PORT_GPIO_MPIN_REG(PORT_GPIO_PTR,0)
- //GPIO_SET
- #define PORT0_GPIO_SET PORT_GPIO_SET_REG(PORT_GPIO_PTR,0)
- //GPIO_CLR
- #define PORT0_GPIO_CLR PORT_GPIO_CLR_REG(PORT_GPIO_PTR,0)
- //GPIO_NOT
- #define PORT0_GPIO_NOT PORT_GPIO_NOT_REG(PORT_GPIO_PTR,0)
复制代码 该寄存器配置模式大家可以模仿继续编写其他的寄存控制配置
关于该寄存器的编写模式可参考该贴: https://www.nxpic.org.cn/module/forum/thread-607140-1-1.html
提供寄存器LED工程源码:
2.LED.zip
(51.67 KB, 下载次数: 25)
|
|