×
嵌入式 > 嵌入式开发 > 详情

LPC1766 GPIO输入和输出

发布时间:2020-10-09 发布时间:
|
前些时间玩了下STM32,感觉不错,资源和功能丰富,用起来挺顺手的。和51根本不是一个档次的,用过后决定专用RAM-M3核心的嵌入式MCU。唯一不足就是STM32的固件库用的不爽,入手直接操作寄存器(51带来的影响)。但是又发现STM32的外部模块操作起来非常困难尤其是I2C,根本就是不能用。

今天入手了LPC1766,完全的寄存器操作和比较了STM32,LPC比较合适直接寄存器操作。今天学习成果如下(基于周立功LPC1766板):

/****************************************Copyright (c)****************************************************
** Guangzhou ZLGMCU Development Co., LTD
**--------------File Info---------------------------------------------------------------------------------
** File name: main.c
** Last modified Date: 2010-09-28
** Last Version: V1.0
** Descriptions: The main() function example template
**
**--------------------------------------------------------------------------------------------------------
** Created by: Lan wuqiang
** Created date: 2010-09-28
** Version: V1.00
** Descriptions: 整理模板,添加用户应用程序
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
** Rechecked by:
*********************************************************************************************************/
#include "LPC17xx.h" /* LPC17xx外设寄存器 */

/*********************************************************************************************************
变量与宏定义
*********************************************************************************************************/
#define BEEP (1 << 11) /* P0.11连接蜂鸣器 */
#define KEY1 (LPC_GPIO2->FIOPIN & (1 << 10)) /* P2.10连接KEY1 */
#define KEY2 (LPC_GPIO2->FIOPIN & (1 << 11)) /* P2.11连接KEY2 */
#define KEY3 (LPC_GPIO2->FIOPIN & (1 << 12)) /* P2.12连接KEY3 */
#define KEY4 (LPC_GPIO2->FIOPIN & (1 << 13)) /* P2.13连接KEY4 */

#define BEEPOFF() LPC_GPIO0->FIODIR |= BEEP;LPC_GPIO0->FIOSET |= BEEP /* 蜂鸣器关 */
#define BEEPON() LPC_GPIO0->FIODIR |= BEEP;LPC_GPIO0->FIOCLR |= BEEP /*蜂鸣器开 */
#define LED1 (1 << 0) /* P2.0连接LED1 */
#define LED2 (1 << 1) /* P2.1连接LED2 */
#define LED3 (1 << 2) /* P2.2连接LED3 */
#define LED4 (1 << 3) /* P2.3连接LED4 */
#define LED1OFF() LPC_GPIO2->FIODIR |= LED1;LPC_GPIO2->FIOSET |= LED1 /* LED1关 */
#define LED1ON() LPC_GPIO2->FIODIR |= LED1;LPC_GPIO2->FIOCLR |= LED1 /* LED1开 */
#define LED2OFF() LPC_GPIO2->FIODIR |= LED2;LPC_GPIO2->FIOSET |= LED2 /* LED2关 */
#define LED2ON() LPC_GPIO2->FIODIR |= LED2;LPC_GPIO2->FIOCLR |= LED2 /* LED2开 */
#define LED3OFF() LPC_GPIO2->FIODIR |= LED3;LPC_GPIO2->FIOSET |= LED3 /* LED1关 */
#define LED3ON() LPC_GPIO2->FIODIR |= LED3;LPC_GPIO2->FIOCLR |= LED3 /* LED1开 */
#define LED4OFF() LPC_GPIO2->FIODIR |= LED4;LPC_GPIO2->FIOSET |= LED4 /* LED2关 */
#define LED4ON() LPC_GPIO2->FIODIR |= LED4;LPC_GPIO2->FIOCLR |= LED4 /* LED2开 */


/*********************************************************************************************************
** Function name: GPIOInit
** Descriptions: GPIO初始化
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void GPIOInit (void)
{
LPC_PINCON->PINSEL0 &= ~(0x03 << 22); /* 配置P0.11为GPIO */
LPC_PINCON->PINSEL4 &= 0XF00FFF00; /* 配置P2.0~P2.3和P2.10~P2.13为GPIO*/

LPC_GPIO0->FIODIR |= BEEP; /* 配置P0.11即BEEP为输出 1 */
LPC_GPIO2->FIODIR |= 0X000000FF; /* 配置P2.0~P2.3为输出 1 */
LPC_GPIO2->FIODIR &= 0XFFC3FFFF; /* 配置P2.10~P2.13为输入 0 */


}

/*********************************************************************************************************
** Function name: main
** Descriptions: 用户程序入口函数,将JP17短接,P0.11脚控制蜂鸣器,
** 将JP4的KEY1与P2_10短接,每按下KEY1,蜂鸣器响一声
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
int main (void)
{
SystemInit(); /* 系统初始化 */

GPIOInit();

while (1) {
if (KEY1 == 0) { /* 如果KEY1按下,蜂鸣器鸣叫 */
BEEPON();
LED1ON();
} else { /* 松开则停止蜂鸣 */
BEEPOFF();
LED1OFF();
}
if (KEY2 == 0) { /* 如果KEY2按下,LED2亮 */
LED2ON();
} else { /* 松开则LED2灭 */
LED2OFF();
}
if (KEY3 == 0) { /* 如果KEY3按下,LED3亮 */
LED3ON();
} else { /* 松开则LED3灭 */
LED3OFF();
}
if (KEY4 == 0) { /* 如果KEY4按下,LED4亮 */
LED4ON();
} else { /* 松开则LED4灭 */
LED4OFF();
}
}
}

/*********************************************************************************************************
End Of File
*********************************************************************************************************/



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
准双向口IO详解