/** Generated Main Source File Company: Microchip Technology Inc. File Name: main.c Summary: This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs Description: This header file provides implementations for driver APIs for all modules selected in the GUI. Generation Information : Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.6 Device : PIC18F46K42 Driver Version : 2.00 */ /* (c) 2018 Microchip Technology Inc. and its subsidiaries. Subject to your compliance with these terms, you may use Microchip software and any derivatives exclusively with Microchip products. It is your responsibility to comply with third party license terms applicable to your use of third party software (including open source software) that may accompany Microchip software. THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. */ #include "mcc_generated_files/mcc.h" #include "BME680/bme680.h" #include "mcc_generated_files/examples/i2c1_master_example.h" void user_delay_ms(uint32_t period) { /* * Return control or wait, * for a period amount of milliseconds */ uint32_t i; for (i = 0; i < period; i++) { __delay_ms(1); } } int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ /* * The parameter dev_id can be used as a variable to store the I2C address of the device */ /* * Data on the bus should be like * |------------+---------------------| * | I2C action | Data | * |------------+---------------------| * | Start | - | * | Write | (reg_addr) | * | Stop | - | * | Start | - | * | Read | (reg_data[0]) | * | Read | (....) | * | Read | (reg_data[len - 1]) | * | Stop | - | * |------------+---------------------| */ I2C1_WriteNBytes((dev_id | 0b00000001), ®_addr, 1); I2C1_ReadNBytes((dev_id | 0b00000010), reg_data, len); return rslt; } int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ /* * The parameter dev_id can be used as a variable to store the I2C address of the device */ /* * Data on the bus should be like * |------------+---------------------| * | I2C action | Data | * |------------+---------------------| * | Start | - | * | Write | (reg_addr) | * | Write | (reg_data[0]) | * | Write | (....) | * | Write | (reg_data[len - 1]) | * | Stop | - | * |------------+---------------------| */ uint8_t i2cWriteBuffer[50]; int i; for (i = len; (i+1) > 0; i--) { i2cWriteBuffer[i+1] = reg_data[i]; } i2cWriteBuffer[0] = reg_addr; I2C1_WriteNBytes((dev_id | 0b00000001), i2cWriteBuffer, len+1); return rslt; } void main(void) { // Initialize the device SYSTEM_Initialize(); // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts // Use the following macros to: // Enable the Global Interrupts //INTERRUPT_GlobalInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); printf("START\n"); // TEST uint8_t buffer[5]; while(0){ user_delay_ms(100); user_i2c_read(0x77, 0xD0, buffer, 1); printf("Read Reg 0x%X: 0x%X (0x61 expected)\n",(0xD0),buffer[0]); user_delay_ms(100); buffer[0] = 0x03; user_i2c_write(0x77, 0x72, buffer, 1); printf("writing 0x03 to 0x72...\n"); buffer[0] = 0x04; user_i2c_write(0x77, 0x64, buffer, 1); printf("writing 0x04 to 0x64...\n"); buffer[0] = 0x05; user_i2c_write(0x77, 0x5A, buffer, 1); printf("writing 0x05 to 0x5A...\n"); buffer[0] = 0x06; user_i2c_write(0x77, 0x50, buffer, 1); printf("writing 0x06 to 0x50...\n"); user_delay_ms(100); buffer[0] = 0; user_i2c_read(0x77, 0x72, buffer, 1); // read printf("Read Reg 0x%X: 0x%X (0x03 expected)\n",(0x72),buffer[0]); buffer[0] = 0; user_i2c_read(0x77, 0x64, buffer, 1); // read printf("Read Reg 0x%X: 0x%X (0x04 expected)\n",(0x64),buffer[0]); buffer[0] = 0; user_i2c_read(0x77, 0x5A, buffer, 1); // read printf("Read Reg 0x%X: 0x%X (0x05 expected)\n",(0x5A),buffer[0]); buffer[0] = 0; user_i2c_read(0x77, 0x50, buffer, 1); // read printf("Read Reg 0x%X: 0x%X (0x06 expected)\n",(0x50),buffer[0]); while(1); } struct bme680_dev gas_sensor; gas_sensor.dev_id = BME680_I2C_ADDR_PRIMARY; gas_sensor.intf = BME680_I2C_INTF; gas_sensor.read = user_i2c_read; gas_sensor.write = user_i2c_write; gas_sensor.delay_ms = user_delay_ms; /* amb_temp can be set to 25 prior to configuring the gas sensor * or by performing a few temperature readings without operating the gas sensor. */ gas_sensor.amb_temp = 25; int8_t rslt = BME680_OK; rslt = bme680_init(&gas_sensor); uint8_t set_required_settings; /* Set the temperature, pressure and humidity settings */ gas_sensor.tph_sett.os_hum = BME680_OS_2X; gas_sensor.tph_sett.os_pres = BME680_OS_4X; gas_sensor.tph_sett.os_temp = BME680_OS_8X; gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3; /* Set the remaining gas sensor settings and link the heating profile */ gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; /* Create a ramp heat waveform in 3 steps */ gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */ gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */ /* Select the power mode */ /* Must be set before writing the sensor configuration */ gas_sensor.power_mode = BME680_FORCED_MODE; /* Set the required sensor settings needed */ set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL; /* Set the desired sensor configuration */ rslt = bme680_set_sensor_settings(set_required_settings,&gas_sensor); /* Set the power mode */ rslt = bme680_set_sensor_mode(&gas_sensor); /* Get the total measurement duration so as to sleep or wait till the * measurement is complete */ uint16_t meas_period; bme680_get_profile_dur(&meas_period, &gas_sensor); struct bme680_field_data data; while(1) { user_delay_ms(meas_period); /* Delay till the measurement is ready */ rslt = bme680_get_sensor_data(&data, &gas_sensor); if(rslt == BME680_W_NO_NEW_DATA){ printf("bme680_get_sensor_data FAILED - BME680_W_NO_NEW_DATA\n"); } printf("T: %.2f degC, P: %.2f hPa, H %.2f %%rH ", data.temperature / 100.0f, data.pressure / 100.0f, data.humidity / 1000.0f ); /* Avoid using measurements from an unstable heating setup */ if(data.status & BME680_GASM_VALID_MSK) printf(", G: %d ohms", data.gas_resistance); printf("\r\n"); /* Trigger the next measurement if you would like to read data out continuously */ if (gas_sensor.power_mode == BME680_FORCED_MODE) { rslt = bme680_set_sensor_mode(&gas_sensor); } // TEST uint8_t buffer[5]; while(0){ user_delay_ms(100); buffer[0] = 0; user_i2c_read(0x77, 0x72, buffer, 1); // read printf("Read Reg 0x%X: 0x%X\n",(0x72),buffer[0]); buffer[0] = 0; user_i2c_read(0x77, 0x64, buffer, 1); // read printf("Read Reg 0x%X: 0x%X\n",(0x64),buffer[0]); buffer[0] = 0; user_i2c_read(0x77, 0x5A, buffer, 1); // read printf("Read Reg 0x%X: 0x%X\n",(0x5A),buffer[0]); buffer[0] = 0; user_i2c_read(0x77, 0x50, buffer, 1); // read printf("Read Reg 0x%X: 0x%X\n",(0x50),buffer[0]); while(1); } } } /** End of File */