Advertisement
BsB5068

ktimer.c

Oct 10th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. /* 14 august 2014
  2. * Oleg Terentiev t.oleg@ymail.com
  3. * *------*
  4. * rst -| |- V+
  5. * PB3 -| |- PB2
  6. * PB4 -| |- PB1 plus 1 minute
  7. * gnd -| |- PB0 plus 5 minutes
  8. * *------*
  9. * PB3, PB4 - both connect to buzzer
  10. * PB[0,1] - buttons-to-GND
  11. */
  12.  
  13. #include <avr/io.h>
  14. #include <avr/interrupt.h>
  15. #include <util/delay.h>
  16. #include <avr/sleep.h>
  17. #include <avr/wdt.h>
  18.  
  19. FUSES =
  20. {
  21. .low = LFUSE_DEFAULT,
  22. .high = HFUSE_DEFAULT
  23. }; /* hfuse=0xff, lfuse=0x6A */
  24.  
  25. #define T1MIN (60*2)
  26. #define T5MIN (T1MIN*5)
  27.  
  28. #define PushTime (4*2+4*2)
  29.  
  30. #define NOPRESS 0b11
  31. #define T1PRESS 0b01
  32. #define T5PRESS 0b10
  33. #define ALLPRESS 0b00
  34. #define FALSE 0
  35. #define TRUE (!FALSE)
  36.  
  37. int16_t time __attribute__ ((section (".noinit")));
  38.  
  39. int16_t timestored __attribute__ ((section (".noinit")));
  40.  
  41. uint8_t buttons __attribute__ ((section (".noinit")));
  42.  
  43. void beep_low (uint8_t time);
  44. void beep_mid (uint8_t time);
  45. void beep_high(uint8_t time);
  46.  
  47. int main (void)
  48. {
  49. uint8_t tmp;
  50. ACSR |= 1<<ACD; // disable comparator
  51. cli();
  52. /* ADCSRA &= ~(1<<ADEN); // default by reset */
  53. wdt_enable(WDTO_500MS);
  54.  
  55. if ( bit_is_set(MCUSR, WDRF)) { // reset by WDT
  56. // tune I/O
  57. DDRB = 0b00011100;
  58. PORTB = 0b00000111;
  59. asm("nop");
  60.  
  61. buttons = PINB & 0x03;
  62. switch (buttons) {
  63.  
  64. case ALLPRESS: // long beep, reset time
  65. time = -PushTime;
  66. timestored = -PushTime;
  67. beep_low(250);
  68. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  69. sleep_enable();
  70. sleep_cpu();
  71. break;
  72.  
  73. case T1PRESS: // beep, add time, sleep
  74. timestored = timestored + T1MIN;
  75. time = timestored;
  76. beep_mid(100);
  77. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  78. sleep_enable();
  79. sleep_cpu();
  80. break;
  81.  
  82. case T5PRESS: // beep, add time, sleep
  83. timestored = timestored + T5MIN;
  84. time = timestored;
  85. beep_high(150);
  86. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  87. sleep_enable();
  88. sleep_cpu();
  89. break;
  90.  
  91. case NOPRESS:
  92. if (time > 0) {
  93. time--;
  94. } else if ( time == 0 ) { // it is TIME! beep for 1 minute!
  95. //wdt_disable(); // no working!
  96.  
  97. wdt_enable(WDTO_2S);
  98.  
  99. tmp = 1; /* seconds */
  100. while ( tmp > 0 ){
  101. PORTB = (0<<PB2);
  102. // _delay_ms(250);
  103. // beep_high(250);
  104. _delay_ms(1000);
  105. wdt_reset();
  106. PORTB = (1<<PB2);
  107. _delay_ms(1500);
  108. wdt_reset();
  109. PORTB = (0<<PB2);
  110. _delay_ms(1000);
  111. PORTB = (1<<PB2);
  112. wdt_reset();
  113. // _delay_ms(250);
  114. // beep_low(250);
  115. wdt_reset();
  116. tmp--;
  117. }
  118.  
  119. time = timestored;
  120. wdt_enable(WDTO_500MS);
  121. }
  122. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  123. sleep_enable();
  124. sleep_cpu();
  125. break;
  126. default:
  127. beep_high(200);
  128. beep_high(200);
  129. beep_high(200);
  130. break;
  131. }
  132.  
  133. } else { // no WDT reset
  134. // first ON, no calibration
  135. time = -PushTime;
  136. timestored = -PushTime;
  137. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  138. sleep_enable();
  139. sleep_cpu();
  140. }
  141. }
  142.  
  143. void beep_low(uint8_t time) /* in milliseconds. 500 Hz */
  144. {
  145. uint8_t tmp;
  146. tmp = time>>2;
  147. while ( --tmp > 0){
  148. PORTB |= (1<<PB3);
  149. PORTB &= ~(1<<PB4);
  150. _delay_us(2000);
  151. PORTB &= ~(1<<PB3);
  152. PORTB |= (1<<PB4);
  153. _delay_us(2000);
  154. }
  155. }
  156.  
  157. void beep_mid(uint8_t time) /* in milliseconds. 1 kHz */
  158. {
  159. uint8_t tmp;
  160. tmp = time>>1;
  161. while ( --tmp > 0){
  162. PORTB |= (1<<PB3);
  163. PORTB &= ~(1<<PB4);
  164. _delay_us(1000);
  165. PORTB &= ~(1<<PB3);
  166. PORTB |= (1<<PB4);
  167. _delay_us(1000);
  168. }
  169. }
  170.  
  171. void beep_high(uint8_t time) /* in milliseconds. 2 kHz */
  172. {
  173. uint8_t tmp;
  174. tmp = time;
  175. while ( --tmp > 0){
  176. PORTB |= (1<<PB3);
  177. PORTB &= ~(1<<PB4);
  178. _delay_us(500);
  179. PORTB &= ~(1<<PB3);
  180. PORTB |= (1<<PB4);
  181. _delay_us(500);
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement