FDC1004学习笔记二:读写数据
FDC1004采用IIC通信,不熟悉的请看这篇博客:FDC1004学习笔记一:IIC通信。
一、通信速率 10k~400k
二、读写操作
2.1写时序
先发送7为地址+“0”,然后发送8位地址,发送两个字节写入数据。
bool fdc1004_write(uint8_t chRegAddr,uint8_t chMSB,uint8_t chLSB)
{if(!I2C_Start()){return false;}I2C_SendByte(FDC1004_COMM_ID & 0xFE); //发送写命令I2C_WaitAck();I2C_SendByte(chRegAddr); //发送地址if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}I2C_SendByte(chMSB);if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}I2C_SendByte(chLSB);if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}I2C_Stop(); //注意:因为这里要等待写完//Systick_Delay_1ms(10);//delay_nms_soft(20);I2C_delay();return TRUE;
}
2.2读时序
(1)先改变指针寄存器地址:发送7为地址+“0”,然后发送8位地址,结束;
(2)执行读取操作:发送7为地址+“1”,然后读取两字节内容,结束。
uint16_t fdc1004_read(uint8_t chRegAddr)
{union{struct{uint8_t chLSB;uint8_t chMSB;};uint16_t hwValue;}temp;temp.hwValue = 0;//修改地址指针寄存器if(!I2C_Start()){return false;}I2C_SendByte(FDC1004_COMM_ID & 0xFE); //发送写命令I2C_WaitAck();I2C_SendByte(chRegAddr); //发送地址I2C_WaitAck();I2C_delay();I2C_Stop(); //注意:因为这里要等待写完,可以采用查询或延时方式(10ms)//Systick_Delay_1ms(10);//delay_nms_soft(20);I2C_delay();//读取数据if(!I2C_Start()){return false;}I2C_SendByte(FDC1004_COMM_ID | 0x01); //发送读命令 if(!I2C_WaitAck()){I2C_Stop(); return false;}temp.chMSB = I2C_ReceiveByte();I2C_Ack();temp.chLSB = I2C_ReceiveByte();I2C_NoAck();I2C_Stop(); //注意:因为这里要等待写完//Systick_Delay_1ms(10);//delay_nms_soft(20);I2C_delay();return temp.hwValue;
}
三、寄存器地址及定义
//地址
#define FDC1004_COMM_ID 0xA0
#define FDC1004_DEVICE_ID 0x1004
#define FDC1004_TI_ID 0x5449//FDC1004寄存器地址
#define MEAS1_MSB_ADDR 0x00
#define MEAS1_LSB_ADDR 0x01
#define MEAS2_MSB_ADDR 0x02
#define MEAS2_LSB_ADDR 0x03
#define MEAS3_MSB_ADDR 0x04
#define MEAS3_LSB_ADDR 0x05
#define MEAS4_MSB_ADDR 0x06
#define MEAS4_LSB_ADDR 0x07
#define CONF_MEAS1_ADDR 0x08
#define CONF_MEAS2_ADDR 0x09
#define CONF_MEAS3_ADDR 0x0A
#define CONF_MEAS4_ADDR 0x0B
#define FDC_CONF_ADDR 0x0C
#define OFFSET_CAL_CIN1_ADDR 0x0D
#define OFFSET_CAL_CIN2_ADDR 0x0E
#define OFFSET_CAL_CIN3_ADDR 0x0F
#define OFFSET_CAL_CIN4_ADDR 0x10
#define GAIN_CAL_CIN1_ADDR 0x11
#define GAIN_CAL_CIN2_ADDR 0x12
#define GAIN_CAL_CIN3_ADDR 0x13
#define GAIN_CAL_CIN4_ADDR 0x14
#define TI_ID_ADDR 0xFE
#define DEVICE_ID_ADDR 0xFF//寄存器
typedef union{struct{uint16_t hwMeasLsb;uint16_t hwMeasMsb;};uint32_t wMeasValue;
}measn_t; //电容值typedef union{struct{uint16_t :5;uint16_t hwCapDac :5;uint16_t hwCHB :3;uint16_t hwCHA :3;};uint16_t hwConfMeasValue;
}conf_measn_t; //通道配置typedef union{struct{uint16_t hwDone4 :1;uint16_t hwDone3 :1;uint16_t hwDone2 :1;uint16_t hwDone1 :1;uint16_t hwMeas4 :1;uint16_t hwMeas3 :1;uint16_t hwMeas2 :1;uint16_t hwMeas1 :1;uint16_t hwRepeat :1;uint16_t :1;uint16_t hwRate :2;uint16_t :3;uint16_t hwRst :1;};uint16_t hwFdcConfValue;
}fdc_conf_t; //数字部分配置typedef union{struct{uint16_t hwDecimalPart :11;uint16_t hwInteherPart :5;};uint16_t hwOffsetCalValue;
}offset_cal_t; //偏移值typedef union{struct{uint16_t hwDecimalPart :14;uint16_t hwInteherPart :2;};uint16_t hwOffsetCalValue;
}gain_cal_t; //增益typedef enum __select_channel_t{SELECT_CIN1 = 0x01,SELECT_CIN2 = 0x02,SELECT_CIN3 = 0x03,SELECT_CIN4 = 0x04,
}select_channel_t;//配置值
//通道配置CHA
typedef enum __cha_t{CHA_CIN1 = 0x00,CHA_CIN2 = 0x01,CHA_CIN3 = 0x02,CHA_CIN4 = 0x03,
}cha_t;//通道配置CHB
typedef enum __chb_t{CHB_CIN1 = 0x00,CHB_CIN2 = 0x01,CHB_CIN3 = 0x02,CHB_CIN4 = 0x03,CHB_CAPDAC_EN = 0x04,CHB_CAPDAC_DIS= 0x07,
}chb_t;//通道配置Rate
typedef enum __rate_t{RATE_100 = 0x01,RATE_200 = 0x02,RATE_400 = 0x03,
}rate_t;
四、电容值读取
两个寄存器32位,低8位无效;剩余24位,其中符号位1位,整数位4位,小数位19位。
int32_t fdc1004_read_cap(uint8_t chRegAddr)
{union{struct{uint16_t hwLSB;uint16_t hwMSB;};int32_t iValue;}temp;temp.iValue = 0;temp.hwMSB = fdc1004_read(chRegAddr);temp.hwLSB = fdc1004_read(chRegAddr+0x01);return temp.iValue;
}float fdc1004_read_cap_value(uint8_t chRegAddr)
{union{struct{uint16_t hwLSB;uint16_t hwMSB;};int32_t iValue;}temp;float fTempValue = 0;temp.iValue = 0;temp.hwMSB = fdc1004_read(chRegAddr);temp.hwLSB = fdc1004_read(chRegAddr+0x01);fTempValue = (temp.iValue >> 8);fTempValue = fTempValue/524288.0;return (fTempValue);
}
运行效果: