|
求教关于MMA7455无法修改传感器寄存器数据的问题
[复制链接]
论文需要,用的龙丘公司的MMA7455传感器,采用SPI通信方式,在测试的时候,不知道为什么,写入寄存器的值,和读出来的老是不一样,就好像无法改变一样,不知道大家碰到过这样的问题不,
如地址为0x16,读出来老是0x0A,默认的不应该是0x00么,
我用的tinyos,底层的驱动大体上和c语言一样。
command error_t Accesor.start()
{//和函数的调用一样
WriteTo(ADDR_MODECTL,0x05);
WriteTo(ADDR_I2CAD,0x80);
clr_INT();
return SUCCESS;
}
command error_t Accesor.setStatus(uint8_t status)
{
post reset();
return SUCCESS;
}
command void Accesor.readData(uint8_t type){
atomic Xout=Yout=Zout=0;
call Accesor.start();
delay(3);
while(!(call INT1.get()))
;
atomic clr_INT();
if(10==type)
read10Bit(&Xout,&Yout,&Zout);
else if(8==type)
read8Bit(&Xout,&Yout,&Zout);
else
return;
delay(50);
signal Accesor.readDone(Xout,Yout,Zout);
return;
}
/******************************函数实体部分****************************************************************/
void InitPIN(){
//初始化引脚
call CS.makeOutput();//设为输出引脚
call SLK.makeOutput();
call SDI.makeOutput();
call SDO.makeInput();//设为输入引脚
call INT1.makeInput();
call INT2.makeInput();
return;
}
void atom_write(uint8_t data){
uint8_t tm;
call SLK.clr();
for(tm=0;tm<8;tm++)
{
delay(3);
if(data&0x80)
call SDI.set();
else
call SDI.clr();
data=data<<1;
delay(3);
call SLK.set();
delay(5);
call SLK.clr();
}
return;
}
uint8_t atom_read()
{
uint8_t tmp;
uint8_t data=0;
call SLK.clr();
for(tmp=0;tmp<8;tmp++)
{
delay(3);
call SLK.set();
if((uint8_t)(call SDO.get()))
data |=0x1;
data=data<<1;
delay(5);
call SLK.clr();
}
return data;
}
void clr_INT(){
WriteTo(ADDR_INTRST,0x03);
WriteTo(ADDR_INTRST,0x00);
}
void delay(uint8_t x){
while(x--)
;
return;
}
void WriteTo(uint8_t addr,uint8_t cmd){
delay(3);
atomic call CS.clr();//和调用函数一样,此处clr为调为低电平,若为set,则是高电平
atom_write(((addr&0x3F)<<1)|0x80);
atom_write(cmd);
delay(3);
atomic call CS.set();
}
uint8_t ReadFrom(uint8_t addr){
uint8_t data;
delay(3);
atomic call CS.clr();
atom_write((addr&0x3F)<<1);
delay(2);
data=atom_read();
delay(3);
atomic call CS.set();
return data;
}
void read10Bit(int16_t* X_out,int16_t* Y_out,int16_t* Z_out){
uint16_t X_L,Y_L,Z_L,X_H,Y_H,Z_H;
atomic X_L=Y_L=Z_L=X_H=Y_H=Z_H=0;
X_L=ReadFrom(ADDR_XOUT);
X_H=ReadFrom(ADDR_XOUT+1);
X_H=X_H<<8;
Y_L=ReadFrom(ADDR_YOUT);
Y_H=ReadFrom(ADDR_YOUT+1);
Y_H=Y_H<<8;
Z_L=ReadFrom(ADDR_ZOUT);
Z_H=ReadFrom(ADDR_ZOUT+1);
Z_H=Z_H<<8;
*X_out=(int16_t)(X_L | X_H);
*Y_out=(int16_t)(Y_L | Y_H);
*Z_out=(int16_t)(Z_L | Y_H);
return;
}
void read8Bit(int16_t* X_out,int16_t* Y_out,int16_t* Z_out){
uint8_t X,Y,Z;
atomic X=Y=Z=0;
X=ReadFrom(ADDR_XOUT8);
Y=ReadFrom(ADDR_YOUT8);
Z=ReadFrom(ADDR_ZOUT8);
*X_out=(int16_t)X;
*Y_out=(int16_t)Y;
*Z_out=(int16_t)Z;
return;
}
task void reset(){
InitPIN();
clr_INT();
WriteTo(ADDR_MODECTL,0x05);
WriteTo(ADDR_I2CAD,0x80);
}
}
|
|