📄 key.c
字号:
/*
file name:key.c
function: receive a key by usart
author :Einsn
date:2006-11-11
*/
/*
name:
function:
author:einsn
date :2006-11-11
*/
//#include "uart.h"
#include "key.h"
#include <avr/signal.h>
#include <avr/io.h> //for uart interrupt
#include "sigtimer.h" // use the timer signal
/*
#define KEYBUFSIZE 4
#define KEYCMDSIZE 3
volatile uint8_t KeyBuf[KEYBUFSIZE];
volatile uint8_t KeyId;
volatile uint8_t KeyReq;
volatile uint8_t KeyValue;
*/
/*
name:KeyInit()
function:init the key variables
author:einsn
date :2006-11-11
*/
/*
void
KeyInit(void)
{
uint8_t i;
KeyId=0;
KeyValue=0;
for(i=0;i<KEYBUFSIZE;i++)
KeyBuf[i]=0;
}
*/
/*
name:SIGNAL(SIG_USART_RECV)
function:usart receive interrupt
for key
author:einsn
date :2006-11-11
*/
/*
SIGNAL(SIG_UART0_RECV)
{
putch(UDR0);
KeyBuf[KeyId++]=UDR0;
if(KeyId>2){
if(KeyBuf[2]=='}'&&KeyBuf[0]=='{'&&KeyReq>0)
{
KeyValue=KeyBuf[1];
}
KeyId=0;
}
}
*/
/*
uint8_t
GetKey(void)
{
uint8_t tmp;
KeyReq=1;
do{
}while(KeyValue==0);
KeyReq=0;
tmp=KeyValue;
KeyValue=0;
return tmp;
}
*/
/*
name:KeyNumToAsc()
function:convert the num's key value to the ascii
author:einsn
date :2006-12-10
uint8 KeyNumToAsc(uint8 key){
return 0;
}
*/
//#define ENPSLCMBLA
///////////////////////
///////////////////////
//NEW KEY
volatile uint8_t KeyState;
volatile uint16_t KeyMsg;
volatile uint16_t KeyRealease;
#ifdef ENPSLCMBLA
volatile uint16 CntLcmBLA;
#define CNTLCMBLALOADDEF(x) (x*(1000/TIMERINTERVAL))
#define LCMLBAON() (PORTB&=~(1<<PB5))
#define LCMLBAOFF() (PORTB|=(1<<PB5))
#endif
#define LONGKEY
#ifdef LONGKEY
static uint16 LongKeyCnt;
#define LongKeyReady 300
#define LongKeyInterval 100 //ms
#endif
//volatile uint8 CntPSMode;
void
KeyInit(void)
{
KEYPORTINIT();
KeyState=0;
KeyMsg=0;
#ifdef ENPSLCMBLA
CntLcmBLA=CNTLCMBLALOADDEF(LCMBLACTL);
#endif
SIGTimerLoad((SIGNALHOOK)KeyTimerHook,(void*)NULL);
}
/*
name:KeyTimerHook
function
author:einsn
date :2006-12-30
*/
void KeyTimerHook(void *p){
uint8 temp;
KeyMsg=0;
//2007-04-13 Add to support Lcm bla control
#ifdef ENPSLCMBLA
if(CntLcmBLA>0){
if(--CntLcmBLA==0)
LCMLBAOFF();
}
#endif
switch(KeyState){
case 0:
CLRALLKOUT();
asm("nop;nop;nop;");
temp=~KINVALUE();
if(temp){//have key undef press
KeyState=1;
}
break;
case 1://qu tou
// CLRALLKOUT();
temp=~KINVALUE();
if(temp){//have key undef press
KeyState=2;
}else KeyState=0;
break;
case 2://Get the key value
{
uint8 i;
for(i=0;i<5;i++){
SETALLKOUT();
CLRKOUT(i);
asm("nop;nop;nop;");
temp=~KINVALUE();
if(temp){
KeyMsg=temp;
KeyMsg=(KeyMsg<<8)|(i+1);
KeyState=3;
KeyRealease=0;
#ifdef LONGKEY
LongKeyCnt=0;
#endif
//reset the lcm bla counter
#ifdef ENPSLCMBLA
CntLcmBLA=CNTLCMBLALOADDEF(LCMBLACTL);
LCMLBAON();
#endif
}
}
if(KeyMsg==0){//no key
KeyState=0;//key state reset
}
}
break;
case 3:
CLRALLKOUT();
asm("nop;nop;nop;");
temp=~KINVALUE();
if(!(temp)){//no key undef press
KeyMsg=KeyRealease|(1<<7);
KeyState=0;//key state reset
}
#ifdef LONGKEY
if(++LongKeyCnt>(LongKeyReady/TIMERINTERVAL)){
KeyState=4;
LongKeyCnt=0;
}
#endif
if(KeyRealease==0){
uint8 i;
for(i=0;i<5;i++){
SETALLKOUT();
CLRKOUT(i);
asm("nop;nop;nop;");
temp=~KINVALUE();
if(temp){
KeyRealease=temp;
//KeyRealease=(KeyRealease<<8)|((i+1)|(1<<7));
KeyRealease=(KeyRealease<<8)|((i+1));
}
}
}
break;
#ifdef LONGKEY
case 4:
CLRALLKOUT();
asm("nop;nop;nop;");
temp=~KINVALUE();
if(!(temp)){//no key undef press
KeyMsg=KeyRealease|(1<<7);
KeyState=0;//key state reset
}
if(++LongKeyCnt>(LongKeyInterval/TIMERINTERVAL)){
LongKeyCnt=0;
KeyMsg=KeyRealease;
}
break;
#endif
default:
break;
}
}
/*
name:GetKeyMsg
function
author:einsn
date :2006-12-30
*/
uint8 GetKeyMsg(uint8 type ){
uint8 ByteMask[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uint16 tmp=0;
uint8 i;
//KeyState=0;
KeyMsg=0;
//SIGTimer0Load(KeyTimerHook);//enable the key msg
do{
if(type==KEYMSGUP){
if((KeyMsg&0x0080)){//have up key msg
tmp=KeyMsg&(~0x0080);
}
}else if(KeyMsg>0&&!(KeyMsg&0x0080)){
tmp=KeyMsg;
}
}while(tmp==0);
//SIGTimer0Remove(KeyTimerHook);
KeyMsg=0;
for(i=0;i<8;i++){
if(ByteMask[i]==(uint8)(tmp>>8)){
break;
}
}
return ((i+1)<<4|(uint8)(tmp&0x000f));
}
/*
* Name:GetKeyMsgNoWait
* Function:
*
* In:
* Out:
*
* Author:Einsn Liu
* Date:2007-04-13
*
*
*/
uint8 GetKeyMsgNoWait(uint8 type ){
uint8 ByteMask[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uint16 tmp=0;
uint8 i;
if(type==KEYMSGUP){
if((KeyMsg&0x0080)){//have up key msg
tmp=KeyMsg&(~0x0080);
}
}else if(KeyMsg>0&&!(KeyMsg&0x0080)){
tmp=KeyMsg;
}
KeyMsg=0;
if(tmp==0)return 0;
for(i=0;i<8;i++){
if(ByteMask[i]==(uint8)(tmp>>8)){
break;
}
}
return ((i+1)<<4|(uint8)(tmp&0x000f));
}
/*
name:KeyNumToAsc()
function:convert the num's key value to the ascii
author:einsn
date :2007-1-1
uint8 KeyNumToAsc(uint8 key){
switch(key){
case KEY_NUM0:
key='0';
break;
case KEY_NUM1:
key='1';
break;
case KEY_NUM2:
key='2';
break;
case KEY_NUM3:
key='3';
break;
case KEY_NUM4:
key='4';
break;
case KEY_NUM5:
key='5';
break;
case KEY_NUM6:
key='6';
break;
case KEY_NUM7:
key='7';
break;
case KEY_NUM8:
key='8';
break;
case KEY_NUM9:
key='9';
break;
case KEY_NUMP:
key='.';
break;
default:
key=0;
break;
}
return key;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -