找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 13186|回復: 25
收起左側(cè)

51單片機BMP280氣壓和溫度值測試程序

  [復制鏈接]
ID:68356 發(fā)表于 2018-5-30 04:00 | 顯示全部樓層 |閱讀模式
51單片機bmp280測試程序,實機測試通過,bmp280+lcd1602顯示氣壓和溫度值。壓縮包附件中包含測試程序和詳細的資料。
測試程序使用的單片機型號是STC90C516RD+,1280字節(jié)RAM,64K ROM,12Mhz晶振
0.png

單片機源程序如下:
  1. #include"bmp280.h"
  2. unsigned short xdata dig_t1,dig_p1;                                                                                                                         //注意數(shù)據(jù)類型
  3. signed short xdata dig_t2,dig_t3,dig_p2,dig_p3,dig_p4,dig_p5,dig_p6,dig_p7,dig_p8,dig_p9;
  4. long signed int xdata bmp280_ut,bmp280_up;
  5. long signed int xdata t_fine;
  6. void bmpreaddata()           //原始壓力溫度數(shù)據(jù)突發(fā)讀取
  7. {
  8.    uchar xdata n,temp=0x09,address=0xf7;
  9.    long unsigned int xdata readdata0[6];                //注意數(shù)據(jù)類型
  10.    i2cstart();
  11.    i2csendbyte(0xec,1);
  12.    i2csendbyte(0xf4,1);
  13.    i2csendbyte(0x55,1);
  14.    i2cstop();
  15.    delay10ms(10);
  16.    while(temp!=0)
  17.    {
  18.       temp=0x09;
  19.       i2cstart();
  20.           i2csendbyte(0xec,1);
  21.           i2csendbyte(0xf3,1);
  22.           i2cstop();
  23.           i2cstart();
  24.           i2csendbyte(0xed,1);
  25.           temp&=i2creadbyte(1);
  26.           i2cstop();
  27.    }
  28.    i2cstart();
  29.    i2csendbyte(0xec,1);
  30.    i2csendbyte(address,1);
  31.    i2cstop();
  32.    i2cstart();
  33.    i2csendbyte(0xed,1);
  34.    for(n=0;n<6;n++)
  35.    {     
  36.       if(n<5)
  37.           {
  38.              readdata0[n]=i2creadbyte(0);
  39.           }
  40.           else
  41.           {
  42.              readdata0[n]=i2creadbyte(1);
  43.           }
  44.    }
  45.    i2cstop();
  46.    bmp280_up=(readdata0[0]<<12)|(readdata0[1]<<4)|(readdata0[2]>>4);
  47.    bmp280_ut=(readdata0[3]<<12)|(readdata0[4]<<4)|(readdata0[5]>>4);        
  48. }
  49. void bmpreset()
  50. {
  51.    i2cstart();
  52.    i2csendbyte(0xec,1);
  53.    i2csendbyte(0xe0,1);
  54.    i2csendbyte(0xb6,1);                   //軟復位
  55.    i2cstop();
  56. }
  57. void bmpconfig()
  58. {
  59.    i2cstart();
  60.    i2csendbyte(0xec,1);
  61.    i2csendbyte(0xf5,1);
  62.    i2csendbyte(0x00,1);                  //強制模式無待機時間,關濾波,禁用3線模式
  63.    i2cstop();
  64.    i2cstart();
  65.    i2csendbyte(0xec,1);
  66.    i2csendbyte(0xf4,1);
  67.    i2csendbyte(0x55,1);                  //壓力16倍,溫度2倍過采樣,轉(zhuǎn)換時間最大43MS,強制模式
  68.    i2cstop();
  69. }
  70. void readtrimming()                      //補償參數(shù)單字節(jié)讀取
  71. {
  72.    uchar xdata n,address=0x88;
  73.    uchar xdata readdata[24];
  74.    for(n=0;n<24;n++)
  75.    {
  76.           i2cstart();
  77.       i2csendbyte(0xec,1);
  78.       i2csendbyte(address,1);
  79.       i2cstop();
  80.       i2cstart();
  81.       i2csendbyte(0xed,1);
  82.           if(n<23)
  83.           {
  84.              readdata[n]=i2creadbyte(0);
  85.           }
  86.           else
  87.           {
  88.              readdata[n]=i2creadbyte(1);
  89.           }
  90.           i2cstop();
  91.           address++;
  92.    }
  93.    dig_t1=(readdata[1]<<8)|(readdata[0]);
  94.    dig_t2=(readdata[3]<<8)|(readdata[2]);
  95.    dig_t3=(readdata[5]<<8)|(readdata[4]);
  96.    dig_p1=(readdata[7]<<8)|(readdata[6]);
  97.    dig_p2=(readdata[9]<<8)|(readdata[8]);
  98.    dig_p3=(readdata[11]<<8)|(readdata[10]);
  99.    dig_p4=(readdata[13]<<8)|(readdata[12]);
  100.    dig_p5=(readdata[15]<<8)|(readdata[14]);
  101.    dig_p6=(readdata[17]<<8)|(readdata[16]);
  102.    dig_p7=(readdata[19]<<8)|(readdata[18]);
  103.    dig_p8=(readdata[21]<<8)|(readdata[20]);
  104.    dig_p9=(readdata[23]<<8)|(readdata[22]);
  105. }
  106. uchar bmpid()
  107. {
  108.    uchar xdata id;
  109.    i2cstart();
  110.    i2csendbyte(0xec,1);
  111.    i2csendbyte(0xd0,1);
  112.    i2cstop();
  113.    i2cstart();
  114.    i2csendbyte(0xed,1);
  115.    id=i2creadbyte(1);
  116.    i2cstop();
  117.    return id;
  118. }
  119. long signed int tempcal(long signed int adc_t)                  //c51單片機不支持64位整數(shù),使用簡化計算
  120. {
  121.    long signed int var1,var2,t;
  122.    var1=((((adc_t>>3)-((long signed int)dig_t1<<1)))*((long signed int)dig_t2))>>11;
  123.    var2=(((((adc_t>>4)-((long signed int)dig_t1))*((adc_t>>4)-((long signed int)dig_t1)))>>12)*((long signed int)dig_t3))>>14;
  124.    t_fine=var1+var2;
  125.    t=(t_fine*5+128)>>8;
  126.    return t;
  127. }
  128. long unsigned int presscal(long signed int adc_p)
  129. {
  130.    long signed int var1,var2;
  131.    long unsigned int p;
  132.    var1=(((long signed int)t_fine)>>1)-(long signed int)64000;
  133.    var2=(((var1>>2)*(var1>>2))>>11)*((long signed int)dig_p6);
  134.    var2=var2+((var1*((long signed int)dig_p5))<<1);
  135.    var2=(var2>>2)+(((long signed int)dig_p4)<<16);
  136.    var1=(((dig_p3*(((var1>>2)*(var1>>2))>>13))>>3)+((((long signed int)dig_p2)*var1)>>1))>>18;
  137.    var1=((((32768+var1))*((long signed int)dig_p1))>>15);
  138.    if(var1==0)
  139.    {
  140.       return 0;
  141.    }
  142.    p=(((long unsigned int)(((long signed int)1048576)-adc_p)-(var2>>12)))*3125;
  143.    if(p<0x80000000)
  144. ……………………

  145. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
bmp280.zip (5.18 MB, 下載次數(shù): 618)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

ID:88606 發(fā)表于 2018-5-30 16:30 | 顯示全部樓層
好資料,學習下!
回復

使用道具 舉報

ID:294397 發(fā)表于 2018-5-31 15:21 | 顯示全部樓層
感謝樓主
回復

使用道具 舉報

ID:312164 發(fā)表于 2018-6-15 21:48 | 顯示全部樓層
樓主,我們調(diào)試了代碼調(diào)試不通
回復

使用道具 舉報

ID:316641 發(fā)表于 2019-3-17 14:51 | 顯示全部樓層
感謝分享,先看下東西能不能用
回復

使用道具 舉報

ID:316641 發(fā)表于 2019-3-17 16:09 | 顯示全部樓層
我是來看看的
回復

使用道具 舉報

ID:343102 發(fā)表于 2019-8-11 00:43 | 顯示全部樓層
學習學習。
回復

使用道具 舉報

ID:64016 發(fā)表于 2019-9-8 10:48 | 顯示全部樓層
很完整的資料,謝謝!
回復

使用道具 舉報

ID:608164 發(fā)表于 2019-9-8 13:08 | 顯示全部樓層
很完整的資料,得到了不一樣的學習
回復

使用道具 舉報

ID:604530 發(fā)表于 2019-9-21 14:46 | 顯示全部樓層
感謝分享
回復

使用道具 舉報

ID:263517 發(fā)表于 2019-9-24 18:15 | 顯示全部樓層
多謝分享正需要
回復

使用道具 舉報

ID:718605 發(fā)表于 2020-4-3 13:10 | 顯示全部樓層
請問csb管腳應該接在哪里呢
回復

使用道具 舉報

ID:214276 發(fā)表于 2020-4-15 01:38 | 顯示全部樓層
我只調(diào)試過BMP180,280的還不會用,有空下載試試。
回復

使用道具 舉報

ID:214276 發(fā)表于 2020-4-15 21:49 | 顯示全部樓層
按上面程序調(diào)試顯示了,可是溫度值和壓力值明顯偏大怎么辦呢,那出問題了嗎
回復

使用道具 舉報

ID:730488 發(fā)表于 2020-4-16 13:14 | 顯示全部樓層
感謝分享, 學習~~
回復

使用道具 舉報

ID:690831 發(fā)表于 2020-6-3 02:19 | 顯示全部樓層
正在調(diào)試BMP_280,學習學習
回復

使用道具 舉報

ID:72088 發(fā)表于 2020-10-30 11:57 | 顯示全部樓層
iws2000 發(fā)表于 2020-4-3 13:10
請問csb管腳應該接在哪里呢

I2C模式就接高電平,SPI模式就下拉到地
回復

使用道具 舉報

ID:72088 發(fā)表于 2020-10-31 11:15 | 顯示全部樓層
我的程序讀出的ID識別碼都是18呢?并不是資料說的58、?????
回復

使用道具 舉報

ID:619615 發(fā)表于 2020-11-11 11:15 | 顯示全部樓層
數(shù)據(jù)偏大啊,博主是哪里的問題呀
回復

使用道具 舉報

ID:313760 發(fā)表于 2021-3-1 22:11 | 顯示全部樓層
溫度和大氣壓數(shù)據(jù)都偏大,是哪里有問題嗎
回復

使用道具 舉報

ID:919104 發(fā)表于 2021-5-11 09:03 | 顯示全部樓層
我太難了,下載了兩遍這個用了十積分。結果被公司的防火墻給攔截了。。。。。虧了呀,,,回家再試試吧
回復

使用道具 舉報

ID:919104 發(fā)表于 2021-5-13 15:45 | 顯示全部樓層
超感謝的,調(diào)了一個月,終于通了。超級工整的程序。數(shù)值問題應該很好解決
回復

使用道具 舉報

ID:82944 發(fā)表于 2021-12-20 20:33 | 顯示全部樓層
很完整的資料,得到了不一樣的學習
回復

使用道具 舉報

ID:67853 發(fā)表于 2022-4-14 08:38 | 顯示全部樓層
路過的,感覺不錯,謝謝了
回復

使用道具 舉報

ID:1021034 發(fā)表于 2022-4-23 19:55 | 顯示全部樓層
89C52的板子通用嘛
回復

使用道具 舉報

ID:400333 發(fā)表于 2023-3-17 18:13 | 顯示全部樓層
為啥我用串口讀出來的溫度高了,室溫28.5,BMP280測出來的35.4
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表