#include "hwCtrl.h" #include "r_cg_port.h" #include "iodefine.h" #include "r_cg_timer.h" #include "PINdef.h" #include "r_cg_adc.h" static uint8_t keystate[KEY_NUM] = {0,0,0,0,0,0}; static uint8_t keyPressFlag[KEY_NUM] = {0,0,0,0,0,0}; static uint8_t keyReleaseFlag[KEY_NUM] = {0,0,0,0,0,0}; static uint16_t keydelay[KEY_NUM] = {0,0,0,0,0,0}; extern uint16_t g_adval[4]; uint8_t getIOstate(IOID_type ioid); #define KEY_DELAY_TIMES 20 //20Ms void ClearKeyState(void) { uint8_t i; for (i = 0; i < KEY_NUM; i++) { keystate[i] = 0; } } void setKeyPressFlag(uint8_t id) { if (id < KEY_NUM) { keyPressFlag[id] = KEY_PRESSED; } } void setKeyReleaseFlag(uint8_t id) { if (id < KEY_NUM) { keyReleaseFlag[id] = KEY_PRESSED; } } uint8_t getKeyPressFlag(uint8_t id) { uint8_t retVal = KEY_NOPRESSED; if (id < KEY_NUM) { retVal = keyPressFlag[id]; keyPressFlag[id] = KEY_NOPRESSED; } return retVal; } uint8_t getKeyReleaseFlag(uint8_t id) { uint8_t retVal = KEY_NOPRESSED; if (id < KEY_NUM) { retVal = keyReleaseFlag[id]; keyReleaseFlag[id] = KEY_NOPRESSED; } return retVal; } extern unsigned char keybyte1,keybyte2,keybyte3; void KeyScan(void)//1ms { uint8_t i,key,key_nopress; key_nopress = 0; for (i = 0; i < KEY_NUM; i++) { key = GetSigState(i); if (key == KEY_PRESSED && keystate[i] == KEY_NOPRESSED) { keydelay[i]++; if (keydelay[i] >= KEY_DELAY_TIMES) { keystate[i] = KEY_PRESSED; setKeyPressFlag(i); } } else if(key == KEY_NOPRESSED) { if (keydelay[i] > 0) { keydelay[i]--; } else { if (keystate[i] == KEY_PRESSED) { setKeyReleaseFlag(i); } keystate[i] = KEY_NOPRESSED; key_nopress++; } } } } uint8_t GetSigState(uint8_t keyno) { uint16_t keyad; switch (keyno) { case SIGID_KEY_KB_F: return (getIOstate(IOID_P92)==0)?1:0; case SIGID_KEY_KB_R: return (getIOstate(IOID_P82)==0)?1:0; case SIGID_KEY_HG_F: return (getIOstate(IOID_P91)==0)?1:0; case SIGID_KEY_HG_R: return (getIOstate(IOID_P83)==0)?1:0; case SIGID_KEY_ZD_UP: return (getIOstate(IOID_P87)==0)?1:0; case SIGID_KEY_ZD_DOWN: return (getIOstate(IOID_P84)==0)?1:0; case SIGID_KEY_TT_UP: return (getIOstate(IOID_P86)==0)?1:0; case SIGID_KEY_TT_DOWN: return (getIOstate(IOID_P85)==0)?1:0; case SIGID_KEY_JS: return (getIOstate(IOID_P120)==0)?1:0; case SIGID_KEY_TP: return (getIOstate(IOID_P125)==0)?1:0; case SIGID_KEY_JY_SET: return (getAdval(ADCH_JYKEY)<=100)?1:0; case SIGID_KEY_JY_1: return (getAdval(ADCH_JYKEY)<=300&&getAdval(ADCH_JYKEY)>=200)?1:0;//246 case SIGID_KEY_JY_2: return (getAdval(ADCH_JYKEY)<=560&&getAdval(ADCH_JYKEY)>=460)?1:0;//512 case SIGID_KEY_JY_3: return (getAdval(ADCH_JYKEY)<=820&&getAdval(ADCH_JYKEY)>=720)?1:0;//771 case SIGID_HALL_KB: return getIOstate(IOID_P60); case SIGID_HALL_HG: return getIOstate(IOID_P61); case SIGID_HALL_ZD: return getIOstate(IOID_P62); case SIGID_HALL_TT: return getIOstate(IOID_P63); default: return 0; } } uint8_t GetKeyState(uint8_t keyno) { if (keyno > 0 && keyno <= 16) { return keystate[keyno-1]; } return KEY_NOPRESSED; } #define OUT_OFF 0 #define OUT_ON 1 void MOTOR1Ctrl(uint8_t act) { switch (act) { case ACT_NOACT: OUT_RLY1P = OUT_OFF; OUT_RLY1N = OUT_OFF; break; case ACT_XH: OUT_RLY1P = OUT_ON; OUT_RLY1N = OUT_OFF; break; case ACT_XQ: OUT_RLY1P = OUT_OFF; OUT_RLY1N = OUT_ON; break; default: break; } } void MOTOR2Ctrl(uint8_t act) { switch (act) { case ACT_NOACT: OUT_RLY2P = OUT_OFF; OUT_RLY2N = OUT_OFF; break; case ACT_XH: OUT_RLY2P = OUT_ON; OUT_RLY2N = OUT_OFF; break; case ACT_XQ: OUT_RLY2P = OUT_OFF; OUT_RLY2N = OUT_ON; break; default: break; } } void MOTOR3Ctrl(uint8_t act) { switch (act) { case ACT_NOACT: OUT_RLY3P = OUT_OFF; OUT_RLY3N = OUT_OFF; break; case ACT_XH: OUT_RLY3P = OUT_ON; OUT_RLY3N = OUT_OFF; break; case ACT_XQ: OUT_RLY3P = OUT_OFF; OUT_RLY3N = OUT_ON; break; default: break; } } void MOTOR4Ctrl(uint8_t act) { switch (act) { case ACT_NOACT: OUT_RLY4P = OUT_OFF; OUT_RLY4N = OUT_OFF; break; case ACT_XH: OUT_RLY4P = OUT_ON; OUT_RLY4N = OUT_OFF; break; case ACT_XQ: OUT_RLY4P = OUT_OFF; OUT_RLY4N = OUT_ON; break; default: break; } } uint16_t getAdval(uint8_t ch) { if (ch < 4) { return g_adval[ch]; } return 0; } uint8_t getIOstate(IOID_type ioid) { switch (ioid) { case IOID_P120: return P12_bit.no0; case IOID_P41: return P4_bit.no1; case IOID_P40: return P4_bit.no0; case IOID_P124: return P12_bit.no4; case IOID_P123: return P12_bit.no3; case IOID_P137: return P13_bit.no7; case IOID_P122: return P12_bit.no2; case IOID_P121: return P12_bit.no1; case IOID_P60: return P6_bit.no0; case IOID_P61: return P6_bit.no1; case IOID_P62: return P6_bit.no2; case IOID_P63: return P6_bit.no3; case IOID_P00: return P0_bit.no0; case IOID_P140: return P14_bit.no0; case IOID_P130: return P13_bit.no0; case IOID_P73: return P7_bit.no3; case IOID_P72: return P7_bit.no2; case IOID_P71: return P7_bit.no1; case IOID_P70: return P7_bit.no0; case IOID_P32: return P3_bit.no2; case IOID_P30: return P3_bit.no0; case IOID_P17: return P1_bit.no7; case IOID_P16: return P1_bit.no6; case IOID_P15: return P1_bit.no5; case IOID_P31: return P3_bit.no1; case IOID_P14: return P1_bit.no4; case IOID_P13: return P1_bit.no3; case IOID_P12: return P1_bit.no2; case IOID_P11: return P1_bit.no1; case IOID_P10: return P1_bit.no0; case IOID_P33: return P3_bit.no3; case IOID_P34: return P3_bit.no4; case IOID_P80: return P8_bit.no0; case IOID_P81: return P8_bit.no1; case IOID_P82: return P8_bit.no2; case IOID_P83: return P8_bit.no3; case IOID_P84: return P8_bit.no4; case IOID_P85: return P8_bit.no5; case IOID_P86: return P8_bit.no6; case IOID_P87: return P8_bit.no7; case IOID_P90: return P9_bit.no0; case IOID_P91: return P9_bit.no1; case IOID_P92: return P9_bit.no2; case IOID_P125: return P12_bit.no5; case IOID_RESET: case IOID_REGC: case IOID_VSS: case IOID_VDD: default: return 0; } } #define OUT_BUZZER P0_bit.no0 #define BUZZER_ON 0 #define BUZZER_OFF 1 void Buzzer_ctrl(uint8_t onoff) { if (onoff == 0) { OUT_BUZZER = BUZZER_OFF; } else { OUT_BUZZER = BUZZER_ON; } }