?? swutl.c
字號(hào):
return ((int)key_arrenge[sw_data]);
/* return SW number */
}
/* if not be initialized */
return ERR; /* return error */
}
/*""FUNC COMMENT""*************************************************************
* ID : ---
* function name : int SW__getdec(void)
* function : input decimal number
* : SW23:1 SW22:2 SW21:3 SW20:'#'
* : SW13:4 SW12:5 SW11:6 SW10:0
* : SW03:7 SW02:8 SW01:9 SW00:'*'
* : input decimal number from 0 to 32767 until ENTER
* : return 0 if only ENTER input
* : return ERR if input number out of scope(0 to 32767)
* : return ERR if port for controlling SW is not initialized
* parameter : none
* return : decimal number input from key matrix
* function used : none
* notice : use global variable sw_data, F_keyfix
* History : ---
*""FUNC COMMENT END""*********************************************************/
int SW__getdec(void)
{
unsigned char key_arrenge[] = { /* key arrenge data(value) */
1 , 4 , 7 ,
2 , 5 , 8 ,
3 , 6 , 9 ,
'#' , 0 , '*' /*'#':CLEAR key,'*':ENTER key */
};
long out_dec = 0; /* result(decimal number) */
unsigned char i;
if(init_hard){ /* if initialized */
for(i = 0; ;i++){ /* input from key matrix */
while(!F_keyfix); /* wait input fix */
F_keyfix = FALSE; /* clear input data fix flag */
if(ENTER_KEY == sw_data) break; /* complete with ENTER input */
if(CLEAR_KEY == sw_data){ /* if CLEAR key is input */
out_dec = 0; /* after clear buffer, input again */
continue ;
}
else{ /* calculate only when number input */
out_dec = out_dec * 10 + (long)key_arrenge[sw_data];
}
}
if((i>5) || (out_dec > 32767)){ /* check scope */
return ERR;
}
else{
return ((int)out_dec); /* return number */
}
}
/* if not be initialized */
return ERR; /* return error */
}
/* #############################################################################
* ### private function
*/
/*""FUNC COMMENT""*************************************************************
* ID : ---
* function name : static int judge_keyMTXr(char *swdata)
* function : control key matrix
* parameter : char *swdata : pointer to space for saving number of pushed SW
* return : int OK ; normal end
* : ERR ; not be initialized
* function used : unsigned char keymatrix_EX( DEF_KEYMTX * ) ; key matrix input function
* static unsigned char detect_edge( unsigned int ) ; detect pushing
* static unsigned char cut_chat( unsigned int ) ;filter chattering and noise
* notice : none
* History : ---
*""FUNC COMMENT END""*********************************************************/
static int judge_keyMTX(unsigned char *swdata)
{
DEF_KEYMTX now_key; /* for key data input */
unsigned char i; /* for count SW number */
int err = ERR; /* error code */
unsigned int now_sw; /* dummy data for detecting SW number */
if(OK == keymatrix_EX(&now_key)){ /* input key data */
if(OK == cut_chat(now_key.all)){ /* filter chattering and noise */
if(OK == detect_edge(now_key.all)){ /* detect pushing */
now_sw = now_key.all;
for(i = 0; i < 12; i++){ /* detect SW number */
if((now_sw <<= 1) == 0){
*swdata = i;
err = OK;
break;
}
}
}
}
}
return err; /* return result */
}
/*""FUNC COMMENT""*************************************************************
* ID : ---
* function name : static int keymatrix_EX( DEF_KEYMTX *swdata )
* function : read SW status every scan line
* parameter : DEF_KEYMTX *swdata : pointer to space for saving 12 SW status
* return : int OK ; all line input completed
* : ERR ; not completed
* function used : none
* notice : none
* History : ---
*""FUNC COMMENT END""*********************************************************/
static int keymatrix_EX(DEF_KEYMTX *swdata)
{
unsigned char input ; /* data input */
static unsigned char key_index = 0 ; /* index for scan line */
int err = ERR ; /* error code */
input = (p4 >> 3)|0xFB ; /* input from return line */
input = input & (( p3 >> 2)|0xFC) ; /* combine mask processing and data */
switch(key_index) { /* save data every scan line */
case 0 : /* (active change over) */
swdata->line.sw0_B = ~input ; /* save return of SW00,SW10,SW20 */
break ;
case 1 :
swdata->line.sw7_9 = ~input ; /* save return of SW01,SW11,SW21 */
break ;
case 2 :
swdata->line.sw4_6 = ~input ; /* save return of SW02,Sw12,SW22 */
break ;
case 3 :
swdata->line.sw1_3 = ~input ; /* save return of SW03,Sw13,SW23 */
break ;
}
if(++key_index >= 4){ /* change scan line */
key_index = 0 ;
swdata->line.DUMMY = 0 ;
err = OK ;
}
p0 = scan_table[ key_index ] ;
return err ; /* return result */
}
/*""FUNC COMMENT""*************************************************************
* ID : ---
* function name : static int detect_edge( unsigned int swdata )
* function : detect edge
* parameter : unsigned int swdata : 12 SW status
* return : int OK ; have edge
* : ERR ; no edge
* function used : none
* notice : none
* History : ---
*""FUNC COMMENT END""*********************************************************/
static int detect_edge(unsigned int swdata)
{
static unsigned int old_key = 0xff; /* former fixed data */
int err = ERR ; /* error code */
if((swdata) && (old_key == 0x00)){ /* there is ON and all was OFF before */
err = OK; /* have edge */
}
old_key = swdata;
return err ; /* return result */
}
/*""FUNC COMMENT""*************************************************************
* ID : ---
* function name : static int cut_chat( unsigned int sw_input )
* function : cancel noise by more than or equal twice same in 3 times
* parameter : unsigned int swdata : 12 SW status
* return : int OK ; fixed
* : ERR ; not fixed
* function used : none
* notice : none
* History : ---
*""FUNC COMMENT END""*********************************************************/
static int cut_chat(unsigned int sw_input)
{
static unsigned int sw_buf = 0x00 ; /* former input data */
static char agree_cnt = 0 ; /* same times */
if(sw_input == sw_buf){ /* if now == former */
if(++agree_cnt >= 2){ /* count up same times */
agree_cnt = 0; /* fix if same times more than or equal twice */
return OK;
}
return ERR;
}
else{ /* if now != former */
sw_buf = sw_input; /* remember this value */
agree_cnt = 0; /* initialize same times */
return ERR;
}
}
/*""FUNC COMMENT""*************************************************************
* ID : ---
* function name : void INT_TimerY( void )
* function : timer Y interrupt function(interrupt occurs every 2ms)
* parameter : none
* return : none
* functin used : judge_keyMTX( )丟function for controlling key matrix
* notice : interrupt function called by interrupt program
* : use global variable sw_data, F_keyfix
* History : ---
*""FUNC COMMENT END""*********************************************************/
void INT_TimerY(void)
{
if(OK == judge_keyMTX( &sw_data)){ /* if there is new SW being ON */
F_keyfix = TRUE; /* set input data fix flag */
}
}
/******************************************************************************
end of file
******************************************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -