📄 main.c
字号:
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
//Enable interrupt(High to Low trigger)
//Set the Active Key input , and start the timed discharge of the pad
P1IES |= Active_Key;
P1IE |= Active_Key;
P1DIR &= ~Active_Key;
//capture TAR
Timer_Count = TAR;
LPM0;
//Return the key to the driven low state, to contribute to the "ground" area
//around the next key to be scanned.
P1IE &= ~Active_Key; //disable active key interrupt
P1OUT &= ~Active_Key; //switch active key to low to discharge pad
P1DIR |= Active_Key; //switch active key to output to save power
Sum = Timer_Count;
//
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
P1OUT |= Partner_Key;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
//Enable interrupt(Low to High trigger)
//Set the Active Key input , and start the timed charge of the pad
P1IES &= ~Active_Key;
P1IE |= Active_Key;
P1DIR &= ~Active_Key;
//capture TAR
Timer_Count = TAR;
LPM0;
//Return the key to the driven low state, to contribute to the "ground" area
//around the next key to be scanned.
P1IE &= ~Active_Key; //disable active key interrupt
P1OUT &= ~Active_Key; //switch active key to low to discharge pad
P1DIR |= Active_Key; //switch active key to output to save power
P1OUT &= ~Partner_Key;
Sum = Sum + Timer_Count;
return (Sum >> 1);
}
/**************************************************************************************************
**函数名称:void touchkey_init(void)
**入口参数:无
**出口参数:无
**功能描述:初始化触摸按键
**************************************************************************************************/
void touchkey_init(void)
{
BCSCTL1 = CALBC1_8MHZ; // DCO = 8MHZ
DCOCTL = CALDCO_8MHZ;
BCSCTL3 |= LFXT1S_2; // ACLK = VLO
P1OUT = 0X00;
P1DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5;
P1SEL = 0X00;
CCTL0 = CCIE; // CCR0 中断使能
CCR0 = 60000 - 1;
TACTL = TASSEL_2 + ID_2 + MC_2 + TACLR; //SMCLK , Divider/4 , Continous Up , TAR Clear
_EINT();
char i,j;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
Key_Capacitance_Basic[i]+=measure_key_capacitance(i);
_NOP();
}
Key_Capacitance_Basic[i]>>=2;
}
}
/**************************************************************************************************
**函数名称:int scan_key(char key, char times)
**入口参数:key: 当前扫描按键的编号
**出口参数:times: 当前按键扫描的次数
**功能描述:判断扫描按键是否按下
**************************************************************************************************/
int scan_key(char key, char times)
{
unsigned char j;
int press=0,rise=0,equ=0,down=0;
for(j=0 ; j<times ; j++)
{
Key_Capacitance_Filter[key] = measure_key_capacitance(key) - Key_Capacitance_Basic[key];
if(Key_Capacitance_Filter[key] > Max_Key_Threshold)
{
press++;
}
else if(Key_Capacitance_Filter[key] > 0)
{
rise++;
}
else if(Key_Capacitance_Filter[key]==0)
{
equ++;
}
else if(Key_Capacitance_Filter[key] < 0)
{
down++;
}
}
if(press>=times/2)
{
return 2;
}
if(rise>=times/2)
{
return 1;
}
if((down>=times/2))
{
return -1;
}
return 0;
}
/**************************************************************************************************
**函数名称:unsigned char scan_result(void)
**入口参数:无
**出口参数:当前按下的按键值
**功能描述:扫描按键键值
**************************************************************************************************/
unsigned char scan_result(void)
{
unsigned char i,key_buffer=0;
int key_value=0;
for(i=0;i<5;i++)
{
key_value=scan_key(i,4);
switch(key_value)
{
case 2:
key_buffer |=(0x01<<i);
break;
case 1:
key_rise_times[i]++;
break;
case -1:
key_down_times[i]++;
break;
default:
break;
}
_NOP();
}
_NOP();
return key_buffer;
}
/**************************************************************************************************
**函数名称:void cap_adjust(void)
**入口参数:无
**出口参数:无
**功能描述:扫描按键键值
**************************************************************************************************/
void cap_adjust(void)
{
unsigned char i;
static char scan_times = basic_cap_adjust_cycle;
if(--scan_times==0)
{
scan_times=basic_cap_adjust_cycle;
for(i=0;i<Num_Of_Key;i++)
{
if(key_rise_times[i]>(basic_cap_adjust_cycle>>1)) //when 3/4 total times of key scan
{
Key_Capacitance_Basic[i]++;
}
if(key_down_times[i]>(basic_cap_adjust_cycle>>1))
{
Key_Capacitance_Basic[i]--;
}
key_rise_times[i]=0;
key_down_times[i]=0;
}
_NOP();
}
}
/**************************************************************************************************
**函数名称:__interrupt void port_1_interrupt(void)
**入口参数:无
**出口参数:无
**功能描述:触摸按键电容放电IO口中断服务函数
**************************************************************************************************/
#pragma vector=PORT1_VECTOR
__interrupt void port_1_interrupt(void)
{
P1IFG = 0; // clear flag
Timer_Count = TAR - Timer_Count; // find the discharge time
LPM0_EXIT;
}
/**************************************************************************************************
**函数名称:__interrupt void port2_ISR (void)
**入口参数:无
**出口参数:无
**功能描述:CC2500无线数据接收中断服务函数
**************************************************************************************************/
#pragma vector=PORT2_VECTOR
__interrupt void port2_ISR (void)
{
char len=16;
if(TI_CC_GDO2_PxIFG & TI_CC_GDO2_PIN)
{
if(system_state&flg_rx_state) //rx inter
{
if (RFReceivePacket(&rxBuffer[0],&len)) // Fetch packet from CCxxxx
{
system_state |= flg_rx_finished;
LPM0_EXIT;
}
}
TI_CC_GDO2_PxIFG &=~ TI_CC_GDO2_PIN;
}
P2IFG = 0;
}
/**************************************************************************************************
**函数名称:__interrupt void Timer_A (void)
**入口参数:无
**出口参数:无
**功能描述:按键扫描间隔计数器中断服务函数
**************************************************************************************************/
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
CCR0 = CCR0 + 60000;
LPM0_EXIT;
}
/**************************************************************************************************
**函数名称:__interrupt void Timer_A1 (void)
**入口参数:无
**出口参数:无
**功能描述:蜂鸣器延时定时器中断服务函数
**************************************************************************************************/
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A1 (void)
{
switch( TAIV )
{
case 2: // TACCR1 not used
TACCTL1 &= ~CCIE; // CCR1 interrupt enabled
BEEP_OFF;
break;
case 4: break; // TACCR2 not used
case 10: break; // overflow
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -