/** 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" 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_spi_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 select which Chip Select pin has * to be set low to activate the relevant device on the SPI bus */ /* * Data on the bus should be like * |----------------+---------------------+-------------| * | MOSI | MISO | Chip Select | * |----------------+---------------------|-------------| * | (don't care) | (don't care) | HIGH | * | (reg_addr) | (don't care) | LOW | * | (don't care) | (reg_data[0]) | LOW | * | (....) | (....) | LOW | * | (don't care) | (reg_data[len - 1]) | LOW | * | (don't care) | (don't care) | HIGH | * |----------------+---------------------|-------------| */ SPI_CS_SetLow(); reg_data[0] = reg_addr | 0x80; reg_data[1] = *reg_data; SPI1_ExchangeBlock(reg_data, 1+len); SPI_CS_SetHigh(); // shift the complete array one byte to the left uint8_t i; for (i = 0; i < len; i++) { reg_data[i] = reg_data[i+1]; } return rslt; } int8_t user_spi_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 select which Chip Select pin has * to be set low to activate the relevant device on the SPI bus */ /* * Data on the bus should be like * |---------------------+--------------+-------------| * | MOSI | MISO | Chip Select | * |---------------------+--------------|-------------| * | (don't care) | (don't care) | HIGH | * | (reg_addr) | (don't care) | LOW | * | (reg_data[0]) | (don't care) | LOW | * | (....) | (....) | LOW | * | (reg_data[len - 1]) | (don't care) | LOW | * | (don't care) | (don't care) | HIGH | * |---------------------+--------------|-------------| */ // first byte should be reg_addr[0], then reg_data[0] // reg_addr[1], then reg_data[1], ... // for every data byte there have to be the addr before! // not auto incrementing uint8_t WriteBuffer[100]; uint8_t i; #if 1 // both implementations are working for (i = 0; i < (len*2); i = i + 2) { WriteBuffer[i] = reg_addr + (i/2); WriteBuffer[i+1] = reg_data[i/2]; } WriteBuffer[0] = reg_addr; SPI_CS_SetLow(); SPI1_WriteBlock(WriteBuffer, len*2); SPI_CS_SetHigh(); #else for (i = 0; i < len; i++) { SPI_CS_SetLow(); WriteBuffer[0] = reg_addr+i; WriteBuffer[1] = reg_data[i]; SPI1_WriteBlock(WriteBuffer, 2); SPI_CS_SetHigh(); } #endif 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"); SPI1_Open(SPI1_DEFAULT); uint8_t buffer[100]; while(0){ // test function // change page to 0 buffer[0] = 0b00000000; user_spi_write(0x00, 0x73, buffer, 1); // reset buffer[0] = 0xB6; user_spi_write(0x00, 0x60, buffer, 1); user_delay_ms(1000); user_spi_read(0x00, 0x50, buffer, 1); // dev id printf("Read Reg 0x%X: 0x%X\n",(0x50),buffer[0]); // change page to 1 buffer[0] = 0b00010000; user_spi_write(0x00, 0x73, buffer, 1); buffer[0] = 0x01; user_spi_write(0x00, 0x50, buffer, 1); user_spi_read(0x00, 0x50, buffer, 1); printf("Read Reg 0x%X: 0x%X\n",(0x50),buffer[0]); user_delay_ms(1000); } while(0){ // test function // change page to 0 buffer[0] = 0b00000000; user_spi_write(0x00, 0x73, buffer, 1); // reset buffer[0] = 0xB6; user_spi_write(0x00, 0x60, buffer, 1); user_delay_ms(1000); user_spi_read(0x00, 0x50, buffer, 1); // dev id printf("Read Reg 0x%X: 0x%X\n...\n",(0x50),buffer[0]); // change page to 1 buffer[0] = 0b00010000; user_spi_write(0x00, 0x73, buffer, 1); user_spi_read(0x00, 0x50, buffer, 30); uint8_t i; for (i = 0; i < 30; i++) { printf("Read Reg 0x%X: 0x%X\n",(0x50+i),buffer[i]); } user_delay_ms(100); for (i = 0; i < 30; i++) { buffer[i] = 0x00+i; } user_spi_write(0x00, 0x50, buffer, 30); printf("writing data...\n"); user_delay_ms(100); user_spi_read(0x00, 0x50, buffer, 30); for (i = 0; i < 30; i++) { printf("Read Reg 0x%X: 0x%X\n",(0x50+i),buffer[i]); } // change page to 0 buffer[0] = 0b00000000; user_spi_write(0x00, 0x73, buffer, 1); // reset buffer[0] = 0xB6; user_spi_write(0x00, 0x60, buffer, 1); printf("reseting...\n"); user_delay_ms(1000); // change page to 1 buffer[0] = 0b00010000; user_spi_write(0x00, 0x73, buffer, 1); user_spi_read(0x00, 0x50, buffer, 30); for (i = 0; i < 30; i++) { printf("Read Reg 0x%X: 0x%X\n",(0x50+i),buffer[i]); } while(1); } struct bme680_dev gas_sensor; /* You may assign a chip select identifier to be handled later */ gas_sensor.dev_id = 0; gas_sensor.intf = BME680_SPI_INTF; gas_sensor.read = user_spi_read; gas_sensor.write = user_spi_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); 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); } } } /** End of File */