LDC1000底层驱动

更新时间:2024-03-15 07:46:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#ifndef LDC1000_CMD_H_ #define LDC1000_CMD_H_

/************************************** ************* VCC ****************** ************* CLK ****************** ************* DI ****************** ************* DO ****************** ************* CS ****************** ************* GND ****************** **************************************/

#define LDC1000_DO 3 //IN #define LDC1000_CS 5 //out #define LDC1000_DI 6 //out #define LDC1000_CLK 7 //out

#define LDC1000_CS_SET() DATA_OUT(LDC1000_CS,1) #define LDC1000_DI_SET() DATA_OUT(LDC1000_DI,1) #define LDC1000_CLK_SET() DATA_OUT(LDC1000_CLK,1)

#define LDC1000_CS_CLR() DATA_OUT(LDC1000_CS,0) #define LDC1000_DI_CLR() DATA_OUT(LDC1000_DI,0) #define LDC1000_CLK_CLR() DATA_OUT(LDC1000_CLK,0)

#define LDC1000_DO_IN gpio_get(PORTB,LDC1000_DO)

#define Dly_LDC1000CLK() delay250ns(1)

#define RPMAX 0x13 #define RPMIN 0x3A

// LDC COMMANDS

#define LDC1000_CMD_REVID 0x00 #define LDC1000_CMD_RPMAX 0x01 #define LDC1000_CMD_RPMIN 0x02 #define LDC1000_CMD_SENSORFREQ 0x03 #define LDC1000_CMD_LDCCONFIG 0x04 #define LDC1000_CMD_CLKCONFIG 0x05 #define LDC1000_CMD_THRESHILSB 0x06 #define LDC1000_CMD_THRESHIMSB 0x07 #define LDC1000_CMD_THRESLOLSB 0x08 #define LDC1000_CMD_THRESLOMSB 0x09 #define LDC1000_CMD_INTCONFIG 0x0A

#define LDC1000_CMD_PWRCONFIG 0x0B #define LDC1000_CMD_STATUS 0x20 #define LDC1000_CMD_PROXLSB 0x21 #define LDC1000_CMD_PROXMSB 0x22 #define LDC1000_CMD_FREQCTRLSB 0x23 #define LDC1000_CMD_FREQCTRMID 0x24 #define LDC1000_CMD_FREQCTRMSB 0x25

// LDC BITMASKS

#define LDC1000_BIT_AMPLITUDE 0x18 #define LDC1000_BIT_RESPTIME 0x07 #define LDC1000_BIT_CLKSEL 0x02 #define LDC1000_BIT_CLKPD 0x01 #define LDC1000_BIT_INTMODE 0x07 #define LDC1000_BIT_PWRMODE 0x01 #define LDC1000_BIT_STATUSOSC 0x80 #define LDC1000_BIT_STATUSDRDYB 0x40 #define LDC1000_BIT_STATUSWAKEUP 0x20 #define LDC1000_BIT_STATUSCOMP 0x10

void delay250ns(uint32 ms);

void DATA_OUT(uint8 pin,uint8 level);

void LDC1000_write(char ADDR, char data);

void LDC1000_read(uint8 ADDR,char *DATA,char len); void LDC1000_init();

#endif /* LDC1000_CMD_H_ */

#include \char orgVal[20];

