電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>使用氣壓傳感器讀取溫度、壓力和高度的項(xiàng)目

使用氣壓傳感器讀取溫度、壓力和高度的項(xiàng)目

2022-12-23 | zip | 0.38 MB | 次下載 | 免費(fèi)

資料介紹

描述

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

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

硬件

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

軟件?

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

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

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

MainPage.xaml.cs

打開 MainPage.xaml.cs 文件。

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

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

現(xiàn)在我們?cè)?OnNavigatedTo 方法中添加代碼,該方法將為氣壓傳感器創(chuàng)建一個(gè)新的 BMP280 對(duì)象并初始化該對(duì)象。

如果您不想在地圖上添加圖釘,請(qǐng)刪除?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();

接下來(lái)我們添加代碼來(lái)執(zhí)行以下操作:

  • 創(chuàng)建變量來(lái)存儲(chǔ)溫度、壓力和高度。將它們?cè)O(shè)置為 0。
  • 為海平面壓力創(chuàng)建一個(gè)變量。默認(rèn)值為 1013.25 hPa。
  • 讀取溫度、壓力和高度 10 次并將值輸出到調(diào)試控制臺(tái)。
 //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 數(shù)據(jù)表中找到。

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

 //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;

接下來(lái)在Initialize函數(shù)中添加如下代碼:

 //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函數(shù)中添加如下代碼:??

 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();
        }

將以下代碼添加到接下來(lái)的 2 個(gè)函數(shù)中以寫入控制寄存器。

 //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 函數(shù)中即可:

 //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 函數(shù)以從寄存器中讀取 8 位數(shù)據(jù)。

 //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;
        }

接下來(lái)的 3 個(gè)功能已為您完成。可以在數(shù)據(jù)表中找到編寫這些函數(shù)所需的信息。

  • ReadCoefficeints:這是從寄存器地址讀取所有校準(zhǔn)數(shù)據(jù)的函數(shù)。
  • BMP280_compensate_T_double:在此函數(shù)中,使用 BMP280 數(shù)據(jù)表中的補(bǔ)償公式計(jì)算以 oC 為單位的溫度。
  • BMP280_compensate_P_Int64:在此函數(shù)中,使用 BMP280 數(shù)據(jù)表中的補(bǔ)償公式計(jì)算以 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;
        }

重復(fù)相同的步驟以完成 ReadPressure 函數(shù)。?

 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函數(shù):

 //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));
        }

您的項(xiàng)目現(xiàn)在可以部署了!

預(yù)期產(chǎn)出

?
poYBAGOkEYWACE_kAABzwWrdxqI393.png
部署代碼后,您應(yīng)該會(huì)在 Visual Studio 輸出窗口中看到 10 個(gè)氣壓數(shù)據(jù)樣本。??
?

在這里查看下一課。


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

評(píng)論

查看更多

下載排行

本周

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

本月

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

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費(fèi)
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  4. 78.1 MB  |  537798次下載  |  免費(fèi)
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費(fèi)
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費(fèi)
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費(fèi)
  11. 6電路仿真軟件multisim 10.0免費(fèi)下載
  12. 340992  |  191187次下載  |  免費(fèi)
  13. 7十天學(xué)會(huì)AVR單片機(jī)與C語(yǔ)言視頻教程 下載
  14. 158M  |  183279次下載  |  免費(fèi)
  15. 8proe5.0野火版下載(中文版免費(fèi)下載)
  16. 未知  |  138040次下載  |  免費(fèi)