话不多说,上接线和代码,用的arduino IDE。
LDC1000 - PicoW
=================
5V ------- VSYS
GND ------ GND
ECK默认不接,板载8M有源晶振时基时钟,如需使用其他外部时钟,请去掉R3;
INTB默认不接;
SCK ------ GP2
CSB ------ GP5
SDO ------ GP3
(miso) (rx)
SDI ------ GP4
(mosi) (tx)
VIO ------ 3V3(OUT)
GND ------ GND
两个最主要的细节是,首先怎么知道LDC1000被联上了,且能通讯?根据datasheet,读0x00的地址,应该拿到0x80,如下图:
怎么拿到感应线圈的导电参数?通过0x20状态寄存器,效果如下。
#define SS 5
#define RD 0x80
#define DVID_REG 0x00
#define RMAX_REG 0x01
#define RMIN_REG 0x02
#define WDTF_REG 0x03
#define LDCC_REG 0x04
#define CLKC_REG 0x05
#define CTHL_REG 0x06
#define CTHM_REG 0x07
#define CTLL_REG 0x08
#define CTLM_REG 0x09
#define INTB_REG 0x0A
#define POWR_REG 0x0B
#define STTS_REG 0x20
#define PRXL_REG 0x21
#define PRXM_REG 0x22
#define FQCL_REG 0x23
#define FQCB_REG 0x24
#define FQCM_REG 0x25
void ldcConfig( void );
uint32_t readRegister(uint8_t add, uint8_t nBytes);
void writeRegister(uint8_t addr, uint8_t data);
/* above is header */
#include <SPI.h>
uint32_t freq_init = 0;
void setup() {
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
Serial.begin(9600);
delay(100);
SPI.setRX(4);
SPI.setTX(3);
SPI.setSCK(2);
SPI.begin(true);
ldcConfig();
freq_init = readFreq();
}
void loop() {
uint32_t freq = readFreq();
Serial.print("READ REGISTER: ");
Serial.println( freq );
if( freq_init - freq > 10 )
digitalWrite(SS, HIGH);
else
digitalWrite(SS, LOW);
delay(500);
//测试能不能读到设备,因返回10000000,即0x80
Serial.println("Device Value: ");
printBits(readRegister(DVID_REG, 1));
delay(500);
//金属放线圈面前数值会变化
Serial.println("Activating Coil ");
printBits(readRegister(STTS_REG, 1));
delay(500);
}
uint32_t readRegister(uint8_t addr, uint8_t nBytes)
{
uint32_t slaveOut = 0;
uint32_t readValue = 0;
digitalWrite(SS, LOW);
SPI.transfer(RD|addr);
readValue = SPI.transfer(0x00);
for( uint8_t i = 1; i != nBytes; i++ )
{
slaveOut = SPI.transfer(0x00);
readValue |= slaveOut<<(8*i);
}
digitalWrite(SS, HIGH);
return readValue;
}
void writeRegister(uint8_t addr, uint8_t data)
{
digitalWrite(SS, LOW);
SPI.transfer(addr);
SPI.transfer(data);
digitalWrite(SS, HIGH);
}
void ldcConfig( void ){
writeRegister(POWR_REG, 0x00);
writeRegister(LDCC_REG, 0x17);
writeRegister(CLKC_REG, 0x00);
writeRegister(RMAX_REG, 0x13); //0x13
writeRegister(RMIN_REG, 0x3D); //0x3D
writeRegister(WDTF_REG, 0x45);
writeRegister(CTHL_REG, 0x50);
writeRegister(CTHM_REG, 0x14);
writeRegister(CTLL_REG, 0xC0);
writeRegister(CTLM_REG, 0x12);
writeRegister(POWR_REG, 0x01);
}
void printBits(byte myByte){
for(byte mask = 0x80; mask; mask >>= 1){
if(mask & myByte)
Serial.print('1');
else
Serial.print('0');
}
Serial.println();
}
uint32_t readFreq( void )
{
uint32_t out = 0;
for(uint8_t i = 0; i<100; i++){
out += readRegister(FQCL_REG, 3);
}
out /= 100;
return out;
}
参考:代码出处以及LDC1000的用法讨论。
https://github.com/liwing1/LDC1000-InductiveSensor/tree/main/spi_teste
https://e2e.ti.com/support/sensors-group/sensors/f/sensors-forum/291621/ldc1000---measuring-different-types-of-metal