51单片机IIC总线通信

51单片机IIC总线通信,第1张

我把iic通信的代码给你吧

/**************************************

向IIC总线发送一个字节数据

**************************************/

/*******************************/

void delay(unsigned int k)

{

unsigned int i,j

for(i=0i<ki++)

{

for(j=0j<121j++)

{}}

}

/**************************************

延时5微秒(STC90C52RC@12M)

不同的工作环境,需要基滑调整此函数,注意时钟过快时需要修改

当改用1T的MCU时,请调整此延时函数

**************************************/

void Delay5us()

{

_nop_()_nop_()_nop_()_nop_()

_nop_()/*_nop_()_nop_()_nop_()

_nop_()_nop_()_nop_()_nop_()*/

}

/**************************************

延时5毫秒(STC90C52RC@12M)

不同的工作环境,需要调整此函数

当改用1T的MCU时,请调整此延时函数

**************************************/

void Delay5ms()

{

WORD n = 1000

while (n--)

{

_nop_()_nop_()_nop_()_nop_()

}

}

void MMA8452_SendByte(BYTE dat)

{

BYTE i

for (i=0i<8i++) /春中/8位计数器

{

dat <<= 1 //移出数据的最高位

SDA = CY //送数据口

SCL = 1 //拉高时钟线

Delay5us()//延时

SCL = 0 //拉低时钟线

Delay5us()//延时

}

MMA8452_RecvACK()

}

/**************************************

从IIC总线接收一个字节数据

**************************************/

BYTE MMA8452_RecvByte()

{

BYTE i

BYTE dat = 0

SDA = 1 //使能内部上拉,准备读取数据,

for (i=0i<8i++) //8位计数器

{

dat <<= 1

SCL = 1 //拉高时钟线

Delay5us()//延时

dat |= SDA//读数据

SCL = 0 //拉低时钟线

Delay5us()//延时

}

return dat

}

这是我以前读取传感器信息时候用的。正好是iic通信的那部分,你自己学一下iic吧扒锋山,我的程序是没问题的哦。

一 介绍Sensor类

SDK只有一句介绍“Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.”,表示一个感应器的类,可以使用getSensorList方法(此方法属于接下来要讲的SensorManager)获铅唤得所有可用的感应器,该方法返回的是一个List<Sensor>

下面的列表显示了,Sensor所提供的所有服务

----------------------------------------------------------------------------------------------------------------------------------------------------------

Constants

int TYPE_ACCELEROMETER A constant describing an accelerometer sensor type. //三轴加速度感应器 返回三个坐标轴的加速度 单位m/s2

int TYPE_ALL A constant describing all sensor types. //用于列出所有感应器

int TYPE_GRAVITY A constant describing a gravity sensor type.//重力感应器

int TYPE_GYROSCOPE A constant describing a gyroscope sensor type//陀螺仪 可判断方向 返回三个坐标轴上的角度

int TYPE_LIGHT A constant describing an light sensor type. //光线感应器 单位神激薯 lux 勒克斯

int TYPE_LINEAR_ACCELERATION A constant describing a linear acceleration sensor type. //线性加速度

int TYPE_MAGNETIC_FIELD A constant describing a magnetic field sensor type. //磁场感应 返回三个坐标轴的数值 微特斯拉

int TYPE_ORIENTATION This constant is deprecated. use SensorManager.getOrientation() instead. /游者/方向感应器 已过时 可以使用方法获得

int TYPE_PRESSURE A constant describing a pressure sensor type //压力感应器 单位 千帕斯卡

int TYPE_PROXIMITY A constant describing an proximity sensor type. //距离传感器

int TYPE_ROTATION_VECTOR A constant describing a rotation vector sensor type. //翻转传感器

int TYPE_TEMPERATURE A constant describing a temperature sensor type //温度传感器 单位 摄氏度

----------------------------------------------------------------------------------------------------------------------------------------------------------

此类中包含的方法都是get型的 用来获取所选sensor的一些属性,sensor类一般不需要new而是通过SensorManager的方法获得

二 介绍SensorManager类

SDK解释:“SensorManager lets you access the device's sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.

Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off. ”

SensorManager 允许你访问设备的感应器。通过传入参数SENSOR_SERVICE参数调用Context.getSystemService方法可以获得一个sensor的实例。永远记得确保当你不需要的时候,特别是Activity暂定的时候,要关闭感应器。忽略这一点肯能导致几个小时就耗尽电池,注意当屏幕关闭时,系统不会自动关闭感应器。

三 常用的感应器

(1) 加速度感应器

可以通过这个感应器获得三个浮点型

x-axis

y-axis

z-axis

X Y Z分别对应values[0]到[2]

X表示左右移动的加速度

Y表示前后移动的加速度

Z表示垂直方向的加速度

下面先看一个基本的获取加速的demo,希望大家好好注意代码中的注释

做的很简单,就是在屏幕上显示三个方向上加速度的值


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12480635.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-24
下一篇2023-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存