#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include <string.h>
#include "LED.h"
#include "PWM.h"
#include "QI.h"
#include "ADC.h"
static volatile UINT16 xdata qi_dly_tick = 0; //QI延时计数用
static volatile UINT16 xdata qi_rx_cmd_timeout = 0; //QI接收命令超时,认为是充电设备被移开,需要停止电能传输
static volatile UINT8 xdata rx_signal_strength; //接收机的信号强度
static volatile UINT8 xdata rx_power_class; //接收机的类型
static volatile UINT8 xdata max_rx_power; //接收机的最大功率
static volatile UINT8 xdata rx_control_error; //误差控制参数
static volatile bit rx_control_error_flag = 0; //误差控制执行标志
static QI_POWER_TRANSFER_PHASE_TYPEDEF data QiPowerChargeState = Idle_Phase;
unsigned char xdata VoitCodeBuf[27] = {0x00};
UINT16 FRQ,ADC_Current;
UINT8 LED;
static void SignalStrengthParse(void)
{
rx_signal_strength = VoitCodeBuf[2];
if(QiPowerChargeState == Selection_Phase)
{//如果当前为PING阶段,进入配置阶段
QiPowerChargeState = Ping_Phase;
// qi_wait_ms = 1;
}
}
void Decode(uint16_t Number) //QI解码
{
UINT16 Time,Data1;
UINT8 Abit,Record,DataReceLen,oddeven,Databit,bit1,DataSetLen,Single,Verify,C,sum;
bit A,CodeStep;
Time = 0;
Time = Number>>4;
// P14 = 1;
if((Time>350)&&(Time<=600))
{
if(A == 1)
{
A =0;
}
Abit = 0X3F; //接收到0
}
if((Time>150)&&(Time<350))
{
if(A == 1)
{
Abit = 0X06;//接收到1
A=0;
}
else
{
A = 1;
Abit = 0X00;
}
}
switch(Abit)
{
case 0x00://没有收到数据
break;
case 0X3F://字节0
Single = 0x00;
if((CodeStep == 0)&&(Record<8))
{
Record = 0; //Restart
}
if((CodeStep == 0)&&(Record>=8))
{
// DataReceLen = 0;
VoitCodeBuf[DataReceLen++] = Record;
CodeStep = 1;
Record = 1;
Data1 = 0x0000;
}
else
{
Data1= Data1>>1; //读入数据位
Record= Record+1; //数据位增加'1'
}
break;
case 0X06://字节1
Single = 0x00;
if(CodeStep == 0)
{
Record++;
}
else
{
Data1 = (Data1 >>1)|0x8000;
Record= Record+1;
if(Record ==11)
{
Record = 0;
oddeven = 0;
Data1 = Data1 >>6;
Verify = (Data1 >>8)&0x01;//奇偶校验位
Data1 = Data1 & 0x00FF;
Databit = Data1;
for(C=0;C<8;C++)
{
bit1 = Databit &0x01;
Databit>>=1;
if(bit1 == 0x01)
{
oddeven++;
}
}
if((oddeven==0)||(oddeven==2)||(oddeven==4)||(oddeven==6)||(oddeven==8))
{
if(Verify == 1)
VoitCodeBuf[DataReceLen++] = Data1;
else
{
Record = 0; //Restart
CodeStep = 0;
DataReceLen = 0;
}
}
else
{
if(Verify == 0)
VoitCodeBuf[DataReceLen++] =Data1;
else
{
Record = 0; //Restart
CodeStep = 0;
DataReceLen = 0;
}
}
if(DataReceLen == 2)
{
// qi_rx_cmd_timeout = SYSTEM_TICK * 3;
// ControlError = 1;
if(VoitCodeBuf[1] <= 0x1F) DataSetLen = (VoitCodeBuf[1] >> 5) + 1;
else if(VoitCodeBuf[1] <= 0x7F) DataSetLen = ((VoitCodeBuf[1]-32) >> 4)+2;
else if(VoitCodeBuf[1] <= 0xDF) DataSetLen = (VoitCodeBuf[1] >> 3) - 8;
else DataSetLen = (VoitCodeBuf[1] >> 2) - 36;
DataSetLen += 3; //数据位长度 + 一个引导位,一个效验位,一个数据包头
}
else if((DataReceLen == DataSetLen)&&(DataSetLen != 0x00))
{
sum = 0x00;
for(C = 1;C<(DataSetLen-1);C++)
{
sum = sum^VoitCodeBuf[C];
}
if(sum ==VoitCodeBuf[DataSetLen-1])
{
qi_rx_cmd_timeout = SYSTEM_TICK * 3;
switch(VoitCodeBuf[1])
{
case 0x01://信号强度
SignalStrengthParse();
break;
case 0x71://版本信息
IdentificationParse();
break;
case 0x51://配置信息
ConfigurationParse();
break;
case 0x03://
ControlErrorParse();
break;
case 0x04://
break;
case 0x02://
EndPowerTransferParse();
break;
case 0x05://
break;
case 0x06://
break;
case 0x81://
break;
}
memset(VoitCodeBuf,0x00,sizeof(VoitCodeBuf));
DataReceLen = 0;
CodeStep = 0;
Data1 = 0x0000;
}
}
}
else if(Record>11)
{
// printf("\n error");
Record = 0;
CodeStep = 0;
DataReceLen = 0;
Data1 = 0x0000;
memset(VoitCodeBuf,0x00,sizeof(VoitCodeBuf));
}
}
break;
}
}
static void FOD_Ping(void) //FOD异物检测
{
// if((Idle_Phase<QiPowerChargeState)&&(QiPowerChargeState<Identify_Config_Phase))
// {
ADC_Current = ADC_transition(1);
ADC_Current = ADC_Current /21;
ADC_Current = ADC_Current/5;
if(ADC_Current>21)
{
Pwm_Turn_Off();
Timer0_Delay1ms(90);
LED = 1;
QiPowerChargeState = Selection_Phase;
}
else
{
LED = 0;
}
// ADC_Voltage = ADC_transition(4);
// ADC_Voltage = ADC_Voltage/100;
// ADC_Voltage = ADC_Voltage*2;
// printf("\n Single3 %d",ADC_Current);
// ADC_Power = ADC_Voltage*ADC_Current;
// }
}
static void QiPing(void)
{
if(qi_dly_tick) return;
qi_dly_tick = SYSTEM_TICK - 10;
FRQ = WPC_QI_INIT_FREQ;
Pwm_SetFrq(FRQ);
Timer0_Delay1ms(90);
if(QiPowerChargeState == Selection_Phase)
{//如果PING成功了,不关闭PWM
Pwm_Turn_Off();
}
FOD_Ping(); //Ping阶段异物检测
}
static void QiConfig(void)
{
Timer0_Delay1ms(270);
if(QiPowerChargeState == Ping_Phase)
{//如果PING成功了,不关闭PWM
Pwm_Turn_Off();
QiPowerChargeState = Idle_Phase;
}
}
static void IdentificationParse(void)
{
// rx_signal_strength = VoitCodeBuf[2];
if(QiPowerChargeState < Ping_Phase)
{//如果当前为PING阶段,进入配置阶段
QiPowerChargeState = Ping_Phase;
// qi_wait_ms = 1;
}
}
static void ConfigurationParse(void)
{
rx_power_class = (VoitCodeBuf[2] >> 6) & 0x03;
max_rx_power = VoitCodeBuf[2] & 0x3f;
if(QiPowerChargeState < Identify_Config_Phase)
{//如果当前为PING阶段,收到配置信息后进入功率传输阶段
QiPowerChargeState = Identify_Config_Phase;
FRQ = 125;
Pwm_SetFrq(FRQ);
// qi_wait_ms = 1;
}
}
static void ControlErrorParse(void)
{
if(QiPowerChargeState == Identify_Config_Phase)
{
rx_control_error = VoitCodeBuf[2];
rx_control_error_flag = 1;
}
}
static void QiTransfer(void)//根据解码调节发射端的PWM
{
if(rx_control_error_flag)
{
评论10