(Kinetis K60)TSI触摸感应……
偶然发现kinetis有个TSI寄存器……作为屌丝 果断体验鸟ipad的触摸赶脚~
关于电容触摸屏原理……手指接近会导致振荡器频率的变化
main.c
#include "LED.h" void TSI_Init(void); void TSI_SelfAdjust(void); volatile U16 Adjust[4]; void main(void) { DisableInterrupts; TSI_Init(); init_LED(); TSI_SelfAdjust(); EnableInterrupts; while(1); } void TSI_Init(void) { SIM_SCGC5 |= (SIM_SCGC5_TSI_MASK | SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK); PORTA_PCR4 = PORT_PCR_MUX(0); PORTB_PCR2 = PORT_PCR_MUX(0); PORTB_PCR3 = PORT_PCR_MUX(0); PORTB_PCR16 = PORT_PCR_MUX(0); TSI0_GENCS |= (TSI_GENCS_NSCN(10) | TSI_GENCS_PS(3)); //扫描数 振荡器分频 TSI0_SCANC |= (TSI_SCANC_EXTCHRG(3) | TSI_SCANC_REFCHRG(31) | TSI_SCANC_DELVOL(7) | TSI_SCANC_SMOD(0) | TSI_SCANC_AMPSC(0)); //外部电流 参考电流 电压 连续扫描 输入时钟分频 TSI0_PEN = TSI_PEN_PEN5_MASK | TSI_PEN_PEN7_MASK | TSI_PEN_PEN8_MASK | TSI_PEN_PEN9_MASK; //打开相应TSI通道 TSI0_GENCS |= TSI_GENCS_TSIEN_MASK; //打开TSI中断 enable_irq(83); } void TSI_SelfAdjust(void) //自校正 { TSI0_GENCS |= TSI_GENCS_SWTS_MASK; //软触发启动 while(!TSI0_GENCS & TSI_GENCS_EOSF_MASK); //等待扫描结束 Delay(300); //TSIx_CNTRn 高十六位存储的是通道n三角计数的值 低16位存储通道(n-1)的值 Adjust[0] = (TSI0_CNTR5>>16)&0x0000FFFF; Adjust[1] = (TSI0_CNTR7>>16)&0x0000FFFF; Adjust[2] = (TSI0_CNTR9)&0x0000FFFF; Adjust[3] = (TSI0_CNTR9>>16)&0x0000FFFF; TSI0_GENCS &= ~TSI_GENCS_TSIEN_MASK; //关闭TSI TSI0_GENCS |= TSI_GENCS_STM_MASK; TSI0_GENCS |= (TSI_GENCS_TSIIE_MASK|TSI_GENCS_ESOR_MASK); TSI0_GENCS |= TSI_GENCS_TSIEN_MASK; }
中断服务函数
#include "isr.h" #include "LED.h" #define VALVE1 800 #define VALVE2 800 #define VALVE3 800 #define VALVE4 800 extern volatile U16 Adjust[4]; U8 LED_1 = 1,LED_2 = 1,LED_3 = 1,LED_4 = 1; void TSI_isr(void) { U16 NOWCounter; TSI0_GENCS |= TSI_GENCS_OUTRGF_MASK; //清除数据 TSI0_GENCS |= TSI_GENCS_EOSF_MASK; //扫描 NOWCounter = (TSI0_CNTR5>>16)&0x0000FFFF -VALVE1; //获取周期 if(NOWCounter>Adjust[0]) { Delay(10); //延迟消抖 NOWCounter = (TSI0_CNTR5>>16)&0x0000FFFF -VALVE1; if(NOWCounter>Adjust[0]) { LED_1 = !LED_1; LED1(LED_1); while(NOWCounter>Adjust[0]) NOWCounter = (TSI0_CNTR5>>16)&0x0000FFFF -VALVE1; } } NOWCounter = (TSI0_CNTR7>>16)&0x0000FFFF -VALVE2; if(NOWCounter>Adjust[1]) { Delay(10); NOWCounter = (TSI0_CNTR7>>16)&0x0000FFFF -VALVE2; if(NOWCounter>Adjust[1]) { LED_2 = !LED_2; LED2(LED_2); while(NOWCounter>Adjust[1]) NOWCounter = (TSI0_CNTR7>>16)&0x0000FFFF -VALVE2; } } NOWCounter = (TSI0_CNTR9)&0x0000FFFF -VALVE3; if(NOWCounter>Adjust[2]) { Delay(10); NOWCounter = (TSI0_CNTR9)&0x0000FFFF -VALVE3; if(NOWCounter>Adjust[2]) { LED_3 = !LED_3; LED3(LED_3); while(NOWCounter>Adjust[2]) NOWCounter = (TSI0_CNTR9)&0x0000FFFF -VALVE3; } } NOWCounter = (TSI0_CNTR9>>16)&0x0000FFFF -VALVE4; if(NOWCounter>Adjust[3]) { Delay(10); NOWCounter = (TSI0_CNTR9>>16)&0x0000FFFF -VALVE4; if(NOWCounter>Adjust[3]) { LED_4 = !LED_4; LED4(LED_4); while(NOWCounter>Adjust[3]) NOWCounter = (TSI0_CNTR9>>16)&0x0000FFFF -VALVE4; } } TSI0_STATUS = 0xFFFFFFFF; }