最近在调试LIS3DH,但是读写一直有问题,往0x20地址写入0x12,但是读出来确实0xff,不知道哪里有问题,下面是代码,请高手赐教
#include"iocc2540.h"
#define u8 unsigned char
#define u16 unsigned int
#define LED_R P0_6
#define LED_G P0_4
#define LED_B P0_1
#define MOT P0_0
#define CLK P1_0
#define SDI P1_1
#define SDO P1_2
#define SS P1_3
#define SINT P1_4
void delayus(u16 t)
{
u16 i;
for(i=0;i<t;i++);
}
void init()
{
//P1SEL=0xff;
P0DIR|=0XFF;
//P1SEL&=0XF0;
P1DIR=0X0b;
//P1INP=0Xff;
MOT=0;
SS=1;
delayus(2);
}
void txdata(u8 dat)
{
u8 i,buf_isd=dat;
SS=0;
delayus(2);
for(i=0;i<8;i++)
{
CLK=0;
delayus(2);
if(buf_isd&0x80) SDI=1;
else SDI=0;
CLK=1;
delayus(2);
buf_isd<<=1;
}
}
u8 senddata(void)
{
u8 i,buf=0;
for(i=0;i<8;i++)
{
buf<<=1;
CLK=0;
delayus(2);
if(SDO==1) buf|=0x01;
CLK=1;
delayus(2);
}
return(buf);
}
void main()
{
u8 a=0;
init();
while(1)
{
txdata(0x20);
txdata(0x12);
SS=1;
delayus(2);
txdata(0x20|0x80);
a=senddata();
SS=1;
if(a==0x12)
LED_R=0;
}
} |