📄 ledkey.c
字号:
#include "LedKey.h"
xdata DWORD m_TickCount = 0;
DWORD GetTickCount(){return m_TickCount;}
void Sleep(DWORD iMilliseconds)
{
DWORD TickCount = GetTickCount();
while((GetTickCount() - TickCount) < iMilliseconds)
{
//WDT_CONTR = 0x3C; //复位看门狗
}
}
//数码管显示娩冲区
uchar xdata g_led_buf1[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uchar xdata g_led_buf2[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
// 这里定义的是数码管对应的字符字根
code uchar disbuf_u[]={
0xC0,/*0*/
0xF9,/*1*/
0xA4,/*2*/
0xB0,/*3*/
0x99,/*4*/
0x92,/*5*/
0x82,/*6*/
0xF8,/*7*/
0x80,/*8*/
0x90,/*9*/
0x88,/*A*/
0x83,/*b*/
0xC6,/*C*/
0xA7,/*c*/
0xA1,/*d*/
0x86,/*E*/
0x8E,/*F*/
0x89,/*H*/
0x8B,/*h*/
0xC7,/*L*/
0xAB,/*n*/
0xC8,/*N*/
0xA3,/*o*/
0x8C,/*P*/
0xAF,/*r*/
0x87,/*t*/
0xC1,/*U*/
0xBF,/*-*/
};
#define StateIO StateIO_1
#define bSerialRSLed bSerialRSLed_1
char bdata OutIO _at_ 0x21;
char bdata InIO = 0;
char bdata StateIO _at_ 0x20;
sbit bSerialRSLed = StateIO^7;
uchar xdata bSerialRSLedDelay = 0;
sbit BeepIO = P4^3;
#define Refurbish(x) x = 1;x = 0;
#define GledIO P2_2
#define Led1IO P2_0
#define Led2IO P2_1
uchar xdata g_LedOn = 0;
//-----------beep------------
int xdata m_OpenDelay = 0;
int xdata m_CloseDelay = 0;
//延时多久执行
int xdata m_BeepDelay = 0;
char xdata m_Times = 0;
int xdata m_beep = 0;
//Key--------------------------
xdata uchar key;
xdata uchar Prvkey;
xdata uint keycount = 0;
xdata uchar keybuf[6];
xdata uchar KeyPos = 0;
void AddKey(uchar ukey)
{
if(KeyPos < 5)
{
keybuf[KeyPos++] = ukey;
Beep(30, 0, 1, 0);
}else
Beep(20, 20, 3, 0);
}
uchar GetKey(void)
{
if(KeyPos)
{
Beep(30, 0, 1, 0);
return keybuf[--KeyPos];
}
return 0;
}
//---------------------------Key
uchar xdata m_uFlash = 0;//闪...
uchar xdata m_uFlashBit1 = 0;
uchar xdata m_uFlashBit2 = 0;
void SetLedFlash1(uchar uFlash)
{
m_uFlashBit1 = uFlash;
}
void SetLedFlash2(uchar uFlash)
{
m_uFlashBit2 = uFlash;
}
//数码管显示中断
void IntLed(void)
{
P2_4 = 0;
InIO = ~P0;
P2_4 = 1;
g_led_buf1[6] = ~OutIO;
g_led_buf1[7] = ~InIO;
g_led_buf2[7] = ~StateIO;
P0 = ~OutIO;
Refurbish(P2_5);
//----------------------------------------
P0 = 0xFF;
Refurbish(Led1IO);
Refurbish(Led2IO);
P0 = ~(0x01 << g_LedOn);
Refurbish(GledIO);
if(g_LedOn < 6)
{
if((m_uFlashBit1&(0x20 >> g_LedOn))&&(m_uFlash < 125))
{
P0 = 0xFF;
}else
{
if(g_led_buf1[g_LedOn] != 0xFF)
{
P0 = disbuf_u[g_led_buf1[g_LedOn]&0x7F];
if(g_led_buf1[g_LedOn]&0x80)
P0 &= 0x7F;
}else P0 = 0xFF;
}
}else P0 = g_led_buf1[g_LedOn];
Refurbish(Led1IO);
if(g_LedOn < 7)
{
if((m_uFlashBit2&(0x40 >> g_LedOn))&&(m_uFlash < 125))
{
P0 = 0xFF;
}else
{
if(g_led_buf2[g_LedOn] != 0xFF)
{
P0 = disbuf_u[g_led_buf2[g_LedOn]&0x7F];
if(g_led_buf2[g_LedOn]&0x80)
P0 &= 0x7F;
}else P0 = 0xFF;
}
}else P0 = g_led_buf2[g_LedOn];
Refurbish(Led2IO);
g_LedOn++;
if(g_LedOn > 8)
{
g_LedOn = 0;
//--------------------
if(bSerialRSLed)
{
bSerialRSLedDelay++;
if(bSerialRSLedDelay > 2)
{
bSerialRSLed = bSerialRSLedDelay = 0;
}
}
//--------------------
}
//---------键盘检测
key = InIO;
if(key)
{
if(Prvkey == key)
{
keycount++;
if(keycount == 30)
{
//AddKey(Prvkey);
if(KeyPos < 5)
{
keybuf[KeyPos++] = Prvkey;
//Beep(30, 0, 1, 0);
}//else
// Beep(20, 20, 3, 0);
}
if(keycount > 600)
{
keycount = 450;
//AddKey(Prvkey);
if(KeyPos < 5)
{
keybuf[KeyPos++] = Prvkey;
//Beep(30, 0, 1, 0);
}//else
// Beep(20, 20, 3, 0);
}
}
}else
{
keycount = 0;
}
Prvkey = key;
//----------------------
//Beep-------------------------------
if(m_BeepDelay <= 0)
{
if((m_Times != 0)&&(m_beep == 0))
{
if(m_Times%2)
{
m_beep = m_CloseDelay;
BeepIO = 1;
}else
{
m_beep = m_OpenDelay;
BeepIO = 0;
}
if(m_Times > 0)
{
m_Times--;
}else
{
if(m_Times == -1)
{
m_Times = -2;
}else
{
m_Times = -1;
}
}
}
if(m_beep > 0)
{
m_beep--;
}
}else
{
m_BeepDelay--;
}
//----------------------------------
if(g_LedOn%2)m_uFlash++;
m_TickCount++;
}
uchar xdata m_uPoint1 = 0;
uchar xdata m_uPoint2 = 0;
void SetPoint1(uchar uPoint)
{
m_uPoint1 = uPoint;
}
void SetPoint2(uchar uPoint)
{
m_uPoint2 = uPoint;
}
void ConvetLed(long lShowLed,uchar *p, uchar ws, uchar uPoint)
{
uchar i = 0;
bit fs = 0;
bit fsfirst = 1;
if(lShowLed < 0)
{
fs = 1;
lShowLed *= -1;
}
for(i = 0; i < ws; i++)
{
if(lShowLed|| (uPoint >= i&& uPoint < ws))
{
p[ws-i-1] = lShowLed%10;
if(uPoint&&(i == uPoint))
{
p[ws-i-1] |= 0x80;
}
lShowLed /= 10;
}else
{
if(fs&&fsfirst)
{
fsfirst = 0;
p[ws-i-1] = 27;
}else
{
p[ws-i-1] = 0xFF;
}
}
}
}
void ConvetLed1(long lShowLed)
{
ConvetLed(lShowLed,g_led_buf1,6, m_uPoint1);
}
void ConvetLed2(long lShowLed)
{
//ConvetLed(lShowLed,g_led_buf2, 7, m_uPoint2);
ConvetLed(lShowLed,g_led_buf2+1, 6, m_uPoint2);
}
void LedCommand(uchar uStart, uchar ledbuf[], uchar *p, uchar uCount)
{
uchar i;
for(i = uStart; i < uStart + uCount; i++)
{
p[i] = ledbuf[i];
}
}
void LedCommand1(uchar uStart, uchar ledbuf[], uchar uCount)
{
LedCommand( uStart, ledbuf, g_led_buf1, uCount);
}
void LedCommand2(uchar uStart, uchar ledbuf[], uchar uCount)
{
LedCommand( uStart, ledbuf, g_led_buf2, uCount);
}
//打开时间,关闭时间,次数,-1等于一直响着
void Beep(int OpenDelay,int CloseDelay, char Times, int BeepDelay)
{
m_BeepDelay = BeepDelay;
m_OpenDelay = OpenDelay;
m_CloseDelay = CloseDelay;
if(Times > 0)
{
m_Times = Times*2;
}else if(Times == 0)
{
m_Times = 0;
BeepIO = 1;
}else
{
m_Times = -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -