messier
Ban Chủ Nhiệm
Ngày mai (19-4-2014) chúng ta sẽ tìm hiểu về Capacitive Touch với MSP430. Phòng học tại 201B1 vào lúc 14h00.
1. Các bạn cần làm module Capacitive Touch trong file đính kèm: Link
2. Slide bài học cho ngày mai: Slide .
Update source code cho bữa nay: (nhớ chọn project properties->advance option->language option->Enable gcc extension).
1. Các bạn cần làm module Capacitive Touch trong file đính kèm: Link
2. Slide bài học cho ngày mai: Slide .
Update source code cho bữa nay: (nhớ chọn project properties->advance option->language option->Enable gcc extension).
Code:
#include <msp430g2553.h>
#define WDT_meas_setting (WDT_MDLY_8) // watchdog interval = 0.5 ms
static unsigned int base_cap[4]; // Baseline capacitance for each pad
struct Element{ // This structure contains informations of sensor pad
unsigned char *Pxsel; // Pointer to Pxsel register
unsigned char *Pxsel2; // Pointer to Pxsel2 register
unsigned char *Pxdir; // Pointer to Pxdir register
unsigned char *Pxout; // Pointer to Pxout register
unsigned char inputBits; // Input bit
unsigned int *baseline; // Pointer to baseline capacitance
};
//*************************************************************************
// Define sensor pad element
//*************************************************************************
const struct Element KEY_1 = {
.Pxsel = (unsigned char *)&P2SEL,
.Pxsel2 = (unsigned char *)&P2SEL2,
.Pxdir = (unsigned char *)&P2DIR,
.inputBits = BIT0,
.baseline = &base_cap[0]
};
const struct Element KEY_2 = {
.Pxsel = (unsigned char *)&P2SEL,
.Pxsel2 = (unsigned char *)&P2SEL2,
.Pxdir = (unsigned char *)&P2DIR,
.inputBits = BIT1,
.baseline = &base_cap[1]
};
const struct Element KEY_3 = {
.Pxsel = (unsigned char *)&P2SEL,
.Pxsel2 = (unsigned char *)&P2SEL2,
.Pxdir = (unsigned char *)&P2DIR,
.inputBits = BIT2,
.baseline = &base_cap[2]
};
const struct Element KEY_4 = {
.Pxsel = (unsigned char *)&P2SEL,
.Pxsel2 = (unsigned char *)&P2SEL2,
.Pxdir = (unsigned char *)&P2DIR,
.inputBits = BIT3,
.baseline = &base_cap[3]
};
const struct Element *KEY[] = {&KEY_1, &KEY_2, &KEY_3, &KEY_4}; // Define Element pointer array
unsigned int measure_count(const struct Element *pad);
//*************************************************************************
// Program subroutines
//*************************************************************************
void base_calib(void){ // Measure the baseline capacitance each pad
char i = 3;
do{
base_cap[i] = measure_count(KEY[i]);
}while(i--);
}
//------------------------------------------------------------------------
/* Measure raw count */
unsigned int measure_count(const struct Element *pad){
int meas_cnt;
TA0CTL = TASSEL_3 + MC_2; // TACLK, continous mode
TA0CCTL1 = CM_3 + CCIS_2 + CAP; // Pos & Neg, GND, capture mode
*(pad->Pxsel) &= ~pad->inputBits; // TACLK input
*(pad->Pxsel2) |= pad->inputBits;
*(pad->Pxdir) &= ~pad->inputBits; // input pins
/*Setup Gate Timer*/
WDTCTL = WDT_meas_setting; // WDT, SMCLK, interval timer
TA0CTL |= TACLR; // Clear Timer_A TAR
_bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt
meas_cnt = TA0CCR1; // Save result
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
TA0CTL = MC_0; // Stop timer0
*(pad->Pxsel2) &= ~pad->inputBits;
return meas_cnt;
}
//------------------------------------------------------------------------
//*************************************************************************
// Main Program
//*************************************************************************
void main(void) {
volatile int delta_count;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_16MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_16MHZ;
IE1 |= WDTIE; // Enable WDT interrupt
base_calib();
while(1){
delta_count = *(KEY_2.baseline) - measure_count(&KEY_2);
if(delta_count > 15){ // If Key 2 is touched -> turned on LED 1
P1DIR |= BIT0;
P1OUT |= BIT0;
}
else P1OUT &= ~BIT0;
delta_count = *(KEY_3.baseline) - measure_count(&KEY_3);
if(delta_count > 20){ // If Key 2 is touched -> turned on LED 2
P1DIR |= BIT6;
P1OUT |= BIT6;
}
else P1OUT &= ~BIT6;
}
}
//*************************************************************************
// Interrupt service routine
//*************************************************************************
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{
TA0CCTL1 ^= CCIS0; // Toggle CM0 to initiate capture
_bic_SR_register_on_exit(LPM3_bits); // Exit LPM3 on reti
}
#pragma vector=ADC10_VECTOR,COMPARATORA_VECTOR,NMI_VECTOR,PORT1_VECTOR, PORT2_VECTOR, TIMER0_A0_VECTOR, TIMER0_A1_VECTOR, TIMER1_A0_VECTOR, TIMER1_A1_VECTOR, USCIAB0RX_VECTOR, USCIAB0TX_VECTOR
__interrupt void Trap_isr(void){
while(1){
_bis_SR_register(LPM4_bits);
}
}