?? main.c
字號(hào):
/****************************************************
*名 稱: 旋轉(zhuǎn)編碼器 *
*功 能: 旋轉(zhuǎn)編碼器、狀態(tài)選擇、串口輸出、中斷學(xué)習(xí)*
*備 注: 2011.1.9 *
*貞明電子: http://shop58972409.taobao.com *
*****************************************************/
/**************** 頭文件調(diào)用&&宏定義 ****************/
#include "LPC11xx.h"
#include "gpio.h"
#include "UART.h"
/******************* 全局變量定義 *******************/
uint8_t state=0;
/********************** 函數(shù)聲明 ********************/
__inline void delay_ms(uint32_t a);
void UserIO(void);
void LED1(void);
void LED2(void);
void LED3(void);
/********************** 主函數(shù) **********************/
int main(void)
{
SystemInit(); //系統(tǒng)初始化,包括使能時(shí)鐘
GPIOInit();
UserIO();
UARTInit(115200); //初始化串口波特率為115200
delay_ms(1000);
UARTSend("貞明電子:http://shop58972409.taobao.com\r\n",42);
while(1)
{
switch (state)
{
case 1: //狀態(tài)1,LED3亮
LED3();
break;
case 2: //狀態(tài)2,LED2亮
LED2();
break;
case 3: //狀態(tài)3,LED1亮
LED1();
break;
default:
break;
}
}
}
/********************** 函數(shù)定義 ********************/
/****************************************************
*名 稱: 延時(shí)函數(shù) *
*參 數(shù): 延時(shí)時(shí)間 *
*備 注: 內(nèi)聯(lián)函數(shù) *
*****************************************************/
__inline void delay_ms(uint32_t a) //1ms延時(shí)函數(shù)
{
uint32_t i;
while( --a != 0)
{
for(i = 0; i<5500; i++);
}
}
/****************************************************
*名 稱: UserIO *
*參 數(shù): 無 *
*備 注: 用戶IO初始化 *
*****************************************************/
void UserIO(void)
{
LPC_IOCON->PIO0_2=0x00; //設(shè)置為輸入
LPC_IOCON->PIO1_8=0x00; //設(shè)置為輸入
LPC_IOCON->PIO1_6=0x01; //配置為UART
LPC_IOCON->PIO1_7=0x01; //配置為UART
GPIOSetDir(PORT0,2,0); //Set PIO0_2 Input
GPIOSetDir(PORT1,8,0); //Set PIO1_8 Input
GPIOSetInterrupt(PORT0,2,0,0,1);//端口號(hào), 位地址, sense, single/doube, 上升/下降沿
GPIOSetInterrupt(PORT1,8,0,0,0); //上升沿觸發(fā)中斷
GPIOIntEnable(PORT0,2); //使能該端口中斷
GPIOIntEnable(PORT1,8); //使能該端口中斷
GPIOSetDir(2,8,1); //設(shè)置P2.8為輸出,LED1
GPIOSetDir(2,9,1); //設(shè)置P2.9為輸出,LED2
GPIOSetDir(3,4,1); //設(shè)置P3.4為輸出,LED3
//調(diào)用頭文件中函數(shù)輸出方法
GPIOSetValue(PORT2,8,1); //設(shè)置P2.8輸出0,點(diǎn)亮LED1
GPIOSetValue(PORT2,9,1); //設(shè)置P2.9輸出0,點(diǎn)亮LED2
GPIOSetValue(PORT3,4,1); //設(shè)置P3.4輸出0,點(diǎn)亮LED3
}
void PIOINT1_IRQHandler(void)
{
uint32_t regVal;
regVal= GPIOIntStatus(PORT1, 8); //得到P1_8的中斷狀態(tài)
if(regVal)
{
if((LPC_GPIO0->DATA&0x04)!=0x04) //上升沿判斷是否為低電平
{
UARTSend("Right",5); //輸出向右
if(state<4) //狀態(tài)變化,LED順序向右移動(dòng)
state++;
if(state==4)
state=1;
}
GPIOIntClear( PORT1, 8); //清除中斷標(biāo)志
}
return;
}
void PIOINT0_IRQHandler(void)
{
uint32_t regVal;
regVal= GPIOIntStatus(PORT0, 2); //得到P0_2的中斷狀態(tài)
if(regVal)
{
if((LPC_GPIO1->DATA&0x100)==0x100) //下降沿判斷是否為高電平
{
UARTSend("Left",4); //狀態(tài)變化,LED順序向左移動(dòng)
if(state>0)
state--;
if(state==0)
state=3;
}
GPIOIntClear( PORT0, 2); //清除中斷標(biāo)志
}
return;
}
void LED1(void) //LED1亮其余滅
{
GPIOSetValue(PORT2,8,0); //設(shè)置P2.8輸出0,LED1亮
GPIOSetValue(PORT2,9,1); //設(shè)置P2.9輸出1,LED2滅
GPIOSetValue(PORT3,4,1); //設(shè)置P3.4輸出1,LED3滅
}
void LED2(void) //LED2亮其余滅
{
GPIOSetValue(PORT2,8,1); //設(shè)置P2.8輸出1,LED1滅
GPIOSetValue(PORT2,9,1); //設(shè)置P2.9輸出1,LED2滅
GPIOSetValue(PORT3,4,0); //設(shè)置P3.4輸出0,LED3亮
}
void LED3(void) //LED3亮其余滅
{
GPIOSetValue(PORT2,8,1); //設(shè)置P2.8輸出1,LED1滅
GPIOSetValue(PORT2,9,0); //設(shè)置P2.9輸出0,LED2亮
GPIOSetValue(PORT3,4,1); //設(shè)置P3.4輸出1,LED3滅
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -