JY901.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "JY901.h"
  2. #include "string.h"
  3. CJY901 ::CJY901 ()
  4. {
  5. ucDevAddr =0x50;
  6. }
  7. void CJY901::StartIIC()
  8. {
  9. ucDevAddr = 0x50;
  10. Wire.begin();
  11. }
  12. void CJY901::StartIIC(unsigned char ucAddr)
  13. {
  14. ucDevAddr = ucAddr;
  15. Wire.begin();
  16. }
  17. void CJY901 ::CopeSerialData(unsigned char ucData)
  18. {
  19. static unsigned char ucRxBuffer[250];
  20. static unsigned char ucRxCnt = 0;
  21. ucRxBuffer[ucRxCnt++]=ucData;
  22. if (ucRxBuffer[0]!=0x55)
  23. {
  24. ucRxCnt=0;
  25. return;
  26. }
  27. if (ucRxCnt<11) {return;}
  28. else
  29. {
  30. switch(ucRxBuffer[1])
  31. {
  32. case 0x50: memcpy(&stcTime,&ucRxBuffer[2],8);break;
  33. case 0x51: memcpy(&stcAcc,&ucRxBuffer[2],8);break;
  34. case 0x52: memcpy(&stcGyro,&ucRxBuffer[2],8);break;
  35. case 0x53: memcpy(&stcAngle,&ucRxBuffer[2],8);break;
  36. case 0x54: memcpy(&stcMag,&ucRxBuffer[2],8);break;
  37. case 0x55: memcpy(&stcDStatus,&ucRxBuffer[2],8);break;
  38. case 0x56: memcpy(&stcPress,&ucRxBuffer[2],8);break;
  39. case 0x57: memcpy(&stcLonLat,&ucRxBuffer[2],8);break;
  40. case 0x58: memcpy(&stcGPSV,&ucRxBuffer[2],8);break;
  41. }
  42. ucRxCnt=0;
  43. }
  44. }
  45. void CJY901::readRegisters(unsigned char deviceAddr,unsigned char addressToRead, unsigned char bytesToRead, char * dest)
  46. {
  47. Wire.beginTransmission(deviceAddr);
  48. Wire.write(addressToRead);
  49. Wire.endTransmission(false); //endTransmission but keep the connection active
  50. Wire.requestFrom(deviceAddr, bytesToRead); //Ask for bytes, once done, bus is released by default
  51. while(Wire.available() < bytesToRead); //Hang out until we get the # of bytes we expect
  52. for(int x = 0 ; x < bytesToRead ; x++)
  53. dest[x] = Wire.read();
  54. }
  55. void CJY901::writeRegister(unsigned char deviceAddr,unsigned char addressToWrite,unsigned char bytesToRead, char *dataToWrite)
  56. {
  57. Wire.beginTransmission(deviceAddr);
  58. Wire.write(addressToWrite);
  59. for(int i = 0 ; i < bytesToRead ; i++)
  60. Wire.write(dataToWrite[i]);
  61. Wire.endTransmission(); //Stop transmitting
  62. }
  63. short CJY901::ReadWord(unsigned char ucAddr)
  64. {
  65. short sResult;
  66. readRegisters(ucDevAddr, ucAddr, 2, (char *)&sResult);
  67. return sResult;
  68. }
  69. void CJY901::WriteWord(unsigned char ucAddr,short sData)
  70. {
  71. writeRegister(ucDevAddr, ucAddr, 2, (char *)&sData);
  72. }
  73. void CJY901::ReadData(unsigned char ucAddr,unsigned char ucLength,char chrData[])
  74. {
  75. readRegisters(ucDevAddr, ucAddr, ucLength, chrData);
  76. }
  77. void CJY901::GetTime()
  78. {
  79. readRegisters(ucDevAddr, 0x30, 8, (char*)&stcTime);
  80. }
  81. void CJY901::GetAcc()
  82. {
  83. readRegisters(ucDevAddr, AX, 6, (char *)&stcAcc);
  84. }
  85. void CJY901::GetGyro()
  86. {
  87. readRegisters(ucDevAddr, GX, 6, (char *)&stcGyro);
  88. }
  89. void CJY901::GetAngle()
  90. {
  91. readRegisters(ucDevAddr, Roll, 6, (char *)&stcAngle);
  92. }
  93. void CJY901::GetMag()
  94. {
  95. readRegisters(ucDevAddr, HX, 6, (char *)&stcMag);
  96. }
  97. void CJY901::GetPress()
  98. {
  99. readRegisters(ucDevAddr, PressureL, 8, (char *)&stcPress);
  100. }
  101. void CJY901::GetDStatus()
  102. {
  103. readRegisters(ucDevAddr, D0Status, 8, (char *)&stcDStatus);
  104. }
  105. void CJY901::GetLonLat()
  106. {
  107. readRegisters(ucDevAddr, LonL, 8, (char *)&stcLonLat);
  108. }
  109. void CJY901::GetGPSV()
  110. {
  111. readRegisters(ucDevAddr, GPSHeight, 8, (char *)&stcGPSV);
  112. }
  113. void CJY901::GetQuater()
  114. {
  115. readRegisters(ucDevAddr, QUATER, 8, (char *)&stcQuater);
  116. }
  117. CJY901 JY901 = CJY901();