单片机控制气压海拔模块BMP180之模块化编程(持续更新中)

来源:互联网 发布:java中级工程师 编辑:程序博客网 时间:2024/04/28 09:54

这里将我写的STC12C5A60S2单片机控制气压海拔测量模块BMP180的程序共享一下,是为了让前辈给予斧正。

更新:

2015/05/05  08:30  完善了温度值的类型及其运算(没有进行实物验证)

2014/04/17 10:22

(也可以用官网封装好的函数BMP180_API  :http://www.general-files.org/download/gs64624bfeh32i0/bmp180_api.zip.html)

(补充:只需要修改bmp180.h文件中含有 “选择” 字样的部分,就可以达到复用的效果,只需注意中文注释部分)

对于lcd2004部分,请参考《单片机控制2004A液晶屏之模块化编程点击进入

模块:


检测到BMP180的存在:



未检测到BMP180的存在:


测试程序:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <reg52.h>  
  2. #include <string.h>  
  3. #include "lcd2004.h"  
  4. #include "bmp180.h"  
  5.   
  6. UB8 *table0 = "Module     :BMP180" ;  
  7. UB8 *table1 = "Temperature:" ;  
  8. UB8 *table2 = "Pressure   :" ;  
  9. UB8 *table3 = "Altitude   :" ;  
  10.   
  11. void display(BMP180_info temp) ;  
  12.   
  13. void main()  
  14. {   
  15.     BMP180_info temp ;  
  16.       
  17.     lcd2004Init();   
  18.     lcd2004WriteCommand(0x0c) ;/*为了显示效果更佳明显,暂时取消光标显示和闪烁,也可以在LCD2004模块内部修改*/  
  19.   
  20.     lcd2004AddressWriteString(LCD2004_ROW0,0,table0) ;  
  21.     lcd2004AddressWriteString(LCD2004_ROW1,0,table1) ;  
  22.     lcd2004AddressWriteString(LCD2004_ROW2,0,table2) ;  
  23.     lcd2004AddressWriteString(LCD2004_ROW3,0,table3) ;  
  24.     
  25.     BMP180Init(&temp);    
  26.       
  27.     while(1)                          
  28.     {   
  29.         if(temp.ExistFlag == BMP180_EXISTENCE)   //存在  
  30.         {  
  31.             BMP180Convert(&temp);  
  32.             display(temp) ;  
  33.         }  
  34.         else                                    //不存在  
  35.         {  
  36.             lcd2004CleanAll() ;  
  37.             lcd2004AddressWriteString(LCD2004_ROW0,0,"Error") ;  
  38.             while(1);  
  39.         }  
  40.     }  
  41. }   
  42.   
  43. void display(BMP180_info temp)  
  44. {  
  45.     // 温度  
  46.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1),((UL32)temp.Temperature)%1000/100+'0') ;  
  47.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1)+1,((UL32)temp.Temperature)%100/10+'0') ;  
  48.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1)+2,((UL32)temp.Temperature)%10+'0') ;  
  49.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1)+3,'.') ;  
  50.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1)+4,((UL32)(temp.Temperature*10))%10+'0') ;  
  51.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1)+5,0xdf) ;  
  52.     lcd2004AddressWriteByte(LCD2004_ROW1,strlen(table1)+6,'C') ;  
  53.       
  54.     //气压  
  55.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2),temp.GasPress%1000000/100000+'0') ;  
  56.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+1,temp.GasPress%100000/10000+'0') ;  
  57.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+2,temp.GasPress%10000/1000+'0') ;  
  58.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+3,'.') ;  
  59.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+4,temp.GasPress%1000/100+'0') ;  
  60.     //数据没有全部显示出来  
  61.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+5,'K') ;  
  62.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+6,'p') ;  
  63.     lcd2004AddressWriteByte(LCD2004_ROW2,strlen(table2)+7,'a') ;  
  64.       
  65.     //海拔  
  66.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3),(UL32)temp.Altitude%10000/1000+'0') ;  
  67.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3)+1,(UL32)temp.Altitude%1000/100+'0') ;  
  68.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3)+2,(UL32)temp.Altitude%100/10+'0') ;  
  69.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3)+3,(UL32)temp.Altitude%10+'0') ;  
  70.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3)+4,'.') ;  
  71.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3)+5,(UL32)(temp.Altitude*10)%10+'0') ;  
  72.     lcd2004AddressWriteByte(LCD2004_ROW3,strlen(table3)+6,'m') ;  
  73. }  


/*################BMP180.h start################*/

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifndef __BMP180_H__  
  2. #define __BMP180_H__  
  3.   
  4. #include <reg52.h>  
  5. #include "common.h"  
  6.   
  7. sbit BMP180_sclk_bit = P3^6 ;   /*根据硬件连接选择*/  
  8. sbit BMP180_sda_bit  = P3^7 ;   /*根据硬件连接选择*/  
  9.   
  10. //BMP180校正参数(calibration param)  
  11. typedef struct {  
  12.     SW16 AC1 ;  
  13.     SW16 AC2 ;  
  14.     SW16 AC3 ;  
  15.     UW16 AC4 ;  
  16.     UW16 AC5 ;  
  17.     UW16 AC6 ;  
  18.     SW16 B1 ;  
  19.     SW16 B2 ;  
  20.     SW16 MB ;  
  21.     SW16 MC ;  
  22.     SW16 MD ;  
  23. }BMP180_cal_param;  
  24.   
  25. typedef struct {  
  26.     UB8  ExistFlag ;  //存在标志  
  27.   
  28.     BMP180_cal_param  cal_param;//修正系数  
  29.   
  30.     UB8 Version ;               //版本  
  31.   
  32.     SL32 UnsetTemperature ;     //未校正的温度值  
  33.     SL32 UnsetGasPress    ;     //未校正的气压值  
  34.   
  35.     float Temperature ;         /*校正后的温度值*/  
  36.     SL32 GasPress ;             /*校正后的气压值*/  
  37.   
  38.     float Altitude ;                /*海拔*/  
  39.       
  40. }BMP180_info ;  
  41.   
  42. #define BMP180_NOT_EXISTENCE 0  /*不存在*/  
  43. #define BMP180_EXISTENCE     1  /*存在*/  
  44.   
  45. #define OSS  2  //范围(0~3)  
  46.   
  47. #define BMP180_READ     0x01  
  48. #define BMP180_WRITE    (0x01 &(~(0x01<<0)))    
  49.   
  50. #define BMP180_DEVICE_ADDRESS_BASE_VALUE   0xee          /*器件地址基值*/                    
  51. //#define BMP180_CONTROL_REGISTER_ADDRESS_BASE_VALUE    0xf4 /*控制寄存器地址*/  
  52. #define BMP180_ID_REGISTER_ADDRESS      0xd0 /*ID编号寄存器(0x55固定)*/  
  53. #define BMP180_VERSION_REGISTER_ADDRESS 0XD1 /*版本编号*/  
  54. //#define BMP180_SOFT_RESET_REGISTER_BASE_VALUE     0xe0 /*软件复位寄存器,只写,设置0xb6*/  
  55.   
  56.   
  57. //control register  
  58. //#define BMP180_CONTROL_REGISTER_SCO_BIT (0X01<<5)  
  59.   
  60. //id register   
  61. #define BMP180_ID_FIXED_VALUE       0x55 /*id固定编号(0x55)*/  
  62.   
  63.   
  64.   
  65. /*****************内部函数******************/  
  66. //初始化  
  67. extern void BMP180Init(BMP180_info *p);               
  68.   
  69. //转换,修正温度、气压,计算海拔  
  70. extern void BMP180Convert(BMP180_info *temp) ;        
  71.   
  72. /*下面两个函数一般不在外部使用,也可以直接声明为BMP180.c的内部函数*/  
  73. //地址写数据  
  74. extern void BMP180AddressWrite(UB8 addresss,UB8 dataCode) ;  
  75.   
  76. //地址读数据  
  77. extern UB8 BMP180AddressReadByte(UB8 address) ;  
  78. /**********************************************/  
  79.   
  80.   
  81. #endif /*__BMP180_H__*/  



