那曲檬骨新材料有限公司

電子發燒友App

硬聲App

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示
電子發燒友網>電子資料下載>電子資料>使用氣壓傳感器讀取溫度、壓力和高度的項目

使用氣壓傳感器讀取溫度、壓力和高度的項目

2022-12-23 | zip | 0.38 MB | 次下載 | 免費

資料介紹

描述

在這個項目中,我們將使用適用于 Windows 10 IoT Core on Raspberry Pi 2套件組件的 Adafruit 入門包來創建一個使用氣壓傳感器讀取溫度、壓力和高度的項目。?

注意:該套件有兩個版本,一個包含 BMP280 傳感器,另一個包含 BME280。如果你有 BME280,

硬件

根據下面“原理圖”部分中的 Fritzing 圖,將 Raspberry Pi2 連接到面包板和其他組件。

軟件?

您可以從https://github.com/ms-iot/adafruitsample下載代碼啟動項目,我們將引導您完成添加與 Web 服務對話并在地圖上獲取您的 pin 所需的代碼。什么地圖?

打開“Lesson_203\StartSolution\lesson_203.sln 并打開 mainpage.xaml.cs 文件。

我們已經填寫了一些方法作為您在此解決方案中的起點。如果你想跳到前面,你可以在以下位置找到所有代碼已完成的解決方案:“Lesson_203\FullSolution\lesson_203.sln”

MainPage.xaml.cs

打開 MainPage.xaml.cs 文件。

添加對氣壓傳感器 (BMP280) 類的引用。

 public sealed partial class MainPage : Page
    {
        //A class which wraps the barometric sensor
        BMP280 BMP280;

現在我們在 OnNavigatedTo 方法中添加代碼,該方法將為氣壓傳感器創建一個新的 BMP280 對象并初始化該對象。

如果您不想在地圖上添加圖釘,請刪除?MakePinWebAPICall();

 //This method will be called by the application framework when the page is first loaded
        protected override async void OnNavigatedTo(NavigationEventArgs navArgs)
        {            
Debug.WriteLine("MainPage::OnNavigatedTo");
            MakePinWebAPICall();
            try
            {
                //Create a new object for our barometric sensor class                BMP280 = new BMP280();
                //Initialize the sensor
                await BMP280.Initialize();

接下來我們添加代碼來執行以下操作:

  • 創建變量來存儲溫度、壓力和高度。將它們設置為 0。
  • 為海平面壓力創建一個變量。默認值為 1013.25 hPa。
  • 讀取溫度、壓力和高度 10 次并將值輸出到調試控制臺。
 //Create variables to store the sensor data: temperature, pressure and altitude.                 
                //Initialize them to 0.
                float temp = 0;
                float pressure = 0;
                float altitude = 0;
                //Create a constant for pressure at sea level.
                //This is based on your local sea level pressure (Unit: Hectopascal)
                const float seaLevelPressure = 1013.25f;
                //Read 10 samples of the data
                for(int i = 0; i < 10; i++)
                {
                    temp = await BMP280.ReadTemperature();
                    pressure = await BMP280.ReadPreasure();
                    altitude = await BMP280.ReadAltitude(seaLevelPressure);
                    //Write the values to your debug console
                    Debug.WriteLine("Temperature: " + temp.ToString() + " deg C");
                    Debug.WriteLine("Pressure: " + pressure.ToString() + " Pa");
                    Debug.WriteLine("Altitude: " + altitude.ToString() + " m");
                }
                          }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

BMP280.cs

打開 BMP280.cs 文件。??

代碼的第一部分是列出 BMP280 中不同寄存器的地址。這些值可以在BMP280 數據表中找到

在 BMP280 類中,在寄存器地址枚舉之后添加以下內容。

 //String for the friendly name of the I2C bus
         const string I2CControllerName = "I2C1";
        //Create an I2C device
        private I2cDevice bmp280 = null;
        //Create new calibration data for the sensor
        BMP280_CalibrationData CalibrationData;
        //Variable to check if device is initialized
        bool init = false;

接下來在Initialize函數中添加如下代碼:

 //Method to initialize the BMP280 sensor
        public async Task Initialize()
        {
            Debug.WriteLine("BMP280::Initialize");
            try
            {
                //Instantiate the I2CConnectionSettings using the device address of the BMP280
                I2cConnectionSettings settings = new I2cConnectionSettings(BMP280_Address);
                //Set the I2C bus speed of connection to fast mode                settings.BusSpeed = I2cBusSpeed.FastMode;
                //Use the I2CBus device selector to create an advanced query syntax string
                string aqs = I2cDevice.GetDeviceSelector(I2CControllerName);                //Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string
                DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);
                //Instantiate the the BMP280 I2C device using the device id of the I2CBus and the I2CConnectionSettings
                bmp280 = await I2cDevice.FromIdAsync(dis[0].Id, settings);
                //Check if device was found
                if (bmp280 == null)
                {
                    Debug.WriteLine("Device not found");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace);
                throw;
            }
        }

在Begin函數中添加如下代碼:??

 private async Task Begin()
        {
            Debug.WriteLine("BMP280::Begin");
            byte[] WriteBuffer = new byte[] { (byte)eRegisters.BMP280_REGISTER_CHIPID };
            byte[] ReadBuffer = new byte[] { 0xFF };
            //Read the device signature
            bmp280.WriteRead(WriteBuffer, ReadBuffer);
            Debug.WriteLine("BMP280 Signature: " + ReadBuffer[0].ToString());            //Verify the device signature
            if (ReadBuffer[0] != BMP280_Signature)
            {
                Debug.WriteLine("BMP280::Begin Signature Mismatch.");
                return;
            }
            //Set the initalize variable to true
            init = true;
            //Read the coefficients table
            CalibrationData = await ReadCoefficeints();
            //Write control register
            await WriteControlRegister();
            //Write humidity control register
            await WriteControlRegisterHumidity();
        }

將以下代碼添加到接下來的 2 個函數中以寫入控制寄存器。

 //Method to write 0x03 to the humidity control register
        private async Task WriteControlRegisterHumidity()
        {
            byte[] WriteBuffer = new byte[] { (byte)eRegisters.BMP280_REGISTER_CONTROLHUMID, 0x03 };
            bmp280.Write(WriteBuffer);
            await Task.Delay(1);
            return;
        }
        //Method to write 0x3F to the control register
        private async Task WriteControlRegister()
        {
            byte[] WriteBuffer = new byte[] { (byte)eRegisters.BMP280_REGISTER_CONTROL, 0x3F };
            bmp280.Write(WriteBuffer);
            await Task.Delay(1);
            return;
        }

將以下代碼添加到 ReadUInt16_LittleEndian 函數中即可:

 //Method to read a 16-bit value from a register and return it in little endian format
        private UInt16 ReadUInt16_LittleEndian(byte register)
        {
            UInt16 value = 0;
            byte[] writeBuffer = new byte[] { 0x00 };
            byte[] readBuffer = new byte[] { 0x00, 0x00 };
            writeBuffer[0] = register;
            bmp280.WriteRead(writeBuffer, readBuffer);
            int h = readBuffer[1] << 8;
            int l = readBuffer[0];
            value = (UInt16)(h + l);
            return value;
        }

將以下代碼添加到 ReadByte 函數以從寄存器中讀取 8 位數據。

 //Method to read an 8-bit value from a register
        private byte ReadByte(byte register)
        {
            byte value = 0;
            byte[] writeBuffer = new byte[] { 0x00 };
            byte[] readBuffer = new byte[] { 0x00 };
            writeBuffer[0] = register;
            bmp280.WriteRead(writeBuffer, readBuffer);
            value = readBuffer[0];
            return value;
        }

接下來的 3 個功能已為您完成。可以在數據表中找到編寫這些函數所需的信息

  • ReadCoefficeints:這是從寄存器地址讀取所有校準數據的函數。
  • BMP280_compensate_T_double:在此函數中,使用 BMP280 數據表中的補償公式計算以 oC 為單位的溫度。
  • BMP280_compensate_P_Int64:在此函數中,使用 BMP280 數據表中的補償公式計算以 Pa 為單位的壓力。

添加如下代碼完成ReadTemperature功能。

 public async Task<float> ReadTemperature()
        {
            //Make sure the I2C device is initialized
            if (!init) await Begin();
            //Read the MSB, LSB and bits 7:4 (XLSB) of the temperature from the BMP280 registers
            byte tmsb = ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_MSB);
            byte tlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_LSB);
            byte txlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_XLSB); // bits 7:4
            //Combine the values into a 32-bit integer
            Int32 t = (tmsb << 12) + (tlsb << 4) + (txlsb >> 4);
            //Convert the raw value to the temperature in degC
            double temp = BMP280_compensate_T_double(t);
            //Return the temperature as a float value
            return (float)temp;
        }

重復相同的步驟以完成 ReadPressure 函數。?

 public async Task<float> ReadPreasure()
        {
            //Make sure the I2C device is initialized
            if (!init) await Begin();
            //Read the temperature first to load the t_fine value for compensation
            if (t_fine == Int32.MinValue)
            {
                               await ReadTemperature();
            }
            //Read the MSB, LSB and bits 7:4 (XLSB) of the pressure from the BMP280 registers
            byte tmsb = ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_MSB);
            byte tlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_LSB);
            byte txlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_XLSB); // bits 7:4
            //Combine the values into a 32-bit integer
            Int32 t = (tmsb << 12) + (tlsb << 4) + (txlsb >> 4);
            //Convert the raw value to the pressure in Pa
            Int64 pres = BMP280_compensate_P_Int64(t);
            //Return the temperature as a float value
            return ((float)pres) / 256;
        }

最后完成ReadAltitude函數:

 //Method to take the sea level pressure in Hectopascals(hPa) as a parameter and calculate the altitude using current pressure.
        public async Task<float> ReadAltitude(float seaLevel)
        {
            //Make sure the I2C device is initialized
            if (!init) await Begin();
            //Read the pressure first
            float pressure = await ReadPreasure();
            //Convert the pressure to Hectopascals(hPa)
            pressure /= 100;
            //Calculate and return the altitude using the international barometric formula
            return 44330.0f * (1.0f - (float)Math.Pow((pressure / seaLevel), 0.1903f));
        }

您的項目現在可以部署了!

預期產出

?
poYBAGOkEYWACE_kAABzwWrdxqI393.png
部署代碼后,您應該會在 Visual Studio 輸出窗口中看到 10 個氣壓數據樣本。??
?

在這里查看下一課。


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數據手冊
  2. 1.06 MB  |  532次下載  |  免費
  3. 2RK3399完整板原理圖(支持平板,盒子VR)
  4. 3.28 MB  |  339次下載  |  免費
  5. 3TC358743XBG評估板參考手冊
  6. 1.36 MB  |  330次下載  |  免費
  7. 4DFM軟件使用教程
  8. 0.84 MB  |  295次下載  |  免費
  9. 5元宇宙深度解析—未來的未來-風口還是泡沫
  10. 6.40 MB  |  227次下載  |  免費
  11. 6迪文DGUS開發指南
  12. 31.67 MB  |  194次下載  |  免費
  13. 7元宇宙底層硬件系列報告
  14. 13.42 MB  |  182次下載  |  免費
  15. 8FP5207XR-G1中文應用手冊
  16. 1.09 MB  |  178次下載  |  免費

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234315次下載  |  免費
  3. 2555集成電路應用800例(新編版)
  4. 0.00 MB  |  33566次下載  |  免費
  5. 3接口電路圖大全
  6. 未知  |  30323次下載  |  免費
  7. 4開關電源設計實例指南
  8. 未知  |  21549次下載  |  免費
  9. 5電氣工程師手冊免費下載(新編第二版pdf電子書)
  10. 0.00 MB  |  15349次下載  |  免費
  11. 6數字電路基礎pdf(下載)
  12. 未知  |  13750次下載  |  免費
  13. 7電子制作實例集錦 下載
  14. 未知  |  8113次下載  |  免費
  15. 8《LED驅動電路設計》 溫德爾著
  16. 0.00 MB  |  6656次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉中文版)
  4. 78.1 MB  |  537798次下載  |  免費
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費
  11. 6電路仿真軟件multisim 10.0免費下載
  12. 340992  |  191187次下載  |  免費
  13. 7十天學會AVR單片機與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138040次下載  |  免費
棋牌室名字| 大发888娱乐城存款| 将军百家乐官网的玩法技巧和规则 | 18皇宝| 威尼斯人娱乐城官方网| 玩百家乐保时捷娱乐城| 嘉禾百家乐官网的玩法技巧和规则 | 百家乐官网最新分析仪 | 博彩网址大全| 百家乐大| 百家乐真人游戏投注网| 八大胜百家乐官网的玩法技巧和规则 | 百家乐官网真人视屏游戏| 必博国际| 大发888下载安全的| 精通百家乐的玩法技巧和规则| 百家乐单机游戏免费| 迪威百家乐官网赌场娱乐网规则| 百家乐官网有真假宝单吗| 文山县| 奔驰娱乐城开户| 大发888真人娱乐场| 博九百家乐的玩法技巧和规则| 百家乐波音平台导航网| 千亿娱百家乐官网的玩法技巧和规则| 百家乐官网筹码方形| 大赢家百家乐官网66| 黄陵县| 肯博娱乐| 德州扑克 盲注| 大发888娱乐场漏洞| 首席百家乐的玩法技巧和规则| 新2百家乐现金网百家乐现金网| 风水学24山看水法| 怎样赢百家乐官网的玩法技巧和规则| 百家乐官网小音箱| 百家乐官网开户送彩网址| 辽宁省| 竹溪县| 林甸县| 游戏|