//#include <iom168pa.h>
//#include <Avr_macros.h>
//#include <Ina90.h>
//#include <string.h>
//#include "stm8l15x.h"
#include<iostm8l052r8.h>
#include "config.h"
#include "main.h"
#include "Sound.h"
#include "delay.h"
#include "RC522.h"
#include "TSM12.h"
#include "IIC.h"
extern uchar key_down;
extern uchar key_ok;
extern uchar key_eff;
extern uchar time_count;
extern uchar lock_flag;
extern unsigned char DOORBELL_Flag;
extern unsigned char key_count;
//------------------------------------
unsigned char reg_w_data[8];
unsigned char reg_r_data[22];
unsigned char NbData;
unsigned char ack;
unsigned char WAddress;
unsigned char RAddress;
//***************************************
void Go_sleep_mode(void)
{
TSM12_sleep_Init();
I2C_EN_off(); //TSM12 I2C Enable
Delayms(50);
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
reg_w_data[0] = 0x09; //Reg. Address 0x09
reg_w_data[1] = 0x07; //0x09狼 Data
NbData = 2; //Write Data肮荐
I2c_Write(); // I2C Write Start
I2C_EN_on(); //TSM12 I2C disable
}
void Exit_sleep_mode(void)
{
I2C_EN_off(); //TSM12 I2C Enable
Delayms(50);
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
reg_w_data[0] = 0x09; //Reg. Address 0x09
reg_w_data[1] = 0x03; //0x09狼 Data
NbData = 2; //Write Data肮荐
I2c_Write(); // I2C Write Start
I2C_EN_on(); //TSM12 I2C disable
//TSM12_Init();
}
void TSM12_Init(void)
{
// H/W POWER ON RESET DELAY 100mSEC
I2C_EN_off(); //TSM12 I2C Enable
Delayms(1);
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
// S/W RESET
reg_w_data[0] = 0x09; //Reg. Address 0x09
reg_w_data[1] = 0x0F; //0x09狼 Data
NbData = 2; //Write Data肮荐
I2c_Write(); // I2C Write Start
// Sensitivity Set
reg_w_data[0] = 0x02; //Reg. Address 0x02
reg_w_data[1] = 0x11; //0x02狼 Data 000最灵敏感,111最不灵 0x55
reg_w_data[2] = 0x11; //0x03狼 Data
reg_w_data[3] = 0x11; //0x04狼 Data PIN4-CS6 PIN3-CS5
reg_w_data[4] = 0x11; //0x05狼 Data PIN6-CS8 PIN5-CS7
reg_w_data[5] = 0x11; //0x06狼 Data PIN7 -CS9 PIN9-CS10
reg_w_data[6] = 0x11; //0x07狼 Data PIN12 -CS12
NbData = 7; //Write Data肮荐
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
I2c_Write(); // I2C Write Start
reg_w_data[0] = 0x08; //Reg. Address 0x08
reg_w_data[1] = 0x33; //0x08狼 Data
//reg_w_data[1] = 0x2B; //低中高等级都产生中断
//reg_w_data[1] = 0x3B; //0x08狼 Data
NbData = 2; //Write Data肮荐
I2c_Write(); // I2C Write Start
reg_w_data[0] = 0x0A; //Reg. Address 0x0A
reg_w_data[1] = 0x00; //0x0A狼 Data
reg_w_data[2] = 0x00; //0x0B狼 Data
reg_w_data[3] = 0x00; //0x0c狼 Data可以开关单独通道
reg_w_data[4] = 0x00; //0x0D狼 Data
reg_w_data[5] = 0x00; //0x0c狼 Data
reg_w_data[6] = 0x00; //0x0D狼 Data
NbData = 7; //Write Data肮荐
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
I2c_Write(); // I2C Write Start
// S/W Reset OFF
reg_w_data[0] = 0x09; //Reg. Address 0x09
reg_w_data[1] = 0x03; //0x09狼 Data
NbData = 2; //Write Data肮荐
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
I2c_Write(); // I2C Write Start
I2C_EN_on(); //TSM12 I2C disable
}
void TSM12_sleep_Init(void)
{
// H/W POWER ON RESET DELAY 100mSEC
I2C_EN_off(); //TSM12 I2C Enable
Delayms(10);
//reg_w_data[0] = 0x08; //Reg. Address 0x08
//reg_w_data[1] = 0x3B; //0x08狼 Data
//reg_w_data[1] = 0x2B; //低中高等级都产生中断
//NbData = 2; //Write Data肮荐
//I2c_Write(); // I2C Write Start
// Sensitivity Set
reg_w_data[0] = 0x02; //Reg. Address 0x02
reg_w_data[1] = 0x11; //0x02狼 Data 000最灵敏感,111最不灵 0x55
reg_w_data[2] = 0x11; //0x03狼 Data
reg_w_data[3] = 0x11; //0x04狼 Data 30 PIN4-CS6 PIN3-CS5
reg_w_data[4] = 0x11; //0x05狼 Data
reg_w_data[5] = 0x33; //0x06狼 Data 33 PIN7 -CS9 PIN9-CS10
reg_w_data[6] = 0x11; //0x07狼 Data 30
NbData = 7; //Write Data肮荐
WAddress = 0xD0; // TSM12 Write Address
RAddress = 0xD1; // TSM12 Read Address
I2c_Write(); // I2C Write Start
I2C_EN_on(); //TSM12 I2C disable
}
//--------------------------硬件IIC多字节读
void I2c_Read(void)
{
uchar i;
i2cstart();
i2cwt(RAddress);
for(i=0;i<NbData;i++)
{
if(i<(NbData-1))
reg_r_data[i]=i2crd();
else
reg_r_data[i]=i2crd_noack();
}
i2cstop();
}
//------------------------硬件IIC多字节写
void I2c_Write(void)
{
uchar i;
i2cstart();
i2cwt(WAddress);
for(i=0;i<NbData;i++)
{
i2cwt(reg_w_data[i]);
}
i2cstop();
}
/*******************************************
函数名称: 按键扫描
功 能:
参 数:
返回值 : 无
********************************************/
unsigned char key_scan(void)
{
uchar i,j,key_val,key_code;
if(!TSM_INT1()) //低电平有按键
{
key_count=0;
I2C_EN_off(); //TSM12 I2C Enable
Delayms(1);
WAddress = 0xD0;
RAddress = 0xD1;
reg_w_data[0] = 0x10; //Reg. Address 0x10
NbData = 1;
I2c_Write(); // I2C Write Start
//------------------------
NbData = 3; // Read Data 肮荐
I2c_Read(); // I2C Read
Delayms(1);
I2C_EN_on(); //TSM12 I2C Enable
//-----------------------
for(i=0;i<3;i++)
{
for(j=1;j<5;j++)
{
if((reg_r_data[i]&0x03)>0)
{
TSM12_Init();
key_val=i*4+j;
if(lock_flag==0 && DOORBELL_Flag!=2) set_bird_singing(); //蜂鸣器发声(鸟叫)
//SPK_ON;
//NVDAudiSendByte(99);//?脲:嘀
//TSM12_Init();
key_code=key_decode(key_val);
time_count=0;
return key_code;
}
reg_r_data[i]>>=2;
}
}
}
return 0xff;
}
/*******************************************
函数名称: key_decode
功 能: 键值转换
参 数:
返回值 : 0x23-># 0x2a->*
********************************************/
uchar key_decode(uchar dat)
{
switch (dat)
{
case 1: LED9_CPL ;Delayms(50);LED9_CPL ;return 9;
case 2: LED11_CPL ;Delayms(50);LED11_CPL;return '#';
case 3: LED4_CPL ;Delayms(50);LED4_CPL ;return 4;
case 4: LED10_CPL ;Delayms(50);LED10_CPL ;return '*';
case 5: LED3_CPL ;Delayms(50);LED3_CPL ;return 3;
case 6: LED2_CPL ;Delayms(50);LED2_CPL ;return 2;
case 7: LED1_CPL ;Delayms(50);LED1_CPL ;return 1;
case 8: LED6_CPL ;Delayms(50);LED6_CPL ;return 6;
case 9: LED5_CPL ;Delayms(50);LED5_CPL ;return 5;
case 10: LED0_CPL ;Delayms(50);LED0_CPL ;return 0;
case 11: LED7_CPL ;Delayms(50);LED7_CPL ;return 7;
case 12: LED8_CPL ;Delayms(50);LED8_CPL ;return 8;
}
return 0xff;
}
/*unsigned char key_scan_exit(void)
{
uchar i,j,key_val;
if(!TSM_INT1()) //低电平有按键
{
key_count=0;
I2C_EN_off(); //TSM12 I2C Enable
Delayms(1);
W