//******************************************************************************
// MSP430G2xx3 Demo - Capacitive Touch, Pin Oscillator Method, 5 buttons, UART
//
// Description: Basic 8-button input using the built-in pin oscillation feature
// on GPIO input structure. PinOsc signal feed into TA0CLK. WDT interval is used
// to gate the measurements. Difference in measurements indicate button touch.
// Pins used for inputs listed in the diagram below.
// After each scan, one UART byte identifying the key# being pressed is
// transmitted via TimerA UART at port pin P1.1.
//
//
// ACLK = VLO = 12kHz, MCLK = SMCLK = 1MHz DCO
//
// MSP430G2xx3
// -----------------
// /|\| |
// | | |
// --|RST |
// | |
// input 1-->|P2.0 P1.1|--> TA UART output TXD
// | |
// input 2-->|P2.1 |
// | |
// input 3-->|P2.2 |
// | |
// input 4-->|P2.3 |
// | |
// input 5-->|P2.4 |
// | |
// ----------------- All five inputs are capacitive touch inputs
//
// Brandon Elliott/D. Dang
// Texas Instruments Inc.
// November 2010
// Built with IAR Embedded Workbench Version: 5.10
//******************************************************************************
#include "msp430g2553.h"
/* Define User Configuration values */
/*----------------------------------*/
/* Defines WDT SMCLK interval for sensor measurements*/
#define WDT_meas_setting (DIV_SMCLK_512)
/* Defines WDT ACLK interval for delay between measurement cycles*/
#define WDT_delay_setting (DIV_ACLK_512)
/* Sensor settings*/
#define NUM_SEN 5 // Total number of sensors
#define KEY_LVL 190 // Defines threshold for a key press
/* Set to ~ half the max delta expected*/
/* Definitions for use with the WDT settings*/
#define DIV_ACLK_32768 (WDT_ADLY_1000) // ACLK/32768
#define DIV_ACLK_8192 (WDT_ADLY_250) // ACLK/8192
#define DIV_ACLK_512 (WDT_ADLY_16) // ACLK/512
#define DIV_ACLK_64 (WDT_ADLY_1_9) // ACLK/64
#define DIV_SMCLK_32768 (WDT_MDLY_32) // SMCLK/32768
#define DIV_SMCLK_8192 (WDT_MDLY_8) // SMCLK/8192
#define DIV_SMCLK_512 (WDT_MDLY_0_5) // SMCLK/512
#define DIV_SMCLK_64 (WDT_MDLY_0_064) // SMCLK/64
/* Global variables for sensing*/
unsigned int base_cnt[NUM_SEN];
unsigned int meas_cnt[NUM_SEN];
int delta_cnt[NUM_SEN];
unsigned char key_press[NUM_SEN];
char key_pressed;
int cycles;
const unsigned char electrode_bit_P2[NUM_SEN]={BIT0, BIT1, BIT2, BIT3, BIT4};
/* System Routines*/
void measure_count(void); // Measures each capacitive sensor
void TX_Byte (char); // Transmits key pressed via UART
/* Main Function*/
void main(void)
{
unsigned int i,j;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO
IE1 |= WDTIE; // enable WDT interrupt
P2SEL = 0x00; // No XTAL
__bis_SR_register(GIE); // Enable interrupts
measure_count(); // Establish baseline capacitance
for (i = 0; i<NUM_SEN; i++)
base_cnt[i] = meas_cnt[i];
for(i=15; i>0; i--) // Repeat and avg base measurement
{
measure_count();
for (j = 0; j<NUM_SEN; j++)
base_cnt[j] = (meas_cnt[j]+base_cnt[j])/2;
}
/* Main loop starts here*/
while (1)
{
j = KEY_LVL;
measure_count(); // Measure all sensors
for (i = 0; i<NUM_SEN; i++)
{
/* Handle baseline measurment for a base C decrease*/
if (base_cnt[i] < meas_cnt[i]) // If negative: result increased
{ // beyond baseline, cap decreased
base_cnt[i] = (base_cnt[i]+meas_cnt[i]) >> 1; // Re-average up quickly
delta_cnt[i] = 0; // Zero out for position determination
}
else
{
delta_cnt[i] = base_cnt[i] - meas_cnt[i]; // Calculate delta: c_change
}
if (delta_cnt[i] > j) // Determine if each key is pressed
{ // per a preset threshold
key_press[i] = 1; // Specific key pressed
j = delta_cnt[i];
key_pressed = i+1; // key pressed
}
else
key_press[i] = 0;
}
/* Delay to next sample, sample more slowly if no keys are pressed*/
if (key_pressed)
{
TX_Byte(key_pressed);
BCSCTL1 = (BCSCTL1 & 0x0CF) + DIVA_0; // ACLK/(0:1,1:2,2:4,3:8)
cycles = 20;
}
else
{
cycles--;
if (cycles > 0) // ACLK/(0:1,1:2,2:4,3:8)
BCSCTL1 = (BCSCTL1 & 0x0CF) + DIVA_0;
else
{ // ACLK/(0:1,1:2,2:4,3:8)
BCSCTL1 = (BCSCTL1 & 0x0CF) + DIVA_3;
cycles = 0;
}
}
WDTCTL = WDT_delay_setting; // WDT, ACLK, interval timer
/* Handle baseline measurment for a base C increase*/
if (!key_pressed) // adjust down if no keys touched
{
for (i = 0; i<NUM_SEN; i++)
base_cnt[i] = base_cnt[i] - 1; // Adjust baseline down, should be
} // slow to accomodate for genuine
__bis_SR_register(LPM3_bits); // changes in sensor C
}
} // End Main
/* Measure count result (capacitance) of each sensor*/
/* Routine setup for four sensors, not dependent on NUM_SEN value!*/
void measure_count(void)
{
char i;
TA0CTL = TASSEL_3+MC_2; // TACLK, cont mode
TA0CCTL1 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap
for (i = 0; i<NUM_SEN; i++)
{
/*Configure Ports for relaxation oscillator*/
/*The P2SEL2 register allows Timer_A to receive it's clock from a GPIO*/
/*See the Application Information section of the device datasheet for info*/
P2DIR &= ~ electrode_bit_P2[i];
P2SEL &= ~ electrode_bit_P2[i];
P2SEL2 |= electrode_bit_P2[i]; // input oscillation feeds TACLK
/* Setup Gate Timer */
WDTCTL = WDT_meas_setting; // WDT, ACLK, interval timer
TA0CTL |= TACLR; // Clear Timer_A TAR
__bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt
TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1
meas_cnt[i] = TACCR1; // Save result
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P2SEL2 &= ~electrode_bit_P2[i];
}
TA0CTL = 0; // Stop Timer_A
}
/* Watchdog Timer interrupt service routine*/
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{
TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1
__bic_SR_register_on_exit(LPM3_bits); // Exit LPM3 on reti
}
/* UART Information: Conditions for 9600 Baud SW TX-only UART, SMCLK = 8MHz */
#define Bitime 0x0341 // x us bit length ~ x baud
#define TXD BIT1 // TXD on P1.1
unsigned int RXTXData;
unsigned char BitCnt;
void TX_Byte (char TX_DATA)
{
BCSCTL1 = CALBC1_8MHZ; // Set DCO to 8MHz
DCOCTL = CALDCO_8MHZ;
TX_DATA += '0';
没有合适的资源?快使用搜索试试~ 我知道了~
LaunchPad例程源代码
共87个文件
c:87个
需积分: 11 17 下载量 51 浏览量
2015-04-02
22:12:22
上传
评论 1
收藏 105KB RAR 举报
温馨提示
里面包含了大量的MSP430单片机实例源代码,拥有实例代码直接看一下datasheet就能重用代码,提高开发进度
资源推荐
资源详情
资源评论
收起资源包目录
LaunchPad例程源代码.rar (87个子文件)
LaunchPad例程源代码
msp430g2xx3_pinosc_04.c 10KB
msp430g2xx3_flashwrite_01.c 3KB
msp430g2xx3_uscib0_i2c_02.c 3KB
msp430g2xx3_pinosc_01.c 7KB
msp430g2xx3_uscib0_i2c_09.c 4KB
msp430g2xx3_wdt_04.c 1KB
msp430g2xx3_uscib0_i2c_10.c 4KB
msp430g2x33_adc10_04.c 2KB
msp430g2x33_adc10_06.c 2KB
msp430g2xx3_P1_04.c 2KB
msp430g2xx3_uscia0_uart_01_115k.c 2KB
msp430g2xx3_P1_02.c 2KB
msp430g2xx3_1.c 1KB
msp430g2xx3_uscia0_irda_01.c 4KB
msp430g2xx3_ta_07.c 3KB
msp430g2x33_adc10_temp.c 3KB
msp430g2xx3_uscia0_uart_01_115k_lpm.c 3KB
msp430g2xx3_ta_08.c 3KB
msp430g2xx3_uscib0_i2c_07.c 2KB
msp430g2xx3_dco_flashcal.c 5KB
msp430g2xx3_pinosc_02.c 8KB
msp430g2xx3_uscia0_irda_03.c 8KB
msp430g2xx3_ta_10.c 1KB
msp430g2xx3_uscib0_i2c_01.c 4KB
msp430g2x33_adc10_08.c 3KB
msp430g2xx3_uscia0_uart_01_19200.c 2KB
msp430g2xx3_wdt_05.c 2KB
msp430g2x33_adc10_03.c 3KB
msp430g2xx3_lpm3.c 2KB
msp430g2xx3_uscib0_i2c_13.c 5KB
msp430g2x33_adc10_10.c 2KB
msp430g2xx3_uscia0_uart_06_9600.c 3KB
msp430g2xx3_uscib0_i2c_12.c 6KB
msp430g2x33_adc10_13.c 2KB
msp430g2xx3_ta_uart9600.c 9KB
msp430g2x13_ca_01.c 2KB
msp430g2xx3_ta_13.c 1KB
msp430g2xx3_wdt_02.c 2KB
msp430g2x33_adc10_07.c 2KB
msp430g2xx3_ta_03.c 2KB
msp430g2x13_ca_03.c 2KB
msp430g2xx3_uscib0_i2c_08.c 4KB
msp430g2x33_adc10_02.c 3KB
msp430g2xx3_ta_14.c 2KB
msp430g2xx3_ta_02.c 1KB
msp430g2x33_adc10_14.c 2KB
msp430g2xx3_P1_03.c 1KB
msp430g2x33_adc10_11.c 3KB
msp430g2xx3_uscib0_i2c_05.c 3KB
msp430g2xx3_clks.c 1KB
msp430g2xx3_uscia0_uart_01_9600.c 2KB
msp430g2xx3_P1_01.c 1KB
msp430g2xx3_ta_19.c 1KB
msp430g2x33_adc10_12.c 3KB
msp430g2xx3_wdt_01.c 1KB
msp430g2x33_adc10_16.c 2KB
msp430g2xx3_ta_17.c 2KB
msp430g2xx3_pinosc_03.c 8KB
msp430g2xx3_dco_calib.c 2KB
msp430g2xx3_LFxtal_nmi.c 2KB
msp430g2xx3_ta_11.c 2KB
msp430g2xx3_nmi.c 2KB
msp430g2xx3_ta_06.c 2KB
msp430g2xx3_uscia0_uart_07_9600.c 3KB
msp430g2xx3_wdt_07.c 2KB
msp430g2xx3_uscib0_i2c_06.c 3KB
msp430g2xx3_uscia0_irda_02.c 6KB
msp430g2xx3_ta_16.c 1KB
msp430g2xx3_uscib0_i2c_04.c 3KB
msp430g2xx3_ta_uart2400.c 5KB
msp430g2xx3_uscib0_i2c_11.c 4KB
msp430g2x33_adc10_05.c 2KB
msp430g2xx3_uscia0_uart_05_9600.c 2KB
msp430g2xx3_wdt_06.c 1KB
msp430g2x33_adc10_01.c 2KB
msp430g2xx3_ta_04.c 2KB
msp430g2xx3_uscia0_uart_08_9600.c 3KB
msp430g2xx3_ta_05.c 2KB
msp430g2xx3_uscia0_spi_10.c 2KB
msp430g2x33_adc10_09.c 3KB
msp430g2xx3_lpm3_vlo.c 2KB
msp430g2xx3_1_vlo.c 2KB
msp430g2xx3_uscib0_i2c_03.c 4KB
msp430g2xx3_uscia0_spi_09.c 4KB
msp430g2xx3_ta_01.c 2KB
msp430g2xx3_ta_20.c 2KB
msp430g2x13_ca_02.c 2KB
共 87 条
- 1
资源评论
qq_20457325
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- unity开发教程.docx
- 代码使用Pygame库实现了一个简单的烟花模拟 核心逻辑包括烟花和粒子类的定义,处理位置、爆炸、尾迹和绘制等操作
- Matlab Simulink 电力电子仿真-Flyback(反激电路)电路分析
- tudou-android-release.apk
- 数据分析教程.docx
- 基于matlab实现用有限元法计算电磁场的Matlab工具 .rar
- 基于matlab实现有限元算法 计算电磁场问题 边界条件包括第一类边界和第二类边界.rar
- 基于matlab实现用于计算不同车重下的电动汽车动力性和经济性.rar
- 基于matlab实现遗传算法求解多车场车辆路径问题 有多组算例可以用.rar
- 浏览器.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功