📄 mian.c
字号:
/****************************************************
*名 称: GPIO测试文件 *
*功 能: 测试GPIO输入输出 *
*备 注: 2011.1.8
*贞明电子:http://shop58972409.taobao.com *
*****************************************************/
/**************** 头文件调用&&宏定义 ****************/
#include "LPC11xx.h"
#include "GPIO.H"
/******************* 全局变量定义 *******************/
/********************** 函数声明 ********************/
__inline void delay_ms(uint32_t a);
/********************** 主函数 **********************/
int main(void)
{
SystemInit(); //系统初始化,包括使能时钟
GPIOInit(); //GPIO初始化,使能GPIO模块时钟
GPIOSetDir(PORT2,8,1); //设置P2.8为输出,LED1
GPIOSetDir(PORT2,9,1); //设置P2.9为输出,LED2
GPIOSetDir(PORT3,4,1); //设置P3.4为输出,LED3
LPC_IOCON->PIO2_11=0x00; //设置P2.11为GPIO功能
LPC_IOCON->PIO1_4=(1<<7); //注意:当有AD功能的引脚作为输入时,需要配置为数字输入。
LPC_IOCON->PIO2_11=0x00; //设置P2.11为GPIO功能
GPIOSetDir(PORT2,11,0); //设置P2.11为输入 KEY1
GPIOSetDir(PORT1,4,0); //设置P1.4为输入 KEY2
GPIOSetDir(PORT3,5,0); //设置P3.5为输入 KEY3
GPIOSetValue(PORT2,8,1); //灭3个灯
GPIOSetValue(PORT2,9,1);
GPIOSetValue(PORT3,4,1);
delay_ms(1000);
while(1)
{
if((LPC_GPIO2->DATA&(1<<11))==0) //如果按键1被按下
{
delay_ms(10); //延时消抖
if((LPC_GPIO2->DATA&(1<<11))==0)//如果还是按下状态
{
while((LPC_GPIO2->DATA&(1<<11))==0); //等待松开按键
LPC_GPIO2->DATA^=(1<<8); //LED1闪烁1次
}
}
if((LPC_GPIO1->DATA&(1<<4))==0) //如果按键2被按下
{
delay_ms(10);
if((LPC_GPIO3->DATA&(1<<5))==0) //如果还是按下状态
while((LPC_GPIO3->DATA&(1<<5))==0); //等待松开按键
LPC_GPIO2->DATA^=(1<<9); //LED3闪烁1次 //延时消抖
}
if((LPC_GPIO3->DATA&(1<<5))==0)
{
delay_ms(10); //延时消抖
if((LPC_GPIO1->DATA&(1<<4))==0) //如果还是按下状态
while((LPC_GPIO1->DATA&(1<<4))==0); //等待松开按键
LPC_GPIO3->DATA^=(1<<4); //LED2闪烁1次
}
}
}
/********************** 函数定义 ********************/
/****************************************************
*名 称: 延时函数 *
*参 数: 延时时间 *
*备 注: 内联函数 *
*****************************************************/
__inline void delay_ms(uint32_t a) //1ms延时函数
{
uint32_t i;
while( --a != 0){
for(i = 0; i<5500; i++);
}
}
/****************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -