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

📄 exercise-2-begin.c

📁 TI MSP430针对cap sense 按键的源码, 可实现单击或滑动键, 如IPOD上的人机接口.
💻 C
字号:
/* Exercise 2 - starting point

    A touch keypad demo for the F4270, constructed from elements of examples:
        fet110_ca_04.c
        fet110_P2_int.c
*/
//              The key pad is constructed from a pcb pad surrounded by ground, which also has a ground
//              plane on the underside of the PCB. The ground plane increases the capacitance.
//              A high value resistance charges and discharges the capacitive key. The discharge
//              time is monitored. As a finger approaches the key's surface the capacitance, and hence
//              the discharge time, increases. This is used to detect whether the key is pressed or not.
//
//              This software use the I/O pin interrupts, available on ports 1 and 2 of any MSP430,
//              to read the TAR. As there is a max of 16 I/O lines with interrupt capability, up to
//              16 keys can be supported. In this demo there are 4 keys.
//
//              It should be noted that this technique is using digital input pins to monitor an analogue
//              signal. If the signal remained in the linear band of the I/O pin for a significant time there
//              would be signficant current consumption (typically this seems to be about 120uA per pin on an
//              MSP430). The charge and discharge is quick, so the signal spends very little time in the
//              linear region. It is important to have low leakage I/O pins for this to work. The charge
//              and discharge current is about 300nA. The I/O pins on many MCUs have too much leakage for this
//              to work. They would require a much higher charge current, with a consequent reduction in the
//              discharge time. This would make if difficult to measure the time well by software methods.
//              The MSP430 pin leakage is only 50nA, so a 300nA discharge current produces a workable discharge
//              time.
//
//               MSP430F4270
//            -----------------
//       /|\ |              XIN|-
//        |  |                 |
//        ---|RST          XOUT|-
//           |                 |
//     +-5M--|P6.7             |
//     |     |                 |	
//     |     |                 |
//     |     |                 |
//     +-----|P1.1         P1.0|-->LED
//     |     |                 |
//    ===pad |                 |
//     |     |                 |			
//     ------|Vss
//
//
//      Pad construction. (other side is ground plane to increase capacitance)
//
//      GND GND GND GND GND GND GND GND
//      GND                         GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD
//      GND     PAD PAD PAD PAD PAD PAD PAD PAD PAD -> to I/O
//      GND     PAD PAD PAD PAD
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND     PAD PAD PAD PAD     GND
//      GND                         GND
//      GND GND GND GND GND GND GND GND
//
#include <msp430x42x0.h>

#define REF     BIT7                          // P6.7 = Reference
#define SENSOR  BIT1                          // P2.1 = Sensor

unsigned int measure(int);                    // measures the time to discharge the capacitor
void init_lcd(void);

unsigned int snapshot,sensor_time;

void main (void)
{

    WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer

    /* Set up the sensor pin to interrupt */
    P1IE |= SENSOR;                           // P1.1 interrupt enabled
    P1IES |= SENSOR;                          // P1.1 hi/lo edge
    P1IFG &= ~SENSOR;                         // P1.1 IFG cleared

    /* Set up the charging reference pin */
    P6OUT &= ~REF;                            // Ref set
    P6DIR |= REF;                             // Ref output
    /* Set up the LED pin */
    P1DIR |= BIT0;
    P1OUT &= ~BIT0;
    TACTL = TASSEL_2 | MC_2;                  // SMCLK, clear TAR
    _EINT();                                  // Enable interrupts

    for (;;)
    {
        sensor_time = measure(SENSOR);        // Measure discharge time through sensor
        _NOP();
    }
}

void init_lcd(void)
{
    int j;

    /* Basic timer setup */
    /* Set ticker to 32768/(256*256) */
    BTCTL = BT_fLCD_DIV64 | BT_fCLK2_DIV128 | BT_fCLK2_ACLK_DIV256;

    for (j = 0;  j < 20;  j++)
        LCDMEM[j] = 0;

    /* Turn on the COM0-COM3 and R03-R33 pins */
    P5SEL |= (BIT4 | BIT3 | BIT2);
    P5DIR |= (BIT4 | BIT3 | BIT2);

    /* LCD-A controller setup */
    LCDACTL = LCDFREQ_128 | LCD4MUX | LCDSON | LCDON;
    LCDAPCTL0 = LCDS0 | LCDS4 | LCDS8 | LCDS12;
    LCDAPCTL1 = 0;
    LCDAVCTL0 = LCDCPEN;
    LCDAVCTL1 = 1 << 1;
}

unsigned int measure(int source)
{
    unsigned int timer_count;

    P6OUT |= REF;                             // Ref set
    P6DIR |= REF;                             // Ref output
    P1OUT |= SENSOR;                          // Help to charge quickly
    P1DIR |= SENSOR;
    TACCR1 = TAR + 5000;                      // TACCR1 = TAR + 5ms
    TACCTL1 = CCIE;                           // Comp, interrupt
    LPM0;                                     // Wait for TACCR1 interrupt
    TACCTL1 = 0;                              // Disable
    P1DIR &= ~SENSOR;                         // Stop driving the sensor
    P6OUT &= ~REF;                            // Discharge through the large resistor
    timer_count = TAR;                        // TAR before discharge
    LPM0;                                     // Wait for port1 interrupt
    timer_count = snapshot - timer_count;     // timer_count = discharge time
    P6DIR &= ~REF;                            // Disable Sensor or Ref
    return (timer_count);
}

// Timer A1 interrupt service routine
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
    switch (TAIV)
    {
    case  2:
        LPM0_EXIT;                            // TACCR1
        break;
    }
}

// Port 2 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void port_1(void)
{
    P1IFG = 0;
    snapshot = TAR;
    LPM0_EXIT;
}

⌨️ 快捷键说明

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