bsp_ms5611.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "main.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 ReadPressure_Pre(void)
  51. {
  52. uint8_t dat = 0;
  53. //I2C_WriteByte(I2C1,MSE5611_Slave,Convert_D1_4096);
  54. MSE5611_register_write_len(MSE5611_Slave, Convert_D1_256, 0, &dat);
  55. }
  56. uint32_t ReadPressure(void)
  57. {
  58. uint8_t dd[3] = {0,0,0};
  59. uint8_t R = 0;
  60. MSE5611_register_read_len(MSE5611_Slave,R,3,dd);
  61. D[0] = ((uint32_t)(dd[0]<<16)|(dd[1]<<8)|(dd[2]<<0));
  62. return D[0];
  63. }
  64. int MS5611_Init(void)
  65. {
  66. uint8_t i;
  67. uint8_t dd[2];
  68. MSE5611_register_read_len(MSE5611_Slave,PROM_Read_0,2,dd);
  69. Reserve = ((uint16_t)dd[0]<<8|dd[1]);
  70. SEGGER_RTT_printf(0,"MSE5611_Config:");
  71. for(i=0;i<6;i++){
  72. MSE5611_register_read_len(MSE5611_Slave,PROM_Read_1+i*2,2,dd);
  73. CFG[i] = ((uint16_t)dd[0]<<8 | dd[1]);
  74. SEGGER_RTT_printf(0,"%02X ",CFG[i]);
  75. }
  76. SEGGER_RTT_printf(0,"\n");
  77. MSE5611_register_read_len(MSE5611_Slave,PROM_Read_7,2,dd);
  78. MSE5611_CRC = ((uint16_t)dd[0]<<8|dd[1]);
  79. }