资料整理 */ result = 0;
value.Byte = data.Byte.hi; if (value.Bit._7 == 1) result += FRAC_2d1; if (value.Bit._6 == 1) result += FRAC_2d2; if (value.Bit._5 == 1) result += FRAC_2d3; if (value.Bit._4 == 1) result += FRAC_2d4; //
data.Word = data.Word <<4; value.Byte = data.Byte.hi; //
if (value.Bit._7 == 1) result += FRAC_2d5; if (value.Bit._6 == 1) result += FRAC_2d6; if (value.Bit._5 == 1) result += FRAC_2d7; if (value.Bit._4 == 1) result += FRAC_2d8; if (value.Bit._3 ==1) result += FRAC_2d9; if (value.Bit._2 ==1) result += FRAC_2d10; //
if (full_scale != FULL_SCALE_8G) {
if (value.Bit._1 == 1) { result += FRAC_2d11; }
if (full_scale == FULL_SCALE_2G) {
if (value.Bit._0 == 1) result += FRAC_2d12; } }
9.0轮询数据与中断
数据可以连续被轮询,或者被设置为每次有新数据已经准备好时发送一个硬件中断或异常到MCU。具体哪种方式更有效,需要视情况而定,虽然轮询通常是低效率的。 9.1轮询数据
轮询需要进行较少的配置,实施起来也很方便。然而,MCU必须轮询传感器的速度必须快于数据采集速率。否则,如
2013年8月27日星期二 沨 第 6 页 共 11 页
资料整理 果在轮询太慢,数据样本可能会错过。该MCU通过检查状态寄存器的覆盖标志(即,ZYXOW,ZOW,YOW,和XOW)可以检测这种情况。到目前为止,这个文件主要示例代码都是采用轮询技术。作为一个总结,这里是一个比较完整的例子的基本代码,特定的IC MMA8451,2,3Q,需要不断轮询14位,12位或10 - 位的XYZ数据的操作。 Code Example: /*
** Go to the Standby Mode */
MMA845xQ_Standby(); /*
** Clear the F_Read bit to ensure both MSB’s and LSB’s are indexed */
IIC_RegWrite(CTRL_REG1,(IIC_RegRead(CTRL_REG1)& ~FREAD_MASK); /*
** Go back to Active Mode */
MMA845xQ_Active(); /*
** Using a basic control loop, continuously poll the sensor. */ for (;;) { /*
** Poll the ZYXDR status bit and wait for it to set. */
RegisterFlag.Byte = IIC_RegRead(STATUS_00_REG); if (RegisterFlag.ZYXDR_BIT == 1) { /*
** Read 14/12/10-bit XYZ results using a 6 byte IIC access. */
IIC_RegReadN(OUT_X_MSB_REG, 6, &value[0]); /*
** Copy and save each result as a 16-bit left-justified value. */
x_value.Byte.hi = value[0]; x_value.Byte.lo = value[1]; y_value.Byte.hi = value[2]; y_value.Byte.lo = value[3]; z_value.Byte.hi = value[4]; z_value.Byte.lo = value[5]; /*
** Go process the XYZ data. */
GoProcessXYZ(&value[0]); } /*
2013年8月27日星期二 沨 第 7 页 共 11 页
资料整理 ** Perform other necessary operations. */ etc(); }
9.2中断程序访问数据
通过硬件中断传输数据与轮询相比是更高效,因为MCU只有当有新的数据时才访问加速度计。只有当新的数据可用时,该数据才被读取。如果在新的样本数据产生时上一个数据还未被读取,覆盖寄存器的标志位将被置位。以下是用来设置每次产生新数据后产生中断信号的寄存器配置。 下面所示的MCU的中断服务程序(ISR)对应地读取14位,12位或10位的XYZ数据和设置软件标志指示收到新数据。保持尽可能快的中断服务程序被认为是很好的做法,所以在中断程序中没有做实际的数据处理。请注意与轮询方法的相似性。访问8位数据,也可以用类似的方式。 Code Example: /*
** Go to the Standby Mode */
MMA845xQ_Standby(); /*
** Clear the F_Read bit to ensure both MSB’s and LSB’s are indexed */
IIC_RegWrite(CTRL_REG1,(IIC_RegRead(CTRL_REG1)& ~FREAD_MASK); /*
** Configure the INT pins for Open Drain and Active Low */
IIC_RegWrite(CTRL_REG3, PP_OD_MASK); /*
** Enable the Data Ready Interrupt and route it to INT1. */
IIC_RegWrite(CTRL_REG4, INT_EN_DRDY_MASK); IIC_RegWrite(CTRL_REG5, INT_CFG_DRDY_MASK); /*
** Go back to Active Mode */
MMA845xQ_Active(); /* ** etc. */
/*********************************************************\\ * MMA8451,2Q Interrupt Service Routine
\\*********************************************************/ interrupt void isr_MMA8451Q (void) { /*
** Clear the MCU’s interrupt flag */
CLEAR_MMA8451Q_INTERRUPT; /*
2013年8月27日星期二 沨 第 8 页 共 11 页
资料整理 ** Go read the Interrupt Source Register */
RegisterFlag.Byte = IIC_RegRead(INT_SOURCE_REG); if (RegisterFlag.SRC_DRDY_BIT == 1) { /*
** Read 14 12 or 10-bit XYZ results using a 6 byte IIC access. */
IIC_RegReadN(OUT_X_MSB_REG, 6, &value[0]); /*
** Copy and save each result as a 16-bit left-justified value. */
x_value.Byte.hi = value[0]; x_value.Byte.lo = value[1]; y_value.Byte.hi = value[2]; y_value.Byte.lo = value[3]; z_value.Byte.hi = value[4]; z_value.Byte.lo = value[5]; /*
** Indicate that new data exists to be processed. */
NEW_DATA = TRUE; } }
10.0使用32采样堆栈
访问数据最有效的方式特别是在数据记录是采用32个内部样本FIFO缓冲器。这最大限度地减少了I2C总线数据量。有关如何配置FIFO的更多信息请参考AN4073。FIFO可配置为循环缓冲模式,每次刷新数据时丢弃最旧的数据。这将设置FIFO中断。 配置FIFO:
?循环缓冲模式F_MODE= 01 ?设置水印 ?设置FIFO中断
?链接FIFO中断到INT1或INT2 ?中断引脚设置为低电平有效的漏极开路 Example Code: /*
** Go to Standby Mode */
MMA845xQ_Standby(); /*
** Set F_Mode to Circular, Set the Watermark Value into the F_SETUP register */
IIC_RegWrite(F_SETUP_REG, F_MODE0_MASK + WATERMARK_VAL); /*
** Enable the FIFO Interrupt and Set it to INT2
2013年8月27日星期二 沨 第 9 页 共 11 页
资料整理 */
IIC_RegWrite(CTRL_REG4, INT_EN_FIFO_MASK); IIC_RegWrite(CTRL_REG5, ~INT_CFG_FIFO_MASK); /** Configure the INT pins for Open Drain and Active Low */
IIC_RegWrite(CTRL_REG3, PP_OD_MASK); /*
** Go to Active Mode */
MMA845xQ_Active();
/*********************************************************\\ * MMA8451Q Interrupt Service Routine for the FIFO
\\*********************************************************/ interrupt void isr_MMA8451Q (void) { /*
** Clear the MCU’s interrupt flag */
CLEAR_MMA8451Q_INTERRUPT; /*
** Go read the Interrupt Source Register */
RegisterFlag.Byte = IIC_RegRead(INT_SOURCE_REG); if (RegisterFlag.SRC_FIFO_BIT == 1) { /*
** Read 14-bit XYZ results using a multi-read IIC access. */
IIC_RegReadN(OUT_X_MSB_REG, WATERMARK_VAL*6, &value[0]); /*
** Copy and save each result as a 16-bit left-justified value. */
x_value.Byte.hi = value[0]; x_value.Byte.lo = value[1]; y_value.Byte.hi = value[2]; y_value.Byte.lo = value[3]; z_value.Byte.hi = value[4]; z_value.Byte.lo = value[5]; /*
** Indicate that new data exists to be processed. */
NEW_DATA = TRUE; } }
The HP_FILTER_CUTOFF register (at 0x0F) sets the high-pass cutoff frequency, Fc, for the data. Th e output of this filter is
2013年8月27日星期二 沨 第 10 页 共 11 页
资料整理 provided in the output data registers (0x01 to 0x06). Note that the high-pass filtered output data is available for the MMA8451Q and the MMA8452Q only. The MMA8453Q has the internal high-pass filter for the embedded functions but does not have access to the output data. The available cutoff frequencies change depending upon the set Output Data Rate.
This register sets the high-pass filter cutoff frequency for removal of the offset and slower changing acceleration data. The output of this filter is indicated by the data registers (0x01-0x06) when bit 4 (HPF_OUT) of Register 0x0E is set. The filter cutoff options change based on the data rate selected as shown in Ta b l e 2 3 . For details of implementation on the high-pass filter, refer to Freescale application note, AN4071.
2013年8月27日星期二 沨 第 11 页 共 11 页
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新小学教育MMA845x数据处理 (2)全文阅读和word下载服务。
相关推荐: