/*
* ltc2945.cpp
*
* Created on: 2012-12-17
* Author: YangYao
*/
#include "../stdafx.h"
//外部Pro对象
extern PROC_INFO g_Pro;
CLtc2945::CLtc2945()
{
//CLtc2945构造函数;
alt_u8 OutVoltage = 16;
}
CLtc2945::~CLtc2945()
{
//CLtc2945析构函数;
}
alt_u8 CLtc2945::ReadByte(alt_u8 cRegAddress)
{
alt_u8 rec_data;
g_Pro.m_I2c.I2c_Read_Data(I2C_BASE,LTC2945_ADDR , cRegAddress, &rec_data, 1);
return rec_data;
}
void CLtc2945::WriteByte(alt_u8 cRegAddress, alt_u8 cData)
{
g_Pro.m_I2c.I2c_Write_Data(I2C_BASE,LTC2945_ADDR , cRegAddress, &cData, 1);
}
void CLtc2945:: Ltc2945Init(void)
{
WriteByte(R_CONTROL , 0x21); //0x21
}
alt_u16 CLtc2945::VoltageGet(void)
{
alt_u8 highbyte, midbyte, lowbyte;
alt_u32 deltavoltagetemp, powervoltagetemp;
alt_u16 resultvoltage;
//printf("-----------------------------------------------------\n");//02X
highbyte = ReadByte(R_DELTA_SENSE_MSB);
lowbyte = ReadByte(R_DELTA_SENSE_LSB);
deltavoltagetemp = (highbyte * 256 + lowbyte)/16;
/*printf("DELTA_SENSE: %d ",highbyte);//02X
printf(" %d ",lowbyte);//02X
printf(" %d !\n",deltavoltagetemp);//02X
printf("--\n");//02X*/
highbyte = ReadByte(R_POWER_MSB2);
midbyte = ReadByte(R_POWER_MSB1);
lowbyte = ReadByte(R_POWER_LSB);
powervoltagetemp = highbyte * 65536 + midbyte * 256 + lowbyte;
/*printf("POWER: %d ",highbyte);//02X
printf(" %d ",midbyte);//02X
printf(" %d ",lowbyte);//02X
printf(" %d !\n",powervoltagetemp);//02X
printf("--\n");//02X*/
resultvoltage = powervoltagetemp/deltavoltagetemp;
/*printf("resultvoltage: %d !\n",resultvoltage);//02X
printf("-----------------------------------------------------\n");//02X*/
return resultvoltage;
}
alt_u8 CLtc2945::VoltageConversion(void* pPara)
{
PROC_INFO * pProc = (PROC_INFO *) pPara;
alt_u16 VoltageGetTmp;
alt_u8 out_result;
VoltageGetTmp = VoltageGet();
//错误检测
if(VoltageGetTmp <= pProc->m_Fpga.IR_Info.voltage_min)
{
out_result = 0;
}
else if(VoltageGetTmp >= pProc->m_Fpga.IR_Info.voltage_max)
{
out_result = 16;
}
else
{
//电压值映射
out_result = (alt_u8)((VoltageGetTmp - pProc->m_Fpga.IR_Info.voltage_min)/V_STEP);
}
return out_result;
}
alt_u8 CLtc2945::VoltageFilter(alt_u8 *sSrcAddrTemp, alt_u8 iArryLenth)
{
alt_u8 i=0,j=0,num=0;
alt_u8 read_Voltage[iArryLenth], Voltage_num[iArryLenth];
alt_u8 max=0,current_V=0;
memcpy(read_Voltage, sSrcAddrTemp, iArryLenth);
//统计每个值出现的次数
for(i=0;i<iArryLenth;i++)
{
Voltage_num[i]=1;
for(j=i+1;j<iArryLenth;j++)
{
if(read_Voltage[i]==read_Voltage[j])
Voltage_num[i]++;
}
}
//找出出现次数最多的元素的下标
max=Voltage_num[0];
for(i=0;i<iArryLenth;i++)
{
if(Voltage_num[i]>max)
{
max=Voltage_num[i];
num=i;
}
}
current_V=read_Voltage[num];
return current_V;
}
评论0