- /*----------------------------------------
- LDC1000-STM32驱动程序
- 作者: BoX
- 时间: 8月6日
- 版本: V1.0
- ----------------------------------------*/
- #include "stm32f10x.h"
- #include "usart1.h"
- #include "led.h"
- #include "delay.h"
- #include "stdio.h"
- #include "ldc1000.h"
- #include "pwm.h"
- /*----------------------------------------
- 全局变量
- ----------------------------------------*/
- extern uint8_t RegArray[REG_LEN];
- int8_t i = 0;
- /*----------------------------------------
- 主函数
- ----------------------------------------*/
- int main(void)
- {
- DelayInit();
- LED_GPIO_Config();
- USART1_Config();
- PWM_tiM4_CH1_Config(8,0); //输出8MHz时钟给TBCLK,LDC1000需要8MHz时钟方可运行
- delayms(100);
- LDC1000_Init();
- //printf("LDC1000-DEMOrn");
- while(1)
- { //LDC1000_WriteReg(LDC1000_CMD_SENSORFREQ, 0x94);
- //delayms(10);
- printf("0x00 = %xrn",LDC1000_ReadReg(0x00));
- printf("0x01 = %xrn",LDC1000_ReadReg(0x01));
- printf("0x02 = %xrn",LDC1000_ReadReg(0x02));
- printf("0x03 = %xrn",LDC1000_ReadReg(0x03));
- printf("0x04 = %xrn",LDC1000_ReadReg(0x04));
- printf("0x05 = %xrn",LDC1000_ReadReg(0x05));
- printf("0x06 = %xrn",LDC1000_ReadReg(0x06));
- printf("0x07 = %xrn",LDC1000_ReadReg(0x07));
- printf("0x08 = %xrn",LDC1000_ReadReg(0x08));
- printf("0x09 = %xrn",LDC1000_ReadReg(0x09));
- printf("0x0A = %xrn",LDC1000_ReadReg(0x0A));
- printf("0x0B = %xrn",LDC1000_ReadReg(0x0B));
- printf("0x20 = %xrn",LDC1000_ReadReg(0x20));
- printf("0x21 = %xrn",LDC1000_ReadReg(0x21));
- printf("0x22 = %xrn",LDC1000_ReadReg(0x22));
- printf("0x23 = %xrn",LDC1000_ReadReg(0x23));
- printf("0x24 = %xrn",LDC1000_ReadReg(0x24));
- printf("0x25 = %xrn",LDC1000_ReadReg(0x25));
- // LDC1000_ReadBytes(LDC1000_CMD_REVID,&RegArray[0],12);
- // for(i=0; i<12; i++)
- // {
- // printf("Addr=%x Data=%xrn",i,RegArray);
- // }
- delayms(1000);
- }
- }
LDC1000.c
单片机源程序如下:
- #include "ldc1000.h"
- /*----------------------------------------
- 全局变量
- ----------------------------------------*/
- uint8_t RegArray[REG_LEN];
- /*----------------------------------------
- 宏定义
- ----------------------------------------*/
- #define CSB_0 GPIOB->BRR = GPIO_Pin_7
- #define CSB_1 GPIOB->BSRR = GPIO_Pin_7
- /*----------------------------------------
- 函数功能: LDC1000初始化
- 函数参数: 无
- ----------------------------------------*/
- void LDC1000_Init(void)
- {
- LDC1000_GPIO_Init();
- LDC1000_SPI_Init();
- LDC1000_WriteReg(LDC1000_CMD_RPMAX, 0x13);
- LDC1000_WriteReg(LDC1000_CMD_RPMIN, 0x3A);
- LDC1000_WriteReg(LDC1000_CMD_SENSORFREQ, 0x94);
- LDC1000_WriteReg(LDC1000_CMD_LDCCONFIG, 0x17);
- LDC1000_WriteReg(LDC1000_CMD_CLKCONFIG, 0x02);
- LDC1000_WriteReg(LDC1000_CMD_INTCONFIG, 0x02);
- LDC1000_WriteReg(LDC1000_CMD_THRESHILSB, 0x50);
- LDC1000_WriteReg(LDC1000_CMD_THRESHIMSB, 0x14);
- LDC1000_WriteReg(LDC1000_CMD_THRESLOLSB, 0xC0);
- LDC1000_WriteReg(LDC1000_CMD_THRESLOMSB, 0x12);
- LDC1000_WriteReg(LDC1000_CMD_PWRCONFIG, 0x01);
- }
- /*----------------------------------------
- 函数功能: LDC1000 GPIO初始化
- 函数参数: IO口连接图
- +5V <-> +5V
- DCLK <-> PB6 <-> TIM4_CH1
- SCLK <-> PA5 <-> SPI1_SCK
- SDI <-> PA7 <-> SPI1_MOSI
- SDO <-> PA6 <-> SPI1_MISO
- VIO <-> 3V3
- GND <-> GND
- INT <-> PC2 <-> GPIO
- CSB <-> PB7 <-> GPIO
- GND <-> GND
- ----------------------------------------*/
- void LDC1000_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //记住使能AFIO时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC , ENABLE);
- //PB6->PWM_OUTPUT
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB6复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //A5->SCK A6->MISO A7->MOSI
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //SPI用复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- //PC2->INT
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- //PB7->CSB
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- }
- /*----------------------------------------
- 函数功能: SPI参数初始化
- 函数参数: LDC1000的时钟频率必须在4.2MHz
- 且不能超过5MHz,不能低于4MHz,否则
- 模块不能够正常通信,所以SPI波特率
- 分频系数选择16分频.
- ----------------------------------------*/
- void LDC1000_SPI_Init(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); //使能APB2外设时钟
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //SPI主
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //数据格式8位
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟极性低,高都可以
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟相位第1,2边沿触发都可以
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //必须是16分频
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //MSB在前,必须
- SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC多项式校验,可有可无
- SPI_Init(SPI1,&SPI_InitStructure);
- SPI_Cmd(SPI1,ENABLE);
- }
- /*----------------------------------------
- 函数功能: LDC1000写寄存器
- 函数参数: - addr 寄存器地址
- - dat 数据
- ----------------------------------------*/
- void LDC1000_WriteReg(int8_t addr, uint8_t dat)
- {
- addr = addr & 0x7F; //MSB为0写寄存器,为1读寄存器
- CSB_0;
- // LDC1000_SendByte(addr);
- // LDC1000_SendByte(dat);
- SPI_I2S_SendData(SPI1, addr);
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
- SPI_I2S_SendData(SPI1, dat);
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
- CSB_1;
- }
- /*----------------------------------------
- 函数功能: LDC1000读寄存器
- 函数参数: - addr 寄存器地址
- ----------------------------------------*/
- uint8_t LDC1000_ReadReg(int8_t addr)
- {
- uint8_t temp;
- addr = addr | 0x80; //MSB为1读寄存器,为0写寄存器
- CSB_0;
- SPI_I2S_SendData(SPI1, addr);
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
- SPI_I2S_SendData(SPI1, 0x00);
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
- temp = (uint8_t)SPI_I2S_ReceiveData(SPI1);
- // LDC1000_SendByte(addr);
- // temp = LDC1000_SendByte(0x00);
- CSB_1;
- return temp;
- }
- /*----------------------------------------
- 函数功能: 向LDC1000发送一字节数据
- 函数参数: 要发送的数据dat
- 返 回 值: 器件发回来的数据
- ----------------------------------------*/
- uint8_t LDC1000_SendByte(uint8_t dat)
- {
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
- SPI_I2S_SendData(SPI1, dat);
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
- return (uint8_t)SPI1->DR;
- }
- /*----------------------------------------
- 函数功能: 读取LDC1000一个字节
- 函数参数: 空
- 返 回 值: 从器件发来的一字节数据
- ----------------------------------------*/
- uint8_t LDC1000_ReadByte(void)
- {
- return 0x00;
- }
- /*----------------------------------------
- 函数功能: 读取LDC1000 len个字节
- ……………………
- …………限于本文篇幅 余下代码请从电子发烧友下载附件…………
所有资料51hei提供下载:
LDC1000 - STM32.rar
|