MSE5611.c 3.9 KB

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