资讯详情
万代触摸芯片8051与WTC6312BSI 6316BSI接口程序范例
发布者:oweis88  发布时间:2015-09-01 10:15:05

万代触摸芯片8051与WTC6312BSI 6316BSI接口程序范例

万代触摸芯片WTC6312BSI程序

万代触摸芯片WTC6316BSI程序

1 多键组合(SHIFT)工作模式的操作程序范例
该范例程序实现的功能是8051 从触摸芯片的SPI 口读取按键信息,并用
LED加以显示。每一个LED显示一个相应按键的状态。并给出了通过SPI口设
置触摸芯片的灵敏度的功能函数。
#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接口数据
void shift_led_disp(void); //用LED 显示按键状态//------------------------------------the register define -----------------------------------
uchar GetKey; //从SPI口读到的数值
uchar SwitchImage[3]; //触摸按键状态寄存器
//------------------------------------------------------------------------------------------
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接口的数据
if((GetKey & 0x03)== 0) //检测低2位的帧信息识别位
{ //第一帧
SwitchImage[0] = GetKey;
shift_led_disp(); //用LED 显示读取的按键状态
}
else if((GetKey & 0x03)== 1) //检测低2位的帧信息识别位
{ //第二帧
SwitchImage[1] = GetKey;
shift_led_disp(); //用LED 显示读取的按键状态
}
else if((GetKey & 0x03)== 2) //检测低2位的帧信息识别位
{ //第三帧
SwitchImage[2] = GetKey;
shift_led_disp(); //用LED 显示读取的按键状态
}
}
}
//------------------------------------------------------------------------------------------
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; //计数器指初始为0SDI = 1; //SDI置为高,以避免错误设置触摸芯片的灵敏度
SCS = 0; //打开SPI口使能
do
{
KeyData <<= 1; //MSB为数据第一位
SCK = 0; //SCK 信号下降沿
SCK = 1; //SCK 信号上升沿
if(SDO == 1) //读取SDO的数据
{ //SDI为高电平
KeyData |= 0x01;
}
else
{ //SDI为低电平
KeyData &= 0xFE;
}
i++; //计数器加1
}while (i < 8); //循环读数8次
SCS = 1; //关闭SPI口使能
return(KeyData); //返回读取的按键信息
}
//------------------------------------------------------------------------------------------
void set_Subtle_SPI(uchar temp)
{
uchar i;
i= 0; //计数器指初始为0
SCS = 0; //打开SPI口使能
do
{
if((temp & 0x80) != 0) //发送数据的第一位为MSB
{ //敏感度设定值的当前位值为1
SDI = 1;
}
else
{ //敏感度设定值的当前位值为0
SDI = 0;
}
SCK = 0; //SCK 信号下降沿
SCK = 1; //SCK 信号上升沿temp <<= 1; //发送数据的第一位为MSB
i++; //计数器加1
}while (i < 8); //循环发送8次
SCS = 1; //关闭SPI口使能
SDI = 1; //SDI设置为高电平
}
//------------------------------------------------------------------------------------------
void delay_loop(uint i)
{
while(i > 0)
{
i--;
}
}
2 8051 与WTC6316BSI 接口的单键工作模式的操作程序范例
该范例程序实现的功能是8051 从触摸芯片的SPI 口读取按键信息,并用
LED加以显示。每一个LED显示一个相应按键的状态。并给出了通过SPI口设
置触摸芯片的灵敏度的功能函数
#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;
//------------------------------------功能函数-------------------------------------
void delay_loop(uchar i); //延时函数
void led_on(uchar LedControler); //开LED
void led_off(); //关LED
void set_Subtle(uchar subtle); //设置WTC6316BSI的敏感度
uchar get_keyValue(void); //读取触摸按键状态
//------------------------------------变量定义 -----------------------------------
uchar GetKey; //从WTC6316BSI读取的按键值
//GetKey == 0xFF 表示没有按键触摸或按键已经弹开
//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; //计数器指初始为0
SDI = 1; //SDI置为高,以避免错误设置触摸芯片的灵敏度
SCS = 0; //打开SPI口使能
do
{
KeyValue <<= 1; //MSB为数据第一位
SCK = 0; //SCK 信号下降沿
SCK = 1; //SCK 信号上升沿
if(SDO == 1) //读取SDO的数据
{ //SDI为高电平
KeyValue |= 0x01;
}
else
{ //SDI为低电平
KeyValue &= 0xFE;
}
i++; //计数器加1
}while (i < 8); //循环读数8次
SCS = 1; //关闭SPI口使能
return(KeyValue); //返回读取的按键信息
}
//------------------------------------------------------------------------------------------
void set_Subtle(uchar subtle)
{
uchar i;
i= 0; //计数器指初始为0
SCS = 0; //打开SPI口使能
do
{
if((temp & 0x80) != 0) //发送数据的第一位为MSB
{ //敏感度设定值的当前位值为1SDI = 1;
}
else
{ //敏感度设定值的当前位值为0
SDI = 0;
}
SCK = 0; //SCK 信号下降沿
SCK = 1; //SCK 信号上升沿
temp <<= 1; //发送数据的第一位为MSB
i++; //计数器加1
}while (i < 8); //循环发送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;
}

 

版权声明:工控网转载作品均注明出处,本网未注明出处和转载的,是出于传递更多信息之目的,并不意味 着赞同其观点或证实其内容的真实性。如转载作品侵犯作者署名权,或有其他诸如版权、肖像权、知识产权等方面的伤害,并非本网故意为之,在接到相关权利人通知后将立即加以更正。联系电话:0571-87774297。
今日最新资讯
热门资讯
0571-87774297