本帖最后由 北海326 于 2015-9-9 11:31 编辑
最近正在使用LIS3DH,苦苦调试始终没有结果。请大家帮我看下。我是用SPI通信,接线如下:
VDD-3.0v
SDI- MOSI
SDO-MISO
GND-GND
现在的问题是MSIO一直没有数据,就是说一直读不到数据。利用示波器查看波形,发现写寄存器的波形是对的,但读取寄存器函数的波形不对。程序如下:
#define CS_EN GPIO_ResetBits(GPIOB,GPIO_Pin_12)
#define CS_DISEN GPIO_SetBits(GPIOB,GPIO_Pin_12)
static void delay_us(uint32_t nus)
{
while(--nus);
}
void SPI_Lis3_Init()
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能SPI2 GPIOB 的时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
/* Configure SPI2 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ; //Pin_5是SCK时钟 Pin7是MOSI输出信号
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //设成复用推挽
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // lis3 的片选脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2);
SPI_I2S_DeInit(SPI2);
SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low ;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2,ENABLE);
}
void Lis3_Init()
{
uint8_t response;
uint8_t position=0, old_position=0;
AxesRaw_t data;
SPI_Lis3_Init(); //初始化SPI设置
LIS3DH_WriteReg(0x20,0x97);
LIS3DH_WriteReg(0x23,0xf1);
LIS3DH_WriteReg(0x24,0x0c);
}
u8_t SPI_Mems_Write_Byte(u8_t byte)
{
/*!< Loop while DR register in not empty */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI peripheral */
SPI_SendData(SPI2, byte);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return (uint8_t)SPI_ReceiveData(SPI2);
}
#define ADDR_MASK 0x3F
u8_t SPI_Mems_Write_Reg(u8_t WriteAddr, u8_t Data)
{
CS_EN;
delay_us(10);
SPI_Mems_Write_Byte(WriteAddr & ADDR_MASK);
SPI_Mems_Write_Byte(Data);
delay_us(10);
CS_DISEN;
return 1;
}
u8_t SPI_Mems_Read_Reg(u8_t Addr)
{
uint8_t data;
CS_EN;
delay_us(10);
SPI_Mems_Write_Byte(Addr | 0x80);
data = SPI_Mems_Write_Byte(0xff);
delay_us(10);
CS_DISEN;
return data;
}
/*******************************************************************************
* Function Name : LIS3DH_ReadReg
* Description : Generic Reading function. It must be fullfilled with either
* : I2C or SPI reading functions
* Input : Register Address
* Output : Data REad
* Return : None
*******************************************************************************/
u8_t LIS3DH_ReadReg(u8_t Reg, u8_t* Data)
{
//To be completed with either I2c or SPI reading function
//i.e. *Data = SPI_Mems_Read_Reg( Reg );
*Data = SPI_Mems_Read_Reg( Reg );
return 1;
}
/*******************************************************************************
* Function Name : LIS3DH_WriteReg
* Description : Generic Writing function. It must be fullfilled with either
* : I2C or SPI writing function
* Input : Register Address, Data to be written
* Output : None
* Return : None
*******************************************************************************/
u8_t LIS3DH_WriteReg(u8_t WriteAddr, u8_t Data)
{
//To be completed with either I2c or SPI writing function
//i.e. SPI_Mems_Write_Reg(WriteAddr, Data);
SPI_Mems_Write_Reg(WriteAddr, Data);
return 1;
}
读写寄存器的两个函数是自己写的,其他的用的是官方的驱动库。
现在我写一个测试程序,在死循环里不断读寄存器0x28的值,MISO一直没有输出,MOSI的发出的数据按照设想应该是0XA8FF,但是实际是0xA800,很奇怪!
|