|
本帖最後由 mzw2008 於 2010-6-14 23:02 編輯
sample code在sensor寄到那天晚上我就寄給你了啊
這是基本的I2C介面控制程式
小修改就能變成用arduino控制了, 相關的規格在這裡,
Address 在官網裡面都有答案, 請自行查詢(打開文件搜尋address)
http://www.roboard.com/Files/G144/HMC5843.pdf
http://www.roboard.com/Files/G144/ADXL345.pdf
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <roboard.h>
int main(void){
unsigned char d1,d2,d3,d4,d5,d6;
unsigned long i2c_clock = 400000L;
unsigned int i2c_address = 0x53;
if (i2c_Initialize(I2CIRQ_DISABLE) == false)
{
printf("FALSE!! %s\n", roboio_GetErrMsg());
return -1;
}
i2c0_SetSpeed(I2CMODE_FAST, i2c_clock);
i2c0master_StartN(i2c_address,I2C_WRITE,2);//write 2 byte
i2c0master_WriteN(0x2d); //Pwoer_Control register
i2c0master_WriteN(0x28); //link and measure mode
delay(100);
i2c0master_StartN(i2c_address,I2C_WRITE,2);//write 2 byte
i2c0master_WriteN(0x31); //Data_Format register
i2c0master_WriteN(0x08); //Full_Resolution
delay(100);
i2c0master_StartN(i2c_address,I2C_WRITE,2);//write 2 byte
i2c0master_WriteN(0x38); //FIFO_Control register
i2c0master_WriteN(0x00); //bypass mode
delay(100);
do
{
i2c0master_StartN(i2c_address, I2C_WRITE, 1);
i2c0master_SetRestartN(I2C_READ, 6);
i2c0master_WriteN(0x32); //Read from X register (Address : 0x32)
d1 = i2c0master_ReadN();//X LSB
d2 = i2c0master_ReadN();//X MSB
d3 = i2c0master_ReadN();//Y LSB
d4 = i2c0master_ReadN();//Y MSB
d5 = i2c0master_ReadN();//Z LSB
d6 = i2c0master_ReadN();//Z MSB
//Data : 10 bits
printf("Acceleration of X-axis :%d\n", (d2 & 0x02) ? ~(0xFFFF ^ (d2*256+d1)) : d2*256+d1);
printf("Acceleration of Y-axis :%d\n", (d4 & 0x02) ? ~(0xFFFF ^ (d4*256+d3)) : d4*256+d3);
printf("Acceleration of Z-axis :%d\n", (d6 & 0x02) ? ~(0xFFFF ^ (d6*256+d5)) : d6*256+d5);
delay(100);
}while(getch() != 27);
i2c_Close();
return 0;
} |
|