#include <reg52.h>
#include "mytype.h"
#include "iic.h"
#include "mma7455.h"
uint8 X,Y,Z;
void main()
{
uint8 i,temp;
EA=1; //打开中断
EX1=1; //打开外部中断INIT1
//初始化IIC总线
IIC_init();
//写数据
IIC_start();
IIC_write_byte(MMA7455_ADDER_WRITE); //1. 在IIC总线上寻址MMA7455
IIC_respons();
IIC_write_byte(MMA7455_Mode_Control_Register); //2. 模式设置寄存器寻址
IIC_respons();
IIC_write_byte(0x05); //3. 模式寄存器数据写入
IIC_respons();
IIC_stop();
//写数据
IIC_start();
IIC_write_byte(MMA7455_ADDER_WRITE); //1. 在IIC总线上寻址MMA7455
IIC_respons();
IIC_write_byte(MMA7455_Mode_Control_Register); //2. 量程检测设置寄存器寻址
IIC_respons();
IIC_write_byte(MMA7455_2G_Measurement_Mode); //3. 设置量程为2G,检测模式。量程检测设置寄存器配置数据写入
IIC_respons();
IIC_stop();
while(1)
{
//读X轴数据
IIC_start();
IIC_write_byte(MMA7455_ADDER_WRITE); //1. IIC地址寻址
IIC_respons();
IIC_write_byte(MMA7455_READ_X); //2. X轴数据寄存器寻址
IIC_respons();
IIC_start(); //3.
IIC_write_byte(MMA7455_ADDER_READ); //改变读写方向
IIC_respons();
X=IIC_read_byte(); //4.读数据
IIC_stop();
//读Y轴数据
IIC_start();
IIC_write_byte(MMA7455_ADDER_WRITE); //1. IIC地址寻址
IIC_respons();
IIC_write_byte(MMA7455_READ_Y); //2. X轴数据寄存器寻址
IIC_respons();
IIC_start(); //3.
IIC_write_byte(MMA7455_ADDER_READ); //改变读写方向
IIC_respons();
Y=IIC_read_byte(); //4.读数据
IIC_stop();
//读X轴数据
IIC_start();
IIC_write_byte(MMA7455_ADDER_WRITE); //1. IIC地址寻址
IIC_respons();
IIC_write_byte(MMA7455_READ_X); //2. X轴数据寄存器寻址
IIC_respons();
IIC_start(); //3.
IIC_write_byte(MMA7455_ADDER_READ); //改变读写方向
IIC_respons();
Z=IIC_read_byte(); //4.读数据
IIC_stop();
}
} |