萬代觸摸芯片8051與WTC6312BSI 6316BSI接口程序范例
萬代觸摸芯片WTC6312BSI程序
萬代觸摸芯片WTC6316BSI程序
1 多鍵組合(SHIFT)工作模式的操作程序范例
該范例程序實現(xiàn)的功能是8051 從觸摸芯片的SPI 口讀取按鍵信息,并用
LED加以顯示。每一個LED顯示一個相應按鍵的狀態(tài)。并給出了通過SPI口設
置觸摸芯片的靈敏度的功能函數(shù)。
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
//--------------------------------WTC6316BSI SPI接口---------------------------------------------
sbit SCS = P1^3;
sbit SCK = P1^2;
sbit SDO = P1^1;
sbit SDI = P1^0;
//--------------------------------LED 顯示界面---------------------------------------------
sbit LED15 = P2^7;
sbit LED14 = P2^6;
sbit LED13 = P2^5;
sbit LED12 = P2^4;
sbit LED11 = P2^3;
sbit LED10 = P2^2;
sbit LED9 = P2^1;
sbit LED8 = P2^0;
sbit LED7 = P0^7;
sbit LED6 = P0^6;
sbit LED5 = P0^5;
sbit LED4 = P0^4;
sbit LED3 = P0^3;
sbit LED2 = P0^2;
sbit LED1 = P0^1;
sbit LED0 = P0^0;
//------------------------------------the funtion define-------------------------------------
void delay_loop(uint i);
void set_Subtle_SPI(uchar temp); //用軟件設置WTC6316BSI靈敏度
uchar get_key_data(void); //讀取WTC6316BSI SPI接口數(shù)據(jù)
void shift_led_disp(void); //用LED 顯示按鍵狀態(tài)//------------------------------------the register define -----------------------------------
uchar GetKey; //從SPI口讀到的數(shù)值
uchar SwitchImage[3]; //觸摸按鍵狀態(tài)寄存器
//------------------------------------------------------------------------------------------
void main(void)
{
init();
SCS = 1; //關閉SPI口使能
SCK = 1; //SCK 初始電平為高
SDI = 1; //SDI設置為高電平
while(1)
{
delay_loop(400); //延時,實際程序中可以每4MS讀一次SPI口
SDI = 1;
GetKey = get_key_data(); //讀取SPI接口的數(shù)據(jù)
if((GetKey & 0x03)== 0) //檢測低2位的幀信息識別位
{ //第一幀
SwitchImage[0] = GetKey;
shift_led_disp(); //用LED 顯示讀取的按鍵狀態(tài)
}
else if((GetKey & 0x03)== 1) //檢測低2位的幀信息識別位
{ //第二幀
SwitchImage[1] = GetKey;
shift_led_disp(); //用LED 顯示讀取的按鍵狀態(tài)
}
else if((GetKey & 0x03)== 2) //檢測低2位的幀信息識別位
{ //第三幀
SwitchImage[2] = GetKey;
shift_led_disp(); //用LED 顯示讀取的按鍵狀態(tài)
}
}
}
//------------------------------------------------------------------------------------------
void shift_led_disp(void)
{
if((SwitchImage[2] & 0x20) != 0) {LED15 = 0;} //點亮LED
else {LED15 = 1;} //熄滅LEDif((SwitchImage[2] & 0x10) != 0) {LED14 = 0;}
else {LED14 = 1;}
if((SwitchImage[2] & 0x08) != 0) {LED13 = 0;}
else {LED13 = 1;}
if((SwitchImage[2] & 0x04) != 0) {LED12 = 0;}
else {LED12 = 1;}
if((SwitchImage[1] & 0x80) != 0) {LED11 = 0;} //點亮LED
else {LED11 = 1;} //熄滅LED
if((SwitchImage[1] & 0x40) != 0) {LED10 = 0;}
else {LED10 = 1;}
if((SwitchImage[1] & 0x20) != 0) {LED9 = 0;}
else {LED9 = 1;}
if((SwitchImage[1] & 0x10) != 0) {LED8 = 0;}
else {LED8 = 1;}
if((SwitchImage[1] & 0x08) != 0) {LED7 = 0;}
else {LED7 = 1;}
if((SwitchImage[1] & 0x04) != 0) {LED6 = 0;}
else {LED6 = 1;}
if((SwitchImage[0] & 0x80) != 0) {LED5 = 0;}
else {LED5 = 1;}
if((SwitchImage[0] & 0x40) != 0) {LED4 = 0;}
else {LED4 = 1;}
if((SwitchImage[0] & 0x20) != 0) {LED3 = 0;}
else {LED3 = 1;}
if((SwitchImage[0] & 0x10) != 0) {LED2 = 0;}
else {LED2 = 1;}
if((SwitchImage[0] & 0x08) != 0) {LED1 = 0;}
else {LED1 = 1;}
if((SwitchImage[0] & 0x04) != 0) {LED0 = 0;}
else {LED0 = 1;}
}
//------------------------------------------------------------------------------------------
uchar get_key_data(void)
{
uchar KeyData;
uchar i;
KeyData = 0;
i = 0; //計數(shù)器指初始為0SDI = 1; //SDI置為高,以避免錯誤設置觸摸芯片的靈敏度
SCS = 0; //打開SPI口使能
do
{
KeyData <<= 1; //MSB為數(shù)據(jù)第一位
SCK = 0; //SCK 信號下降沿
SCK = 1; //SCK 信號上升沿
if(SDO == 1) //讀取SDO的數(shù)據(jù)
{ //SDI為高電平
KeyData |= 0x01;
}
else
{ //SDI為低電平
KeyData &= 0xFE;
}
i++; //計數(shù)器加1
}while (i < 8); //循環(huán)讀數(shù)8次
SCS = 1; //關閉SPI口使能
return(KeyData); //返回讀取的按鍵信息
}
//------------------------------------------------------------------------------------------
void set_Subtle_SPI(uchar temp)
{
uchar i;
i= 0; //計數(shù)器指初始為0
SCS = 0; //打開SPI口使能
do
{
if((temp & 0x80) != 0) //發(fā)送數(shù)據(jù)的第一位為MSB
{ //敏感度設定值的當前位值為1
SDI = 1;
}
else
{ //敏感度設定值的當前位值為0
SDI = 0;
}
SCK = 0; //SCK 信號下降沿
SCK = 1; //SCK 信號上升沿temp <<= 1; //發(fā)送數(shù)據(jù)的第一位為MSB
i++; //計數(shù)器加1
}while (i < 8); //循環(huán)發(fā)送8次
SCS = 1; //關閉SPI口使能
SDI = 1; //SDI設置為高電平
}
//------------------------------------------------------------------------------------------
void delay_loop(uint i)
{
while(i > 0)
{
i--;
}
}
2 8051 與WTC6316BSI 接口的單鍵工作模式的操作程序范例
該范例程序實現(xiàn)的功能是8051 從觸摸芯片的SPI 口讀取按鍵信息,并用
LED加以顯示。每一個LED顯示一個相應按鍵的狀態(tài)。并給出了通過SPI口設
置觸摸芯片的靈敏度的功能函數(shù)
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
//--------------------------------WTC6316BSI SPI接口---------------------------------------------
sbit SCS = P1^3;
sbit SCK = P1^2;
sbit SDO = P1^1;
sbit SDI = P1^0;
//--------------------------------LED 顯示界面---------------------------------------------
sbit LED15 = P2^7;
sbit LED14 = P2^6;
sbit LED13 = P2^5;
sbit LED12 = P2^4;
sbit LED11 = P2^3;
sbit LED10 = P2^2;
sbit LED9 = P2^1;
sbit LED8 = P2^0;sbit LED7 = P0^7;
sbit LED6 = P0^6;
sbit LED5 = P0^5;
sbit LED4 = P0^4;
sbit LED3 = P0^3;
sbit LED2 = P0^2;
sbit LED1 = P0^1;
sbit LED0 = P0^0;
//------------------------------------功能函數(shù)-------------------------------------
void delay_loop(uchar i); //延時函數(shù)
void led_on(uchar LedControler); //開LED
void led_off(); //關LED
void set_Subtle(uchar subtle); //設置WTC6316BSI的敏感度
uchar get_keyValue(void); //讀取觸摸按鍵狀態(tài)
//------------------------------------變量定義 -----------------------------------
uchar GetKey; //從WTC6316BSI讀取的按鍵值
//GetKey == 0xFF 表示沒有按鍵觸摸或按鍵已經(jīng)彈開
//GetKey 不等于 0xFF 表示有按鍵正在被觸摸
//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------
void main(void)
{
SCS = 1; //關閉SPI口使能
SCK = 1; //SCK 初始電平為高
SDI = 1; //SDI設置為高電平
set_Subtle(10); //將WTC6316BSI的敏感度初始化為10
while(1)
{
delay_loop(500); //延時,實際程序中可以每4MS讀一次SPI口
GetKey = get_keyValue(); //讀取按鍵信息
if(GetKey != 0xFF)
{ //有鍵按下點亮相應鍵的指示LED
led_on(GetKey);
}
else
{ //沒有按鍵按下或鍵彈開,關閉所有指示LED
led_off();}
}
}
//------------------------------------------------------------------------------------------
uchar get_keyValue(void)
{
uchar KeyValue;
uchar i;
i = 0; //計數(shù)器指初始為0
SDI = 1; //SDI置為高,以避免錯誤設置觸摸芯片的靈敏度
SCS = 0; //打開SPI口使能
do
{
KeyValue <<= 1; //MSB為數(shù)據(jù)第一位
SCK = 0; //SCK 信號下降沿
SCK = 1; //SCK 信號上升沿
if(SDO == 1) //讀取SDO的數(shù)據(jù)
{ //SDI為高電平
KeyValue |= 0x01;
}
else
{ //SDI為低電平
KeyValue &= 0xFE;
}
i++; //計數(shù)器加1
}while (i < 8); //循環(huán)讀數(shù)8次
SCS = 1; //關閉SPI口使能
return(KeyValue); //返回讀取的按鍵信息
}
//------------------------------------------------------------------------------------------
void set_Subtle(uchar subtle)
{
uchar i;
i= 0; //計數(shù)器指初始為0
SCS = 0; //打開SPI口使能
do
{
if((temp & 0x80) != 0) //發(fā)送數(shù)據(jù)的第一位為MSB
{ //敏感度設定值的當前位值為1SDI = 1;
}
else
{ //敏感度設定值的當前位值為0
SDI = 0;
}
SCK = 0; //SCK 信號下降沿
SCK = 1; //SCK 信號上升沿
temp <<= 1; //發(fā)送數(shù)據(jù)的第一位為MSB
i++; //計數(shù)器加1
}while (i < 8); //循環(huán)發(fā)送8次
SCS = 1; //關閉SPI口使能
SDI = 1; //SDI設置為高電平
}
//-----------------------------------------------------------------------------------------
void delay_loop(uint i)
{
while(i)
{
i--;
}
}
//------------------------------------------------------------------------------------------
void led_on(uchar LedControler)
{
switch(LedControler)
{
case 0: LED0 = 0; break;
case 1: LED1 = 0; break;
case 2: LED2 = 0; break;
case 3: LED3 = 0; break;
case 4: LED4 = 0; break;
case 5: LED5 = 0; break;
case 6: LED6 = 0; break;
case 7: LED7 = 0; break;
case 8: LED8 = 0; break;
case 9: LED9 = 0; break;
case 10: LED10 = 0; break;case 11: LED11 = 0; break;
case 12: LED12 = 0; break;
case 13: LED13 = 0; break;
case 14: LED14 = 0; break;
case 15: LED15 = 0; break;
}
}
//------------------------------------------------------------------------------------------
void led_off (void)
{
LED0 = 1;
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 1;
LED5 = 1;
LED6 = 1;
LED7 = 1;
LED8 = 1;
LED9 = 1;
LED10 = 1;
LED11 = 1;
LED12 = 1;
LED13 = 1;
LED14 = 1;
LED15 = 1;
}