- #include <REG52.H>
- #include <math.h> //Keil library
- #include <stdio.h> //Keil library
- #include <INTRINS.H>
- #define Byte unsigned char
- #define Word unsigned short
- sbit SCL=P1^0; //IIC時鐘引腳定義
- sbit SDA=P1^1; //IIC數據引腳定義
- Byte BUF[6]; //接收數據緩存區
- Word Wbuf[2];
- void ADXL345_Start();
- void ADXL345_Stop();
- void ADXL345_SendACK(bit ack);
- bit ADXL345_RecvACK();
- void ADXL345_SendByte(Byte dat);
- Byte ADXL345_RecvByte();
- void Multiple_read_ADXL345(void);
- void Single_Write_ADXL345(Byte REG_Address,Byte REG_data);
- void Init_ADXL345();
- void Data_Convert();
- void Delay(unsigned int k);
- void Delay5us();
- void send(Byte pd);
- void send_init();
- void Delay5us() //@11.0592MHz
- {
- }
- //起始信號
- void ADXL345_Start()
- {
- SDA = 1; //拉高數據線
- Delay5us(); //延時
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SDA = 0; //產生下降沿
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- }
- //停止信號
- void ADXL345_Stop()
- {
- SDA = 0; //拉低數據線
- Delay5us(); //延時
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SDA = 1; //產生上升沿
- Delay5us(); //延時
- }
- //發送應答信號
- //入口參數:ack (0:ACK 1:NAK)
- void ADXL345_SendACK(bit ack)
- {
- SDA = ack; //寫應答信號
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- //接收應答信號
- bit ADXL345_RecvACK()
- {
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- CY = SDA; //讀應答信號
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- return CY;
- }
- //向IIC總線發送一個字節數據
- void ADXL345_SendByte(Byte dat)
- {
- Byte i;
- for (i=0; i<8; i++) //8位計數器
- {
- dat <<= 1; //移出數據的最高位
- SDA = CY; //送數據口
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- ADXL345_RecvACK();
- }
- //從IIC總線接收一個字節數據
- Byte ADXL345_RecvByte()
- {
- Byte i;
- Byte date = 0;
- SDA = 1; //使能內部上拉,準備讀取數據,
- for (i=0; i<8; i++) //8位計數器
- {
- date <<= 1;
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- date |= SDA; //讀數據
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- return date;
- }
- //單字節讀取
- //Byte Single_Read_ADXL345(Byte REG_Address)
- //{ Byte REG_data;
- // ADXL345_Start(); //起始信號
- // ADXL345_SendByte(0xA6); //發送設備地址+寫信號
- // ADXL345_SendByte(REG_Address); //發送存儲單元地址,從0開始
- // ADXL345_Start(); //起始信號
- // ADXL345_SendByte(0xA7); //發送設備地址+讀信號
- // REG_data=ADXL345_RecvByte(); //讀出寄存器數據
- // ADXL345_SendACK(1);
- // ADXL345_Stop(); //停止信號
- // return REG_data;
- //}
- //連續讀出ADXL345內部加速度數據,地址范圍0x32~0x37
- void Multiple_read_ADXL345(void)
- { Byte i;
- ADXL345_Start(); //起始信號
- ADXL345_SendByte(0xA6); //發送設備地址+寫信號
- ADXL345_SendByte(0x32); //發送存儲單元地址,從0x32開始
- ADXL345_Start(); //起始信號
- ADXL345_SendByte(0xA7); //發送設備地址+讀信號
- for (i=0; i<6; i++) //連續讀取6個地址數據,存儲中BUF
- {
- BUF[i] = ADXL345_RecvByte(); //BUF[0]存儲0x32地址中的數據
- if (i == 5)
- {
- ADXL345_SendACK(1); //最后一個數據需要回NOACK
- }
- else
- {
- ADXL345_SendACK(0); //回應ACK
- }
- }
- ADXL345_Stop(); //停止信號
- Delay(5);
- }
- //單字節寫入
- void Single_Write_ADXL345(Byte REG_Address,Byte REG_data)
- {
- ADXL345_Start(); //起始信號
- ADXL345_SendByte(0xA6); //發送設備地址+寫信號
- ADXL345_SendByte(REG_Address); //內部寄存器地址
- ADXL345_SendByte(REG_data); //內部寄存器數據
- ADXL345_Stop(); //發送停止信號
- }
- //初始化ADXL345,根據需要請參考pdf進行修改
- void Init_ADXL345()
- {
- Single_Write_ADXL345(0x31,0x0B); //測量范圍,正負16g,13位模式
- Single_Write_ADXL345(0x2C,0x08); //速率設定為12.5 參考pdf13頁
- Single_Write_ADXL345(0x2D,0x08); //選擇電源模式 參考pdf24頁
- Single_Write_ADXL345(0x2E,0x80); //使能 DATA_READY 中斷
- Single_Write_ADXL345(0x1E,0x00); //X 偏移量 根據測試傳感器的狀態寫入pdf29頁
- Single_Write_ADXL345(0x1F,0x00); //Y 偏移量 根據測試傳感器的狀態寫入pdf29頁
- Single_Write_ADXL345(0x20,0x05); //Z 偏移量 根據測試傳感器的狀態寫入pdf29頁
- }
- //將兩個八位數據合成為一個16位數據
- void Data_Convert()
- {
- Wbuf[0]=BUF[1]<<8|BUF[0];
- Wbuf[0]=Wbuf[0]/64;
- Wbuf[1]=BUF[3]<<8|BUF[2];
- Wbuf[1]=Wbuf[1]/64;
- Wbuf[2]=BUF[5]<<8|BUF[4];
- Wbuf[2]=Wbuf[2]/64;
- }
- //****************************************
- //延時
- //****************************************
- void Delay(unsigned int k)
- {
- unsigned int i,j;
- for(i=0;i<k;i++)
- {
- for(j=0;j<121;j++);
- }
- }
- void send(Byte pd)
- {
- SBUF=pd;
- while(!TI);
- TI=0;
- }
- void send_init()
- {
- TMOD=0X20;
- TH1=0XFD;
- TL1=0XFD;
- TR1=1;
- SM0=0;
- SM1=1;
- EA=1;
- ES=1;
- }
- void main()
- {
- Delay(500); //上電延時
- Init_ADXL345();
- send_init();
- Delay(150);
- while(1)
- {
- if(Wbuf[0]>350)
- {
- if(Wbuf[1]>350)
- {send('7');}//右前
- if(Wbuf[1]<-350)
- {send('5');}//左前
- if(350>Wbuf[1]>-350)
- {send('4');}//右
- }
- if(Wbuf[0]<-350)
- {
-
- if(Wbuf[1]>350)
- {send('8');}//右后
- if(Wbuf[1]<-350)
- {send('6');}//左后
- if(350>Wbuf[1]>-350)
- {send('3');}//左
- }
- if(350>Wbuf[0]>-350)
- {
- if(Wbuf[1]>350)
- {send('1');}//前
- if(Wbuf[1]<-350)
- {send('2');}//后
- if(350>Wbuf[1]>-350)
- {send('9');}//停
- }
-
-
- Delay(500);
- }
- }
復制代碼 |