Advertisement
BsB5068

Untitled

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