MMA9553L是飞思卡尔的一款计步传感器,本文就如何快速使用该传感器做一个简单介绍。 你可能还见到过MMA955xL, 它与MMA9553L是什么关系呢?简单的来说MMA955xL是一个统称,它包括MMA9550L、MMA9551L、MMA9553L和MMA9559L这几个具体型号,其实这四种传感器在硬件上都是一样的。其内部主要由ColdFire 内核、模拟前端、Flash、IIC和SPI接口等部分组成,原理框图如下图所示: 它们的不同之处在于内部的Firmware不同,Firmware在芯片出厂时就已经固化在芯片里面了,不同的Firmware对于不同的功能。这里介绍的MMA9553L主要就用作计步器功能。 MMA9553L和MCU之间可以通过IIC接口或者SPI接口通讯,所以使用MMA9553L的首要前提是把MCU的IIC或者SPI调通。接口调通之后就可以来操作此传感器了。这里以IIC为例来说明。附件为参考代码。 测试平台:IAR7.2 + FRDM_KL25Z+FRDM-FXS-MULTI FRDM-FXS-MULTI开发板上带有MMA9553L,将FRDM-FXS-MULTI开发板和FRDM_KL25Z连接在一起就可以使用了。 下面分析一下源代码: 首先是调用初始化函数pedometer_init(),此函数主要调用以下几个函数: pedometer_write_config(); // config pedometer_enable(); // enable pedometer pedometer_int0_enable(); // enable INT_O pin pedometer_active(); // active MMA9553 pedometer_wakeup(); // wakeup 在此重点分析前两个函数。第一个函数 pedometer_write_config(),该函数的具体实现如下: void pedometer_write_config(void) { unsigned char Buf[]={0x15,0x20,0x00,0x10, 0x0C,0xE0, 0x13,0x20, 0x00,0x96, 0x60,0x50, 0xAF,0x50, 0x04,0x03, 0x05,0x01, 0x00,0x00}; dvMMA9553_Write(MMA9553_Slave_Addr, MMA9553_Sub_Addr, Buf, 20); } 此函数很简单,就是通过IIC给9553发送一条命令,命令的内容Buf数组中的20个字节 的数据。 dvMMA9553_Write()函数的第一个参数代表MMA9553L的地址,为0x4C。datasheet中有说明。 #define MMA9553_Slave_Addr 0x4C dvMMA9553_Write()函数的第二个参数代表寄存器地址,为0x00。 #define MMA9553_Sub_Addr 0x00 发送的这一串命令:0x15,0x20,0x00,0x10,0x0C,0xE0,0x13,0x20,0x00,0x96,0x60,0x50,0xAF,0x50,0x04,0x03,0x05,0x01,0x00,0x00 具体是什么含义呢? 0x15:表示Application ID,计步器的Application ID就是0x15 0x20:表示这条命令是Write Config command,即这条命令是用来写Configuration 寄存器的。 0x00:表示配置寄存器的偏移地址。 0x10:表示要写16字节的内容。 0x0C,0xE0,0x13,0x20,0x00,0x96,0x60,0x50,0xAF,0x50,0x04,0x03,0x05,0x01,0x00,0x00 这16字节就是写入配置寄存器中的具体内容。 配置寄存器共用8个,分别是Sleep Minimum register,Sleep Maximum register,Sleep Count Threshold register,Configuration/Step Length register,Height/Weight register,Filter register,Speed Period/Step Coalesce register,Activity Count Threshold register,每个寄存器为16 bit(2 字节),所以总共16字节。 第二个函数 pedometer_enable(),该函数的具体实现如下: void pedometer_enable(void) { unsigned char Buf[]={0x17,0x20,0x05,0x01,0x00}; dvMMA9553_Write(MMA9553_Slave_Addr, MMA9553_Sub_Addr, Buf, 5); } 这次写入的命令是0x17,0x20,0x05,0x01,0x00 0x17:表示Application ID 0x20:表示这条命令是Write Config command 0x05,0x01,0x00 这三个表示在偏移地址0x5处,写入一个字节的数据0x00 其他几个函数也类似,都是写入一条命令,对某种Application的配置寄存进行设置。 初始化完了,现在就可以读取步数了。 通过调用pedometer_main() 函数就可以读取到步数。 该函数的实现如下: void pedometer_main(void) { unsigned char Buf[20]; pedometer_cmd_readstatus(); // read status while(1) { dvMMA9553_Read(MMA9553_Slave_Addr, MMA9553_Sub_Addr, Buf, 2); if(Buf[1]==0x80) { dvMMA9553_Read(MMA9553_Slave_Addr, MMA9553_Sub_Addr, Buf, 16); break; } } m_status.StepCount = Buf[6] * 256 + Buf[7]; m_status.Distance = Buf[8] * 256 + Buf[9]; m_status.Calories = Buf[12] * 256 + Buf[13]; } 主要调用了两个函数,一是pedometer_cmd_readstatus(),这个函数的实现如下: void pedometer_cmd_readstatus(void) { unsigned char Buf[]={0x15,0x30,0x00,0x0C}; dvMMA9553_Write(MMA9553_Slave_Addr, MMA9553_Sub_Addr, Buf, 4 ); } 它是发送了0x15,0x30,0x00,0x0C这条命令 0x15:表示Application ID 0x30:表示Read Status command 0x00:表示偏移地址 0x0C:表示需要读的字节数为12 之后调用dvMMA9553_Read()函数,通过IIC读取16字节的数据(4字节起始信息+12字节status register内容),读到的16字节数据如下: Step count register寄存器如下,通过其值可以算出步数来。 另外还可以读取三轴加速度的值,过程与读取步数是类似的,也是先写配置寄存器,然后再读取状态寄存器。 总的来说操作MMA955L的关键搞清楚有两个重要的寄存器:配置寄存器和状态寄存器。配置寄存器可读可写,状态寄存器只可读。 写配置寄存器,格式是: APP_ID+0x20+offset+number+number字节的内容 读配置寄存器,格式为: 先发送:APP_ID+0x10+offset+number, 再通过IIC读number+4字节的内容,前4字节为起始信息。 读状态寄存器,格式为: 先发送:APP_ID+0x30+offset+number,再通过IIC读number+4字节的内容,前4字节为起始信息。 读Command 回的内容如下:
記事全体を表示