|
那為什麼這程式不行?
#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 = 0x3C;
if (i2c_Initialize(I2CIRQ_DISABLE) == false)
{
printf("FALSE!! %s\n", roboio_GetErrMsg());
return -1;
}
i2c0_SetSpeed(I2CMODE_FAST, i2c_clock);
i2c0master_StartN(i2c_address >> 1,I2C_WRITE,2);//write 2 byte
i2c0master_WriteN(0x02); //mode register
i2c0master_WriteN(0x00); //continue-measureture mode
delay(100);
do
{
i2c0master_StartN(i2c_address >> 1, I2C_WRITE, 1);
i2c0master_SetRestartN(I2C_READ, 6);
i2c0master_WriteN(0x03); //Read from data register (Address : 0x03)
d1 = i2c0master_ReadN();//X MSB
d2 = i2c0master_ReadN();//X LSB
d3 = i2c0master_ReadN();//Y MSB
d4 = i2c0master_ReadN();//Y LSB
d5 = i2c0master_ReadN();//Z MSB
d6 = i2c0master_ReadN();//Z LSB
//Data : 16bits
printf("X axis :%d\n", ((d1 & 0xF0) > 0) ? ~(0xFFFF ^ (d1*256+d2)): d1*256+d2);
printf("Y axis :%d\n", ((d3 & 0xF0) > 0) ? ~(0xFFFF ^ (d3*256+d4)): d3*256+d4);
printf("Z axis :%d\n", ((d5 & 0xF0) > 0) ? ~(0xFFFF ^ (d5*256+d6)): d5*256+d6);
delay(100);
}while(getch() != 27);
i2c_Close();
return 0;
} |
|