请问我的MMA7660程序哪里有问题?
麻烦各位请看看我的程序哪里有问题MMA7660 三轴数据怎么读不出来?谢谢!//********************************************************/
#include<reg51.h>
#include "intrins.h"
sbit SDA=P2^5; //IIC数据线定义
sbit SCL=P2^6; //IIC时钟线定义
unsignedchar j;
unsignedchartxbuf;
unsignedcharrxbuf;
unsignedcharaddr;
unsignedcharflag;
unsignedcharbuff1;
unsignedcharx1;
unsigned char k;
void IIC_start();
void IIC_STAOP();
void iic_ack();
void delay();
void delay1();
void read();
voidwtite0();
voidic_rx();
voidic_tx();
voidic_eeread();
void sendbyte(unsigned char bytebuf)
{
SBUF=bytebuf;
while(!TI);
TI=0;
}
void IIC_start(void)
{
SDA=1;
SCL=1;
delay();
SDA=0;
delay();
SCL=0;
}
//************************************************
//送停止位 SDA=0->1
void IIC_stop(void)
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
//===============================================
//====================================
void ic_tx()
{
unsignedchar cnt;
_nop_();
for(cnt=0;cnt<8;cnt++)
{
delay();
if((txbuf&0x80)==0x80)
SDA=1;
else
SDA=0;
delay();
SCL=1;
delay();
txbuf=(txbuf<<1);
SCL=0;
}
//========================
delay();
SDA=1;
delay();
flag=flag&0xfd;;
SCL=1;
delay();
if(SDA==1)
flag=flag|0x02;
SCL=0;
}
//=========================================
void ic_rx()
{
unsignedchar cnt;
_nop_();;
rxbuf=0;
for(cnt=0;cnt<8;cnt++)
{
delay();
SCL=1;
CY=0;
rxbuf=(rxbuf<<1);
delay();
if(SDA==1)
rxbuf=(rxbuf|0x01);
SCL=0;
}
}
//==============================================
void ic_eeread()
{
unsignedchar i;
IIC_start();
txbuf=0x98; //写入
ic_tx();
txbuf=addr;
ic_tx();
IIC_start();
txbuf=0x99; //读出
ic_tx();
//==========================
i=0;
for(; x1!=0;x1--)
{
ic_rx();
buff1=rxbuf;
i++;
_nop_();
SDA=0;
delay();
SCL=1;
delay();
SCL=0;
}
//=========================
_nop_();
SDA=1;
delay();
SCL=1;
delay();
SCL=1;
IIC_stop();
}
//============================================
void delay()
{
unsignedcharf;
f=0;
while(f<20)
{
f++;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void delay1()
{
unsignedcharf;
f=90;
while(f<250)
{
f++;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void main()
{unsigned char x, y, z;
unsigned int p;
TMOD=0x20; //选择定时器1
PCON=0x00; ///波特率不加倍
SCON=0x50; //串行工作方式1允许串行接收
TH1=0xFB; //晶振位18.432MHZ,选择波特率为9600
TL1=0xFB; //在相应计数器上赋值
EA=1; //总中断开启
ES=1; //串行中断开启
TR1=1; //开启定时器1
x1=3;
k=0;
addr=1;
for(x=0;x<6;x++)
{
write0();
addr+=2;
p=0;
while(p<100)
{delay1();
delay1();
delay1();
p++;
}
}
p=0;
while(p<600)
{
delay1();
delay1();
delay1();
p++;
}
while(1)
{
x1=3;
addr=0x00; //X轴地址
ic_eeread();//读三轴数据
for(k=0;k<6;k++)
{
sendbyte(buff1);
}
p=0;
while(p<600)
{
delay1();
delay1();
delay1();
p++;
}
}
MMA7660 三轴 静态数据是多少/ 你没有初始化好,
首先你得设置好工作模式,然后设置进入工作状态,
需要特别注意的是,一旦设置进入工作状态后不能修改工作模式了的! F_IIC_Write(MODE,0x00);//Stand mode
F_IIC_Write(INTSU,0x40); //SHINT X SHINT Y SHINT Z GINT ASINT PDINT PLINT FBINT
//0 1 0 0 0 0 0 0
F_IIC_Write(SPCNT,0x00); //SPCNT
F_IIC_Write(PDET,0xE0); //PDET
F_IIC_Write(SR,0x00); //SR
F_IIC_Write(PD,0x00); //PD
F_IIC_Write(MODE,0x41);//59
这样初始化,在Y轴有加速度的时候可以产生中断,并能读出相应的值,但感觉灵敏度不够。
页:
[1]