LDC1000配Pico - 通过不同金属导电性自制金属分类(其实线圈很重要)

话不多说,上接线和代码,用的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值