Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/sleep.h>
  4. #include <util/delay.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "nrf24l01.h"
  10. #include "nrf24l01_definitions.h"
  11. #include "bme280_interface.h"
  12. #include "bme280_defs.h"
  13. /* SPI: */
  14. /* TODO: move to spi.h */
  15. #define SPI_DDR DDRB
  16. #define SPI_SCK_PIN PB5
  17. #define SPI_MOSI_PIN PB3
  18. #define SPI_MISO_PIN PB4
  19. /* Debug LED: */
  20. #define LED_DDR DDRD
  21. #define LED_PORT PORTD
  22. #define LED_PIN PD3
  23. char bool_case = 0;
  24. int timer = 0;
  25. int timer_max = 0;
  26. volatile uint8_t cycle = 0;
  27. volatile uint8_t interruptCounter;
  28. volatile uint8_t executionFlag;
  29. void Initialize_SPI(void);
  30. void Enter_Power_Save_Mode(void);
  31. void Exit_Power_Save_Mode(void);
  32. /* TODO Notes for power saving:
  33. * - Power-save-mode needed -> SM2...0 bits written to 011
  34. * - Entering by issuing the SLEEP instruction -> What call in C?
  35. * - Before executing the SLEEP instruction, write bit SE of the SMCR to 1
  36. * - Let Timer/Counter2 run with the necessary period and enable an interrupt.
  37. * -> The Global Interrupt Enable bit in SREG has to be set.
  38. * - asynchronous/synchronous clock source?
  39. * - When waking up
  40. * - Set PRR bits for needed peripherals
  41. * -
  42. */
  43. void Set_Up_Power_Save_Mode(void);
  44. void Enter_Power_Save_Mode(void);
  45. ISR( TIMER2_COMPA_vect )
  46. {
  47. /* Do nothing as the interrupt is only used to wake up the MCU. */
  48. }
  49. int main (void)
  50. {
  51. struct bme280_data sensorData;
  52. // uint8_t testRegisterContent = 0x0A;
  53. // uint8_t registerContent[5];
  54. // char registerContentString[30];
  55. // uint8_t lengthRead;
  56. uint8_t temp = 0;
  57. /* Enable the debug LED */
  58. LED_DDR |= (1 << LED_PIN);
  59. /* Initialize the SPI */
  60. Initialize_SPI();
  61. /* Initialize the nrf24l01 */
  62. Initialize_NRF24L01();
  63. // The NRF24L01 is now in the mode Standby-I.
  64. /* Configure the transmission parameters (Enhanced ShockBurst)*/
  65. Configure_Transmission();
  66. /* Initialize the BME280 */
  67. Initialize_BME280();
  68. Set_Up_Power_Save_Mode();
  69. /* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
  70. * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
  71. * and MCU are mismatched and the flashing will fail.
  72. */
  73. _delay_ms(500);
  74. while(1)
  75. {
  76. Enter_Power_Save_Mode(); // The MCU enters the Power Save Mode here.
  77. Exit_Power_Save_Mode(); // The MCU exits the Power Save Mode here.
  78. if (cycle == 0) // TODO cycle == 7 to execute this every 60 s
  79. {
  80. /* Re-Initialize the peripherals */
  81. Set_BME280_Pins();
  82. Set_NRF24L01_Pins();
  83. /* Get measurement and send it */
  84. BME280_Get_Measurement(&sensorData);
  85. NRF24L01_Send_Message((uint8_t*)&sensorData, sizeof(sensorData));
  86. if (temp == 0)
  87. {
  88. LED_PORT |= (1 << LED_PIN);
  89. temp = 1;
  90. }
  91. else
  92. {
  93. temp = 0;
  94. LED_PORT &= ~(1 << LED_PIN);
  95. }
  96. cycle = 0;
  97. }
  98. else
  99. {
  100. cycle ++;
  101. }
  102. }
  103. }
  104. void Initialize_SPI(void)
  105. {
  106. /* TODO: move to spi.h */
  107. /* Set MOSI and SCK output, all others input */
  108. SPI_DDR = (1<<SPI_MOSI_PIN)|(1<<SPI_SCK_PIN);
  109. /* Enable SPI, Master, set clock rate fck/16 */
  110. SPCR = (1<<SPE)|(1<<MSTR);
  111. }
  112. void Set_Up_Power_Save_Mode(void)
  113. {
  114. // Disable Brown-Out-Detection by setting the BODLEVEL 2:0 Fuses to 0b111 (should be default)
  115. // Disable the on-chip debug system by setting the DWEN Fuse to 1 (should be default)
  116. /* Disable some unused peripherals that are not used during operation of the weather station: */
  117. // The ADC is turned off by default
  118. ACSR &= ~(1<<ACD); // Analog comperator
  119. ACSR &= ~(1<<ACIE); // The interrupt bit has to be cleared after switchin of the analog comperator
  120. /*
  121. * The Internal voltager reference is automatically disabled if the BOD, ADC and Voltage
  122. * Reference are disabled
  123. */
  124. /* Set up Timer/Counter2 */
  125. PRR &= ~(1<<PRTIM2); // Enable the timer 2 in the Power Reduction Register
  126. TCCR2A |= (1<<COM2A1)|(1<<COM2A0)|(1<<WGM21); // Set the timer to ClearTimer on Compare Match with output compare mode for channel A
  127. /* There is a problem: The maximum time until the counter 2 overflows is 262ms at the nominal
  128. * core frquency of 1MHz and a timer prescaler of 1024.
  129. * This can be attenuated by lowering the system frequency to 31.25 kHz via the clock prescaler
  130. * (CLKPS3...0) of the register CLKPR. This gives a overflow time of 8.3s.
  131. * The cycle time of one minute can thus be accomplished by setting the output compare register
  132. * to 229. This spawns an interrupt every 7.5s which means the operation has to be executed
  133. * every 8 interrupt calls.
  134. */
  135. TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20); // Set the timer prescaler to 1/1024
  136. OCR2A = 229; // TODO: calculate this number from the wanted cycle time and the timer frequency.
  137. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  138. /* Enable global interrupts: */
  139. sei();
  140. }
  141. void Enter_Power_Save_Mode(void)
  142. {
  143. PRR |= (1<<PRTWI) | (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRSPI) | (1<<PRUSART0) | (1<<PRADC); /* Only timer 2 is needed for wake-up interrupt */
  144. /* Set the clock prescaler for a frequency of 32.25 kHz*/
  145. CLKPR = (1<<CLKPCE);
  146. CLKPR = (1<<CLKPS3);
  147. TCNT2 = 0;// Reset timer 2
  148. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  149. set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  150. sleep_mode();
  151. }
  152. void Exit_Power_Save_Mode(void)
  153. {
  154. TIMSK2 &= ~(1<<OCIE2A); // Disable the Output Compare Match A Interrupt.
  155. /* Set the normal operating frequency of 1 MHz */
  156. CLKPR = (1<<CLKPCE);
  157. CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
  158. PRR &= ~(1<<PRSPI); // Enable SPI
  159. Initialize_SPI(); // reinitalize SPI
  160. }