?? system.c
字號:
#include "System.h"
//25ms定時器2計數值=0x10000-22.1184M/12/40
#define T2L_VAL 0x00
#define T2H_VAL 0x4C
unsigned char CheckKeyF=0; //鍵盤檢測標志位
unsigned int counter=0; //T2計數器,25ms++
unsigned int Mycounter; //計數器,用于用戶定時
unsigned char Myflag=0; //用戶定時標志
//鍵盤數據結構
struct KeyStruct Key[8];
void delay(int n) //延時函數
{
int i=0;
while(i<n) i++;
}
#define XBARE 0x40 //ENABLE XBR, set XBR2.6 = 1
//配置交叉開關和I/O口
void PORT_INIT()
{
SFRPAGE = CONFIG_PAGE;
/* XBR0 control follow digital utils
uart0:xbr0.2->p0.0,p0.1
spi0:xbr0.1->p0.2,p0.3,p0.4,(p0.5)
smb0:xbr0.0->p0.6,p0.7
*/
XBR0 = 0x07;
/*XBR2 control follows
UART1:xbr2.2->p1.0,p1.1
*/
XBR2 = 0x4;
// set P1MDIN.2 to ADC2-in, skip p1.2 pin
P1MDIN = 0xFB;
/*XBR1 control follows
T0:XBR1.1->p1.3
INT0:XBR1.2->p1.4
*/
XBR1 = 0x06;
//for test
// P1MDOUT |= 0x10;
// XBR1 |= 0x82; //XBR1.7:sysclk->p1.4
//ENABLE switch
XBR2 |= XBARE;
P5MDOUT|=0x0F; //LCD控制信號
P6MDOUT|=0xFF; //LCD數據信號
P7MDOUT|=0x13; //背光,RES,HC157控制
P3MDOUT|=0x04; //KEY_EN
}
//系統時鐘初始化
void SYSCLK_INIT() //使用外部晶陣22.1184M
{
SFRPAGE = CONFIG_PAGE;
OSCXCN |=0x67; //使能外部晶陣
while((OSCXCN & 0x80)==0) //延時,直至外部晶陣就緒
delay(100);
CLKSEL |=0x01; //切換到外部時鐘
}
void Timer2_INIT() //16位自動重裝模式,產成25ms中斷
{
SFRPAGE = TMR2_PAGE;
TMR2L=T2L_VAL; //賦值
TMR2H=T2H_VAL;
RCAP2L=T2L_VAL;
RCAP2H=T2H_VAL;
TMR2CF |=0x01; //SYSCLK/12作為T2輸入,向上計數
TMR2CN &=0xf4; //T2工作模式: 自動重裝,計時器,忽略T2EX信號
ET2=1;
TR2=1;
}
void Timer2_ISR() interrupt 5 //T2 25ms中斷
{
TF2=0;
counter++;
CheckKeyF=1; //鍵盤檢測
if(Mycounter==counter)
Myflag=1; //用戶定時到,置位
}
void CheckKey() //鍵盤檢測.要求每次按鍵時間超過100ms;持續按鍵以每秒4次計算
{
unsigned char data i;
unsigned char data M=1;
SFRPAGE = CONFIG_PAGE;
OE=1; //關閉并口
Key_En=0; //使能鍵盤
P6MDOUT=0x00; //配置P6為數字輸入 (數字輸入=漏極開路+SFR寫1)
P6=0xff;
delay(5);
for(i=0;i<8;i++) //依次檢測各鍵
{
if((P6&M)==0) //當前第i位為0
{
if(Key[i].pressold==0) //前一次為0
{
Key[i].pressing++;
if(Key[i].pressing==4) Key[i].pressed=1;
if(Key[i].pressing==10) Key[i].pressing=0; //持續按鍵以每秒4次計算
}
else //前一次為1
{
Key[i].pressing=1;
}
Key[i].pressold=0;
}
else //當前第i位為1
{
if(Key[i].pressold==0) //前一次為0
{
Key[i].pressing=0;
}
else //前一次為1
{
}
Key[i].pressold=1;
}
M=M<<1;
}
CheckKeyF=0;
Key_En=1;
P6MDOUT=0xff; //P6置回輸出狀態
}
void Key_INIT()
{
int i;
for(i=0;i<8;i++)
{
Key[i].pressed=0;
Key[i].pressold=1;
Key[i].pressing=0;
}
}
void ClearKey()
{
int i;
for(i=0;i<8;i++)
{
Key[i].pressed=0;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -