我又修改了以下代码,顺时针滑动发送clockwise,逆时针滑动发送countclockwise
下面是程序://******************************************************************************
// RO_PINOSC_TA0_WDTp_Wheel_Buttons example
// Touch the five wheel buttons to turn on/off the center button LED
// RO method capactiance measurement using PinOsc IO, TimerA0, and WDT+
//
// Schematic Description:
//
// MSP430G2452
// +---------------+
// |
// C--------|P2.4
// C----------|P2.1
// C------|P2.3
// C--------|P2.2
// |
// C----------|P2.5
// |
// C----------|P2.0
//
// The WDT+ interval represents the measurement window. The number of
// counts within the TA0R that have accumulated during the measurement
// window represents the capacitance of the element. This is lowest
// power option with either LPM3 (ACLK WDTp source) or LPM0 (SMCLK WDTp
// source).
//
//******************************************************************************
#include "CTS_Layer.h"
// Uncomment to have this compiler directive run characterization functions only
// Comment to have this compiler directive run example application
//#define ELEMENT_CHARACTERIZATION_MODE
#define DELAY 1000 // Timer delay timeout count, 5000*0.1msec = 500 msec
struct Element * keyPressed; // Pointer to the Element structure
#ifdef ELEMENT_CHARACTERIZATION_MODE
// Delta Counts returned from the API function for the sensor during characterization
unsigned int wheelCnt[5]; // Becuase the Wheel is composed of five elements
#endif
// Sleep Function
// Configures Timer A to run off ACLK, count in UP mode, places the CPU in LPM3
// and enables the interrupt vector to jump to ISR upon timeout
void sleep(unsigned int time)
{
TA0CCR0 = time;
TA0CTL = TASSEL_1+MC_1+TACLR;
TA0CCTL0 &= ~CCIFG;
TA0CCTL0 |= CCIE;
__bis_SR_register(LPM3_bits+GIE);
__no_operation();
}
/*void sleep()
{
WDTCTL=WDT_ADLY_250;
IE1 |= WDTIE;
__bis_SR_register(LPM3_bits+GIE);
__no_operation();
}*/
void time1()
{
P1DIR|=BIT6;
P1SEL|=BIT6;
TA1CTL = TASSEL_1+MC_1;
TA1CCTL1=OUTMOD_3;
TA1CCR1=100;
}
void delay(unsigned char x)
{
unsigned char a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void uart_init()
{
DCOCTL=CALDCO_1MHZ; //将DCO设为1mhz
BCSCTL1=CALBC1_1MHZ;
UCA0CTL1|=UCSSEL_2;
UCA0BR0=13; //波特率为9600
UCA0BR1=0;
UCA0MCTL=UCBRS0;
P1SEL|=BIT1+BIT2; //将P1.1 P1.2设为第二功能
P1SEL2 = BIT1 + BIT2;
UCA0CTL1&=~UCSWRST;
}
void send(unsigned char c)
{
uart_init();
//delay(100);
UCA0TXBUF=c;
while(!(IFG2&UCA0TXIFG));
IFG2&=~UCA0TXIFG;
}
void sendstring(unsigned char *str)
{
unsigned char i=0;
while(str[i])
{
send(str[i]);
i++;
}
}
// Main Function
void main(void)
{
unsigned char fu1,fu2,fd1,fd2,fl1,fl2,fr1,fr2,cw,ccw;
//unsigned char a,b,c,d;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1, 8, 12 or 16MHz
DCOCTL = CALDCO_1MHZ;
BCSCTL1 |= DIVA_0; // ACLK/1 [ACLK/(0:1,1:2,2:4,3:8)]
BCSCTL2 |= DIVS_3; // SMCLK/8 [SMCLK/(0:1,1:2,2:4,3:8)]
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO Clock Source
P1OUT = 0x00; // Drive all Port 1 pins low
P1DIR = 0xFF; // Configure all Port 1 pins outputs
P2SEL &= ~(BIT6 + BIT7); // Configure XIN (P2.6) and XOUT (P2.7) to GPIO
P2OUT = 0x00; // Drive all Port 2 pins low
P2DIR = 0xFF; // Configure all Port 2 pins outputs
// time1();
// Initialize Baseline measurement
TI_CAPT_Init_Baseline(&wheel_buttons);
// Update baseline measurement (Average 5 measurements)
TI_CAPT_Update_Baseline(&wheel_buttons,5);
// Main loop starts here
while (1)
{
#ifdef ELEMENT_CHARACTERIZATION_MODE
// Get the raw delta counts for element characterization
TI_CAPT_Custom(&wheel_buttons,wheelCnt);
__no_operation(); // Set breakpoint here
#endif
#ifndef ELEMENT_CHARACTERIZATION_MODE
// Return the pointer to the element which has been touched
keyPressed = (struct Element *)TI_CAPT_Buttons(&wheel_buttons);
// If a button has been touched, then take some action
if(keyPressed)
{
// Up Element
if(keyPressed == &up_element) //如果上键按下
{
P1OUT |= BIT0;
fu1=1;
if((ccw==0)&&((fl1==1)||(fl2==1))) //如果不是逆时针状态,且左键被按下过
{
cw=1; //说明是顺时针状态
fu2=1; //且此时是手指滑动状态,将上键的滑动状态置1
fu1=0; //将第一次按键的状态清0,这一句可不要
}
if((cw==0)&&((fr1==1)||(fr2==1)))//如果不是顺时针状态,且右键被按下过
{
ccw=1; //说明是逆时针状态
fu2=1; //且此时是手指滑动状态,将上键的滑动状态置1
fu1=0; //将第一次按键的状态清0,这一句可不要
}
}
// Down Element
if(keyPressed == &down_element)
{
P1OUT |= BIT0;
fd1=1;
if((ccw==0)&&((fr1==1)||(fr2==1)))
{
cw=1;
fd2=1;
fd1=0;
}
if((cw==0)&&((fl1==1)||(fl2==1)))
{
ccw=1;
fd2=1;
fd1=0;
}
}
// Left Element
if(keyPressed == &left_element)
{
P1OUT |= BIT0;
fl1=1;
if((ccw==0)&&((fd1==1)||(fd2==1)))
{
cw=1;
fl2=1;
fl1=0;
}
if((cw==0)&&((fu1==1)||(fu2==1)))
{
ccw=1;
fl2=1;
fl1=0;
}
}
// Right Element
if(keyPressed == &right_element)
{
P1OUT |= BIT0;
fr1=1;
if((ccw==0)&&((fu1==1)||(fu2==1)))
{
cw=1;
fr2=1;
fr1=0;
}
if((cw==0)&&((fd1==1)||(fd2==1)))
{
ccw=1;
fr2=1;
fr1=0;
}
}
// Middle Element
if(keyPressed == &middle_element)
{
P1OUT |= BIT0; // Turn on center LED
sendstring("middle\r\n");
}
if(cw&&(fu2||fr2||fd2||fl2))
sendstring("clockwise\r\n");
if(ccw&&(fu2||fr2||fd2||fl2))
sendstring("counterclockwise\r\n");
}
else
{
P1OUT&=~(BIT0); // Turn off center LED
if((fu1==1)&&(cw==0)&&(ccw==0))
sendstring("up\r\n");
if((fd1==1)&&(cw==0)&&(ccw==0))
sendstring("down\r\n");
if((fl1==1)&&(cw==0)&&(ccw==0))
sendstring("left\r\n");
if((fr1==1)&&(cw==0)&&(ccw==0))
sendstring("right\r\n");
fu2=fr2=fd2=fl2=0;
fu1=fr1=fd1=fl1=0;
cw=ccw=0;
}
// Put the MSP430 into LPM3 for a certain DELAY period
sleep(DELAY);
#endif
}
} // End Main
/******************************************************************************/
// Timer0_A0 Interrupt Service Routine: Disables the timer and exists LPM3
/******************************************************************************/
#pragma vector=TIMER0_A0_VECTOR
__interrupt void ISR_Timer0_A0(void)
{
TA0CTL &= ~(MC_1);
TA0CCTL0 &= ~(CCIE);
__bic_SR_register_on_exit(LPM3_bits+GIE);
} |