单片机源程序如下:
/**
******************************************************************************
* @file FDC2214.c
* @author BOB
* @version V1.0
* @date 2018-07-5
* @email
* @brief FDC2214配置及数据读取函数
******************************************************************************
**/
#include
#include "fdc2214.h"
#include "iic.h"
#include "math.h"
#include "Config.h"
//ADDR和SD引脚初始化
#define FDC_ADDR_0 P1OUT &= ~BIT4 //FDC_ADDR置低
#define FDC_ADDR_1 P1OUT |= BIT4 //FDC_ADDR置高
#define FDC_SD_0 P1OUT &= ~BIT5 //FDC_SD置低
#define FDC_SD_1 P1OUT |= BIT5 //FDC_SD置高
/***************************************
* 函数名:ADDR_SD_Init
* 描述 :配置LDC用到的I/O口
ADDR--PC11
SD --PC12
* 输入 :无
* 输出 :无
**************************************/
/*
void ADDR_SD_Init(void) //ADDR和SD引脚初始化
{
}
*/
/***************************************
* 函数名:InitFDC2214
* 描述 :初始化FDC2214
* 输入 :无
* 输出 :retVal 0 and 1
**************************************/
u8 FDC2214_init(void)
{
u16 id;
u8 retVal=1;
u8 ErrCount = 0;
FDC_ADDR_0;// ADDR=0(FDC2214_IIC地址选择=0X2A) // ADDR=1(FDC2214_IIC地址选择=0X2B)
FDC_SD_0; // SD=0开启输入 // SD=1关闭输入
default_addr = EVM_DEFAULT_I2CADDR; //FDC2214地址选择暂存
delay_ms(10);
// software reset
MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_RESET_DEVICE,0x8000);
delay_ms(10);
do
{
if (ErrCount++ > 50)
{
return 0;
}
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_REF_COUNT_CH0,0xFFFF); // 4 clock periods
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_REF_COUNT_CH1,0xFFFF);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_REF_COUNT_CH2,0xFFFF);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_REF_COUNT_CH3,0xFFFF);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_OFFSET_CH0,0x0000); //补偿值
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_OFFSET_CH1,0x0000);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_OFFSET_CH2,0x0000);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_OFFSET_CH3,0x0000);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_SETTLE_COUNT_CH0,0x0400); // 1 clock period
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_SETTLE_COUNT_CH1,0x0400);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_SETTLE_COUNT_CH2,0x0400);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_SETTLE_COUNT_CH3,0x0400);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_CLOCK_DIVIDERS_CH0,0x1001); // 1000
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_CLOCK_DIVIDERS_CH1,0x1001);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_CLOCK_DIVIDERS_CH2,0x1001);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_CLOCK_DIVIDERS_CH3,0x1001);
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_DRIVE_CURRENT_CH0,0xF800); //
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_DRIVE_CURRENT_CH1,0xF800); //
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_DRIVE_CURRENT_CH2,0xF800); //
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_DRIVE_CURRENT_CH3,0xF800); //
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_ERROR_CONFIG,0x0001); // report only DRDYs to INT
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_MUX_CONFIG,0xC20C); // ch0, ch1,ch2,ch3-> Wipro for 4 ch
retVal &= MPU_Write_2Byte(default_addr,LDC13xx16xx_CMD_CONFIG,0x1E01); // CLKIN pin1281
}while(retVal!=0);
// id=MPU_Read_2Byte(default_addr,LDC13xx16xx_CMD_DEVID);//读设备ID
// if(id==0x3055||id==0x3054)
// {
// return 1;
// }
// else
// {
// return 0;
// }
return 1;
}
/***************************************
* 函数名:evm_processDRDY
* 描述 :读取寄存器数据
* 输入 :无
* 输出 :无
**************************************/
void evm_processDRDY(unsigned long allData[4])
{
u8 i=0;
unsigned long reading = 0;
float CAP_Data[4];
float conver = 0; //最终的转换的结果
for(i=0;i<=3;i++)
{ //0x0FFF 最高到只28位
reading = (unsigned long)(MPU_Read_2Byte(default_addr,i*2) & 0x0FFF) << 16;
reading |= MPU_Read_2Byte(default_addr,(i*2)+1);
allData=reading;
//CAP_Data=reading;
reading=0;
}
//***注意Init中各通道寄存器0x10-0x17的时钟分频***//
//*******以下四步计算根据datasheet P19公式*******//
for(i=0;i<=3;i++)
{
CAP_Data = (CAP_Data * 40000000.0F)/268435456.0F;//得到F(sensorx)
CAP_Data = CAP_Data * 2.0F *3.1515926F;
CAP_Data = 1.0F/((CAP_Data * CAP_Data) * 0.000018F);
CAP_Data = CAP_Data - 0.000000000033F;
CAP_Data = CAP_Data * 1000000000000.0F;//F转换pF;
//allData=CAP_Data;
}
}
/***********************************/
/******FDC2214检测电容值程序********/
/************************************/
double GetCapacitance(unsigned long value)
{
double pi = 3.14159265359;
double L = 18; //uH
double Cboard = 33; //pf
double Cparacitic = 3; //pf
unsigned long long fsensor;
double temp;
fsensor = 1 * 40000000 * value;
fsensor = fsensor / pow(2,28);
temp = 2 * pi * fsensor;
temp = temp * temp;
temp = temp / 1000000; //uH
temp *= L;
return temp;
}