Alarm Sensor Module  0.1
⇒ back to Project Page
main.c
Go to the documentation of this file.
1 /****************************************************************************/
31 /******************************************
32  \internal
33  Include files
34 *******************************************/
35 
36 #include <avr/io.h>
37 #include <stdlib.h>
38 #include <util/delay.h>
39 #include <avr/interrupt.h>
40 #include <avr/pgmspace.h>
41 #include <avr/sleep.h>
42 
43 #include <math.h>
44 #include <stdio.h>
45 
46 #include "basic_definitions.h"
47 #include "port_definitions.h"
48 #include "alarm_sensor.h"
49 #include "rfm12_io.h"
50 #include "adc.h"
51 #include "encryption.h"
52 #include "subfunctions.h"
53 #include "crc.h"
54 
55 #if LCD_DEBUG == 1
56  #include "lcd.h"
57 #endif
58 
59 
60 
61 /******************************************
62  \internal
63  Definitions
64 *******************************************/
65 
66 //Definition for Frequency Setting
67 #ifndef F_CPU
68  #warning "F_CPU was not defined! It will now be set to 20000000UL."
69  #define F_CPU 20000000UL
70 #endif
71 
72 
73 /******************************************
74  \internal
75  Global variables
76 *******************************************/
77 
78 volatile uint8_t time_scale;
79 volatile uint8_t prev_time_scale;
80 
81 
82 
83 
84 /****************************************************************************/
120 /****************************************************************************/
121 
122 int main(void) {
123 
124  static uint8_t contact_check_counter = 0u;
125  static uint8_t battery_check_counter = 0u;
126  static uint8_t life_sign_counter = 0u;
127  static uint8_t warn_cycle_counter = WARN_CYCLES;
128  static uint32_t battery_value = 0u;
129  static uint32_t unencrypted_value = 0u;
130  static uint8_t sensor1_value = 0u;
131  static uint8_t sensor2_value = 0u;
132 
133  static uint8_t warn_memory_u8 = 0u;
134 
135  static uint8_t message_counter_u8 = 0u;
136 
137  uint8_t i;
138 
139  int16_t transmission_return_value;
140  uint16_t transmit_buffer[RFM12_TXBUFFER_LENGTH];
141  uint16_t receive_buffer[RFM12_RXBUFFER_LENGTH];
142  uint32_t data_buffer[2];
143  uint32_t encrypted_data_buffer[2];
144  uint8_t checksum_buffer[4];
145  uint32_t checksum;
146 
147 
148 
149  //timer and bus-related
150 #if LCD_DEBUG == 1
151  //uint8_t timer_value_start;
152  //uint8_t max_diff_time = 0;
153  static char tmp_char[40];
154 #endif
155 
156 
157 
158  /***************************************************************************
159  \internal
160  port and pin configuration
161  ***************************************************************************/
162 
163  //enable pull-ups for all pins an set them to input port (default state)
164  PORTB = 0xff;
165  DDRB = 0x00;
166  PORTC = 0xff;
167  DDRC = 0x00;
168  PORTD = 0xff;
169  DDRD = 0x00;
170 
171  //set LED output port
172  LED_DDR |= (1 << LED_ADDR);
173  LED_PORT &= ~(1 << LED_ADDR); //switch it off
174 
175  //set battery drive output port
176  BAT_DDR |= (1 << BAT_PIN);
177  BAT_PORT |= (1 << BAT_PIN); //switch off the measurement (active low)
178 
179  //set sensor 1 drive output port
180  SEN1_DRV_DDR |= (1 << SEN1_DRV_PIN);
181  SEN1_DRV_PORT &= ~(1 << SEN1_DRV_PIN); //switch off the measurement
182 
183  //set sensor 1 drive output port
184  SEN2_DRV_DDR |= (1 << SEN2_DRV_PIN);
185  SEN2_DRV_PORT &= ~(1 << SEN2_DRV_PIN); //switch off the measurement
186 
187  //set sensor 1 input port
188  SEN1_IN_DDR |= (1 << SEN1_IN_PIN);
189  SEN1_IN_PORT &= ~(1 << SEN1_IN_PIN); //disable the pull-up
190 
191  //set sensor 2 input port
192  SEN2_IN_DDR |= (1 << SEN2_IN_PIN);
193  SEN2_IN_PORT &= ~(1 << SEN2_IN_PIN); //disable the pull-up
194 
195 
196  rfm12_pin_init();
197 
198 #if LCD_DEBUG == 1
199  //pin initialization for LCD module
200  lcd_pin_init();
201  lcd_blpin_init();
202 #endif
203 
204 
205 
206  /***************************************************************************
207  \internal
208  Initialization of sub-modules
209  ***************************************************************************/
210 
211 #if TRANSMISSION_ENABLED == 1
212  //initialize the RFM12 Wireless Module with power safe mode
214 #endif // TRANSMISSION_ENABLED
215 
216 #if LCD_DEBUG == 1
217  lcd_init();
218 #endif
219 
220 
221  /***************************************************************************
222  \internal
223  Initialization of data structures
224  ***************************************************************************/
225 
226 
227  contact_check_counter = 0u;
228  battery_check_counter = 0u;
229  life_sign_counter = 0u;
230 
231 
232  /***************************************************************************
233  \internal
234  Setup LCD debug output, if enabled
235  ***************************************************************************/
236 
237 #if LCD_DEBUG == 1
238  lcd_clear();
239  lcd_send_string("AS V0.1", FALSE);
240 #endif
241 
242  _delay_ms(1500);
243 
244 #if LCD_DEBUG == 1
245  //clear LCD
246  lcd_clear();
247 #endif
248 
249 
250  /***************************************************************************
251  \internal
252  Initialization of interrupts, first measurements and power reduction
253  ***************************************************************************/
254 
255  //startup indication
256  _delay_ms(500);
257  for (i = 1; i < ALARM_SENSOR_ID; i++) {
258  LED_PORT |= (1 << LED_ADDR);
259  _delay_ms(300);
260  LED_PORT &= ~(1 << LED_ADDR);
261  _delay_ms(300);
262  }
263 
264 
265  //set up Timer ISR for Timer2 - system clock has 1 MHz
266  TCCR2A = 0x02; //CTC mode, OC2A and OC2B ports in normal operation
267  TCCR2B = 0x07; //WGM22 not set, Prescaler: 1024
268  OCR2A = AS_OCR2A; //value for compare match (79 = 10 ms for 8 MHz, 156 = 10 ms for 16 MHz clock, 195 = 10 ms for 20 MHz clock)
269  TIMSK2 = TIMSK2 | (1 << OCIE2A); //enable interrupt for Timer2 A
270  time_scale = 0; //reset time scale (basis for some operations) [*10 ms]
271  prev_time_scale = 0; //previous time scale to detect changes
272 
273 
274  //activate global Interrupts
275  sei();
276 
277 
278  LED_PORT |= (1 << LED_ADDR);
279 
280 
281  //get the battery value for the first time
282  adc_pin_init();
283  battery_value = single_adc_read(ADC_REF_VREF, 1100, 5, 200);
284  adc_pin_deinit();
285 
286 
287  //enable power safe modes
288  PRR |= (1 << PRTWI) | (1 << PRTIM0) | (1 << PRTIM1) | (1 << PRSPI) | (1 << PRUSART0) | (1 << PRADC);
289 
290 
291  /***************************************************************************
292  \internal
293  Main Loop
294  ***************************************************************************/
295 
296  while (1) {
297 
298  if (prev_time_scale != time_scale) {
299 
300 
301 #if LCD_DEBUG == 1
302  //get timer value to calculate computing time
303  //timer_value_start = TCNT2;
304 #endif
305 
306 
307  /***************************************************************************
308  \internal
309  If timer elapsed - get the battery voltage
310  ADC module is automatically enabled and disabled by the function
311  single_adc_read()
312  ***************************************************************************/
313 
314  if (battery_check_counter < BATTERY_CHECK_COUNTER) {
315  battery_check_counter++;
316  } else {
317 
318  //LED_PORT |= (1 << LED_ADDR);
319 
320  //switch on battery drive
321  BAT_PORT &= ~(1 << BAT_PIN);
322  _delay_ms(20);
323 
324 
325  PRR &= ~(1 << PRADC); // remove ADC flag from power reduction register
326  adc_pin_init();
327 
328  //do the battery check
329  battery_value = single_adc_read(ADC_REF_VREF, 1100, 5, 200);
330 
331  //get voltage with respect to the resistors
332  battery_value = calculate_battery_voltage(battery_value);
333 
334  adc_pin_deinit();
335  PRR |= (1 << PRADC); // add ADC flag to power reduction register
336 
337  //switch off battery drive
338  BAT_PORT |= (1 << BAT_PIN);
339 
340 
341  //start new cycle for battery check
342  battery_check_counter = 0;
343 
344 
345 #if LCD_DEBUG == 1 && LCD_BAT_DEBUG == 1
346  //print battery value
347  lcd_clear();
348  lcd_set_cursor(0, 0);
349  lcd_send_string("BAT:", FALSE);
350  lcd_set_cursor(0, 1);
351  itoa(battery_value, tmp_char, 10);
352  lcd_send_string(tmp_char, FALSE);
353 #endif
354 
355  //LED_PORT &= ~(1 << LED_ADDR);
356 
357  }
358 
359 
360 
361  /***************************************************************************
362  \internal
363  Check the sensor input values if counter elapsed
364  ***************************************************************************/
365 
366  if (contact_check_counter < CONTACT_CHECK_COUNTER) {
367  contact_check_counter++;
368  } else {
369 
370  //LED_PORT |= (1 << LED_ADDR);
371 
372  // check and send alarm message to main station if REED sensor is open
373 #if ALARM_SENSOR_INPUT1_ENABLED == 1
374  SEN1_IN_PORT |= (1 << SEN1_IN_PIN); //enable pull-up
375  SEN1_DRV_PORT |= (1 << SEN1_DRV_PIN); //switch on the measurement
376  NOP();
377  NOP();
378  sensor1_value = (uint8_t) ((SEN1_IN_INPORT >> SEN1_IN_PIN) & 0x01u); //will be 0 if contact is closed, otherwise 1
379  SEN1_DRV_PORT &= ~(1 << SEN1_DRV_PIN); //switch off the measurement
380  SEN1_IN_PORT &= ~(1 << SEN1_IN_PIN); //disable the pull-up
381 
382  //update corresponding warning memory for contact 1
383  if (sensor1_value > 0u) {
384  warn_memory_u8 = warn_memory_u8 | 0x01;
385  }
386 
387 #else
388  sensor1_value = 0u;
389 #endif // ALARM_SENSOR_INPUT1_ENABLED
390 
391 #if ALARM_SENSOR_INPUT2_ENABLED == 1
392  SEN2_IN_PORT |= (1 << SEN2_IN_PIN); //enable pull-up
393  SEN2_DRV_PORT |= (1 << SEN2_DRV_PIN); //switch on the measurement
394  NOP();
395  NOP();
396  sensor2_value = (uint8_t) ((SEN2_IN_INPORT >> SEN2_IN_PIN) & 0x01u); //will be 0 if contact is closed, otherwise 1
397  SEN2_DRV_PORT &= ~(1 << SEN2_DRV_PIN); //switch off the measurement
398  SEN2_IN_PORT &= ~(1 << SEN2_IN_PIN); //disable pull-up
399 
400  //update corresponding warning memory for contact 2
401  if (sensor2_value > 0u) {
402  warn_memory_u8 = warn_memory_u8 | 0x02;
403  }
404 #else
405  sensor2_value = 0u;
406 #endif // ALARM_SENSOR_INPUT2_ENABLED
407 
408  contact_check_counter = 0u;
409 
410 #if LCD_DEBUG == 1 && LCD_SEN_DEBUG == 1
411  //print sensor values
412 // lcd_clear();
413 // lcd_set_cursor(0, 0);
414 // lcd_send_string("SEN:", FALSE);
415 // lcd_set_cursor(0, 1);
416 // itoa((sensor1_value | (sensor2_value << 1)), tmp_char, 10);
417 // lcd_send_string(tmp_char, FALSE);
418 // lcd_set_cursor(3, 1);
419 // itoa(sensor2_value, tmp_char, 10);
420 // lcd_send_string(tmp_char, FALSE);
421 #endif
422  //LED_PORT &= ~(1 << LED_ADDR);
423  }
424 
425 
426 
427  /***************************************************************************
428  \internal
429  Send an alarm message if sensors report an opened contact for WARN_CYCLES
430  number of cycles - return after that to the normal life sign update rate
431  ***************************************************************************/
432 
433  if (((sensor1_value > 0u) || (sensor2_value > 0u)) && warn_cycle_counter > 0) {
434 
435  LED_PORT |= (1 << LED_ADDR);
436 
437  // message format: BYTE 0 || BYTE 1 || Byte 2 || Byte 3
438  // ID || Message Counter || Battery Voltage[0...7] || Sensor 2 Input | Sensor 1 Input | Enable Flag | Battery Voltage [12...8] ||
439  // ID: 0...255
440  // Message Counter: 0...255
441  // Battery Voltage: 0...8192 mV
442  // Sensor 1 Input: 0...1
443  // Sensor 2 Input: 0...1
444  // Enable Flag: 0...1
445 
446  //prepare unencrypted data
447  unencrypted_value = (uint32_t) ALARM_SENSOR_ID & (uint32_t) 0x000000ff;
448  unencrypted_value = unencrypted_value | (((uint32_t) message_counter_u8 << 8u) & (uint32_t) 0x0000ff00);
449  unencrypted_value = unencrypted_value | (((uint32_t) battery_value << 16u) & (uint32_t) 0x1fff0000);
450  unencrypted_value = unencrypted_value | (((uint32_t) ALARM_SENSOR_ENABLED << 29u) & (uint32_t) 0x20000000);
451  unencrypted_value = unencrypted_value | (((uint32_t) sensor1_value << 30u) & (uint32_t) 0x40000000);
452  unencrypted_value = unencrypted_value | (((uint32_t) sensor2_value << 31u) & (uint32_t) 0x80000000);
453 
454  //prepare unencrypted checksum
455  checksum_buffer[0] = (uint8_t) (unencrypted_value & 0xff);
456  checksum_buffer[1] = (uint8_t) ((unencrypted_value >> 8) & 0xff);
457  checksum_buffer[2] = (uint8_t) ((unencrypted_value >> 16) & 0xff);
458  checksum_buffer[3] = (uint8_t) ((unencrypted_value >> 24) & 0xff);
459 
460  checksum = calculate_crc32(&checksum_buffer[0], 1, CRC_INITIALIZATION);
461  checksum = calculate_crc32(&checksum_buffer[1], 1, CRC_UPDATE);
462  checksum = calculate_crc32(&checksum_buffer[2], 1, CRC_UPDATE);
463  checksum = calculate_crc32(&checksum_buffer[3], 1, CRC_FINALIZATION);
464 
465  data_buffer[0] = checksum;
466  data_buffer[1] = unencrypted_value;
467 
468  sym_encrypt_buffer(data_buffer, encrypted_data_buffer, 2);
469 
470 
471  PRR &= ~(1 << PRSPI); // remove SPI flag from power reduction register
472 
473  //send message via RFM12 Wireless Module
474  transmit_buffer[0] = 0xb8aa; //sync byte
475  transmit_buffer[1] = 0xb8aa; //sync byte
476  transmit_buffer[2] = 0xb8aa; //sync byte
477  transmit_buffer[3] = 0xb8aa; //sync byte
478  transmit_buffer[4] = 0xb82d; //sync pattern
479  transmit_buffer[5] = 0xb8d4; //sync pattern
480  transmit_buffer[6] = 0xb800 | ALAM_SENSOR_PROT; //identifies the message as an alarm sensor message
481  transmit_buffer[7] = 0xb800 | ALARM_SENSOR_ID; //ID of alarm sensor, used for decryption of message
482  transmit_buffer[8] = 0xb800 | ((encrypted_data_buffer[1] >> 24) & 0xff);
483  transmit_buffer[9] = 0xb800 | ((encrypted_data_buffer[1] >> 16) & 0xff);
484  transmit_buffer[10] = 0xb800 | ((encrypted_data_buffer[1] >> 8) & 0xff);
485  transmit_buffer[11] = 0xb800 | (encrypted_data_buffer[1] & 0xff);
486  transmit_buffer[12] = 0xb800 | ((encrypted_data_buffer[0] >> 24) & 0xff);
487  transmit_buffer[13] = 0xb800 | ((encrypted_data_buffer[0] >> 16) & 0xff);
488  transmit_buffer[14] = 0xb800 | ((encrypted_data_buffer[0] >> 8) & 0xff);
489  transmit_buffer[15] = 0xb800 | (encrypted_data_buffer[0] & 0xff);
490  transmit_buffer[16] = 0xb8aa; //sync byte
491  transmit_buffer[17] = 0xb8aa; //sync byte
492 #if TRANSMISSION_ENABLED == 1
493  transmission_return_value = rfm12_data_io(transmit_buffer, receive_buffer, RFM12_TXBUFFER_LENGTH, RFM12_TX_DATA, RFM12_POWER_SAFE_ENABLED);
494 #endif // TRANSMISSION_ENABLED
495 
496  message_counter_u8++;
497 
498  PRR |= (1 << PRSPI); // add SPI flag to power reduction register
499 
500 #if LCD_DEBUG == 1 && LCD_CON_DEBUG == 1
501  //print battery value
502  lcd_send_string("ALARM:", FALSE);
503  lcd_set_cursor(0, 1);
504  itoa(transmission_return_value, tmp_char, 10);
505  lcd_send_string(tmp_char, FALSE);
506 #endif
507 
508  warn_cycle_counter--;
509 
510  LED_PORT &= ~(1 << LED_ADDR);
511 
512  //sensor contacts are closed
513  } else if ((sensor1_value == 0u) && (sensor2_value == 0u)) { // if ((sensor1_value > 0u) || (sensor2_value > 0u)) {
514 
515  warn_cycle_counter = WARN_CYCLES; //reset number of warn cycles in case of closed sensors
516 
517  }
518 
519 
520 
521  /***************************************************************************
522  \internal
523  Send status message if transmission cycle is reached
524  ***************************************************************************/
525 
526  if (life_sign_counter < LIFE_SIGN_COUNTER) {
527  life_sign_counter++;
528  } else {
529 
530  LED_PORT |= (1 << LED_ADDR);
531 
532  // message format: BYTE 0 || BYTE 1 || Byte 2 || Byte 3
533  // ID || Message Counter || Battery Voltage[0...7] || Sensor 2 Input | Sensor 1 Input | Enable Flag | Battery Voltage [12...8] ||
534  // ID: 0...255
535  // Message Counter: 0...255
536  // Battery Voltage: 0...8192 mV
537  // Sensor 1 Input: 0...1
538  // Sensor 2 Input: 0...1
539  // Enable Flag: 0...1
540 
541  //prepare unencrypted data
542  unencrypted_value = (uint32_t) ALARM_SENSOR_ID & (uint32_t) 0x000000ff;
543  unencrypted_value = unencrypted_value | (((uint32_t) message_counter_u8 << 8u) & (uint32_t) 0x0000ff00);
544  unencrypted_value = unencrypted_value | (((uint32_t) battery_value << 16u) & (uint32_t) 0x1fff0000);
545  unencrypted_value = unencrypted_value | (((uint32_t) ALARM_SENSOR_ENABLED << 29u) & (uint32_t) 0x20000000);
546  unencrypted_value = unencrypted_value | (((uint32_t) sensor1_value << 30u) & (uint32_t) 0x40000000);
547  unencrypted_value = unencrypted_value | (((uint32_t) sensor2_value << 31u) & (uint32_t) 0x80000000);
548 
549  //prepare unencrypted checksum
550  checksum_buffer[0] = (uint8_t) (unencrypted_value & 0xff);
551  checksum_buffer[1] = (uint8_t) ((unencrypted_value >> 8) & 0xff);
552  checksum_buffer[2] = (uint8_t) ((unencrypted_value >> 16) & 0xff);
553  checksum_buffer[3] = (uint8_t) ((unencrypted_value >> 24) & 0xff);
554 
555  checksum = calculate_crc32(&checksum_buffer[0], 1, CRC_INITIALIZATION);
556  checksum = calculate_crc32(&checksum_buffer[1], 1, CRC_UPDATE);
557  checksum = calculate_crc32(&checksum_buffer[2], 1, CRC_UPDATE);
558  checksum = calculate_crc32(&checksum_buffer[3], 1, CRC_FINALIZATION);
559 
560  data_buffer[0] = checksum;
561  data_buffer[1] = unencrypted_value;
562 
563  sym_encrypt_buffer(data_buffer, encrypted_data_buffer, 2);
564 
565 
566  PRR &= ~(1 << PRSPI); // remove SPI flag from power reduction register
567 
568  //send message via RFM12 Wireless Module
569  transmit_buffer[0] = 0xb8aa; //sync byte
570  transmit_buffer[1] = 0xb8aa; //sync byte
571  transmit_buffer[2] = 0xb8aa; //sync byte
572  transmit_buffer[3] = 0xb8aa; //sync byte
573  transmit_buffer[4] = 0xb82d; //sync pattern
574  transmit_buffer[5] = 0xb8d4; //sync pattern
575  transmit_buffer[6] = 0xb800 | ALAM_SENSOR_PROT; //identifies the message as an alarm sensor message
576  transmit_buffer[7] = 0xb800 | ALARM_SENSOR_ID; //ID of alarm sensor, used for decryption of message
577  transmit_buffer[8] = 0xb800 | ((encrypted_data_buffer[1] >> 24) & 0xff);
578  transmit_buffer[9] = 0xb800 | ((encrypted_data_buffer[1] >> 16) & 0xff);
579  transmit_buffer[10] = 0xb800 | ((encrypted_data_buffer[1] >> 8) & 0xff);
580  transmit_buffer[11] = 0xb800 | (encrypted_data_buffer[1] & 0xff);
581  transmit_buffer[12] = 0xb800 | ((encrypted_data_buffer[0] >> 24) & 0xff);
582  transmit_buffer[13] = 0xb800 | ((encrypted_data_buffer[0] >> 16) & 0xff);
583  transmit_buffer[14] = 0xb800 | ((encrypted_data_buffer[0] >> 8) & 0xff);
584  transmit_buffer[15] = 0xb800 | (encrypted_data_buffer[0] & 0xff);
585  transmit_buffer[16] = 0xb8aa; //sync byte
586  transmit_buffer[17] = 0xb8aa; //sync byte
587 #if TRANSMISSION_ENABLED == 1
588  transmission_return_value = rfm12_data_io(transmit_buffer, receive_buffer, RFM12_TXBUFFER_LENGTH, RFM12_TX_DATA, RFM12_POWER_SAFE_ENABLED);
589 #endif // TRANSMISSION_ENABLED
590 
591  message_counter_u8++;
592 
593  PRR |= (1 << PRSPI); // add SPI flag to power reduction register
594 
595  life_sign_counter = 0u;
596 
597 #if LCD_DEBUG == 1 && LCD_CON_DEBUG == 1
598  //print battery value
599  lcd_clear();
600  lcd_set_cursor(0, 0);
601 
602  lcd_clear();
603  lcd_set_cursor(0, 0);
604  sprintf(tmp_char, "%04x%04x", (uint16_t) ((data_buffer[1] >> 16) & 0xffff), (uint16_t) (data_buffer[1] & 0xffff));
605  lcd_send_string(tmp_char, FALSE);
606  lcd_set_cursor(0, 1);
607  sprintf(tmp_char, "%04x%04x", (uint16_t) ((encrypted_data_buffer[1] >> 16) & 0xffff), (uint16_t) (encrypted_data_buffer[1] & 0xffff));
608  lcd_send_string(tmp_char, FALSE);
609 
610 // lcd_send_string("LIFE:", FALSE);
611 // lcd_set_cursor(0, 1);
612 // itoa(receive_buffer[0], tmp_char, 10);
613 // lcd_send_string(tmp_char, FALSE);
614 #endif
615  LED_PORT &= ~(1 << LED_ADDR);
616  }
617 
618 
619  /***************************************************************************
620  \internal
621  Indicate warnings from the past
622  ***************************************************************************/
623 
624  if ((life_sign_counter == 1) && ((warn_memory_u8 & 0x01u) == 0x01u)) {
625  LED_PORT |= (1 << LED_ADDR);
626  NOP();
627  NOP();
628  NOP();
629  NOP();
630  }
631 
632  if ((life_sign_counter == 3) && ((warn_memory_u8 & 0x02u) == 0x02u)) {
633  LED_PORT |= (1 << LED_ADDR);
634  NOP();
635  NOP();
636  NOP();
637  NOP();
638  }
639 
640  if ((life_sign_counter == 5) && ((warn_memory_u8 & 0x03u) == 0x03u)) {
641  LED_PORT |= (1 << LED_ADDR);
642  NOP();
643  NOP();
644  NOP();
645  NOP();
646  }
647 
648 
649 
650  /***************************************************************************
651  \internal
652  End of cycle: Switch off all and go to sleep
653  ***************************************************************************/
654 
655  //save timescale value
657 
658 
660 
661  LED_PORT &= ~(1 << LED_ADDR);
662 
663 
665  PRR |= (1 << PRTWI) | (1 << PRTIM0) | (1 << PRTIM1) | (1 << PRSPI) | (1 << PRUSART0) | (1 << PRADC);
666  SMCR = POWER_SAFE_MODE;
667  sleep_mode();
668 
669 
670 
671 
672  } //if (prev_time_scale != time_scale) {...
673 
674 
675  } //while(1)
676 
677 
678 }
679 
680 
#define BAT_PORT
#define ALARM_SENSOR_ID
sensor related identification and configuration data (TO BE SET FOR EACH SENSOR SEPARATELY!) each ala...
Definition: alarm_sensor.h:77
#define BAT_DDR
#define SEN2_IN_DDR
volatile uint8_t time_scale
counter triggered by timer
Definition: main.c:78
#define ALARM_SENSOR_ENABLED
ENABLE flag of current sensor –> to be adapted for each sensor!
Definition: alarm_sensor.h:80
#define NOP()
void lcd_blpin_init(void)
Definition: lcd.c:698
uint16_t rfm12_init(uint8_t)
Definition: rfm12_io.c:538
Contains all the basic definitions for several projects.
#define ALAM_SENSOR_PROT
alarm sensor protocol identifier
Definition: alarm_sensor.h:82
#define SEN2_DRV_PORT
#define WARN_CYCLES
number of warn cycles until the sensor falls back to the normal life sign rate
Definition: alarm_sensor.h:89
uint32_t single_adc_read(uint8_t vref_reg, uint16_t vref_val, uint16_t number_cycles, uint16_t min_freq)
Definition: adc.c:74
void rfm12_pin_init(void)
Definition: rfm12_io.c:679
uint8_t sym_encrypt_buffer(uint32_t *, uint32_t *, uint8_t)
Definition: encryption.c:195
#define SEN1_IN_INPORT
#define RFM12_TX_DATA
Definition: rfm12_io.h:65
#define CRC_UPDATE
do only an updating and return the current crc value
Definition: crc.h:53
Contains CRC calculation functions.
void adc_pin_init(void)
Definition: adc.c:194
#define SEN2_DRV_DDR
#define BATTERY_CHECK_COUNTER
counter (related to TIME_BASE_MS) for battery checking (==> 10 s)
Definition: alarm_sensor.h:101
#define LED_PORT
#define SEN2_IN_PORT
#define SEN2_DRV_PIN
#define POWER_SAFE_MODE
SM0 and SM1 activated, SE set to 1 –> power safe mode.
Definition: alarm_sensor.h:104
int main(void)
Definition: main.c:122
#define ADC_REF_VREF
Definition: adc.h:54
#define SEN1_IN_PIN
void lcd_clear(void)
Definition: lcd.c:502
void adc_pin_deinit(void)
Definition: adc.c:222
#define SEN1_DRV_PIN
#define LIFE_SIGN_COUNTER
counter (related to TIME_BASE_MS) for life sign to main unit (==> 20 s + (ID * 0.25 s)) ...
Definition: alarm_sensor.h:102
#define CRC_FINALIZATION
do only the finalization and return the crc value
Definition: crc.h:54
#define SEN2_IN_INPORT
#define CRC_INITIALIZATION
do only the initialization and return the init value
Definition: crc.h:52
#define LED_DDR
void lcd_send_string(const char *data, uint8_t flash_mem)
Definition: lcd.c:463
void lcd_pin_init(void)
Definition: lcd.c:663
void lcd_init(void)
Definition: lcd.c:551
#define LED_ADDR
#define SEN2_IN_PIN
#define FALSE
value for FALSE
Definition: alarm_sensor.h:64
uint32_t calculate_crc32(uint8_t *, uint8_t, uint8_t)
Definition: crc.c:70
4 Bit Interface to LCD displays with up to 4 rows and a single Controller (e.g. ST7066U or HD44780)...
#define BAT_PIN
#define RFM12_POWER_SAFE_ENABLED
enables or disables the power safe mode of the RFM12(B) module
Definition: alarm_sensor.h:106
#define CONTACT_CHECK_COUNTER
counter (related to TIME_BASE_MS) for contact checking (==> 1 s)
Definition: alarm_sensor.h:100
void lcd_set_cursor(uint8_t column, uint8_t row)
Definition: lcd.c:379
#define RFM12_RXBUFFER_LENGTH
Definition: rfm12_io.h:46
volatile uint8_t prev_time_scale
previous counter value (triggered by timer)
Definition: main.c:79
#define SEN1_DRV_DDR
#define RFM12_TXBUFFER_LENGTH
Definition: rfm12_io.h:45
uint32_t calculate_battery_voltage(uint32_t)
Definition: subfunctions.c:58
#define SEN1_IN_DDR
Contains the main definitions of the alarm sensor module.
int16_t rfm12_data_io(volatile uint16_t *, volatile uint16_t *, uint8_t, uint8_t, uint8_t)
Definition: rfm12_io.c:78
#define SEN1_IN_PORT
#define AS_OCR2A
value for compare match (244 = 250 ms for 1 MHz clock)
Definition: alarm_sensor.h:97
#define SEN1_DRV_PORT