#include<iostm8l052r8.h>
#include "RC522.h"
#include "delay.h"
#include "lpcd.h"
#include "config.h"
#include "main.h"
#include "Sound.h"
#include "ReadCard.h"
#include "Ble.h"
#define ADC_REFERNCE_MIN 0x0 //实际并联电容值最小
#define ADC_REFERNCE_MAX 0xEF //实际电容值最大
unsigned char T3ClkDivK ;
unsigned char LPCDBiasCurrent;
unsigned char LpcdGainReduce;//2bit
unsigned char LpcdGainAmplify;//3bit
unsigned char LpcdADCRefernce;//7bit
unsigned char Timer1Cfg;//4bit
unsigned char Timer2Cfg;//5bit
unsigned char Timer3Cfg;//5bit
unsigned char ADCResultFullScale; //T3下满幅ADCResult信息
unsigned char ADCResultThreshold; //检测幅度,设置成相对值
unsigned char LpcdThreshold_L; //LPCD幅度低阈值
unsigned char LpcdThreshold_H; //LPCD幅度高阈值
unsigned char ADCResultCenter; //LPCD幅度中心点
unsigned char LpcdADCResult[10]; //Lpcd幅度信息,用于误触发判断
lpcd_para_t Lpcd = {0};
/////////////////////////////////////////////////////////////////////
//功 能:写NXP520 寄存器
//参数说明:addr:寄存器地址
// date:数据
/////////////////////////////////////////////////////////////////////
void nxp520_write_reg(unsigned char addr,unsigned char date)
{
//rc522Write(addr,date);
WriteRawRC(addr,date);
}
/////////////////////////////////////////////////////////////////////
//功 能:读NXP520 寄存器
//参数说明:addr:寄存器地址
/////////////////////////////////////////////////////////////////////
unsigned char nxp520_read_reg(unsigned char addr)
{
unsigned char ret ;
//rc522Read(addr,&ret);
ret=ReadRawRC(addr);
return ret;
}
/*---------------------------------------------------------------------------
函数名称:nxp520_write_exreg
函数功能:向扩展寄存器写数据
入口参数:addr1 一级地址
addr2 二级地址
data 写入数据
出口参数:
创建时间:2015年6月3日
备 注:
-----------------------------------------------------------------------------*/
void nxp520_write_exreg(unsigned char addr1,unsigned char addr2,unsigned char data)
{
// send_uart2_byte(addr1&0x7F);/*设置成写模式*/
// send_uart2_byte((addr2&0x3F)|0x40);/*写入二级地址*/
// send_uart2_byte(addr1&0x7F);/*设置成写模式*/
// send_uart2_byte((data&0x3F)|0xC0);/*写入数据*/
//rc522Write(addr1,addr2+0x40);/*写入二级地址*/
WriteRawRC(addr1,addr2+0x40);
//rc522Write(addr1,data+0xC0);/*写入数据*/
WriteRawRC(addr1,data+0xC0);
}
/*---------------------------------------------------------------------------
函数名称:nxp520_read_exreg
函数功能:读扩展寄存器数据
入口参数:addr1 一级地址
addr2 二级地址
出口参数:
创建时间:2015年6月3日
备 注:
-----------------------------------------------------------------------------*/
unsigned char nxp520_read_exreg(unsigned char addr1,unsigned char addr2)
{
unsigned char ret ;
//rc522Write(addr1,addr2+0x80);/*写入二级地址*/
WriteRawRC(addr1,addr2+0x80);
//rc522Read(addr1,&ret);
ret=ReadRawRC(addr1);
return ret;
}
/*---------------------------------------------------------------------------
函数名称:LPCD_Init
函数功能:低功耗外部卡片侦测
入口参数:
出口参数:
创建时间:2015年6月3日
备 注:
-----------------------------------------------------------------------------*/
void LPCD_Init(void)
{
//rc522Write(WaterLevelReg,0x20);/*接收一半数据产生中断*/
WriteRawRC(WaterLevelReg,0x20);
}
/*---------------------------------------------------------------------------
函数名称:LPCD_ParamInit
函数功能:LPCD参数设置
入口参数:
出口参数:
创建时间:2015年6月3日
备 注:
-----------------------------------------------------------------------------*/
void LPCD_ParamInit(void)
{
LPCDBiasCurrent = LPCD_BIAS_CURRENT ; //由样片测试指标决定,在config文件中设定
LpcdGainReduce = 0x3; //1x
LpcdGainAmplify = 0x0; //1x
LpcdADCRefernce = 0;
Timer1Cfg = TIMER1_CFG;
Timer2Cfg = TIMER2_CFG;
Timer3Cfg = TIMER3_CFG;
if (Timer3Cfg > 0xF) //Timer3Cfg用到5bit,所以只能选择16分频
{
T3ClkDivK = 2; //16分频
ADCResultFullScale = ((Timer3Cfg - 1)<<3);
ADCResultCenter = (ADCResultFullScale >>1);
ADCResultThreshold = (ADCResultFullScale >> LPCD_THRESHOLD_RATIO);
}
else if(Timer3Cfg > 0x7) //Timer3Cfg用到4bit,所以只能选择8分频
{
T3ClkDivK = 1; //8分频
ADCResultFullScale = ((Timer3Cfg - 1)<<4);
ADCResultCenter = (ADCResultFullScale >>1);
//ADCResultThreshold = (ADCResultFullScale >> LPCD_THRESHOLD_RATIO);
ADCResultThreshold = CARD_LPCD_REF;
}
else
{
T3ClkDivK = 0; //4分频
ADCResultFullScale = ((Timer3Cfg - 1)<<5);
ADCResultCenter = (ADCResultFullScale >>1);
ADCResultThreshold = (ADCResultFullScale >> LPCD_THRESHOLD_RATIO);
}
LpcdThreshold_H = ADCResultCenter + ADCResultThreshold;
LpcdThreshold_L= ADCResultCenter - ADCResultThreshold;
}
/*---------------------------------------------------------------------------
函数名称:LpcdRegisterInit
函数功能:LPCD寄存器初始化
入口参数:
出口参数:
创建时间:2015年6月3日
备 注:
-----------------------------------------------------------------------------*/
void LpcdRegisterInit(void)
{
/*中断设置*/
nxp520_write_reg(ComIEnReg,0x80);
nxp520_write_reg(DivlEnReg,0x80);
/*LpcdCtrl1*/
nxp520_write_exreg(ExReg,LpcdCtrl1,0x02);/*复位寄存器*/
nxp520_write_exreg(ExReg,LpcdCtrl1,0x22);/*复位放开寄存器*/
nxp520_write_exreg(ExReg,LpcdCtrl1,0x21);/*使能LPCD功能*/
nxp520_write_exreg(ExReg,LpcdCtrl1,(LPCD_IE<<5)|0x10);/*使能LPCD中断*/
nxp520_write_exreg(ExReg,LpcdCtrl1,(LPCD_AUTO_DETECT_TIMES<<5)|0x08);/*配置进场检测次数*/
/*LpcdCtrl2*/
nxp520_write_exreg(ExReg,LpcdCtrl2,((LPCD_TX2RFEN<<4)+(LPCD_CWN<<3)+LPCD_CWP));/*TX2使能,N管大驱动,P管驱动能力为4*/
/*LpcdCtrl3*/
nxp520_write_exreg(ExReg,LpcdCtrl3,LPCD_MODE<<3);/*低功耗模式为DPD,HPDEN=0*/
/*LpcdT1Cfg*/
nxp520_write_exreg(ExReg,LpcdT1Cfg,(T3ClkDivK<<4)+Timer1Cfg);/*T3分频,T1阶段时长*/
/*LpcdT2Cfg*/
nxp520_write_exreg(ExReg,LpcdT2Cfg,Timer2Cfg);/*T2阶段时长*/
/*LpcdT3Cfg*/
nxp520_write_exreg(ExReg,LpcdT3Cfg,Timer3Cfg);/*T2阶段时长*/
/*LpcdVmidBdCfg*/
nxp520_write_exreg(ExReg,LpcdVmidBdCfg,VMID_BG_CFG);/*配置Vmid建立时间*/
/*LpcdAutoWupCfg*/
nxp520_write_exreg(ExReg,LpcdAutoWupCfg,(AUTO_WUP_EN<<3)+AUTO_WUP_CFG);/*配置自动唤醒时间,12s*/
/*LpcdThresholdMin_L*/
nxp520_write_exreg(ExReg,LpcdThresholdMin_L,(LpcdThreshold_L & 0x3F));/*卡片侦测下阈值低6位*/
/*LpcdThresholdMin_H*/
nxp520_write_exreg(ExReg,LpcdThresholdMin_H,(LpcdThreshold_L>>6));/*卡片侦测下阈值高2位*/
/*LpcdThresholdMax_L*/
nxp520_write_exreg(ExReg,LpcdThresholdMax_L,(LpcdThreshold_H& 0x3F));/*卡片侦测上阈值低6位*/
/*LpcdThresholdMax_H*/
nxp520_write_exreg(ExReg,LpcdThresholdMax_H,(LpcdThreshold_H>>6));/*卡片侦测上阈值高2位*/
/*LpcdAutoWupCfg*/
nxp520_write_exreg(ExReg,LpcdAutoWupCfg,(AUTO_WUP_EN<<3)+AUTO_WUP_CFG);/*配置自动唤醒时间,12s*/
}
/*---------------------------------------------------------------------------
函数名称:LpcdInitCalibra
函数功能:Lpcd 调校
入口参数:*CalibraFlag调校标志,是否需要调校
出口参数: TRUE:调校成功 FALSE:调校失败
创建时间:2015年6月3日
备 注:
-----------------------------------------------------------------------------*/
unsigned char LpcdInitCalibra(unsigned char *CalibraFlag)
{
unsigned char ret;
unsigned char ADCResult; /*LPCD幅度信息*/
unsigned char GainCalibraFlag; /*增益调教结果*/
unsigned char ADCResult_Pre; /*幅度信息的前一个值*/
/*LpcdCtrl4*/
nxp520_write_exreg(ExReg,LpcdCtrl4,((LpcdGainAmplify << 2) + LpcdGainReduce));/*配置增益*/
/*LpcdBiasCurrent*/
nxp520_write_exreg(ExReg,LpcdBiasCurrent,((LpcdADCRefernce&0x40)>>1)+LPCDBiasCurrent&0x7);/*配置基准电流和参考电压*/
/*LpcdMisc*/
nxp520_write_exreg(ExReg,LpcdMisc,0x01);/*Calib