📄 fg439_magic_keys.c
字号:
//******************************************************************************
// MSP430xG439 capacitive key pad demo
//
// Description: This codes uses I/O pin interrupts and Timer_A to construct a contactless key pad.
//
// 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.
//
// MSP430xG43x MCU STK/EVK 6.5 digit 4 mux LCD
// #T218010
// ----------------- --------------
// | COM3 |-----|2 COM4 |
// | COM2 |-----|1 COM3 |
// | COM1 |-----|3 COM2 |
// | COM0 |-----|4,20 COM1 |
// | SEG0 |-----|19 |
// | SEG1 |-----|18 |
// | SEG2 |-----|17 |
// | SEG3 |-----|16 |
// | SEG4 |-----|15 |
// | SEG5 |-----|14 |
// | SEG6 |-----|13 |
// | SEG7 |-----|12 |
// | SEG8 |-----|11 |
// | SEG9 |-----|10 |
// | SEG10|-----|9 |
// | SEG11|-----|8 |
// | SEG12|-----|7 |
// | SEG13|-----|6 |
// | SEG14|-----|5 (bits C,E,H |
// | | | of digit 7)|
// | | --------------
// | |
// /|\ | XIN|-
// | | | 32768Hz
// ---|RST XOUT|-
// | |
// +-----|P3.0 ------------|----+-------+-------+
// | | | | | |
// 6M | | 6M 6M 6M
// | | | | | |
// +-----|P1.0 P1.1 |----+ | |
// | | | | | |
// | | P1.2 |------------+ |
// | | | | | |
// | | P1.3 |--------------------+
// | | | | | |
// ===~5pF| | ===~5pF ===~5pF ===~5pF
// | | | | | |
// ------|Vss |----+-------+-------+
// -----------------
//
// Pad construction. (other side is ground plane to increase capacitor)
//
// 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
//
//
// Vincent Chan and Steve Underwood
// Texas Instruments Hong Kong Ltd.
// July 2005
//
//******************************************************************************
#include <msp430xG43x.h>
// This structure defines the static data to maintain one key
typedef struct
{
unsigned char port;
unsigned char port_bit;
} key_config_data_t;
// This structure defines the working data to maintain one key
typedef struct
{
unsigned int acc_ref_time;
unsigned int acc_ref_time_counter;
unsigned int base_capacitance;
} key_data_t;
// This structure defines the application data for one key
typedef struct
{
unsigned char key_down;
unsigned int data;
} key_app_data_t;
// This definition allows the number of keys to be set
#define NUM_KEYS 4
#define KEY_CHARGE_BIT BIT1 // P3.0 charges the cap.
#define KEY_CHARGE_DIR P3DIR // P3.0 charges the cap.
#define KEY_CHARGE_OUT P3OUT // P3.0 charges the cap.
const key_config_data_t key_config[NUM_KEYS] =
{
{1, BIT1},
{1, BIT2},
{1, BIT3},
{1, BIT4}
};
unsigned int timer_count;
unsigned char active_key;
key_data_t key[NUM_KEYS];
key_app_data_t key_app[NUM_KEYS];
#define FET_TARGET_BOARD 0
// There seems to be two types of LCD glass, with different segment layouts
#if FET_TARGET_BOARD
#define SEG_a 0x01
#define SEG_b 0x02
#define SEG_c 0x10
#define SEG_d 0x08
#define SEG_e 0x40
#define SEG_f 0x20
#define SEG_g 0x04
#define SEG_h 0x80
#else
#define SEG_a 0x01
#define SEG_b 0x02
#define SEG_c 0x10
#define SEG_d 0x04
#define SEG_e 0x80
#define SEG_f 0x20
#define SEG_g 0x08
#define SEG_h 0x40
#endif
//By changing the about definitions appropriately, the follow
//definitions are generic, and may be used for most byte
//addressed displays.
//First blank, and the hex codes, which all display quite
//well on a 7-segment display.
#define CHAR_SPACE 0
#define CHAR_ALL (SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f|SEG_g|SEG_h)
#define CHAR_0 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f)
#define CHAR_1 (SEG_b|SEG_c)
#define CHAR_2 (SEG_a|SEG_b|SEG_d|SEG_e|SEG_g)
#define CHAR_3 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_g)
#define CHAR_4 (SEG_b|SEG_c|SEG_f|SEG_g)
#define CHAR_5 (SEG_a|SEG_c|SEG_d|SEG_f|SEG_g)
#define CHAR_6 (SEG_a|SEG_c|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_7 (SEG_a|SEG_b|SEG_c)
#define CHAR_8 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_9 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_f|SEG_g)
#define CHAR_A (SEG_a|SEG_b|SEG_c|SEG_e|SEG_f|SEG_g)
#define CHAR_B (SEG_c|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_C (SEG_a|SEG_d|SEG_e|SEG_f)
#define CHAR_D (SEG_b|SEG_c|SEG_d|SEG_e|SEG_g)
#define CHAR_E (SEG_a|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_F (SEG_a|SEG_e|SEG_f|SEG_g)
#define CHAR_n (SEG_c|SEG_e|SEG_g)
char digit[10] =
{
CHAR_0, // "0" LCD segments a+b+c+d+e+f
CHAR_1, // "1"
CHAR_2, // "2"
CHAR_3, // "3"
CHAR_4, // "4"
CHAR_5, // "5"
CHAR_6, // "6"
CHAR_7, // "7"
CHAR_8, // "8"
CHAR_9 // "9"
};
unsigned int measure_key_capacitance(const key_config_data_t *key_config);
unsigned int measure_key_capacitance(const key_config_data_t *key_config)
{
unsigned int i;
unsigned int y;
active_key = key_config->port_bit;
y = 0;
for (i = 0; i < 4; i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -