#ifndef __ANSG08SH_H
#define __ANSG08SH_H
#include <INTRINS.H>
#include "SH79F166A.h"
#include "delay.h"
#include "li_led.h"
#define P0INMODE P0CR=0x1e; //输入数据模式
#define P0OUTMODE P0CR=0x3e; //输出数据模式
sbit SDA=P0^5;
sbit SCL=P0^4;
sbit KEY_INT=P0^6;
void delay_us(uchar t);//延时函数
void IICInit(void);//IIC初始化
void IICStart(void);//IIC起始信号
void IICStop(void);//IIC终止信号
void IICAck(void);//应答信号
void IICNoAck(void);//非应答信号
uchar IICWaitAck(void);//接受从机的应答信号
void IICWriteByte(uchar Data);//向SDA数据线写入一个byte数据
uchar IICReaderByte(void);//从SDA数据线读取一个byte数据
uchar ReadKey(void);//从触摸感应寄存器中读一个触摸判断的值
uchar JudgeKey(uchar Data); //判断按键编码
void SignalCapture(void);//捕获按键信号
void WriteSen(void);//灵敏度调节
#endif
#include "ansg08sh.h"
//IIC³õʼ»¯
void IICInit(void)
{
P0OUTMODE;
SDA=1;
SCL=1;
}
//IICÆðʼÐźÅ
void IICStart(void)
{
P0OUTMODE;
SDA=1;
delay_1us(1);
SCL=1;
delay_1us(2);
SDA=0;
delay_1us(2);
//SCL=0;
//delay_1us(2);
}
//IICÖÕÖ¹ÐźÅ
void IICStop(void)
{
P0OUTMODE;
//SCL=0;
//delay_1us(1);
SDA=0;
delay_1us(2);
SCL=1;
delay_us(2);
SDA=1;
delay_1us(2);
}
//Ó¦´ðÐźÅ
void IICAck(void)
{
uchar i=0;
P0OUTMODE;
SCL=1;
delay_1us(2);
while((SDA==1)&&(i<150))
i++;
SCL=0;
delay_1us(2);
}
//·ÇÓ¦´ðÐźÅ
void IICNoAck(void)
{
P0OUTMODE;
SCL=0;
SDA=1;
delay_1us(2);
SCL=1;
delay_1us(2);
SCL=0;
delay_1us(2);
}
//½ÓÊÜ´Ó»úµÄÓ¦´ðÐźÅ
uchar IICWaitAck(void)
{
uchar i=0;
SCL=0;
delay_1us(5);
SDA=1;
P0INMODE;
delay_1us(5);
SCL=1;
delay_1us(5);
while(SDA)
{
i++;
if(i>200)
{
// IICStop();
return 1;
}
return 1;
}
delay_1us(5);
SCL=0;
return 0;
}
//ÏòSDAÊý¾ÝÏßдÈëÒ»¸öbyteÊý¾Ý
void IICWriteByte(uchar Data)
{
uchar i,temp;
P0OUTMODE;
temp=Data;
for(i=0;i<8;i++)
{
SCL=0;
delay_1us(2);
if((Data&0x80)==0x80)
{
SDA=1;
}
else
{
SDA=0;
}
Data=Data<<1;
delay_1us(2);
SCL=1;
delay_1us(2);
}
SCL=0;
delay_1us(2);
SDA=1;//дÍêÊý¾ÝºóÊÍ·ÅÐźÅ
delay_1us(1);
}
//´ÓSDAÊý¾ÝÏ߶ÁÈ¡Ò»¸öbyteÊý¾Ý
uchar IICReaderByte(void)
{
uchar i,receive=0x00;
P0INMODE;
SCL=0; //ÊÍ·ÅʱÖÓÏß
delay_1us(1);
SDA=1; //ÊÍ·ÅÊý¾ÝÏߣ¬Èç¹û²»À¸ß£¬±íʾSDAÒ»Ö±ÔÚͨÐÅ
delay_1us(1);
for(i=0;i<8;i++)
{
SCL=1;
delay_1us(2);
receive=(receive<<1)|SDA;
//Li1_led();
//Li1_led_OFF();
SCL=0;
delay_1us(2);
}
return receive;
}
//³õʼ»¯ ANSG08SH
void WriteSen(void)
{
uchar i;
P0CR=0x1e;
IICInit();
IICStart();
IICWriteByte(0x48); //ANSG08оƬµØÖ·+0(д²Ù×÷)
IICAck();
IICWriteByte(0x00); //ANSG08 EEPROM ÉèÖõØÖ·
IICAck();
IICWriteByte(0xAA); //ANSG08 EEPROM Êý¾Ý
IICAck();
IICWriteByte(0xff); //0x01µØÖ·ÖØÖÃÖµ
IICStart();
IICWriteByte(0x48); //ANSG08оƬµØÖ·+0(д²Ù×÷)
IICAck();
IICWriteByte(0x39); //´¥Ãþ°´¼üÊä³ö¼Ä´æÆ÷µØÖ·Ê×µØÖ·
for(i=0;i<8;i++)
{
IICAck();
IICWriteByte(0x16); //ÉèÖÃÁéÃô¶È
}
IICAck();
IICStart();
IICWriteByte(0x48); //ANSG08оƬµØÖ·+0(д²Ù×÷)
IICAck();
IICWriteByte(0x36); //È«¾ÖÑ¡Ïî¼Ä´æÆ÷µØÖ·Ê×µØÖ·
IICAck();
IICWriteByte(0x4f); //È«¾ÖÑ¡ÏîÊý¾Ý0x4f
IICNoAck();
//IICStop();
//Li1_led(); //²âÊÔÓ¦´ð
//Li1_led_OFF();
}
//´Ó´¥Ãþ¸ÐÓ¦¼Ä´æÆ÷ÖжÁÒ»¸ö´¥ÃþÅжϵÄÖµ
uchar ReadKey(void)
{
uchar key1;
//uchar key2;
// P0CR=0x3e; //ÊäÈëģʽ
//IICInit();
IICStart();
IICWriteByte(0x48); //ANSG08оƬµØÖ·+0(д²Ù×÷)
IICAck();
IICWriteByte(0x50); //´¥Ãþ°´¼üÊä³ö¼Ä´æÆ÷µØÖ·
IICAck();
//IICWriteByte(0x50); //´¥Ãþ°´¼üÊä³ö¼Ä´æÆ÷µØÖ·
//IICAck();
IICStart();
IICWriteByte(0x49); //ANSG08оƬµØÖ·+0(¶Á²Ù×÷)
IICAck();
key1=IICReaderByte(); //´¥Ãþ°´¼üÊä³ö¼Ä´æÆ÷µØÖ·ÖÐÊý¾Ý
//IICAck();
//key2=IICReaderByte(); //´¥Ãþ°´¼üÊä³ö¼Ä´æÆ÷µØÖ·ÖÐÊý¾Ý
IICNoAck();
//Li1_led(); //²âÊÔÓ¦´ð
//Li1_led_OFF();
//IICStop(); //³ÌÐò²»ÔËÐÐ
return key1;
}
uchar JudgeKey(uchar Data) //Åжϰ´¼ü±àÂë
{
uchar i;
for(i=2;i<=255;i++)
{
if(Data==i)
{
//Li1_led();
// Li1_led_OFF();
return 1;
}
}
/*
if((Data&0xFC)==0x04)
{
Li1_led();
return 1;
}
if((Data&0xFC)==0x08)
{
return 2;
}
if((Data&0xFC)==0x10)
{
return 3;
}
if((Data&0xFC)==0x20)
{
return 4;
}
if((Data&0xFC)==0x40)
{
return 5;
}
if((Data&0xFC)==0x80)
{
return 6;
}
*/
return 0;
}
//²¶»ñ°´¼üÐźÅ
void SignalCapture(void)
{
uchar key,num;
//KEY_INT=1;
//P0INMODE;
//Li1_led();
P0CR=0x3e;
//KEY_INT=0;
if((KEY_INT==0))
{
//while(KEY_INT);
//delay_1us(1);
num=ReadKey();
key=JudgeKey(num);
if(ReadKey()==0xff)
{
Li1_led();
Li1_led_OFF();
}
if(key==2)
{
Li1_led();
delay_50ms(20);
}
if(key==3)
{
Li1_led();
delay_50ms(20);
}
if(key==4)
{
Li1_led();
delay_50ms(20);
}
if(key==5)
{
Li1_led();
delay_50ms(20);
}
if(key==6)
{
Li2_led();
delay_50ms(20);
}
/*
Li1_led();
delay_50ms(20);
Li1_led_OFF();
delay_50ms(2);
*/
}
}
示波器上只有三个波段
读到的值是0xff。 |