📄 i2c.#3
字号:
#include "MEMSIC.h"
uint8 xdata Temp0;
uint8 xdata Temp1;
uint8 xdata Temp2;
uint8 xdata Temp3;
int16 xdata Ax;
int16 xdata Ay;
void SMBus_Initial(void)
{
SMB0CF = 0x54;
SMB0CF |= 0x80;
}
void Read_Acc (void) //read acceleration
{
SMB0CF &= 0x7f;
SMB0CF |= 0x80;
STA = 1;//打开启动信号
while(SI != 1);//等待启动中断
if( (SMB0CN & 0xf0) == 0xe0 )
{
SMB0DAT = 0x20;//地址+写
STA = 0;//关闭启动信号
SI = 0;//中断清除
}
while(SI != 1);//等待 地址+写 中断
if( SMB0CN == 0xc3 )
{
SMB0DAT = 0x01;//输入将访问地址
SI = 0;//中断清除
}
while(SI != 1);//等待 输入将访问地址 中断
if( SMB0CN == 0xc3 )
{
STA = 1;
STO = 1;//STOP + START
SI = 0;//clear interrupt
}
while(SI != 1);
if( (SMB0CN & 0xf0) == 0xe0 )
{
SMB0DAT = 0x21;//address + read
STA = 0;//clear start signal
SI = 0;//clear interrupt
}
while(SI != 1);
if( SMB0CN == 0xc3 )
{
SI = 0;// master send convert to master receive
}
while(SI != 1);
if( (SMB0CN & 0xf8) == 0x88 )
{
Temp0 = SMB0DAT;//receive the AxH
ACK = 1;//send acknowledge
SI = 0;//clear interrupe
}
while(SI != 1);
if( (SMB0CN & 0xf8) == 0x88 )
{
Temp1 = SMB0DAT;//receive the AxL
ACK = 1;//send acknowledge
SI = 0;//clear interrupe
}
while(SI != 1);
if( (SMB0CN & 0xf8) == 0x88 )
{
Temp2 = SMB0DAT;//receive the AyH
ACK = 1;
SI = 0;
}
while(SI != 1);
if( (SMB0CN & 0xf8) == 0x88 )
{
Temp3 = SMB0DAT;//receive the AyL
ACK = 1;
SI = 0;
}
while(SI != 1);
if( (SMB0CN & 0xf8) == 0x88 )
{
ACK = 0;//send NonACK
STO = 1;//send Stop signal
SI = 0;//clear interrupt
}
Ax=Temp0;
Ax=Ax<<8;
Ax+=Temp1;
Ay=Temp2;
Ay=Ay<<8;
Ay+=Temp3;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -