资讯详情

LPC1766 GPIO输入和输出

前些时间玩了下M32,感觉不错,资源和功能丰富,用起来挺顺手的。和51根本不是一个档次的,用过后决定专用RAM-M3核心的嵌入式。唯一不足就是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->FIOD |= BEEP;LPC_GPIO0->FIOSET |= BEEP /* 蜂鸣器关 */ #define BEEP() 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 *********************************************************************************************************/

-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台