When using an accelerometer, pay attention to the following points:
First, make sure that your IIC is correct;
Second, you must first calibration the system. The calibration method is as follows: Flat 7455 to ensure that the Z axis is down. If the system is OK, the X axis output is 0, and the Y axis output is 0, the Z-axis output is about 63. If the number of workers is not above, the following adjustments should be made: When the scalar value is smaller than the actual value, a 2 * error value should be written into the calibration register; assume that the scalar value is greater than the actual value, and an ascii code with the negative value corresponding to the error should be written. For example, if the maximum output value is 70,-16 should be written, that is, (0xf0 );
The following code is used to debug mma7455:
#include "msp430f5438.h"#include "public.h"#include "simulate_iic.h"#include <stdbool.h>#include "init.h"#include "mma7455.h"#include "lcd1602.h"#include<math.h>typedef unsigned int uint;typedef unsigned char uchar;char mma7455write_byte(unsigned char reg,unsigned char data)//寄存器地址,数据{ char flag; // WDTCTL = WDTPW + WDTHOLD; // 禁止看门狗定时器 iic_start();//起始信号 write_byte(0x3a);//数据发送 flag=get_ack();//接受应答位,即数据成功发送后,接受到的应答 if(flag) { flag=0; write_byte(reg);//数据发送 } flag=get_ack();//接受应答位,即数据成功发送后,接受到的应答 if(flag) { flag=0; write_byte(data); } flag=get_ack(); if(flag) { flag=0; iic_stop(); return 0; } return 1; } char readMMA7455Byte(unsigned char regadd){ char flag; char z; z=0; iic_start(); write_byte(0x3a);//先写入器件地址 flag=get_ack(); if(flag) { write_byte(regadd);//有应答之后再写寄存器地址 flag=0; } flag=get_ack(); if(flag) { flag=0; iic_start();//继续等应答之后写入该地址和读命令,但是认为这不必要这么做 write_byte(0x3b);//但是时间的原因,仅仅有找着实例的操作先写着,以后再改动 } flag=get_ack(); if(flag) { flag=0; z=read_byte(); } send_ack(); iic_stop(); return z; }// X:255 1.65V -1.00g// 012345678901234567void Cvt_Str(char zifu[], char V1){ char characters[17]="0123456789ABCDEF"; char tv=0; tv=V1/100; zifu[2] = characters[tv]; tv=(V1%100)/10; zifu[3] = characters[tv]; tv=V1%10; zifu[4] = characters[tv]; zifu[5] = ‘ ‘; zifu[6] = ‘0‘; zifu[7] = ‘x‘; tv=V1/16; zifu[8] = characters[tv]; tv=V1%16; zifu[9] = characters[tv]; zifu[10] = ‘ ‘; if(V1>127) { zifu[11] = ‘-‘; tv=255-V1; zifu[12] = characters[tv/63]; zifu[13] = ‘.‘; zifu[14] = characters[((tv*100/63)%100)/10]; zifu[15] = characters[(tv*100/63)%10]; } else { zifu[11] = ‘+‘; tv=V1; zifu[12] = characters[tv/63]; zifu[13] = ‘.‘; zifu[14] = characters[((tv*100/63)%100)/10]; zifu[15] = characters[(tv*100/63)%10]; } zifu[16] = ‘g‘;}uintarc_tan2(uchar Ax,uchar Ay){intdiat_t;floatm;m=atan2(Ay,Ax);diat_t=(int)(m*1800/3.14);returndiat_t;}uint measure(){uchar x;uchar y;uint x1,y1;ucharxsign,ysign;uint angle;angle=0;x=readMMA7455Byte(0x06);y=readMMA7455Byte(0x07);x=x+0x2C;y=y+0x25;if(x>127){x=255-x;x1=((float)x*100)/63.0;xsign=0x2b;}else{x1=((float)x*100)/63.0;xsign=0x2d;}if(y>127){y=255-y;y1=((float)y*100)/63.0;ysign=0x2b;}else{y1=((float)y*100)/63.0;ysign=0x2d;}angle = arc_tan2(x1,y1);/*if(xsign==0x2b&&ysign==0x2b){angle = arc_tan2(x1,y1);}elseif(xsign==0x2b&&ysign==0x2d){angle = 900+arc_tan2(y1,x1);;}elseif(xsign==0x2d&&ysign==0x2d){angle = 2700-angle;}elseif(xsign==0x2d&&ysign==0x2b){angle = 2700+angle;}*/return angle;}void main(){ char txtbuf[16]="X:255 -1.00g"; //uchar x,y,x2,y2; //volatile uint x1,y1; // uchar j,k; clk_init(); lcd1602_pin_init(); lcd_init(); delay_ms(50); while(mma7455write_byte(0x16,0x005)); while(mma7455write_byte(0x10,0xff));//校正X值 while(mma7455write_byte(0x11,0x07)); while(mma7455write_byte(0x12,0x18));//校正Y值 while(mma7455write_byte(0x14,0xDC));//校正Z值 while(mma7455write_byte(0x15,0xFF)); while(1) {/*lcd_pos(0,0); lcd_wdat(‘a‘); lcd_wdat(‘n‘); lcd_wdat(‘g‘); lcd_wdat(‘l‘); lcd_wdat(‘e‘); lcd_wdat(‘:‘); lcd_printf(measure());//x //delay_ms(1000); //lcd_wcmd(0x01);*///显示清屏/*x=readMMA7455Byte(0x06);y=readMMA7455Byte(0x07);x2=x+0x2C;y2=y+0x25;x=x+0x2C;y=y+0x25;if(x>0x7f){x=255-x;j=1;x1=((float)x*100)/63.0;}else{x1=((float)x*100)/63.0;j=0;}if(y>0x7f){y=255-y;y1=((float)y*100)/63.0;k=1;}else{y1=((float)y*100)/63.0;k=0;}lcd_pos(0,0);if(j==1) lcd_wdat(‘-‘);else lcd_wdat(‘+‘);lcd_char(x1);lcd_pos(0,1);if(k==1) lcd_wdat(‘-‘);else lcd_wdat(‘+‘);lcd_char(y1);delay_ms(1000);lcd_wcmd(0x01); lcd_pos(0,0); Cvt_Str(txtbuf,x2); txtbuf[0]=‘X‘; lcd_string(txtbuf); lcd_pos(0,1); Cvt_Str(txtbuf,y2); txtbuf[0]=‘Y‘; lcd_string(txtbuf); delay_ms(1000); lcd_wcmd(0x01);*///显示清屏 lcd_pos(0,0); Cvt_Str(txtbuf,readMMA7455Byte(0x08)); txtbuf[0]=‘Z‘; lcd_string(txtbuf); lcd_pos(0,1); lcd_printf(arc_tan2(readMMA7455Byte(0x08),readMMA7455Byte(0x06))); delay_ms(1000); lcd_wcmd(0x01);//显示清屏 lcd_pos(0,1); Cvt_Str(txtbuf,readMMA7455Byte(0x06)); txtbuf[0]=‘X‘; lcd_string(txtbuf); lcd_pos(0,0); Cvt_Str(txtbuf,readMMA7455Byte(0x07)); txtbuf[0]=‘Y‘; lcd_string(txtbuf); delay_ms(1000); lcd_wcmd(0x01);//显示清屏 }}
Mma7455 acceleration sensor volume Angle