|
楼主 |
发表于 2015-3-2 18:02:17
|
显示全部楼层
下边谈谈GPIO的基本控制。
1:SIM_SCGC5 寄存器。使能GPIO的时钟的。下边是mkl25z.h关于SIM_SCGC5的基本定义。
/* SCGC5 Bit Fields */
#define SIM_SCGC5_LPTMR_MASK 0x1u
#define SIM_SCGC5_LPTMR_SHIFT 0
#define SIM_SCGC5_TSI_MASK 0x20u
#define SIM_SCGC5_TSI_SHIFT 5
#define SIM_SCGC5_PORTA_MASK 0x200u
#define SIM_SCGC5_PORTA_SHIFT 9
#define SIM_SCGC5_PORTB_MASK 0x400u
#define SIM_SCGC5_PORTB_SHIFT 10
#define SIM_SCGC5_PORTC_MASK 0x800u
#define SIM_SCGC5_PORTC_SHIFT 11
#define SIM_SCGC5_PORTD_MASK 0x1000u
#define SIM_SCGC5_PORTD_SHIFT 12
#define SIM_SCGC5_PORTE_MASK 0x2000u
#define SIM_SCGC5_PORTE_SHIFT 13
在GPIO口初始化配置前先要是能 相应的 位。
SIM->SCGC5 |= (SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK ); /* Enable Clock to Port A - E */
上边这个例子就是 使能 PORTA PORTB PORTC PORTD PORTE 的时钟。
其次就是配置引脚功能复用配置。
下边这个是 mkl25z.h 里对于 GPIO的结构体定义。
typedef struct {
__IO uint32_t PCR[32]; /**< Pin Control Register n, array offset: 0x0, array step: 0x4 */
__O uint32_t GPCLR; /**< Global Pin Control Low Register, offset: 0x80 */
__O uint32_t GPCHR; /**< Global Pin Control High Register, offset: 0x84 */
uint8_t RESERVED_0[24];
__IO uint32_t ISFR; /**< Interrupt Status Flag Register, offset: 0xA0 */
} PORT_Type;
下边是 每一组IO的 地址定义
/** Peripheral PORTA base address */
#define PORTA_BASE (0x40049000u)
/** Peripheral PORTA base pointer */
#define PORTA ((PORT_Type *)PORTA_BASE)
/** Peripheral PORTB base address */
#define PORTB_BASE (0x4004A000u)
/** Peripheral PORTB base pointer */
#define PORTB ((PORT_Type *)PORTB_BASE)
/** Peripheral PORTC base address */
#define PORTC_BASE (0x4004B000u)
/** Peripheral PORTC base pointer */
#define PORTC ((PORT_Type *)PORTC_BASE)
/** Peripheral PORTD base address */
#define PORTD_BASE (0x4004C000u)
/** Peripheral PORTD base pointer */
#define PORTD ((PORT_Type *)PORTD_BASE)
/** Peripheral PORTE base address */
#define PORTE_BASE (0x4004D000u)
/** Peripheral PORTE base pointer */
#define PORTE ((PORT_Type *)PORTE_BASE)
/** Array initializer of PORT peripheral base pointers */
#define PORT_BASES { PORTA, PORTB, PORTC, PORTD, PORTE }
例如想设置PORTB的第1位(PB0,PB1)为输出可以这样写。
1:首先选择 引脚复用为基本GPIO。
PORTB->PCR[0] = PORT_PCR_MUX(1);
PORTB->PCR[1] = PORT_PCR_MUX(1);
PORTB 上边已经又定义了。PCR 每一组(PORTA,PORTB......)的每一个管脚都有一个 PCR寄存器。
为什么要 PORT_PCR_MUX(1)
下边是 mkl25z.h 对于 PORT_PCR_MUX 的定义。
#define PORT_PCR_MUX(x) (((uint32_t)(((uint32_t)(x))<<PORT_PCR_MUX_SHIFT))&PORT_PCR_MUX_MASK)
参考手册
PORTB 的第0个管脚 ALT0 : ADC0SE8 ALT1: PTB0 ALT2:IIC0_SCL ALT3 :TPM1_CH0 ;
PORTB 的第1个管脚 ALT0 : ADC0SE9 ALT1: PTB1 ALT2:IIC0_SDA ALT3 :TPM1_CH1 ;
想使用什么功能,就PORT_PCR_MUX(n)。
我们要的是输出,属于PTBn功能。我们就 PORT_PCR_MUX(1);
2:下边开始配置引脚功能的输入输出。
这是 mkl25z.h 对于引脚控制配置的结构体定义。
/** FGPIO - Register Layout Typedef */
typedef struct {
__IO uint32_t PDOR; /**< IO数据输出寄存器 */
__O uint32_t PSOR; /**< IO数据置一寄存器 */
__O uint32_t PCOR; /**< IO数据清零寄存器 */
__O uint32_t PTOR; /**< IO数据翻转寄存器 */
__I uint32_t PDIR; /**< IO数据输入寄存器 */
__IO uint32_t PDDR; /**< IO引脚方向寄存器 */
} FGPIO_Type;
FPTB->PDDR |= (1UL<<0)|(1UL<<1); /* enable PTB0/1 as Output */
PDDR 是引脚输入输出控制寄存器 ,是一个32位宽的寄存器。 从低位开始 每一个bit 对应一个 引脚。 (bit0 -》PTB0 ,bit1-》PTB1..........)如果某BIT 为1 则相应的 PTBn 为输出。 为0 则为输入。芯片复位默认的是 0,即为输入。
现在引脚功能已经基本配置玩了。
3:在电路中我们可能还要设置 默认输出电平。PODR 为输出电平控制,芯片复位上电是0所以输出是0,如果我们想输出是1则可以按下边的方法操作。
FPTB->PODR |= (1UL<<0)|(1UL<<1);
设置 PTB0,PTB1 输出 高电平。
至此 我们引脚的功能配置就完成了。
下边再说说引脚的基本控制
PSOR 某1BIT为1 则输出引脚 会输出1.PDOR的相应BIT也会为1.
PCOR 某1BIT为1 则输出引脚 会输出0.PDOR的相应BIT也会为0.
PTOR 某1BIT为1 则输出引脚 会翻转电平,即原来为0,现在为1。
下边是我们对于输出高低电平的定义
#define PTB0L FPTB->PCOR |= 1UL << 0;
#define PTB0H FPTB->PSOR |= 1UL << 0;
#define PTB1L FPTB->PCOR |= 1UL << 1;
#define PTB1H FPTB->PSOR |= 1UL << 1;
.如果将引脚配置为输入的话,PD0R 的值决定了是否上拉。
读输入口状态的话
#define PTB0IN FPTB->PDIR &(1UL<<0)
#define PTB1IN FPTB->PDIR &(1UL<<1)
|
|