Kodingan untuk Sensor CMPS14 membaca nilai sudut Yaw
Dependencies: mbed
coba_cmps14.cpp
00001 #include "mbed.h" 00002 00003 #define i2c_address 0x60 00004 00005 int BEARING_Register; 00006 char bits[2]; 00007 int _byteHigh; 00008 int _byteLow; 00009 int bearing; 00010 00011 I2C CMPS(PB_4, PA_8); 00012 Serial pc(USBTX, USBRX); 00013 00014 int main() 00015 { 00016 BEARING_Register = 0x02; 00017 pc.baud(9600); 00018 while(1) 00019 { 00020 CMPS.unlock(); 00021 CMPS.start(); 00022 // to indicate an i2c read, shift the 7 bit address up 1 bit and set bit 0 to a 1 00023 CMPS.write(i2c_address << 1); 00024 int writeResult = CMPS.write(BEARING_Register); 00025 if(writeResult != 1) 00026 { 00027 pc.printf("%d\n", writeResult); 00028 return 0; 00029 } 00030 else 00031 { 00032 CMPS.stop(); 00033 CMPS.read(i2c_address <<1, bits, 2); 00034 _byteHigh = bits[0]; 00035 _byteLow = bits[1]; 00036 bearing = ((_byteHigh<<=8) + _byteLow) / 10; 00037 pc.printf("Bearing : %d\n", bearing); 00038 } 00039 } 00040 } 00041 00042
Generated on Thu Oct 6 2022 11:20:58 by
1.7.2