Microchip PIC18F46K42 Handleiding


Lees hieronder de 📖 handleiding in het Nederlandse voor Microchip PIC18F46K42 (40 pagina's) in de categorie Niet gecategoriseerd. Deze handleiding was nuttig voor 22 personen en werd door 2 gebruikers gemiddeld met 4.5 sterren beoordeeld

Pagina 1/40
User's Guide
Interfacing the MikroElektronika Weather Click Board™
with the PIC18F25K42
Preface
Important:  Notice to customers:
All documentation becomes dated, and this manual is no exception. Microchip tools and
documentation are constantly evolving to meet customer needs, so some actual dialogs and/or
tool descriptions may differ from those in this document. Please refer to our website
(www.microchip.com) to obtain the latest documentation available.
Documents are identified with a “DS” number. This number is located on the bottom of each
page, in front of the page number. The numbering convention for the DS number is
“DSXXXXXA”, where “XXXXX” is the document number and “A” is the revision level of the
document.
For the most up-to-date information on development tools, see the MPLAB ÂŽ IDE online help.
Select the Help menu, and then Topics to open a list of available online help files.
Introduction
This document describes how to use the device as a development tool to emulate and debug firmware on
a target board, as well as how to program devices.
The MikroElektronika Weather Click module contains the Bosch BME280 Environmental Sensor that is
used to detect humidity, barometric pressure, and temperature. This user's guide explains how to connect
the Weather Click to the PIC18F25K42 microcontroller, send and receive commands and raw weather
data, and convert the data into a usable format.
Recommended Reading
For the latest information on using the device, read the “ ” file (an HTMLReadme for Device #.htm
file) in the Readmes subdirectory of the MPLABÂŽ IDE installation directory. The release notes (Readme)
contain update information and known issues that may not be included in this user’s guide.
Hardware/Software Requirements
This demonstration uses the following hardware and software components:
Hardware:
• Curiosity High Pin Count (HPC) Development Board (DM164136)
• PIC18F25K42 Microcontroller (PIC18F25K42-I/SP)
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 1
• MikroElektronika Weather Click Board™ (MIKROE-1978)
• MCP2200 USB-to-UART Breakout Module (ADM00393)
• USB Micro-B 5-pin cable (Curiosity to PC for programming/USB power)
• USB Mini-B 5-pin cable (MCP2200 to PC for displaying data)
• Jumper wires
Software:
• MPLABX® IDE v4.15 or higher
• XC8 Compiler v1.45 or higher
• Tera Term or equivalent PC terminal program
Other Relevant Documents:
• Bosch Data Sheet"BME280 Combined Humidity and Pressure Sensor"
•"PIC18(L)F24/25K42 28-Pin, Low-Power High-Performance Microcontrollers with XLP Technology"
Data Sheet (DS40001869)
•TB3192, "Using the SPI module on 8-bit PIC® Microcontrollers" Technical Brief (90003192)
User's Guide
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 2
Table of Contents
Preface............................................................................................................................ 1
Hardware/Software Requirements.................................................................................. 1
1. Application Overview................................................................................................. 4
1.1. BME280 Environmental Sensor................................................................................................... 4
2. Application Configuration...........................................................................................9
2.1. Hardware Configuration............................................................................................................... 9
2.2. Software Configuration............................................................................................................... 11
3. Conclusion...............................................................................................................36
The Microchip Web Site................................................................................................ 37
Customer Change Notification Service..........................................................................37
Customer Support......................................................................................................... 37
Microchip Devices Code Protection Feature................................................................. 37
Legal Notice...................................................................................................................38
Trademarks................................................................................................................... 38
Quality Management System Certified by DNV.............................................................39
Worldwide Sales and Service........................................................................................40
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 3
1. Application Overview
The demo uses a PIC18F25K42 microcontroller to communicate with the MikroElektronika Weather Click
Board to read temperature, pressure, and humidity levels of the environment in which the demo is placed.
Once the weather data has been acquired and processed, the information is transmitted to a PC terminal
program for display.
1.1 BME280 Environmental Sensor
The Weather Click contains the Bosch BME280 Environmental Sensor. Communication with the sensor is
handled either over the I2C or SPI interfaces. For this demo, the SPI interface is used; therefore, the
jumper selector resistors must be changed from the default right position to the left position (see Figure
1-1). The Weather Click operates at 3.3V nominal, and routes power to the sensor’s V DD and VDDIO pins.
The sensor features three operating modes: Sleep, Forced, and Normal modes. This application uses
Forced mode as recommended by Bosch for weather monitoring since the temperature, humidity, and
pressure readings will not rapidly change. Forced mode allows user software to decide when to read the
sensor. Each time a measurement has completed, the sensor returns to Sleep mode, reducing power
consumption.
Figure 1-1. Weather Click Jumper Locations
1.1.1 Register Map
The BME280 contains several important registers, as shown in .Figure 1-2
Data Registers:
The ‘hum_lsb’ and ‘hum_msb’ registers are read-only and contain the 16-bit raw humidity measurement
output data.
User's Guide
Application Overview
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 4
The ‘temp_xlsb’, ‘temp_lsb’, and ‘temp_msb’ registers are read-only and are combined to form the 20-bit
raw temperature output data.
The ‘press_xlsb’, ‘press_lsb’, and ‘press_msb’ registers are read-only and are combined to form the 20-
bit raw pressure output data.
Control Registers:
The ‘config’ register controls the rate, filter, and interface options of the sensor. The ‘t_sb<2:0>’ bits
determine the inactive standby time when operating in normal mode. The ‘filter<2:0>’ bits determine the
time constant of the IIR filter. The ‘spi3w_en’ bit determines whether the SPI interface uses the standard
four-wire interface or a three-wire interface.
The ‘ctrl_meas’ register controls the oversampling settings for temperature and pressure readings and
the sensor’s mode of operation. The ‘osrs_t<2:0>’ bits control oversampling of temperature data. The
‘osrs_p<2:0>’ bits control oversampling of pressure data. The ‘mode<1:0>’ bits determine which mode
the sensor operates in: Sleep, Forced, or Normal. It is important to note that the ‘crtl_meas’ register must
be written to after changing settings in the ‘ctrl_hum’ register for the changes to become effective.
The ‘status’ register is read-only and is used to indicate the status of the sensor. The ‘measuring’ bit is set
whenever a conversion is running and is clear when the conversion results have been transferred to the
data registers. The ‘im_update’ bit is set when the Nonvolatile Memory (NVM) data is actively being
copied into the calibration registers.
The ‘ctrl_hum’ register contains the ‘osrs_h<2:0>’ bits, which control the oversampling of humidity data. It
is important to note that changes to the ‘ctrl_hum’ register only become effective after a write to the
‘ctrl_meas’ register.
The ‘reset’ register is used to perform a Power-on Reset (POR) procedure. Writing the value of 0xB6 will
direct the sensor to perform a POR. Writing any other values will have no effect. This register is write-
only; reads from this register will always result in 0x00.
The ‘id’ register contains the sensor’s chip identification number, which is always a value of 0x60.
Calibration Registers:
Register ‘calib00’ through ‘calib31’ holds the sensor’s calibration values for humidity, temperature, and
pressure readings. These registers have to be used to compensate for variations that occur due to the
individual sensing elements of the sensor.
User's Guide
Application Overview
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 5
Figure 1-2. BME280 Register Map
Register
Address bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 Reset Value
hum_lsb 0xFE 0x00
hum_msb 0xFD 0x80
temp_xlsb 0xFC 0 0 0 0 0x00
temp_lsb 0xFB 0x00
temp_msb 0xFA 0x80
press_xlsb 0xF9 0 0 0 0 0x00
press_lsb 0xF8 0x00
press_msb 0xF7 0x80
config 0xF5 reserved spi3w_en 0x00
ctrl_meas 0xF4 0x00
status 0xF3 measuring im_update 0x00
ctrl_hum 0xF2 0x00
calib31 0xE7
calib30 0xE6
calib29 0xE5
calib28 0xE4
calib27 0xE3
calib26 0xE2
calib25 0xE1
reset 0xE0 0x00
id 0xD0 0x60
calib24 0xA1
calib23 0x9F
calib22 0x9E
calib21 0x9D
calib20 0x9C
calib19 0x9B
calib18 0x9A
calib17 0x99
calib16 0x98
calib15 0x97
calib14 0x96
calib13 0x95
calib12 0x94
calib11 0x93
calib10 0x92
calib09 0x91
calib08 0x90
calib07 0x8F
calib06 0x8E
calib05 0x8D
calib04 0x8C
calib03 0x8B
calib02 0x8A
calib01 0x89
calib00 0x88
varies part-
to-part
varies part-
to-part
t_sb<2:0>
filter<2:0>
osrs_t<2:0>
osrs_p<2:0>
mode<1:0>
osrs_h<2:0>
reserved
reserved
dig_H6
dig_P5<15:8>
dig_P5<7:0>
reset<7:0>
chip_id<7:0>
hum<7:0>
hum<15:8>
temp<19:12>
temp<11:4>
press<19:12>
press<11:4>
dig_H3<7:0>
dig_H3<15:8>
dig_H4<7:0>
dig_H4<15:8>
dig_H5<7:0>
dig_H5<15:8>
dig_H1<7:0>
dig_P9<15:8>
dig_P9<7:0>
dig_P8<15:8>
dig_P8<7:0>
dig_P7<15:8>
dig_P7<7:0>
dig_P6<15:8>
dig_P6<7:0>
dig_P1<7:0>
dig_P1<15:8>
dig_P4<15:8>
dig_P4<7:0>
dig_P3<15:8>
dig_P3<7:0>
dig_P2<15:8>
dig_P2<7:0>
dig_T3<15:8>
dig_T1<7:0>
dig_T1<15:8>
dig_T2<7:0>
dig_T2<15:8>
dig_T3<7:0>
1.1.2 Measurement Flow
The measurement cycle depends on the sensor’s operating mode. Since this application is operating in
Forced mode, the measurement cycle begins with a write to the ‘mode<1:0>’ bits of the ‘ctrl_meas’
register. Writing a value of 0x01 or 0x10 places the sensor into Forced mode. Next, the temperature,
pressure, and humidity measurements are performed. Once the raw environmental conditions have been
measured, each measured value may be passed through an Infinite Impulse Response (IIR) filter, if
enabled. Finally, the filtered (if enabled) or unfiltered results are stored in the data registers, and the
User's Guide
Application Overview
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 6
sensor returns to Sleep mode (see ). It is important to note that in Forced mode, theFigure 1-3
‘mode<1:0>’ bits must be written to each time a new measurement cycle is desired.
Figure 1-3. Measurement Flow in Forced Mode
Start measurement
(software sets
mode<1:0> = 0x01)
Measure temperature
Measure pressure
Measure humidity
IIR filter enabled?
(filter<2:0> ! = 000)
IIR filter initialized?
Update filter memory using
previous filter memory, ADC
value, and filter coefficient
Copy ADC values to filter
memory
(initializes IIR filter)
Copy filter memory to data
output registers
End measurement
(hardware clears mode<1:0>)
No
No
Yes
Yes
1.1.2.1 Humidity Measurement
Humidity measurements are controlled by the ‘osrs_h<2:0>’ bits of the ‘ctrl_hum’ register. Humidity
measurements can be skipped by clearing the ‘osrs_h<2:0>’ bits; otherwise, the ‘osrs_h<2:0>’ bits control
the oversampling setting. Since humidity values do not change rapidly, they do not require the use of the
IIR filter; therefore, the output resolution is fixed at 16 bits.
1.1.2.2 Pressure Measurement
Pressure measurements are controlled by the ‘osrs_p<2:0>’ bits of the ‘ctrl_meas’ register. Pressure
measurements can be skipped by clearing the ‘osrs_p<2:0>’ bits; otherwise, they control the
oversampling rate of the temperature measurement.
The output resolution is determined by the IIR filter and oversampling settings:
• When the IIR filter is enabled, the output pressure resolution is 20 bits.
• When the IIR filter is disabled, the pressure resolution is 16 bits plus the value of the ‘osrs_p<2:0>’
bits minus 1 (osrs_p<2:0> – 1). For example, if the IIR filter is disabled, and a value of ‘ ’ is011
written into ‘osrs_p<2:0>’, the pressure resolution is 18 bits (16 + (3-1)). It is important to note that
the pressure resolution has a maximum of 20 bits; ‘osrs_p<2:0>’ values at or above ‘ ’ will result101
in 20 bits of resolution.
1.1.2.3 Temperature Measurement
Temperature measurements are controlled by the ‘osrs_t<2:0>’ bits of the ‘ctrl_meas’ register. Pressure
measurements can be skipped by clearing the ‘osrs_t<2:0>’ bits; otherwise, they control the temperature
measurement’s oversampling rate. The output resolution is determined by the IIR filter and oversampling
settings:
User's Guide
Application Overview
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 7
• When the IIR filter is enabled, the output temperature resolution is 20 bits.
• When the IIR filter is disabled, the temperature resolution is 16 bits plus the value of the
‘osrs_t<2:0>’ bits minus 1 (osrs_t<2:0> – 1). For example, if the IIR filter is disabled, and a value of
‘ ’ is written into ‘osrs_t<2:0>’, the pressure resolution is 20 bits (16 + (5-1)). It is important to101
note that the pressure resolution has a maximum 20 bits; ‘osrs_p<2:0>’ values at or above ‘ ’101
will result in 20 bits of resolution.
User's Guide
Application Overview
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 8
2. Application Configuration
The following pages describe the configuration of the hardware and software components necessary for
data acquisition.
2.1 Hardware Configuration
1. Place the PIC18F25K42 microcontroller into the 28-lead socket (J9) of the Curiosity HPC board
(see ).Figure 2-1
Figure 2-1. PIC18F25K42 Inserted into 28-Lead Socket (J9)
2. Place the MikroElektronika Weather Click into mikroBUS™ Socket 1 of the Curiosity HPC board
(see ).Figure 2-2
Figure 2-2. Weather Click Inserted in mikroBUS™ Socket 1
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 9
3. Connect the MCP2200 to the Curiosity board using jumper wires. Connect one end of a jumper
wire to pin RB4 of header J11, and the other end to pin 6 of the MCP2200. Next, connect one end
of a jumper wire to the GND pin of header J11, and the other end to pin 3 of the MCP2200. See
Figure 2-3 for the connection points. Finally, connect the MCP2200 to a PC using the USB Mini-B
5-pin cable. Note that the PIC device’s UART receive line is not used, so no additional wiring is
needed.
Figure 2-3. MCP2200 USB-to-UART Connections
(PICÂŽ)
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 10
4. Connect the Curiosity HPC to the PC using the USB Micro-B 5-pin cable, as shown in .Figure 2-4
This is the final step in the hardware configuration.
Figure 2-4. Curiosity HPC Connection to the PC
2.2 Software Configuration
1. Download and install the latest MPLAB X IDE. The latest MPLAB X version can be found at http://
www.microchip.com/mplab/mplab-x-ide.
2. Download and install the latest MPLAB XC8 compiler. The latest MPLAB XC8 compiler can be
found at http://www.microchip.com/mplab/compilers.
3. Open the MPLAB X IDE. On the main toolbar, select Tools Plugin, and then select the tab in the
drop-down menu, as shown in .Figure 2-5
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 11
Figure 2-5. Tools Drop-Down Menu
4. A new window, Plugins, will open. Under the tab, find the MPLAB CodeAvailable Plugins
Configurator selection and add a checkmark to the check box. Press the button (see Install Figure
2-6). This installs the MCC tool used to configure the necessary peripherals contained in this
project. Once the install is complete, MPLAB will require a restart to enable the MCC tool.
Figure 2-6. Installing the MCC Plug-in Tool
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 12
5. Create a new project in MPLAB X. Click on , then “New Project” from the drop-down menu, asFile
shown in .Figure 2-7
Figure 2-7. Create a New Project
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 13
6. The New Project window will open. Select “Standalone Project” in the Projects window and click
Next > (see ).Figure 2-8
Figure 2-8. Choose Type of Project
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 14
7. In the Select Device window, select from the “Device:” drop-down menu, as shownPIC18F25K42
in . Click .Figure 2-9 Next >
Figure 2-9. Select Device
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 15
8. In the Select Tool window, under the menu selection,Microchip Starter Kits > Starter Kits (PKOB)
click (see ). Click . It is important to note that the Curiosity HPC boardCuriosity Figure 2-10 Next >
must be connected to the PC for MPLAB X to recognize it as a tool. The Curiosity selection will not
be visible if the board is not connected to the PC.
Figure 2-10. Select Tool
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 16
9. In the Select Compiler window, select the desired XC compiler under the XC8 menu list (see Figure
2-11 Figure 2-11). Click . Note that Next > shows two XC8 compiler choices. XC8 (v2.00) must be
used with MPLAB X v.5.00, while XC8 (v1.45) is compatible with the MPLAB X v.4.15 utilized in this
application.
Figure 2-11. Select Compiler
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 17
10. In the Select Project Name and Folder window, type the name of the project into the “Project
Name:” text box. Next, choose the location to store the project. For this application, the Desktop file
location was chosen (see ). After the project name and location have been determined,Figure 2-12
click .Finish
Figure 2-12. Select Project Name and Folder
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 18
11. At this point, the PIC microcontroller is ready for configuration. The MCC tool is used to configure
the peripherals needed for the application and generate the necessary driver files. Click on the
MCC icon in the MPLAB X toolbar, as shown in . This opens the MCC tool.Figure 2-13
Figure 2-13. MCC Icon
12. MCC will default to the System Module window when it opens. There are two tabs within this
window: and . The tab allows for simple changes to the systemEasy Setup Registers Easy Setup
oscillator, configures the Windowed Watchdog Timer, and enables Low-Voltage Programming
(LVP). The tab allows for changes to be made to the Configuration Words, oscillatorRegisters
registers, Peripheral Module Disable (PMD) registers, and the Windowed Watchdog Timer
registers. The tab has to be used when the default settings of the above registers cannotRegisters
be utilized by the application. For this application, the system can be configured using the Easy
Setup tab. The settings are as follows:
• In the INTERNAL OSCILLATOR window, under the “Oscillator Select” drop-down menu,
select . When HFINTOSC is selected, MCC automatically configures the “HFHFINTOSC
Internal Clock” and “Clock Divider” selections to achieve a 1 MHz system clock, as shown in
Figure 2-14.
Figure 2-14. Oscillator Selection
• The WWDT and Programming windows can be left in their default states.
13. Next, in the Device Resources window, scroll down to the SPI selection, click on the arrow next to
the selection, click the arrow next to , and double click on the SPI SPI1 SPI1 [PIC10/PIC12/PIC16/
PIC18 MCUs by Microchip Technology, Inc.] SPI1 selection. A new tab will appear named . The
SPI1 window contains the and tabs. For this application, the Easy Setup Registers Easy Setup
tab is used.
Configure the SPI as follows (see ):Figure 2-15
• In the “Mode” drop-down menu, select .SPI Master
• Leave the “Enable SPI” check box checked (default).
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 19
• In the “Input Data Sampled at” drop-down menu, select (default).Middle
• In the “Clock Polarity” drop-down menu, select Idle:Low, Active:High.
• In the “Clock Edge” drop-down menu, select .Active to idle
• In the “Clock Source” drop-down menu, select (default).FOSC
• In the “Clock Divider” box, enter (no divider).0x0
Figure 2-15. SPI Configuration
14. Under the Device Resources window, scroll down to “UART”, click on the arrow next to “UART”,
then click on the arrow next to “UART1”, and double click on the UART1 [PIC10/PIC12/PIC16/
PIC18 MCUs by Microchip Technology, Inc.] UART1 selection. A new tab will appear named .
Configure the SPI in the tab as follows (see ):Easy Setup Figure 2-16
• In the “Mode” drop-down menu, select (default).Asynchronous 8-bit mode
• Leave the “Baud Rate:” setting at 9600 (default).
• Leave the “Enable UART” check box checked (default).
• Add a checkmark in the “Enable Transmit” check box.
• Leave the “Transmit Polarity” and “Receive Polarity” drop-down menu selections as ‘not
inverted’ (default).
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 20
• Add a checkmark to the “Redirect STDIO to UART” check box.
• Leave the “Transmit Polarity” and “Receive Polarity” drop-down menu selections as ‘not
inverted’ (default).
• Add a checkmark to the “Redirect STDIO to UART” check box.
Figure 2-16. UART Configuration
15. In the Project Resources window, click on . This opens the tab, allowing forPin Module Pin Module
the configuration of the I/Os used for the application. The user will need to change the SPI pins
from the default settings, and add the UART TX pin.
Under the tab, change the following:Registers
• Register RB1PPS – enter (SCK1 output).0x1E
• Register RB3PPS – enter (SDO output).0x1F
• Register RB4PPS – enter (TX output).0x13
• Register RC3PPS – change the value to 0x0 (unused).
• Register SPI1SCKPPS – change the value to 0x0 (SCK input not used).
• Register SPI1SDIPPS – change the value to 0x0A (SDI input on RB1).
• Register SPI1SSPPS – change the value to 0x0 (SS input not used).
In the Pin Manager: Grid View window, click on the Port A number 3 box in the “Pin Module > GPIO
> output” selection row (see ).Figure 2-17
Switch from the tab to the tab in the Pin Module window, then make theRegisters Easy Setup
following changes (see ):Figure 2-18
• Pin RA3 – In the “Custom Name” column, change the name to , add a checkmark to theCS
“Start High” check box, and remove the checkmark from the “Analog” check box.
• Pin RB3 – Remove the checkmark from the “Analog” check box.
• Pin RB4 – Remove the checkmark from the “Analog” check box.
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 21
Figure 2-17. Pin Manager: Grid View Window
Figure 2-18. Pin Module Easy Setup Window
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 22
16. In the Project Resources window, click the button (see ). MCC creates aGenerate Figure 2-19
project that contains the driver files for the peripherals that had been configured.
Figure 2-19. MCC Generate Button
17. At this point, the user has all the necessary files to run the application, except for the drivers for the
Weather Click. Additionally, the file needs to have some routines added.main.c
To add the Weather Click drivers and code, follow these steps in MPLAB X:main.c
a. In the tab, right-click on , then drag the mouse over , and click on Projects Source Files New C
Source File (see ).Figure 2-20
b. The New C Source File window will open. In the “File Name” text box, enter asWeather_click
the C file name (see ). The remaining text boxes do not need modification unless aFigure 2-21
separate file location is desired. Once the file name has been entered, click the button. TheFinish
new file will appear under the Source Files file menu, as shown in .Figure 2-22
c. Next, the user will essentially perform steps ‘a’ and ‘b’ again for the header file. In the Projects
tab, right-click on , then drag the mouse over “New”, and click on Header Files xc8_header.h
(see ).Figure 2-23
d. The window will open. In the “File Name” text box, enter New xc8_header.h Weather_click
as the header file name (see ). Click the button, and the new file will appearFigure 2-24 Finish
under the “Header Files” file menu, as shown in .Figure 2-25
e. The BME280 sensor’s data sheet provides the necessary compensation routines and
recommends these routines be used for calculations. For simplicity, these routines have been
extracted from the data sheet and modified for this application. Copy the entire code example in
Example 2-1 and paste into the newly created file.Weather_click.c
f. Copy the entire code example in and paste into the newly createdExample 2-2
Weather_click.h file.
g. Copy the entire code example in and paste into the project’s file.Example 2-8 main.c
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 23
h. Click on the button (see ). If all the steps have beenClean and Build Main Project Figure 2-26
followed, there will be no errors. If there are any errors, confirm that the code examples were
copied properly.
i. Click on the button to program the PIC device (see Make and Program Device Main Project
Figure 2-27).
Figure 2-20. Creating the FileWeather_click.c
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 24
Figure 2-21. New C Source File Window
Figure 2-22. Generated File in Projects WindowWeather_click.c
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 25
Figure 2-23. Creating the FileWeather_click.h
Figure 2-24. New File Windowxc8_header.h
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 26
Figure 2-25. Generated File in Projects WindowWeather_click.h
Figure 2-26. Clean and Build Main Project Button
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 27
Example 2-5.  CodeWeather_click.c
void WEATHERclick_TempCalc(void)
{
double var1, var2;
temp_raw.byte2 = temp[1];
temp_raw.byte1 = temp[2];
temp_raw.byte0 = temp[3];
temp_raw.raw = temp_raw.raw 4;>>
uint16_t digT1 = (uDigT[2] 8) (uDigT[1]);<< +
int16_t digT2 = (sDigT[2] 8) (sDigT[1]);<< +
int16_t digT3 = (sDigT[4] 8) (sDigT[3]);<< +
var1 = (((double)temp_raw.raw) 16384.0 ((double)digT1) 1024.0) / - / *
((double)digT2);
var2 = ((((double)temp_raw.raw) 131072.0 ((double)digT1) 8192.0) / - / *
(((double)temp_raw.raw) 131072.0 ((double)digT1) 8192.0)) / - / *
((double)digT3);
t_fine = (int32_t)(var1 var2);+
T = (var1 var2) 5120.0;+ /
}
void WEATHERclick_HumCalc(void)
{
int32_t var1;
hum_raw.byte1 = hum[1];
hum_raw.byte0 = hum[2];
uint8_t digH1 = uDigH[1];
int16_t digH2 = (sDigH[2] 8) (sDigH[1]);<< +
uint8_t digH3 = uDigH[2];
int16_t digH4 = (sDigH[3] 4) (sDigH[4] 0x0F);<< + &
int16_t digH5 = (sDigH[5] 4) ((sDigH[4] 4) 0x0F);<< + >> &
int8_t digH6 = sDigH[6];
var1 = (t_fine ((int32_t) 76000));-
var1 = (((((temp_raw.raw 14) (((int32_t) digH4) 20) << - << -
(((int32_t) digH5) var1)) ((int32_t) 16384)) 15) * + >> *
(((((((var1 ((int32_t) digH6)) 10) * >> *
(((var1 ((int32_t) digH3)) 11) ((int32_t)32768))) 10) * >> + >> +
((int32_t) 2097152)) ((int32_t)digH2) 8192) 14));* + >>
var1 = (var1 (((((var1 15) (var1 15)) 7) - >> * >> >> *
((int32_t) digH1)) 4));>>
var1 = (var1 0 0 var1);<?:
var1 = (var1 419430400 419430400 var1);>?:
H = (uint32_t) (var1 12);>>
H = H 1024.0;/
}
Example 2-6.  CodeWeather_click.c
void WEATHERclick_PressCalc(void)
{
double var1, var2;
press_raw.byte2 = press[1];
press_raw.byte1 = press[2];
press_raw.byte0 = press[3];
press_raw.raw = press_raw.raw 4;>>
uint16_t digP1 = (uDigP[2] 8) (uDigP[1]);<< +
int16_t digP2 = (sDigP[2] 8) (sDigP[1]);<< +
int16_t digP3 = (sDigP[4] 8) (sDigP[3]);<< +
int16_t digP4 = (sDigP[6] 8) (sDigP[5]);<< +
int16_t digP5 = (sDigP[8] 8) (sDigP[7]);<< +
int16_t digP6 = (sDigP[10] 8) (sDigP[9]);<< +
int16_t digP7 = (sDigP[12] 8) (sDigP[11]);<< +
int16_t digP8 = (sDigP[14] 8) (sDigP[13]);<< +
int16_t digP9 = (sDigP[16] 8) (sDigP[15]);<< +
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 33
var1 = ((double)t_fine 2.0) 64000.0;/ -
var2 = var1 var1 ((double)digP6) 32768.0;* * /
var2 = var2 var1 ((double)digP5) 2.0;+ * *
var2 = (var2 4.0) (((double)digP4) 65536.0);/ + *
var1 = (((double)digP3) var1 var1 524288.0 ((double)digP2) * * / + *
var1) 524288.0;/
var1 = (1.0 var1 32768.0) ((double)digP1);+ / *
if (var1 == 0.0)
{
P = 0;
}
P = 1048576.0 (double)press_raw.raw;-
P = (P (var2 4096.0)) 6250.0 var1;- / * /
var1 = ((double)digP9) P P 2147483648.0;* * /
var2 = P ((double)digP8) 32768.0;* /
P = P (var1 var2 ((double)digP7)) 16.0;+ + + /
}
Example 2-7.  CodeWeather_click.h
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
// Section: Macro Declarations
void WEATHERclick_Config(void);
void WEATHERclick_ReadID(void);
void WEATHERclick_ReadTempParameters(void);
void WEATHERclick_ReadPressParameters(void);
void WEATHERclick_ReadHumParameters(void);
void WEATHERclick_ReadTemp(void);
void WEATHERclick_ReadPress(void);
void WEATHERclick_ReadHum(void);
void WEATHERclick_TempCalc(void);
void WEATHERclick_PressCalc(void);
void WEATHERclick_HumCalc(void);
void WEATHERclick_PrintResults(void);
#ifdef __cplusplus
}
#endif
#endif
// End of File
Example 2-8.  Codemain.c
#include "mcc_generated_files/mcc.h"
#include "Weather_click.h"
void main(void)
{
SYSTEM_Initialize();
WEATHERclick_ReadTempParameters();
WEATHERclick_ReadPressParameters();
WEATHERclick_ReadHumParameters();
printf("\r\n\r\nPIC18F25K42 Weather Station\r\n");
printf("Curiosity HPC with MikroeElektronika WEATHERclick\r\n");
printf("BME280 Environmental Sensor BOSCH SensorTec\r\n\r\n");
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 34
WEATHERclick_ReadID();
while (1)
{
WEATHERclick_Config();
WEATHERclick_ReadTemp();
WEATHERclick_ReadPress();
WEATHERclick_ReadHum();
WEATHERclick_TempCalc();
WEATHERclick_PressCalc();
WEATHERclick_HumCalc();
WEATHERclick_PrintResults();
__delay_ms(500);
}
}
// End of File
User's Guide
Application Configuration
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 35
3. Conclusion
This user guide details the steps needed to create a project that interfaces the MikroElektronika Weather
Click with the PIC18F25K42 microcontroller. The Weather Click detects temperature, pressure, and
humidity values which are read and calibrated by the PIC18F25K42. The environmental data are then
transmitted to a PC terminal program for viewing. Although this document provides the procedures to
create the user's own project, it can also be found as an example project on the MPLAB Xpress website
at: .https://mplabxpress.microchip.com/mplabcloud/example/details/415
For more information on the PIC18F25K42, or on any other Microchip device, visit www.microchip.com.
User's Guide
Conclusion
Š 2018 Microchip Technology Inc. User Guide DS40002073A-page 36


Product specificaties

Merk: Microchip
Categorie: Niet gecategoriseerd
Model: PIC18F46K42

Heb je hulp nodig?

Als je hulp nodig hebt met Microchip PIC18F46K42 stel dan hieronder een vraag en andere gebruikers zullen je antwoorden




Handleiding Niet gecategoriseerd Microchip

Handleiding Niet gecategoriseerd

Nieuwste handleidingen voor Niet gecategoriseerd