/* ATtiny10 Cricket - 1 MHz Version --------------------------------- Clock: 1 MHz internal PB0 -> Piezo
ATtiny10 (SOT-23-6 package) ================================= _________ | | PB0 1 |• 6| VCC GND 2 | 5| PB3 / RESET PB1 3 | 4| PB2 |_________|
Pin Functions --------------------------------- 1 PB0 - OC0A / PWM output 2 GND 3 PB1 - OC0B / ADC1 4 PB2 - ADC2 / INT0 5 PB3 - RESET / ADC3 6 VCC
Notes: - Cricket speaker on PB0 (OC0A) - 8 MHz internal oscillator - Timer0 used in CTC toggle mode*/
#define F_CPU 1000000UL
#include <avr/io.h>#include <avr/interrupt.h>#include <avr/sleep.h>#include <avr/wdt.h>
#define OCR_VALUE 14 // ~4200 Hz tone (prescaler 8) //// Not cleared on RESETvolatile unsigned char count __attribute__((section(".noinit")));const uint16_t chirpTime[]={2000,500,1000,3000,1000,2000,1000};
volatile uint8_t chirp_on = 0;volatile uint8_t chirp_count = 0;volatile uint16_t ms_count = 0;volatile uint16_t runtime_ms = 0;
void timer_start(void);void timer_stop(void);void watchdog_setup(void);
ISR(TIM0_COMPA_vect){ static uint8_t sub_ms = 0; static uint16_t phase_timer = 0;
if (chirp_on) PINB = (1 << PB0); // toggle pin
sub_ms++; //speed 4-fast 8-slow if (sub_ms >= 8) //8 // ~1 ms (8 × 119us) { sub_ms = 0; ms_count++; runtime_ms++; phase_timer++;
if (chirp_on) { //messed with sound putting to 5 if (phase_timer >= 25) //25 { chirp_on = 0; phase_timer = 0; chirp_count++; } } else { //made it a much shorter chirp at 2 instead of 4 if (chirp_count < 4) //4 { if (phase_timer >= 25) { chirp_on = 1; phase_timer = 0; } } else { //made it much faster at 200 if (phase_timer >= 500) //500 { chirp_count = 0; chirp_on = 1; phase_timer = 0; } } } }}
int main(void){ DDRB |= (1 << PB0);
// Disable ADC for lower power ADCSRA &= ~(1 << ADEN);
sei(); timer_start();
while (1) { if (runtime_ms >= chirpTime[count]) //2000=2sec, 5000=5sec chirping { timer_stop(); PORTB &= ~(1 << PB0);
runtime_ms = 0; chirp_count = 0; chirp_on = 0; count++; if(count>6){ count=0; } watchdog_setup(); set_sleep_mode(SLEEP_MODE_PWR_DOWN);
//total sleep time:(put to count 0-6) for (uint8_t i = 0; i < count; i++) // 15 = ~120 sec { sleep_enable(); sleep_cpu(); sleep_disable(); }
sei(); //added to see if it helped the second round timing. timer_start(); } }}
//Wakefrom sleepISR(WDT_vect){}
void timer_start(void){ TCCR0A = 0; TCCR0B = (1 << WGM02) | (1 << CS01); // CTC, prescaler 8 OCR0A = OCR_VALUE; TIMSK0 = (1 << OCIE0A);}
void timer_stop(void){ TCCR0B = 0; TIMSK0 = 0;}
void watchdog_setup(void){ cli();
// Clear watchdog reset flag (important on tiny10) RSTFLR |= (1 << WDRF);
// Timed sequence to change WDT CCP = 0xD8; WDTCSR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0); // 8 sec interrupt only
sei();}