MSE5611.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "MSE5611.h"
  2. #define MSE5611_Slave 0xEC
  3. //#define MSE5611_Slave 0xEE
  4. #define MSE5611_Reset 0x1E
  5. #define Convert_D1_256 0x40
  6. #define Convert_D1_512 0x42
  7. #define Convert_D1_1024 0x44
  8. #define Convert_D1_2048 0x46
  9. #define Convert_D1_4096 0x48
  10. #define Convert_D2_256 0x50
  11. #define Convert_D2_512 0x52
  12. #define Convert_D2_1024 0x54
  13. #define Convert_D2_2048 0x56
  14. #define Convert_D2_4096 0x58
  15. #define ADC_Read 0x00
  16. #define PROM_Read_0 0xA0
  17. #define PROM_Read_1 0xA2
  18. #define PROM_Read_2 0xA4
  19. #define PROM_Read_3 0xA6
  20. #define PROM_Read_4 0xA8
  21. #define PROM_Read_5 0xAA
  22. #define PROM_Read_6 0xAC
  23. #define PROM_Read_7 0xAE
  24. #define Sea_Level_Pressure 101325.0
  25. uint16_t Reserve = 0;
  26. uint16_t CFG[6] = {0};
  27. uint16_t MSE5611_CRC = 0;
  28. uint32_t D[2] = {0};
  29. int32_t Temp = 0;
  30. int32_t Press = 0;
  31. float Height = 0;
  32. int MSE5611_register_write_len(uint8_t addr,uint8_t register_address, uint8_t len,uint8_t *buf)
  33. {
  34. uint8_t w2_data[50],i;
  35. w2_data[0] = register_address;
  36. for(i=0;i<len;i++){
  37. w2_data[i+1] = *(buf+i);
  38. }
  39. if(twi_master_transfer(addr, w2_data, len+1, TWI_ISSUE_STOP) == true) return 0;
  40. else return -1;
  41. }
  42. int MSE5611_register_read_len(uint8_t addr,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  43. {
  44. bool transfer_succeeded;
  45. transfer_succeeded = twi_master_transfer(addr, &register_address, 1, TWI_DONT_ISSUE_STOP);
  46. transfer_succeeded &= twi_master_transfer(addr|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP);
  47. if(transfer_succeeded == true)return 0;
  48. else return -1;
  49. }
  50. void Reset_MSE5611(void)
  51. {
  52. uint8_t dat = 0;
  53. //I2C_WriteByte(I2C1,MSE5611_Slave,MSE5611_Reset);
  54. MSE5611_register_write_len(MSE5611_Slave, MSE5611_Reset, 0, &dat);
  55. nrf_delay_ms(20);
  56. }
  57. void ReadPressure_Pre(void)
  58. {
  59. uint8_t dat = 0;
  60. //I2C_WriteByte(I2C1,MSE5611_Slave,Convert_D1_4096);
  61. MSE5611_register_write_len(MSE5611_Slave, Convert_D1_256, 0, &dat);
  62. }
  63. uint32_t ReadPressure(void)
  64. {
  65. uint8_t dd[3] = {0,0,0};
  66. uint8_t R = 0;
  67. MSE5611_register_read_len(MSE5611_Slave,R,3,dd);
  68. D[0] = ((uint32_t)(dd[0]<<16)|(dd[1]<<8)|(dd[2]<<0));
  69. // printf("D[0]=%d\r\n",D[0]);
  70. return D[0];
  71. //I2C_ReadBytes(I2C1,MSE5611_Slave,ADC_Read,3,(uint8_t*)D,MSB);
  72. // MSE5611_register_read_len(MSE5611_Slave,&R,3,(uint8_t*)&D[0]);
  73. }
  74. void Single_Read_Pressure(void)
  75. {
  76. ReadPressure_Pre();
  77. nrf_delay_ms(2);
  78. ReadPressure();
  79. }
  80. void ReadTemp_Pre(void)
  81. {
  82. uint8_t dat = 0;
  83. //I2C_WriteByte(I2C1,MSE5611_Slave,Convert_D2_4096);
  84. MSE5611_register_write_len(MSE5611_Slave, Convert_D2_4096, 0, &dat);
  85. }
  86. void ReadTemp(void)
  87. {
  88. //I2C_ReadBytes(I2C1,MSE5611_Slave,ADC_Read,3,(uint8_t*)( D + 1 ),MSB);
  89. MSE5611_register_read_len(MSE5611_Slave,ADC_Read,3,(uint8_t*)&D[1]);
  90. printf("T:%d\n",D[1]);
  91. }
  92. void Single_Read_Temp(void)
  93. {
  94. ReadTemp_Pre();
  95. nrf_delay_ms(2);
  96. ReadTemp();
  97. }
  98. float Pressure_Caculate(void)
  99. {
  100. int64_t OFF = 0,OFF2 = 0,SENS = 0, SENS2 = 0,T2 = 0;
  101. int64_t dT = 0;
  102. dT = D[1] - (((int64_t)CFG[4]) << 8);
  103. Temp = 2000 + (((int64_t)dT * CFG[5])>>23);
  104. if(Temp < 2000)
  105. {
  106. T2 = ( dT*dT )>>31;
  107. OFF2 = 5 * (Temp - 2000) * (Temp - 2000) >> 1;
  108. SENS2 = 5 * (Temp - 2000) * (Temp - 2000) >> 2;
  109. }
  110. if(Temp < -1500)
  111. {
  112. OFF2 = OFF2 + 7 * (Temp + 1500) * (Temp + 1500);
  113. SENS2 = SENS2 + ( 11 * (Temp +1500) * (Temp +1500) >> 1 );
  114. }
  115. OFF = (((int64_t)CFG[1]) << 16) + ((CFG[3] * dT) >> 7);
  116. SENS = (((int64_t)CFG[0]) << 15) + ((CFG[2] * dT) >> 8);
  117. OFF -= OFF2;
  118. Temp -= T2;
  119. SENS -= SENS2;
  120. Press = (((D[0] * SENS)>>21) - OFF)>>15;
  121. Height = 44330 * (1 - pow(Press/Sea_Level_Pressure ,1 / 5.225));
  122. return Height;
  123. }
  124. void MSE5611_Config(void)
  125. {
  126. uint8_t i;
  127. uint8_t dd[2];
  128. Reset_MSE5611();
  129. MSE5611_register_read_len(MSE5611_Slave,PROM_Read_0,2,dd);
  130. Reserve = ((uint16_t)dd[0]<<8|dd[1]);
  131. printf("Reserve=%x\r\n",Reserve);
  132. for(i=0;i<6;i++){
  133. MSE5611_register_read_len(MSE5611_Slave,PROM_Read_1+i*2,2,dd);
  134. CFG[i] = ((uint16_t)dd[0]<<8 | dd[1]);
  135. printf("CFG[%d]=%x\r\n",i,CFG[i]);
  136. }
  137. MSE5611_register_read_len(MSE5611_Slave,PROM_Read_7,2,dd);
  138. MSE5611_CRC = ((uint16_t)dd[0]<<8|dd[1]);
  139. printf("MSE5611_CRC=%x\r\n",MSE5611_CRC);
  140. }