|
哪位大神帮我分析下我写的LPS22HB驱动程序,用的STM32单片机,采用的是SPI四线协议,看我的程序哪里有问题,特别是Read_LPS22HB()和Wrrite_LPS22HB()两个函数是不是有问题?
#include "LPS22HB.h"
#include "delay.h"
void delay(void)
{
u8 i=0;
for(i=0;i<10;i++)
{
;
}
}
void WRITE_LPS22HB(u8 adress ,u8 data)
{
u8 i;
u16 temp=adress*256+data;
LPS_CS = 1;
LPS_SCLK = 1;
LPS_CS = 0;
delay_ms(1);
for(i=0;i<16;i++)
{
LPS_SCLK= 0;
temp=temp<
if(temp&0x8000)
{
Write_LPS=1;
}
else
{
Write_LPS=0;
}
delay();
delay_us(1);
LPS_SCLK = 1;
delay();
delay_us(1);
}
LPS_CS = 1;
}
void Read_LPS22HB(u8 adress,u8*data,u8 nBytesToRead)
{
u8 i,j;
LPS_CS = 0;
delay_ms(1);
LPS_SCLK = 1;
for(i=0;i<8;i++)
{
LPS_SCLK= 0;
adress=adress<
if(adress&0x80)
{
Write_LPS=1;
}
else
{
Write_LPS=0;
}
delay();
delay_us(1);
LPS_SCLK = 1;
delay();
delay_us(1);
}
for(j=0;j
{
for(i=0;i<8;i++)
{
LPS_SCLK= 0;
data[j] = data[j]<< 1;
if(Read_LPS==1)
{
data[j] = data[j]+1;
}
delay();
delay_us(1);
LPS_SCLK = 1;
delay();
delay_us(1);
}
}
LPS_CS = 1;
}
void LPS22HB_Init(void)
{
RCC->APB2ENR|=1<<3; //使能PORTB时钟
GPIOB->CRH&=0X0000FFFF;
GPIOB->CRH|=0X83330000;
WRITE_LPS22HB(0x10,0x20); // 写控制寄存器地址
delay_ms(1);
}
u16 adress=0;
u32 press=0;
u16 temper=0;
void LPS22HB_test(void)
{
u8 temp[5]={0};
Read_LPS22HB(0x0F,temp,1);
adress=temp[0];
delay_ms(100);
Read_LPS22HB(0x28,temp,5);
press=temp[0]*65536+temp[1]*256+temp[2];
temper=temp[3]*256+temp[4];
} |
|