LPC1766 GPIO輸入和輸出
今天入手了LPC1766,完全的寄存器操作和比較了STM32,LPC比較合適直接寄存器操作。今天學(xué)習(xí)成果如下(基于周立功LPC1766板):
本文引用地址:http://www.ex-cimer.com/article/201612/324407.htm/****************************************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: 整理模板,添加用戶應(yīng)用程序
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
** Rechecked by:
*********************************************************************************************************/
#include "LPC17xx.h" /* LPC17xx外設(shè)寄存器 */
/*********************************************************************************************************
變量與宏定義
*********************************************************************************************************/
#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 /* 蜂鳴器關(guān) */
#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關(guān) */
#define LED1ON() LPC_GPIO2->FIODIR |= LED1;LPC_GPIO2->FIOCLR |= LED1 /* LED1開 */
#define LED2OFF() LPC_GPIO2->FIODIR |= LED2;LPC_GPIO2->FIOSET |= LED2 /* LED2關(guān) */
#define LED2ON() LPC_GPIO2->FIODIR |= LED2;LPC_GPIO2->FIOCLR |= LED2 /* LED2開 */
#define LED3OFF() LPC_GPIO2->FIODIR |= LED3;LPC_GPIO2->FIOSET |= LED3 /* LED1關(guān) */
#define LED3ON() LPC_GPIO2->FIODIR |= LED3;LPC_GPIO2->FIOCLR |= LED3 /* LED1開 */
#define LED4OFF() LPC_GPIO2->FIODIR |= LED4;LPC_GPIO2->FIOSET |= LED4 /* LED2關(guān) */
#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: 用戶程序入口函數(shù),將JP17短接,P0.11腳控制蜂鳴器,
** 將JP4的KEY1與P2_10短接,每按下KEY1,蜂鳴器響一聲
** input parameters: 無
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
int main (void)
{
SystemInit(); /* 系統(tǒng)初始化 */
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
*********************************************************************************************************/
評(píng)論