由于更换工作,需要学习nxp系列单片机,记录(1)
-
普通的GPIO口控制,有简单的库函数操作,也有直接控制寄存器
-
对GPIO的控制就是对寄存器PINSEL的控制,一共有PINSEL0-PINSEL10,其中每个PINSELx是一个32bit的寄存器,其中每两位控制一个io口的功能配置,(lpc18xx系列的io口功能有八种模式可选)
LPC_PINCON->PINSEL0 |= (1 << 4); /* Pin P0.2 used as TXD0 (Com0) */ LPC_PINCON->PINSEL0 |= (1 << 6); /* Pin P0.3 used as RXD0 (Com0) */
-
寄存器操作,其中LPC_PINCON是启动头文件定义的一个结构体,是指针类型的
- #define LPC_PINCON ((LPC_PINCON_TypeDef *) LPC_PINCON_BASE )
-
也可以使用库函数实现,下面是根据现有库函数重新封装
-
@file lpc17xx_gpio.c
-
@brief Contains all functions support for GPIO firmware
-
library on LPC17xx
-
@version 2.0
-
@date 21. May. 2010**
-
固件库版本信息
void LED_Configuration(void)
{
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum = 0;//普通GPIO功能
PinCfg.Portnum = 2; //端口号
PinCfg.Pinnum = 0;//GPIO2_0
PinCfg.Pinnum = 1;//GPIO2_1
PinCfg.Pinnum = 2;//GPIO2_2
PinCfg.Pinnum = 3;//GPIO2_3
PinCfg.Pinnum = 4;//GPIO2_4
PinCfg.Pinnum = 5;//GPIO2_5
PinCfg.Pinnum = 6;//GPIO2_6
PinCfg.Pinnum = 7;//GPIO2_7
PINSEL_ConfigPin(&PinCfg);
GPIO_SetDir(2, (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7), 1); //设置端口输出模式,0输入 1输出
GPIO_ClearValue(2, (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7));//清除io口的值
}
typedef struct
{
uint8_t Portnum; /**< Port Number, should be PINSEL_PORT_x,
where x should be in range from 0 to 4 */
uint8_t Pinnum; /**< Pin Number, should be PINSEL_PIN_x,
where x should be in range from 0 to 31 */
uint8_t Funcnum; /**< Function Number, should be PINSEL_FUNC_x,
where x should be in range from 0 to 3 */
uint8_t Pinmode; /**< Pin Mode, should be:
- PINSEL_PINMODE_PULLUP: Internal pull-up resistor
- PINSEL_PINMODE_TRISTATE: Tri-state
- PINSEL_PINMODE_PULLDOWN: Internal pull-down resistor */
uint8_t OpenDrain; /**< OpenDrain mode, should be:
- PINSEL_PINMODE_NORMAL: Pin is in the normal (not open drain) mode
- PINSEL_PINMODE_OPENDRAIN: Pin is in the open drain mode */
} PINSEL_CFG_Type;
#define LED4_ON(x) ((x) ? GPIO_SetValue(2, (1<<0)) : GPIO_ClearValue(2,(1<<0)));
- 实现控制某一位的状态