⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wm430_wheel.c

📁 德州仪器生产的MSP430单片机的一种应用
💻 C
字号:
//*******************************************************************************
//   TI/Agilent Wireless Mouse Reference Design
//
//   File: "wm430_wheel.c"
//
//   Description: Implements the logic to detect scroll wheel movement
//
//  Author: Randy Wu
//  Company: Texas Instruments, Inc.
//  Date: May 2006
//  IDE: Built with IAR Embedded Workbench Version 3.41A
//
// THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
// REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
// INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
// COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
// TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
// POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
// INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
// YOUR USE OF THE PROGRAM.
//
// IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
// THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
// OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
// EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
// REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
// OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
// USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
// AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
// YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
// (U.S.$500).
//
// Unless otherwise stated, the Program written and copyrighted
// by Texas Instruments is distributed as "freeware".  You may,
// only under TI's copyright in the Program, use and modify the
// Program without any charge or restriction.  You may
// distribute to third parties, provided that you transfer a
// copy of this license to the third party and the third party
// agrees to these terms by its first use of the Program. You
// must reproduce the copyright notice and any other legend of
// ownership on each copy or partial copy, of the Program.
//
// You acknowledge and agree that the Program contains
// copyrighted material, trade secrets and other TI proprietary
// information and is protected by copyright laws,
// international copyright treaties, and trade secret laws, as
// well as other intellectual property laws.  To protect TI's
// rights in the Program, you agree not to decompile, reverse
// engineer, disassemble or otherwise translate any object code
// versions of the Program to a human-readable form.  You agree
// that in no event will you alter, remove or destroy any
// copyright notice included in the Program.  TI reserves all
// rights not specifically granted under this license. Except
// as specifically provided herein, nothing in this agreement
// shall be construed as conferring by implication, estoppel,
// or otherwise, upon you, any license or other right under any
// TI patents, copyrights or trade secrets.
//
// You may not use the Program in non-TI devices.
//*******************************************************************************
#include "wm430_wheel.h"

unsigned char WM430_WHEEL_currQuadState;    // Holds current quadrature state
unsigned char WM430_WHEEL_prevQuadState;    // Holds previous quadrature state
                                            // Stores overall Z displacement
signed char   WM430_WHEEL_currZDisplacement;
unsigned int  WM430_WHEEL_timeout = 0;      // Keeps track of when SCROLL WHEEL detection times out


void WM430_WHEEL_delayOperation(void)
{
  WM430_SYS_delaymsec(1);                   // Delay for exactly 1 millisecond
}

void WM430_WHEEL_disableZENC(void)
{
  P2OUT &= ~ZWHEEL_EN;                      // Z-Encoder is deactive LOW
}

void WM430_WHEEL_enableZENC(void)
{
  P2OUT |= ZWHEEL_EN;                       // Z-Encoder is active HIGH
  WM430_WHEEL_delayOperation();             // Give Z-Encoder chip time to become fully lit before reading
}

void WM430_WHEEL_disableZLED(void)
{
  P2OUT |= ZWHEEL_EN;                       // Z-Encoder LED is active LOW
}

void WM430_WHEEL_enableZLED(void)
{
  P2OUT &= ~ZWHEEL_EN;                      // Z-Encoder LED is active LOW
  WM430_WHEEL_delayOperation();             // Give Z-Encoder LED time to become fully lit before reading
}

void WM430_WHEEL_resetActiveTimeout(void)
{
                                            // Reset counter to maximum (initial) value
  WM430_SYS_timeout = WM430_WHEEL_TIMEOUTMSECS;
}

void WM430_WHEEL_init(void)
{
  P2SEL &= ~ZWHEEL_EN;                      // Select GPIO function(s)
  P2DIR |= ZWHEEL_EN;                       // Configure as output(s)

  P2SEL &= ~(ZENCODER_QA + ZENCODER_QB);    // Select GPIO function(s)
  P2DIR &= ~(ZENCODER_QA + ZENCODER_QB);    // Configure as input(s)
}

void WM430_WHEEL_decodeQuadState(void)
                                            // Scroll Wheel   UP: decrement counter (S2 -> S0 -> S1 -> S3 -> ..)
                                            // Scroll Wheel DOWN: increment counter (S3 -> S1 -> S0 -> S2 -> ..)
{
  unsigned char direction = WM430_WHEEL_NONE;

                                            // Check to see if states have NOT changed
  if (WM430_WHEEL_prevQuadState == WM430_WHEEL_currQuadState) {
#ifdef _BANDAID
    WM430_SYS_bandaid();
#endif /* _BANDAID */
    return;
  }
  else {                                    // State change detected
    WM430_SYS_resetActiveTimeout();         // Hold off on going to SLEEP too soon
    switch(WM430_WHEEL_currQuadState) {     // Decode current quadrature state based on previous
      case WM430_WHEEL_QUADSTATE0:          // Quadrature State 0
        switch(WM430_WHEEL_prevQuadState) {
          case WM430_WHEEL_QUADSTATE2: direction = WM430_WHEEL_UP; break;
          case WM430_WHEEL_QUADSTATE1: direction = WM430_WHEEL_DOWN; break;
          default: break;
        }
      break;
      case WM430_WHEEL_QUADSTATE1:          // Quadrature State 1
        switch(WM430_WHEEL_prevQuadState) {
          case WM430_WHEEL_QUADSTATE0: direction = WM430_WHEEL_UP; break;
          case WM430_WHEEL_QUADSTATE3: direction = WM430_WHEEL_DOWN; break;
          default: break;
        }
      break;
      case WM430_WHEEL_QUADSTATE2:          // Quadrature State 2
        switch(WM430_WHEEL_prevQuadState) {
          case WM430_WHEEL_QUADSTATE3: direction = WM430_WHEEL_UP; break;
          case WM430_WHEEL_QUADSTATE0: direction = WM430_WHEEL_DOWN; break;
          default: break;
        }
      break;
      case WM430_WHEEL_QUADSTATE3:          // Quadrature State 3
        switch(WM430_WHEEL_prevQuadState) {
          case WM430_WHEEL_QUADSTATE1: direction = WM430_WHEEL_UP; break;
          case WM430_WHEEL_QUADSTATE2: direction = WM430_WHEEL_DOWN; break;
          default: break;
        }
      break;
      default: break;
    }

    if (direction) {                        // Update state only if valid transition
      switch (direction) {
        case WM430_WHEEL_DOWN:              // Direction is changing downwards
          if (WM430_WHEEL_currZDisplacement == WM430_SYS_08BITSIGNEDCHARMINVALUE)
                                            // Reset Z displacement
            WM430_WHEEL_currZDisplacement = 0;
          else
                                            // Decrement Z displacement
            WM430_WHEEL_currZDisplacement--;
        break;
        case WM430_WHEEL_UP:                // Direction is changing upwards
          if (WM430_WHEEL_currZDisplacement == WM430_SYS_08BITSIGNEDCHARMAXVALUE)
                                            // Reset Z displacement
            WM430_WHEEL_currZDisplacement = 0;
          else
                                            // Increment Z displacement
            WM430_WHEEL_currZDisplacement++;
        break;
        default: break;
      }
    }
  }
                                            // Save current quadrature state
  WM430_WHEEL_prevQuadState = WM430_WHEEL_currQuadState;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -