ls1x-master-v0.4/private/ls1c102/ls1c102_touch.c
2024-11-27 15:39:05 +08:00

207 lines
6.0 KiB
C
Executable File

//#include"Main.h"
#include "ls1c102_touch.h"
#include "Config.h"
#include "ls1x.h"
void Touch_CountBaseInit(Touch_CountBaseInitTypeDef* Touch_CountBaseInitStruct){
TS_OSCTH = ((Touch_CountBaseInitStruct->TOUCH_up_th&0xff)<<24)|\
((Touch_CountBaseInitStruct->TOUCH_bas_th&0xff)<<16)|\
((Touch_CountBaseInitStruct->TOUCH_cnt_neg&0x01)<<15)|\
((Touch_CountBaseInitStruct->TOUCH_cnt_prd&0x1f)<<8) |\
((Touch_CountBaseInitStruct->TOUCH_rsel&0x0f)<<0);
}
void Touch_PerformInit(Touch_PerformInitTypeDef* Touch_PerformInitStruct){
TS_POLLTIM = ((Touch_PerformInitStruct->TOUCH_dbc_prd&0xff)<<24)|\
((Touch_PerformInitStruct->TOUCH_act_stb&0x3)<<22)|\
((Touch_PerformInitStruct->TOUCH_stb_prd&0x3f)<<16)|\
((Touch_PerformInitStruct->TOUCH_act_prd&0x0f)<<8)|\
((Touch_PerformInitStruct->TOUCH_act_num&0xff)<<0);
}
void Touch_ControlConfig(Touch_ControlConfigTypeDef* Touch_ControlConfigStruct){
TS_CTRL = ((Touch_ControlConfigStruct->TOUCH_chn_en & 0xffff)<<16)|\
((Touch_ControlConfigStruct->TOUCH_dbc_en & 0x1)<<15)|\
((Touch_ControlConfigStruct->TOUCH_dbc_num & 0x7)<<12)|\
((Touch_ControlConfigStruct->TOUCH_eos_ov & 0x1)<<11)|\
((Touch_ControlConfigStruct->TOUCH_flt_level & 0x03)<<8)|\
((Touch_ControlConfigStruct->TOUCH_int_en & 0x0f)<<4)|\
((Touch_ControlConfigStruct->TOUCH_poll_en & 0x1)<<1)|\
((Touch_ControlConfigStruct->TOUCH_scan_en & 0x1)<<0);
}
void TOUCH_Cmd(FunctionalState NewState){
if (NewState != DISABLE)
{
PMU_CMDSTS |= (1<<9); //触摸按键中断使能
}else{
PMU_CMDSTS &= ~(((INT32U)0x01) << 9);
}
}
INT16U TOUCH_GetBaseVal(INT16U KeyChannel)
{
return (TS_CHNATTR(KeyChannel) & 0xFFF);
}
INT16U TOUCH_GetCountValue(INT16U KeyChannel)
{
return (TS_CNTRES(KeyChannel) & 0xFFF);
}
void TOUCH_EnablePollScan(void)
{
TS_CTRL |= 0x2;
}
void TOUCH_disablePollScan(void)
{
TS_CTRL &= ~(0x2);
}
void TOUCH_EnableSingleScan(void)
{
TS_CTRL |= 0x1;
}
void TOUCH_SetDownTh(INT8U channel, INT8U down_th)
{
TS_CHNATTR(channel) = ( (TS_CHNATTR(channel) & 0xfff) | (down_th << 24) );
}
void TOUCH_SetBaseVal(INT8U channel, INT16U base_val)
{
TS_CHNATTR(channel) = ( (TS_CHNATTR(channel) & 0xff000000) | (base_val & 0xfff) );
}
void TOUCH_SetChnAttr(INT8U channel, INT8U down_th)
{
TS_CHNATTR(channel) = ( down_th << 24) | (TS_CNTRES(channel) & 0xfff);
}
void TOUCH_SetAllChnAttr(INT8U down_th)
{
INT16U KeyChannel;
for(KeyChannel=0; KeyChannel<CHANNEL_NUM; KeyChannel++)
{
TOUCH_SetChnAttr(KeyChannel, down_th);
}
}
void TOUCH_Init(INT8U down_th)
{
Touch_CountBaseInitTypeDef Touch_CountBaseInitStruct;
Touch_PerformInitTypeDef Touch_PerformInitStruct;
Touch_ControlConfigTypeDef Touch_ControlConfigStruct;
Touch_CountBaseInitStruct.TOUCH_up_th = 0x14;
Touch_CountBaseInitStruct.TOUCH_bas_th = 0x10;
Touch_CountBaseInitStruct.TOUCH_cnt_neg = 1;
Touch_CountBaseInitStruct.TOUCH_cnt_prd = 0x1;
Touch_CountBaseInitStruct.TOUCH_rsel = 0x1;
Touch_CountBaseInit(&Touch_CountBaseInitStruct);
Touch_PerformInitStruct.TOUCH_dbc_prd = 0x3;
Touch_PerformInitStruct.TOUCH_act_stb = 0x0;
Touch_PerformInitStruct.TOUCH_stb_prd = 0x8;
Touch_PerformInitStruct.TOUCH_act_prd = 0x8;
Touch_PerformInitStruct.TOUCH_act_num = 0xff;
Touch_PerformInit(&Touch_PerformInitStruct);
Touch_ControlConfigStruct.TOUCH_chn_en = 0xfff;
Touch_ControlConfigStruct.TOUCH_dbc_en = 0x1;
Touch_ControlConfigStruct.TOUCH_dbc_num = 0x3;
Touch_ControlConfigStruct.TOUCH_eos_ov = 0x0;
Touch_ControlConfigStruct.TOUCH_flt_level = 0x3;
Touch_ControlConfigStruct.TOUCH_int_en = 0xf;
Touch_ControlConfigStruct.TOUCH_poll_en = 0x0;
Touch_ControlConfigStruct.TOUCH_scan_en = 0x0;
Touch_ControlConfig(&Touch_ControlConfigStruct);
TOUCH_EnableSingleScan();
while(TS_CTRL & 0x1); //wait scan end
TOUCH_SetAllChnAttr(down_th);
TOUCH_Cmd(ENABLE);
}
void Printf_KeyChannel(INT16U KeyChannel)
{
INT32U i;
for(i=0; i<CHANNEL_NUM; i++)
{
if( KeyChannel & BIT(i))
printf("\t%d", i);
}
printf("\n");
}
void Printf_KeyType(INT8U KeyType)
{
if(KeyType & BIT(0))
printf("\r\nDown");
if(KeyType & BIT(1))
printf("\r\nUP");
if(KeyType & BIT(2))
printf("\r\nMuliDown");
if(KeyType & BIT(3))
printf("\r\nCount OV");
}
void Printf_KeyVal(void)
{
INT32S KeyChannel, Ratio[CHANNEL_NUM], SubVal[CHANNEL_NUM], BaseVal[CHANNEL_NUM], CountVal[CHANNEL_NUM];
for(KeyChannel=0; KeyChannel<CHANNEL_NUM; KeyChannel++)
{
CountVal[KeyChannel] = TOUCH_GetCountValue(KeyChannel);
BaseVal[KeyChannel] = TOUCH_GetBaseVal(KeyChannel);
if(BaseVal[KeyChannel])
{
SubVal[KeyChannel] = BaseVal[KeyChannel] - CountVal[KeyChannel];
Ratio[KeyChannel] = 1000*SubVal[KeyChannel]/BaseVal[KeyChannel];
}
else
{
SubVal[KeyChannel] = 0;
Ratio[KeyChannel] = 0;
}
}
printf("\r\n\t00\t01\t02\t03\t04\t05\t06\t07\t08\t09\t10\t11");
printf("\r\nBaseVal");
for(KeyChannel=0; KeyChannel<CHANNEL_NUM; KeyChannel++)
{
printf("\t%4d", BaseVal[KeyChannel]);
}
printf("\r\nSubVal");
for(KeyChannel=0; KeyChannel<CHANNEL_NUM; KeyChannel++)
{
if (SubVal[KeyChannel]<0)
printf("\t%2d", SubVal[KeyChannel]);
else
printf("\t%3d", SubVal[KeyChannel]);
}
printf("\n");
}
void Printf_CountVal(void)
{
INT32S KeyChannel, CountVal[CHANNEL_NUM];
for(KeyChannel=0; KeyChannel<CHANNEL_NUM; KeyChannel++)
{
CountVal[KeyChannel] = TOUCH_GetCountValue(KeyChannel);
}
printf("\r\n\t00\t01\t02\t03\t04\t05\t06\t07\t08\t09\t10\t11");
printf("\nCntVal");
for(KeyChannel=0; KeyChannel<CHANNEL_NUM; KeyChannel++)
{
printf("\t%4d", CountVal[KeyChannel]);
}
printf("\n");
}