JY901.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. case 0x59: memcpy(&stcQuater,&ucRxBuffer[2],8);break;
  42. }
  43. ucRxCnt=0;
  44. }
  45. }
  46. void CJY901::readRegisters(unsigned char deviceAddr,unsigned char addressToRead, unsigned char bytesToRead, char * dest)
  47. {
  48. Wire.beginTransmission(deviceAddr);
  49. Wire.write(addressToRead);
  50. Wire.endTransmission(false); //endTransmission but keep the connection active
  51. Wire.requestFrom(deviceAddr, bytesToRead); //Ask for bytes, once done, bus is released by default
  52. while(Wire.available() < bytesToRead); //Hang out until we get the # of bytes we expect
  53. for(int x = 0 ; x < bytesToRead ; x++)
  54. dest[x] = Wire.read();
  55. }
  56. void CJY901::writeRegister(unsigned char deviceAddr,unsigned char addressToWrite,unsigned char bytesToRead, char *dataToWrite)
  57. {
  58. Wire.beginTransmission(deviceAddr);
  59. Wire.write(addressToWrite);
  60. for(int i = 0 ; i < bytesToRead ; i++)
  61. Wire.write(dataToWrite[i]);
  62. Wire.endTransmission(); //Stop transmitting
  63. }
  64. short CJY901::ReadWord(unsigned char ucAddr)
  65. {
  66. short sResult;
  67. readRegisters(ucDevAddr, ucAddr, 2, (char *)&sResult);
  68. return sResult;
  69. }
  70. void CJY901::WriteWord(unsigned char ucAddr,short sData)
  71. {
  72. writeRegister(ucDevAddr, ucAddr, 2, (char *)&sData);
  73. }
  74. void CJY901::ReadData(unsigned char ucAddr,unsigned char ucLength,char chrData[])
  75. {
  76. readRegisters(ucDevAddr, ucAddr, ucLength, chrData);
  77. }
  78. void CJY901::GetTime()
  79. {
  80. readRegisters(ucDevAddr, 0x30, 8, (char*)&stcTime);
  81. }
  82. void CJY901::GetAcc()
  83. {
  84. readRegisters(ucDevAddr, AX, 6, (char *)&stcAcc);
  85. }
  86. void CJY901::GetGyro()
  87. {
  88. readRegisters(ucDevAddr, GX, 6, (char *)&stcGyro);
  89. }
  90. void CJY901::GetAngle()
  91. {
  92. readRegisters(ucDevAddr, Roll, 6, (char *)&stcAngle);
  93. }
  94. void CJY901::GetMag()
  95. {
  96. readRegisters(ucDevAddr, HX, 6, (char *)&stcMag);
  97. }
  98. void CJY901::GetPress()
  99. {
  100. readRegisters(ucDevAddr, PressureL, 8, (char *)&stcPress);
  101. }
  102. void CJY901::GetDStatus()
  103. {
  104. readRegisters(ucDevAddr, D0Status, 8, (char *)&stcDStatus);
  105. }
  106. void CJY901::GetLonLat()
  107. {
  108. readRegisters(ucDevAddr, LonL, 8, (char *)&stcLonLat);
  109. }
  110. void CJY901::GetGPSV()
  111. {
  112. readRegisters(ucDevAddr, GPSHeight, 8, (char *)&stcGPSV);
  113. }
  114. CJY901 JY901 = CJY901();