一、硬件原理图
如上图:通过检测【4,8】 UART3_CTS管脚可知SW3按键是否按下,低电平按下高电平松开
如上图:可知UART3_CTS管脚连接cpu的gpio1的第26管脚
二、imx6ull相关寄存器分析
1、CCM_CCGR Register
时钟控制寄存器
寄存器封装:
#define CCM_CCGR1 *((volatile unsigned int *)0x20C406C)
2、SW_MUX_CTL Register
管脚复用功能控制寄存器
寄存器封装:
#define IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO26 *((volatile unsigned int *)0x20E00AC)
3、SW_PAD_CTL Register
管脚电器属性控制寄存器
寄存器封装:
#define IOMUXC_SW_PAD_CTL_PAD_GPIO1_IO26 *((volatile unsigned int *)0x20E0338)
4、GPIO direction Register
管脚方向控制寄存器
寄存器封装:
#define GPIO1_BASE (0x209C000u)
typedef struct
{
__IO uint32_t DR; /**< GPIO data register, offset: 0x0 */
__IO uint32_t GDIR; /**< GPIO direction register, offset: 0x4 */
__IO uint32_t PSR; /**< GPIO pad status register, offset: 0x8 */
__IO uint32_t ICR1; /**< GPIO interrupt configuration register1, offset: 0xC */
__IO uint32_t ICR2; /**< GPIO interrupt configuration register2, offset: 0x10 */
__IO uint32_t IMR; /**< GPIO interrupt mask register, offset: 0x14 */
__IO uint32_t ISR; /**< GPIO interrupt status register, offset: 0x18 */
__IO uint32_t EDGE_SEL; /**< GPIO edge select register, offset: 0x1C */
} GPIO_Type;
5、GPIO pad status Register
管脚状态寄存器
寄存器封装:
#define GPIO1_BASE (0x209C000u)
typedef struct
{
__IO uint32_t DR; /**< GPIO data register, offset: 0x0 */
__IO uint32_t GDIR; /**< GPIO direction register, offset: 0x4 */
__IO uint32_t PSR; /**< GPIO pad status register, offset: 0x8 */
__IO uint32_t ICR1; /**< GPIO interrupt configuration register1, offset: 0xC */
__IO uint32_t ICR2; /**< GPIO interrupt configuration register2, offset: 0x10 */
__IO uint32_t IMR; /**< GPIO interrupt mask register, offset: 0x14 */
__IO uint32_t ISR; /**< GPIO interrupt status register, offset: 0x18 */
__IO uint32_t EDGE_SEL; /**< GPIO edge select register, offset: 0x1C */
} GPIO_Type;
三、实验代码(按键控制LED灯)
imx6ull.h
代码接另一篇博客" imx6ull_GPIO控制LED灯 "
key.c
#include "imx6ull.h"
/*按键初始化*/
void key_init(void)
{
/* GPIO Mode */
IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO26 &= ~(0xf << 0);//管脚清零
IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO26 |= (0x5 << 0);//管脚工作模式设置为gpio模式
/*电器属性默认*/
/*GOIO Direction:Input*/
GPIO1->GDIR &= ~(1 << 26);//设置26管脚为输入模式
/*Enable GPIO1 Clock*/
CCM_CCGR1 |= (0x30 << 26);//打开GPIO管脚的时钟信号
return ;
}
/*读按键状态函数*/
int is_key_press(void)
{
int level = GPIO1->PSR & (1 <<26);//读第26为的电平状态
return level == 0 ? 1 : 0;
}
/*测试代码按下按键LED点亮*/
void key_test(void)
{
key_init();//按键初始化
led_init();//led初始化
while (1)
{
if(is_key_press())
{
led_on();//开灯
}
else
{
led_off();//关灯
}
}
return ;
}
led.c
#include "imx6ull.h"
/* LED初始化 */
void led_init(void)
{
/*GPIO MODE*/
IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO27 &= ~(0xf << 0);//位清零
IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO27 |= (0x5 << 0);//设置IOMUXC控制器将管脚复用为GOIO功能模式
/*Direction:Output*/
GPIO1->GDIR |= (1 << 27);//设置GPIO管脚为输出方向
/*Enable GPIO1 Clock*/
CCM_CCGR1 |= (0x30 << 26);//打开GPIO管脚的时钟信号
}
/* 开灯 */
void led_on(void)
{
GPIO1->DR |= (1 << 27);//设置GPIO 管脚输出高电平
}
/* 关灯 */
void led_off(void)
{
GPIO1->DR &= ~(1 << 27);//设置GPIO 管脚输出低电平
}
/* 延时 */
void delay_time(uint32_t time)
{
int i;
int j;
for(i = 0; i < time * 1000; i++)
{
for(j = 0; j < 50000; j++)
{
}
}
}
void led_test(void)
{
led_init();//LED初始化
while (1)
{
led_on();//开灯
delay_time(1);//大约延时1s
led_off();//关灯
delay_time(1);//大约延时1s
}
return;
}
main.c
extern void key_test(void);
int main(void)
{
key_test();
return 0;
}