Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 14 august 2014
- * Oleg Terentiev t.oleg@ymail.com
- * *------*
- * rst -| |- V+
- * PB3 -| |- PB2
- * PB4 -| |- PB1 plus 1 minute
- * gnd -| |- PB0 plus 5 minutes
- * *------*
- * PB3, PB4 - both connect to buzzer
- * PB[0,1] - buttons-to-GND
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <avr/sleep.h>
- #include <avr/wdt.h>
- FUSES =
- {
- .low = LFUSE_DEFAULT,
- .high = HFUSE_DEFAULT
- }; /* hfuse=0xff, lfuse=0x6A */
- #define T1MIN (10*2)
- #define T5MIN (T1MIN*2)
- #define NOPRESS 0b11
- #define T1PRESS 0b01
- #define T5PRESS 0b10
- #define ALLPRESS 0b00
- #define FALSE 0
- #define TRUE (!FALSE)
- int16_t time __attribute__ ((section (".noinit")));
- int16_t timestored __attribute__ ((section (".noinit")));
- uint8_t buttons __attribute__ ((section (".noinit")));
- void beep_low (uint8_t time);
- void beep_mid (uint8_t time);
- void beep_high(uint8_t time);
- int main (void)
- {
- uint8_t tmp;
- ACSR |= 1<<ACD; // disable comparator
- cli();
- /* ADCSRA &= ~(1<<ADEN); // default by reset */
- wdt_enable(WDTO_500MS);
- if ( bit_is_set(MCUSR, WDRF)) { // reset by WDT
- // tune I/O
- DDRB = 0b00011100;
- PORTB = 0b00000111;
- asm("nop");
- buttons = PINB & 0x03;
- switch (buttons) {
- case ALLPRESS: // long beep, reset time
- time = -1;
- timestored = -1;
- beep_low(400);
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_cpu();
- break;
- case T1PRESS: // beep, add time, sleep
- timestored = timestored + T1MIN;
- time = timestored;
- beep_mid(100);
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_cpu();
- break;
- case T5PRESS: // beep, add time, sleep
- timestored = timestored + T5MIN;
- time = timestored;
- beep_high(200);
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_cpu();
- break;
- case NOPRESS:
- if (time > 0) {
- time--;
- } else if ( time == 0 ) { // it is TIME! beep for 1 minute!
- //wdt_disable(); // no working!
- wdt_enable(WDTO_2S);
- tmp = 1; /* seconds */
- while ( tmp > 0 ){
- PORTB = (0<<PB2);
- beep_high(250);
- _delay_ms(1000);
- wdt_reset();
- PORTB = (1<<PB2);
- _delay_ms(1500);
- wdt_reset();
- PORTB = (0<<PB2);
- _delay_ms(1000);
- PORTB = (1<<PB2);
- wdt_reset();
- beep_low(250);
- wdt_reset();
- tmp--;
- }
- time = timestored;
- wdt_enable(WDTO_500MS);
- }
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_cpu();
- break;
- default:
- beep_high(200);
- beep_high(200);
- beep_high(200);
- break;
- }
- } else { // no WDT reset
- // first ON, no calibration
- time = -1;
- timestored = -1;
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_cpu();
- }
- }
- void beep_low(uint8_t time) /* in milliseconds. 500 Hz */
- {
- uint8_t tmp;
- tmp = time>>2;
- while ( --tmp > 0){
- PORTB |= (1<<PB3);
- PORTB &= ~(1<<PB4);
- _delay_us(2000);
- PORTB &= ~(1<<PB3);
- PORTB |= (1<<PB4);
- _delay_us(2000);
- }
- }
- void beep_mid(uint8_t time) /* in milliseconds. 1 kHz */
- {
- uint8_t tmp;
- tmp = time>>1;
- while ( --tmp > 0){
- PORTB |= (1<<PB3);
- PORTB &= ~(1<<PB4);
- _delay_us(1000);
- PORTB &= ~(1<<PB3);
- PORTB |= (1<<PB4);
- _delay_us(1000);
- }
- }
- void beep_high(uint8_t time) /* in milliseconds. 2 kHz */
- {
- uint8_t tmp;
- tmp = time;
- while ( --tmp > 0){
- PORTB |= (1<<PB3);
- PORTB &= ~(1<<PB4);
- _delay_us(500);
- PORTB &= ~(1<<PB3);
- PORTB |= (1<<PB4);
- _delay_us(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement