#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[]; #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++) { if (i<8) { key = (keybyte1 & 0x01<= 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) { switch (keyno) { case SIGID_HALL1: return IN_HALL1; case SIGID_HALL2: return IN_HALL2; case SIGID_HALL3: return IN_HALL3; case SIGID_HALL4: return IN_HALL4; case SIGID_HALL5: return IN_HALL5; case SIGID_HALL6: return IN_HALL6; default: return 0; } } uint8_t GetKeyState(uint8_t keyno) { if (keyno > 0 && keyno <= KEY_NUM) { 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; } } void MOTOR5Ctrl(uint8_t act) { switch (act) { case ACT_NOACT: OUT_RLY5P = OUT_OFF; OUT_RLY5N = OUT_OFF; break; case ACT_XH: OUT_RLY5P = OUT_ON; OUT_RLY5N = OUT_OFF; break; case ACT_XQ: OUT_RLY5P = OUT_OFF; OUT_RLY5N = OUT_ON; break; default: break; } } void MOTOR6Ctrl(uint8_t act) { switch (act) { case ACT_NOACT: OUT_RLY6P = OUT_OFF; OUT_RLY6N = OUT_OFF; break; case ACT_XH: OUT_RLY6P = OUT_ON; OUT_RLY6N = OUT_OFF; break; case ACT_XQ: OUT_RLY6P = OUT_OFF; OUT_RLY6N = OUT_ON; break; default: break; } } uint16_t getAdval(uint8_t ch) { if (ch < 4) { return g_adval[ch]; } return 0; } void SetTfDuty(uint8_t duty) { uint32_t reg = 0U; if (duty > 100U) { duty = 100U; } reg = TDR00; reg = (reg + 1U) * duty / 100U; TDR01 = (uint16_t)reg; } void SetJrDuty(uint8_t duty) { uint32_t reg = 0U; if (duty > 100U) { duty = 100U; } reg = TDR00; reg = (reg + 1U) * duty / 100U; TDR03 = (uint16_t)reg; //TDR07 = (uint16_t)reg; }