#include #include #include #include #include #include #include #include #include "nrf24l01.h" #include "nrf24l01_definitions.h" #include "bme280_interface.h" #include "bme280_defs.h" /* SPI: */ /* TODO: move to spi.h */ #define SPI_DDR DDRB #define SPI_SCK_PIN PB5 #define SPI_MOSI_PIN PB3 #define SPI_MISO_PIN PB4 /* Debug LED: */ #define LED_DDR DDRD #define LED_PORT PORTD #define LED_PIN PD3 char bool_case = 0; int timer = 0; int timer_max = 0; volatile uint8_t cycle = 0; volatile uint8_t interruptCounter; volatile uint8_t executionFlag; void Initialize_SPI(void); void Enter_Power_Save_Mode(void); void Exit_Power_Save_Mode(void); /* TODO Notes for power saving: * - Power-save-mode needed -> SM2...0 bits written to 011 * - Entering by issuing the SLEEP instruction -> What call in C? * - Before executing the SLEEP instruction, write bit SE of the SMCR to 1 * - Let Timer/Counter2 run with the necessary period and enable an interrupt. * -> The Global Interrupt Enable bit in SREG has to be set. * - asynchronous/synchronous clock source? * - When waking up * - Set PRR bits for needed peripherals * - */ void Set_Up_Power_Save_Mode(void); void Enter_Power_Save_Mode(void); ISR( TIMER2_COMPA_vect ) { /* Do nothing as the interrupt is only used to wake up the MCU. */ } int main (void) { struct bme280_data sensorData; // uint8_t testRegisterContent = 0x0A; // uint8_t registerContent[5]; // char registerContentString[30]; // uint8_t lengthRead; uint8_t temp = 0; /* Enable the debug LED */ LED_DDR |= (1 << LED_PIN); /* Initialize the SPI */ Initialize_SPI(); /* Initialize the nrf24l01 */ Initialize_NRF24L01(); // The NRF24L01 is now in the mode Standby-I. /* Configure the transmission parameters (Enhanced ShockBurst)*/ Configure_Transmission(); /* Initialize the BME280 */ Initialize_BME280(); Set_Up_Power_Save_Mode(); /* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP * and MCU are mismatched and the flashing will fail. */ _delay_ms(500); while(1) { Enter_Power_Save_Mode(); // The MCU enters the Power Save Mode here. Exit_Power_Save_Mode(); // The MCU exits the Power Save Mode here. if (cycle == 0) // TODO cycle == 7 to execute this every 60 s { /* Re-Initialize the peripherals */ Set_BME280_Pins(); Set_NRF24L01_Pins(); /* Get measurement and send it */ BME280_Get_Measurement(&sensorData); NRF24L01_Send_Message((uint8_t*)&sensorData, sizeof(sensorData)); if (temp == 0) { LED_PORT |= (1 << LED_PIN); temp = 1; } else { temp = 0; LED_PORT &= ~(1 << LED_PIN); } cycle = 0; } else { cycle ++; } } } void Initialize_SPI(void) { /* TODO: move to spi.h */ /* Set MOSI and SCK output, all others input */ SPI_DDR = (1<