MMA7455電路原理圖如下:
0.png (57.48 KB, 下載次數: 28)
下載附件
2018-5-15 17:07 上傳
單片機源程序如下:
- /*
- * MMA7455模塊
- *
- * 用途:MMA7455模塊測試程序
- *
- * 作者 日期 備注
- * Huafeng Lin 2010/12/10 新增
- * Huafeng Lin 2011/08/13 修改
- *
- */
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- /***************************************************************************/
- /*********** 單片機引腳定義 ************/
- /***************************************************************************/
- //CS 接3.3V
- sbit sda=P0^6; //I2C 數據傳送位
- sbit scl=P0^7; //I2C 時鐘傳送位
- //
- sbit rs=P0^3; //1602RS控制位
- sbit rw=P0^2; //1602RW控制位
- sbit e =P0^1; //1602E 控制位
- /******************************************************************************/
- /********** 數據部分 ***********/
- /******************************************************************************/
- #define IIC_READ 0x1D //定義讀指令
- #define IIC_WRITE 0x1D //定義寫指令
- uchar table1[16]=" MMA7455 X= 0.00"; //1602顯示數據
- uchar table2[16]=" Y= 0.00 Z= 0.00";
- uchar table3[16]="No Find MMA7455!";
- uchar table4[10]="0123456789";
- uchar table5[8] =" MMA7455";
- uchar table7[16]="Value: X=0.50 ";
- /**************************************************************************/
- /************ 各延時程序 **************/
- /**************************************************************************/
- void iic_delay() //5us延時
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- void delay_50us(uint t)
- {
- uchar j;
- for(;t>0;t--)
- for(j=19;j>0;j--);
- }
- void delay_50ms(uchar t)
- {
- uint j;
- for(;t>0;t--)
- for(j=6245;j>0;j--);
- }
- /************************************************************************/
- /************** 1602顯示部分 *****************/
- /************************************************************************/
- void write_com(uchar com) //函數功能:寫指令
- {
- e=0;
- rs=0;
- rw=0;
- P2=com;
- delay_50us(10);
- e=1;
- delay_50us(20);
- e=0;
- }
- void write_data(uchar dat) //函數功能:寫數據
- {
- e=0;
- rs=1;
- rw=0;
- P2=dat;
- delay_50us(10);
- e=1;
- delay_50us(20);
- e=0;
- }
- void init1602(void) //函數功能:初始化1602
- {
- delay_50us(300);
- write_com(0x38);
- delay_50us(100);
- write_com(0x38);
- delay_50us(100);
- write_com(0x38);
- write_com(0x38);
- write_com(0x01);
- write_com(0x01);
- write_com(0x06);
- write_com(0x0c);
- }
- /*********************************************************************/
- /************** I2C通信部分 ***************/
- /*********************************************************************/
- void iic_start() //函數功能:I2C通信開始
- {
- sda=1;
- iic_delay();
- scl=1;
- iic_delay();
- sda=0;
- iic_delay();
- }
- void iic_stop() //函數功能:I2C通信停止
- {
- sda=0;
- iic_delay();
- scl=1;
- iic_delay();
- sda=1;
- iic_delay();
- }
- void iic_ack() //函數功能:I2C通信查應答位
- {
- sda=1;
- scl=1;
- iic_delay();
- scl=0;
- }
- void iic_write_byte(uchar wdata) //函數功能:向I2C從機寫入一個字節
- {
- uchar i,temp,temp1;
-
- temp1=wdata;
- for(i=0;i<8;i++)
- {
- scl = 0;
- iic_delay();
- temp=temp1;
- temp=temp&0x80;
- if(temp==0x80)
- sda=1;
- else
- sda=0;
- iic_delay();
- scl=1;
- iic_delay();
- scl=0;
- iic_delay();
- temp1=temp1<<1;
- }
- }
- char iic_read_byte(void) //函數功能:從I2C從機中讀出一個字節
- {
- uchar x;
- char data_data;
- for(x=0;x<8;x++)
- {
- data_data=data_data<<1;
- sda=1;
- iic_delay();
- scl=0;
- iic_delay();
- scl=1;
- iic_delay();
- if(sda==1)
- data_data|=0x01;
- //else
- // data_data&=0xfe;
- }
- return data_data;
- }
- void iic_write(uchar byte_add,uchar wdata) //函數功能:按地址寫入一字節數據
- {
- uchar t;
- t=(IIC_WRITE<<1);
- iic_start();
- iic_write_byte(t);
- iic_ack();
- iic_write_byte(byte_add);
- iic_ack();
- iic_write_byte(wdata);
- iic_ack();
- iic_stop();
- }
- char iic_read(uchar byte_add) //函數功能:按地址讀出一字節數據
- {
- uchar t;
- char x;
- t=(IIC_WRITE<<1);
- iic_start();
- iic_write_byte(t);
- iic_ack();
- iic_write_byte(byte_add);
- iic_ack();
- t=((IIC_READ<<1)|0x01);
- iic_start();
- iic_write_byte(t);
- iic_ack();
- x=iic_read_byte();
- iic_ack();
- iic_stop();
- return x;
- }
- /************************************************************************/
- /************* LED顯示陣列部分 **************/
- /************************************************************************/
- char self_test7455(void) //函數功能:檢測7455有沒有插好
- { // 如果沒有插好,1602將會顯示
- uchar j; // “No acceleration!"
- char t;
- delay_50us(10);
- iic_write(0x16,0x05);
- delay_50us(20);
- t=iic_read(0x16);
- if(t!=0x05)
- {
- write_com(0x80);
- for(j=0;j<16;j++)
- {
- write_data(table3[j]);
- delay_50us(10);
- }
- return 0;
- }
- return 1;
- }
- void Testinit1602()
- {
- char j;
- write_com(0x80);
- for(j=0;j<16;j++)
- {
- write_data(table1[j]);
- delay_50us(10);
- }
- write_com(0x80+0x40);
- for(j=0;j<16;j++)
- {
- write_data(table2[j]);
- delay_50us(10);
- }
- }
- void sendx() //函數功能:向1602發送x軸測量數據
- {
- uchar x1,x2,x3,xsign;
- char x;
- x=iic_read(0x06);
- if((x&0x80)==0x00)
- {
- xsign=0x2b; //+
- }
- else
- {
- xsign=0x2d; //-
- x=x-0x01;
- x=~x;
- }
- x1=(x/63);
- x2=((x*100/63)%100)/10;
- x3=(x*100/63)%10;
- write_com(0x8B);
- write_data(xsign);
- delay_50us(10);
- write_com(0x8C);
- write_data(table4[x1]);
- delay_50us(10);
- write_com(0x8E);
- write_data(table4[x2]);
- delay_50us(10);
- write_com(0x8F);
- write_data(table4[x3]);
- delay_50us(20);
- }
- void sendy() //函數功能:向1602發送y軸測量數據
- {
- uchar y1,y2,y3,ysign;
- char y;
- y=iic_read(0x07);
- if((y&0x80)==0x00)
- {
- ysign=0x2b; //+
- }
- else
- {
- ysign=0x2d; //-
- y=y-0x01;
- y=~y;
- }
- y1=(y/63);
- y2=((y*100/63)%100)/10;
- y3=(y*100/63)%10;
- write_com(0xC3);
- write_data(ysign);
- delay_50us(10);
- write_com(0xC4);
- write_data(table4[y1]);
- delay_50us(10);
- write_com(0xC6);
- write_data(table4[y2]);
- delay_50us(10);
- write_com(0xC7);
- write_data(table4[y3]);
- delay_50us(20);
- }
- void sendz() //函數功能:向1602發送z軸測量數據
- {
- uchar z1,z2,z3,zsign;
- char z;
- z=iic_read(0x08);
- if((z&0x80)==0x00)
- {
- zsign=0x2b; //+
- }
- else
- {
- zsign=0x2d; //-
- z=z-0x01;
- z=~z;
- }
- z1=(z/63);
- z2=((z*100/63)%100)/10;
- z3=(z*100/63)%10;
- write_com(0xCB);
- write_data(zsign);
- delay_50us(10);
- write_com(0xCC);
- write_data(table4[z1]);
- delay_50us(10);
- write_com(0xCE);
- write_data(table4[z2]);
- delay_50us(10);
- write_com(0xCF);
- write_data(table4[z3]);
- delay_50us(20);
- }
- /***********************************************************/
- /**************** 主函數 ****************/
- /***********************************************************/
- void main()
- {
- uchar j;
- init1602();
- self_test7455();
- iic_write(0x10,0x15);//校正X值
- iic_write(0x12,0x25);//校正Y值
- iic_write(0x14,0x10);//校正Z值
- delay_50ms(2);
- write_com(0x80);
- for(j=0;j<8;j++)
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
0.jpg (20.73 KB, 下載次數: 27)
下載附件
2018-5-15 17:08 上傳
所有資料51hei提供下載:
MMA7455模塊.rar
(729.26 KB, 下載次數: 16)
2018-5-15 11:14 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|