/*################BMP180.h start################*/


/*################BMP180.c start################*/

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*************************************************************************** 
  2. Module  :BMP180.c 
  3.  
  4. Purpose :Implementation of BMP180 module. 
  5.  
  6. Version :0.01                           2014/02/03 12:00(OK) 
  7.  
  8. Complier:Keil 8051 C complier V9.01 
  9.  
  10. MCU     :STC12C5A60S2 
  11.  
  12. Author  :yangrui 
  13.  
  14. QQ      :279729201 
  15.  
  16. Email   :yangrui90s@163.com 
  17.  
  18.  
  19. Modification: 
  20. ================= 
  21.     2015/05/05 09:06 
  22.     Reason: 
  23.         1.在BMP180.h中的结构体BMP180_info中修改 
  24.         SL32 Temperature ;  //校正后的温度值 
  25.         为 
  26.         float Temperature ; //校正后的温度值 
  27.         因为BMP180内部通过校正得到的温度值为的单位是0.1摄氏度,比如校正后得到280,表示28 
  28.         摄氏度 。 之前的程序是在其他地方修改注意一下,但是这样做不是太好。这里完善一下。 
  29.  
  30.         2.将函数BMP180Convert(BMP180_info *temp)里面的: 
  31.         temp->Temperature= ((B5 + 8) >> 4; 
  32.         修改为: 
  33.         temp->Temperature= (((B5 + 8) >> 4)*0.1; 
  34. ================= 
  35.  
  36. ================= 
  37.     2014/04/16 23:06 
  38.     Reason: 
  39.         1.在BMP180.h中的结构体BMP180_info中添加了器件存在标志位。 
  40.         2.在初始化函数BMP180Init(...)中添加了器件存在检测功能。 
  41.  
  42. ================= 
  43.  
  44. ================= 
  45.     2014/04/16 15:39 
  46.     Reason: 
  47.         1.完成 
  48.  
  49. ================= 
  50.  
  51. ***************************************************************************/  
  52. #include <reg52.h>  
  53. #include <intrins.h>   
  54. #include <math.h>  
  55. #include "bmp180.h"  
  56.   
  57.   
  58. /*外部接口函数在BMP180.h中声明*/  
  59.   
  60. /*****************内部函数******************/  
  61. static void delay5msForBMP180(void) ;  
  62. static void BMP180StartSignal(void) ;  
  63. static void BMP180StopSignal(void) ;  
  64. static void BMP180Acknowledge(void) ;  
  65. static void BMP180WriteByte(UB8 dataCode) ;  
  66. static UB8  BMP180ReadByte(void) ;  
  67. static SL32 BMP180AddressRead2Byte(UB8 address) ;  
  68. static SL32 BMP180ReadUnsetTemperature(void) ;  
  69. static SL32 BMP180ReadUnsetPressure(void) ;  
  70. static void BMP180ReadCalibrateParam(BMP180_info *p) ;  
  71. /**********************************************/  
  72.   
  73.   
  74.   
  75. /****************************************************** 
  76. Function    :delay5msForBMP180 
  77. Input       :N/A 
  78. Output      :N/A 
  79. Return      :N/A 
  80. Description :N/A 
  81. Note        :由STC-ISP V6.67软件针对相应MCU生成,若MCU不同 
  82.             最好也要修改此函数。 
  83. ******************************************************/  
  84. static void delay5msForBMP180(void//@11.0592MHZ  
  85. {  
  86.     unsigned char i, j;  
  87.   
  88.     _nop_();  
  89.     _nop_();  
  90.     i = 54;  
  91.     j = 198;  
  92.     do  
  93.     {  
  94.         while (--j);  
  95.     } while (--i);  
  96.   
  97. }  
  98.   
  99. /****************************************************** 
  100. Function    :BMP180StartSignal 
  101. Input       :N/A 
  102. Output      :N/A 
  103. Return      :N/A 
  104. Description :BMP180 start signal 
  105. Note        :N/A 
  106. ******************************************************/  
  107. static void BMP180StartSignal(void)  
  108. {  
  109.     BMP180_sda_bit = HIGH_LEVEL ;  
  110.     //_nop_() ;  
  111.     BMP180_sclk_bit = HIGH_LEVEL ;  
  112.     //_nop_() ;  
  113.     BMP180_sda_bit = LOW_LEVEL ;  
  114.     //_nop_();      
  115. }  
  116.   
  117. /****************************************************** 
  118. Function    :BMP180StopSignal 
  119. Input       :N/A 
  120. Output      :N/A 
  121. Return      :N/A 
  122. Description :BMP180  stop signal 
  123. Note        :N/A 
  124. ******************************************************/  
  125. static void BMP180StopSignal(void)  
  126. {  
  127.     BMP180_sda_bit = LOW_LEVEL ;  
  128.     //_nop_() ;  
  129.     BMP180_sclk_bit = HIGH_LEVEL ;  
  130.     //_nop_() ;  
  131.     BMP180_sda_bit = HIGH_LEVEL ;  
  132.   
  133.     /*BMP180 self timed write cycle (5ms max)*/  
  134.     delay5msForBMP180();      
  135. }  
  136.   
  137. /****************************************************** 
  138. Function    :BMP180Acknowledge 
  139. Input       :N/A 
  140. Output      :N/A 
  141. Return      :N/A 
  142. Description :When BMP180 receive a data from mcu , BMP180 write the data 
  143.             to internal address, after completed write ,BMP180 send a zero 
  144.             to mcu,then mcu can input data into BMP180. 
  145.             (Once the internally timed write cycle has started and  
  146.             the EEPROM inputs are disabled until write finished.) 
  147. Note        :N/A 
  148. ******************************************************/  
  149. static void BMP180Acknowledge(void)  
  150. {  
  151.     UB8 i=0 ;  
  152.   
  153.     BMP180_sclk_bit = LOW_LEVEL ;  
  154.     //_nop_() ;  
  155.   
  156.     BMP180_sclk_bit = HIGH_LEVEL ;  
  157.     //_nop_() ;  
  158.   
  159.       
  160.     while((BMP180_sda_bit) && (i<250))  
  161.     {  
  162.         i++;//暂时,具体见调试  
  163.         //经过测试,这里的250足够大了  
  164.     }  
  165.     BMP180_sclk_bit = LOW_LEVEL ;  
  166. }  
  167.   
  168.   
  169. /****************************************************** 
  170. Function    :BMP180WriteByte 
  171. Input       :the data which is ready to write to BMP180 
  172. Output      :N/A 
  173. Return      :N/A 
  174. Description :N/A 
  175. Note        :N/A 
  176. ******************************************************/  
  177. static void BMP180WriteByte(UB8 dataCode)  
  178. {  
  179.     UB8 i ;  
  180.     UB8 temp = dataCode ;  
  181.   
  182.     for(i=0 ; i<8 ; i++)  
  183.     {  
  184.         BMP180_sclk_bit = LOW_LEVEL ;  
  185.         //_nop_();  
  186.   
  187.         //方法一  
  188.         BMP180_sda_bit = (bit)(temp & (0x80>>i)) ;  
  189.   
  190.         //方法二  
  191.         //temp <<= 1 ;  
  192.         //BMP180_sda_bit = CY ;  
  193.   
  194.         //_nop_();  
  195.         BMP180_sclk_bit = HIGH_LEVEL ;  
  196.         //_nop_();  
  197.     }  
  198. }  
  199.   
  200. /****************************************************** 
  201. Function    :BMP180AddressWrite 
  202. Input       :address,data 
  203. Output      :N/A 
  204. Return      :N/A 
  205. Description :write 'dataCode' to 'address' 
  206. Note        :N/A 
  207. ******************************************************/  
  208. void BMP180AddressWrite(UB8 addresss,UB8 dataCode)  
  209. {  
  210.     BMP180StartSignal();      
  211.       
  212.     BMP180WriteByte(BMP180_DEVICE_ADDRESS_BASE_VALUE | BMP180_WRITE);     
  213.     BMP180Acknowledge() ;  
  214.       
  215.     BMP180WriteByte(addresss);     
  216.     BMP180Acknowledge() ;  
  217.       
  218.     BMP180WriteByte(dataCode);         
  219.     BMP180Acknowledge() ;  
  220.       
  221.     BMP180StopSignal();                     
  222. }  
  223.   
  224. /****************************************************** 
  225. Function    :BMP180ReadByte 
  226. Input       :N/A 
  227. Output      :N/A 
  228. Return      :the byte-data which read from BMP180 
  229. Description :N/A 
  230. Note        :N/A 
  231. ******************************************************/  
  232. static UB8 BMP180ReadByte(void)  
  233. {  
  234.     UB8 i ;  
  235.     UB8 dataCode = 0x00 ;  
  236.   
  237.     //Ready  
  238.     /*Data on sda pin may change during scl low timer period*/  
  239.     BMP180_sclk_bit = LOW_LEVEL ;  
  240.     //_nop_() ;  
  241.     BMP180_sda_bit = HIGH_LEVEL ;//ready to read  
  242.     //_nop_() ;  
  243.   
  244.     for(i=0; i<8 ; i++)  
  245.     {  
  246.         BMP180_sclk_bit = HIGH_LEVEL ;  
  247.         //_nop_() ;  
  248.         dataCode<<= 1;  
  249.         dataCode |= BMP180_sda_bit ;  
  250.         //_nop_() ;  
  251.         BMP180_sclk_bit = LOW_LEVEL ;  
  252.         //_nop_() ;  
  253.     }  
  254.   
  255.     return dataCode ;  
  256. }  
  257.   
  258.   
  259. /****************************************************** 
  260. Function    :BMP180AddressReadByte 
  261. Input       :address 
  262. Output      :N/A 
  263. Return      :data which read from bmp180's address 
  264. Description :read byte-data from bmp180's address 
  265. Note        :不需要应答. 
  266. ******************************************************/  
  267. UB8 BMP180AddressReadByte(UB8 address)  
  268. {    
  269.     UB8 dataCode;  
  270.       
  271.     BMP180StartSignal();                            
  272.       
  273.     BMP180WriteByte(BMP180_DEVICE_ADDRESS_BASE_VALUE | BMP180_WRITE);     
  274.     BMP180Acknowledge() ;  
  275.       
  276.     BMP180WriteByte(address);             
  277.     BMP180Acknowledge() ;  
  278.       
  279.     BMP180StartSignal();                            
  280.       
  281.     BMP180WriteByte(BMP180_DEVICE_ADDRESS_BASE_VALUE | BMP180_READ);    
  282.     BMP180Acknowledge() ;  
  283.       
  284.     dataCode=BMP180ReadByte();               
  285.         
  286.     BMP180StopSignal();    
  287.       
  288.     return dataCode;   
  289. }  
  290.   
  291. /****************************************************** 
  292. Function    :BMP180AddressRead2Byte 
  293. Input       :address 
  294. Output      :N/A 
  295. Return      :long data 
  296. Description :从连续地址读取数据,并"组装"为long型数据 
  297. Note        :N/A 
  298. ******************************************************/  
  299. static SL32 BMP180AddressRead2Byte(UB8 address)  
  300. {  
  301.     UB8 msb , lsb ;  
  302.   
  303.     msb = BMP180AddressReadByte(address)   ;  
  304.     lsb = BMP180AddressReadByte(address+1) ;  
  305.   
  306.     return ( ((SL32)msb) << 8 | lsb) ;  
  307. }  
  308.   
  309. /****************************************************** 
  310. Function    :BMP180ReadUnsetTemperature 
  311. Input       :address 
  312. Output      :N/A 
  313. Return      :shour int byte-data 
  314. Description :读取未校正的温度值 
  315. Note        :接收后面的一字节数据,主机不需要应答 
  316. ******************************************************/  
  317. static SL32 BMP180ReadUnsetTemperature(void)  
  318. {  
  319.     BMP180AddressWrite(0xf4,0x2e) ;  
  320.       
  321.     return (BMP180AddressRead2Byte(0xf6));  
  322. }  
  323.   
  324. /****************************************************** 
  325. Function    :BMP180ReadUnsetPressure 
  326. Input       :N/A 
  327. Output      :N/A 
  328. Return      :未校正气压值 
  329. Description :读取未校正的气压值 
  330. Note        :N/A 
  331. ******************************************************/  
  332. static SL32 BMP180ReadUnsetPressure(void)  
  333. {  
  334.       
  335.     SL32 pressure = 0;  
  336.   
  337.     BMP180AddressWrite(0xf4,0x34 + (OSS<<6)) ;  
  338.   
  339.     delay5msForBMP180();  
  340.     delay5msForBMP180();  
  341.       
  342.   
  343.     pressure = BMP180AddressRead2Byte(0xf6) ;  
  344.     pressure = (((SL32)pressure <<8) + BMP180AddressReadByte(0xf8)) >>(8-OSS) ;  
  345.       
  346.     return pressure;      
  347.   
  348. }  
  349.   
  350. /****************************************************** 
  351. Function    :BMP180ReadCalibrateParam 
  352. Input       :BMP180_info type point 
  353. Output      :AC1,AC3,AC3,AC4,AC5,AC6,B1,B2,MB,MC,MD 
  354. Return      :N/A 
  355. Description :读取校正参数 
  356. Note        :N/A 
  357. ******************************************************/  
  358. static void BMP180ReadCalibrateParam(BMP180_info *p)  
  359. {  
  360.     p->cal_param.AC1= BMP180AddressRead2Byte(0xAA);  
  361.     p->cal_param.AC2= BMP180AddressRead2Byte(0xAC);  
  362.     p->cal_param.AC3= BMP180AddressRead2Byte(0xAE);  
  363.     p->cal_param.AC4= BMP180AddressRead2Byte(0xB0);  
  364.     p->cal_param.AC5= BMP180AddressRead2Byte(0xB2);  
  365.     p->cal_param.AC6= BMP180AddressRead2Byte(0xB4);  
  366.     p->cal_param.B1=  BMP180AddressRead2Byte(0xB6);  
  367.     p->cal_param.B2=  BMP180AddressRead2Byte(0xB8);  
  368.     p->cal_param.MB=  BMP180AddressRead2Byte(0xBA);  
  369.     p->cal_param.MC=  BMP180AddressRead2Byte(0xBC);  
  370.     p->cal_param.MD=  BMP180AddressRead2Byte(0xBE);  
  371. }  
  372.   
  373.   
  374. /****************************************************** 
  375. Function    :Init_BMP180 
  376. Input       :BMP180_info type point 
  377. Output      :p->ExistFlag  存在标志位 
  378.              p->Version    版本号 
  379. Return      :N/A 
  380. Description :初始化 
  381. Note        :N/A 
  382. ******************************************************/  
  383. void BMP180Init(BMP180_info *p)  
  384. {  
  385.     if(BMP180AddressReadByte(BMP180_ID_REGISTER_ADDRESS)== BMP180_ID_FIXED_VALUE)  
  386.     {//存在  
  387.         p->ExistFlag = BMP180_EXISTENCE ;  
  388.           
  389.         BMP180ReadCalibrateParam(p);  
  390.   
  391.         p->Version = BMP180AddressReadByte(BMP180_VERSION_REGISTER_ADDRESS);  
  392.     }  
  393.     else  
  394.     {//不存在  
  395.         p->ExistFlag = BMP180_NOT_EXISTENCE ;  
  396.     }  
  397. }  
  398.   
  399. /****************************************************** 
  400. Function    :BMP180Convert 
  401. Input       :BMP180_info type point 
  402. Output      :temp->UnsetTemperature 未经过校正的温度值 
  403.              temp->UnsetGasPress    未经过校正的气压值 
  404.              temp->Temperature      校正后的温度值 
  405.              temp->GasPress         校正后的气压值 
  406.              temp->Altitude         海拔计算值 
  407. Return      :N/A 
  408. Description :温度值、气压值的校正和海拔的计算 
  409. Note        :N/A 
  410. ******************************************************/  
  411. void BMP180Convert(BMP180_info *temp)  
  412. {     
  413.     SL32 x1, x2, B5, B6, x3, B3, p;  
  414.     unsigned long b4, b7;  
  415.   
  416.     //未校正的温度值  
  417.     temp->UnsetTemperature = BMP180ReadUnsetTemperature();  
  418.     //未校正的气压值  
  419.     temp->UnsetGasPress = BMP180ReadUnsetPressure();  
  420.   
  421.     //温度校正  
  422.     x1 = ((temp->UnsetTemperature) - temp->cal_param.AC6) * (temp->cal_param.AC5) >> 15;  
  423.     x2 = ((SL32)(temp->cal_param.MC) << 11) / (x1 + temp->cal_param.MD);  
  424.     B5 = x1 + x2;  
  425.     temp->Temperature= ((B5 + 8) >> 4)*0.1;  
  426.   
  427.     //气压校正  
  428.     B6 = B5- 4000;  
  429.     x1 = ((SL32)(temp->cal_param.B2) * (B6 * B6 >> 12)) >> 11;  
  430.     x2 = ((SL32)temp->cal_param.AC2) * B6 >> 11;  
  431.     x3 = x1 + x2;  
  432.     B3 = ((((SL32)(temp->cal_param.AC1) * 4 + x3)<<OSS) + 2)/4;  
  433.     x1 = ((SL32)temp->cal_param.AC3) * B6 >> 13;  
  434.     x2 = ((SL32)(temp->cal_param.B1) * (B6 * B6 >> 12)) >> 16;  
  435.     x3 = ((x1 + x2) + 2) >> 2;  
  436.     b4 = ((SL32)(temp->cal_param.AC4) * (unsigned long) (x3 + 32768)) >> 15;  
  437.     b7 = ((unsigned long)(temp->UnsetGasPress) - B3) * (50000 >> OSS);  
  438.     if( b7 < 0x80000000)  
  439.     {  
  440.          p = (b7 * 2) / b4 ;  
  441.     }  
  442.     else  
  443.     {  
  444.          p = (b7 / b4) * 2;  
  445.     }  
  446.     x1 = (p >> 8) * (p >> 8);  
  447.     x1 = ((SL32)x1 * 3038) >> 16;  
  448.     x2 = (-7357 * p) >> 16;  
  449.     temp->GasPress= p + ((x1 + x2 + 3791) >> 4);  
  450.   
  451.     //海拔计算  
  452.     temp->Altitude =(44330.0 * (1.0-pow((float)(temp->GasPress) / 101325.0, 1.0/5.255)) );  
  453. }  




/*################BMP180.c start################*/


补充:common.h

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifndef __COMMON_H__  
  2. #define __COMMON_H__  
  3.   
  4. typedef unsigned char UB8 ;  
  5. typedef unsigned short int UW16 ;  
  6. typedef unsigned long UL32 ;  
  7.   
  8. typedef char SB8;  
  9. typedef short int SW16 ;  
  10. typedef long SL32 ;  
  11.       
  12. #define HIGH_LEVEL 1      
  13. #define LOW_LEVEL  0  
  14.   
  15.   
  16. #endif  /*__COMMON_H__*/  

转自:http://blog.csdn.net/yagnruinihao/article/details/23914675



0 1
原创粉丝点击