diff --git a/Lib/GPIO.C b/Lib/GPIO.C index 1642a28..465c1ff 100644 --- a/Lib/GPIO.C +++ b/Lib/GPIO.C @@ -91,29 +91,102 @@ void EC_Cfg() // INT off EX0 = 0; EX1 = 0; - // Init gpios + // Init gpios Port3Cfg(0, 4); // P3.4 EC_KEY Port3Cfg(0, 2); // P3.2 EC_A Port3Cfg(0, 3); // P3.3 EC_B // Init ints IT0 = 1; // FALLEDGE trig IT1 = 1; - // INT on + // INT on EX0 = 1; EX1 = 1; } +volatile short data ec_count = 0; +volatile bit data ec_rotating = 0; + void EC_IntA(void) interrupt INT_NO_INT0 using 2 // INT0中断服务程序,使用寄存器组2 { - ; + if (ec_rotating) + { + // A相后动 + if (EC_B == 1) + { + // 不合理的,丢步了 + // LEDA = 1; + // LEDB = 1; + } + else + { + // 方向1转完了 + ec_count++; + // LEDA = 1; + } + ec_rotating = 0; + } + else + { + // A相先动 + if (EC_B == 1) + { + // 方向1起转 + ec_rotating = 1; + // LEDA = 0; + } + else + { + // 不合理的,丢步了 + ec_rotating = 0; + // LEDA = 1; + // LEDB = 1; + } + } } -void EC_IntB(void) interrupt INT_NO_INT1 using 2 // INT0中断服务程序,使用寄存器组2 +void EC_IntB(void) interrupt INT_NO_INT1 using 3 // INT1中断服务程序,使用寄存器组2 { - ; + if (ec_rotating) + { + // B相后动 + if (EC_A == 1) + { + // 不合理的,丢步了 + // LEDA = 1; + // LEDB = 1; + } + else + { + // 方向2转完了 + ec_count--; + // LEDB = 1; + } + ec_rotating = 0; + } + else + { + // B相先动 + if (EC_A == 1) + { + // 方向2起转 + ec_rotating = 1; + // LEDB = 0; + } + else + { + // 不合理的,丢步了 + ec_rotating = 0; + // LEDA = 1; + // LEDB = 1; + } + } } short EC_Read() { - return 0; + + short cnt = ec_count; + // if (!ec_rotating) + ec_count = 0; + return cnt; } \ No newline at end of file diff --git a/Lib/GPIO.H b/Lib/GPIO.H index 949c754..aae18eb 100644 --- a/Lib/GPIO.H +++ b/Lib/GPIO.H @@ -15,5 +15,12 @@ void Port1Cfg(UINT8 Mode, UINT8 Pin); void Port3Cfg(UINT8 Mode, UINT8 Pin); +sbit EC_A = P3 ^ 2; +sbit EC_B = P3 ^ 3; +sbit EC_KEY = P3 ^ 4; + +sbit LEDA = P1 ^ 4; +sbit LEDB = P1 ^ 5; + void EC_Cfg(); short EC_Read(); \ No newline at end of file diff --git a/SnapDial.C b/SnapDial.C index 0d7181b..3813296 100644 --- a/SnapDial.C +++ b/SnapDial.C @@ -4,10 +4,6 @@ #include #include -sbit EC_A = P3 ^ 2; -sbit EC_B = P3 ^ 3; -sbit EC_KEY = P3 ^ 4; - #define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE #define ENDP1_IN_SIZE 16 @@ -455,6 +451,10 @@ main() CfgFsys(); mDelaymS(5); // 修改主频等待内部晶振稳定,必加 + Port1Cfg(3, 4); + Port1Cfg(3, 5); + LEDA = 1; + LEDB = 1; EC_Cfg(); // USBDeviceReset(); @@ -469,7 +469,7 @@ main() { FLAG_EP1 = 0; HIDDial[1] = (EC_KEY) ? 0x00 : 0x01; - DialRotation = EC_Read(); + DialRotation = EC_Read()*50; HIDDial[2] = DialRotation & 0x00ff; HIDDial[3] = ((DialRotation & 0xff00) >> 8); Enp1IntIn();