#include "hwCtrl.h" #include "r_cg_port.h" #include "iodefine.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]; #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) { uint8_t i,key,key_nopress; key_nopress = 0; for (i = 0; i < KEY_NUM; i++) { key = GetIOState(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 GetIOState(uint8_t keyno) { uint16_t keyad; switch (keyno) { case KEYID_KBXQ: return IN_KEY_KBXQ; case KEYID_KBXH: return IN_KEY_KBXH; case KEYID_HGXQ: return IN_KEY_HGXQ; case KEYID_HGXH: return IN_KEY_HGXH; case KEYID_ZDUP: return IN_KEY_ZDUP; case KEYID_ZDDOWN: return IN_KEY_ZDDOWN; case KEYID_TTUP: return IN_KEY_TTUP; case KEYID_TTDOWN: return IN_KEY_TTDOWN; case KEYID_TP: return IN_KEY_TP; case KEYID_FW: return IN_KEY_FW; case KEYID_SET: keyad = getAdval(ADCH_JYKEY); return keyad<20?1:0; case KEYID_M1: keyad = getAdval(ADCH_JYKEY); return (keyad>200 && keyad<250)?1:0; case KEYID_M2: keyad = getAdval(ADCH_JYKEY); return (keyad>450 && keyad<550)?1:0; case KEYID_M3: keyad = getAdval(ADCH_JYKEY); return (keyad>700 && keyad<800)?1:0; case SIGID_HALL1: return IN_HALL1; case SIGID_HALL2: return IN_HALL2; case SIGID_HALL3: return IN_HALL3; case SIGID_HALL4: return IN_HALL4; 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; }