/************************************************************************* * 函数名称:delayms

* 功能说明:延时函数(不准确),内核频率为100M时较为准确 * 备 注:

*************************************************************************/ void delay250ns(uint32 ms) {

uint32 i, j;

for(i = 0; i < ms; i++) {

for(j = core_clk_mhz/8; j > 0; j--)

{

asm(\ } } }

/*******************************************************************************

* Function Name : DATA_OUT

* Description : SCCB数据线的状态 包括输入输出 电平 * Input : uint8 pin 输入?输出? 1为输出 0输入 * :uint8 data 电平

*******************************************************************************/

void DATA_OUT(uint8 pin,uint8 level) {

if(level) {

GPIO_PDOR_REG(PTB_BASE_PTR) |= 1<

GPIO_PDOR_REG(PTB_BASE_PTR) &= ~(1<

void LDC1000_write(char ADDR, char data) {

uint8 addr=0,dat=0,i=0;

addr = ADDR & 0x7f; dat = data;

LDC1000_CS_CLR(); //片选低 Dly_LDC1000CLK();

LDC1000_CLK_CLR(); // Set clock line low for(i=0;i<16;i++) {

if(i<8)//写8位命令段 {

if(addr&0X80) LDC1000_DI_SET(); else LDC1000_DI_CLR(); Dly_LDC1000CLK();

LDC1000_CLK_SET(); // Set clock line high

addr <<= 1; // Shift DATA_BUF Dly_LDC1000CLK();

LDC1000_CLK_CLR(); // Set clock line low } else {

if(dat&0X80) LDC1000_DI_SET(); else LDC1000_DI_CLR(); Dly_LDC1000CLK();

LDC1000_CLK_SET(); // Set clock line high dat <<= 1; // Shift DATA_BUF Dly_LDC1000CLK();

LDC1000_CLK_CLR(); // Set clock line low } }

Dly_LDC1000CLK();

LDC1000_CS_SET(); //片选高 }

void LDC1000_read(uint8 ADDR,char *DATA,char len) {

uint8 addr=0,dat=0,i=0,j=0;

addr = ADDR | 0x80;

LDC1000_CLK_CLR(); // Set clock line low for(i=0;i<8;i++)//写8位命令段 {

if(addr&0X80) LDC1000_DI_SET(); else LDC1000_DI_CLR(); Dly_LDC1000CLK(); LDC1000_CLK_SET();

addr <<= 1; // Shift DATA_BUF Dly_LDC1000CLK();

LDC1000_CLK_CLR(); // Set clock line high }

for(i=0;i

dat=0;

for(j=0;j<8;j++) {

dat <<=1 ; // Right shift DATA_BUF Dly_LDC1000CLK(); LDC1000_CLK_SET(); // Set clock line high

Dly_LDC1000CLK();

if(LDC1000_DO_IN) dat |= 0x01; // Read data LDC1000_CLK_CLR(); // Set clock line low }

DATA[i] = dat; } }

void LDC1000_init() {

/* 打开B端口的时钟源 */

SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK ;

/* LDC使用IO端口初始化 */

PORTB_PCR3 = PORT_PCR_MUX(1); PORTB_PCR5 = PORT_PCR_MUX(1); PORTB_PCR6 = PORT_PCR_MUX(1); PORTB_PCR7 = PORT_PCR_MUX(1);

gpio_init(PORTB,LDC1000_CLK,GPO,LOW); gpio_init(PORTB,LDC1000_DI,GPO,LOW); gpio_init(PORTB,LDC1000_CS,GPO,LOW); gpio_init(PORTB,LDC1000_DO,GPI,LOW);

/* LDC寄存器初始化 */

LDC1000_write(LDC1000_CMD_RPMAX, RPMAX); LDC1000_write(LDC1000_CMD_RPMIN, RPMIN); LDC1000_write(LDC1000_CMD_SENSORFREQ, 0x94); LDC1000_write(LDC1000_CMD_LDCCONFIG, 0x17); LDC1000_write(LDC1000_CMD_CLKCONFIG, 0x02); LDC1000_write(LDC1000_CMD_INTCONFIG, 0x02);

LDC1000_write(LDC1000_CMD_THRESHILSB, 0x50); LDC1000_write(LDC1000_CMD_THRESHIMSB, 0x14); LDC1000_write(LDC1000_CMD_THRESLOLSB, 0xC0); LDC1000_write(LDC1000_CMD_THRESLOMSB, 0x12);

LDC1000_write(LDC1000_CMD_PWRCONFIG, 0x01);

//read all registers LDC1000_CS_CLR(); Dly_LDC1000CLK();

LDC1000_read(LDC1000_CMD_REVID , orgVal , 12); Dly_LDC1000CLK();

LDC1000_CS_SET(); }

本文来源:https://www.bwwdw.com/article/b018.html

Top