Skip to content

Instantly share code, notes, and snippets.

@monpetit
Created June 29, 2016 04:23
Show Gist options
  • Save monpetit/d62b823bb24fd87e780bcf607b43bc6b to your computer and use it in GitHub Desktop.
Save monpetit/d62b823bb24fd87e780bcf607b43bc6b to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
// Routines to set and claer bits (used in the sleep code)
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
const int led = 0;
volatile bool timer1_trigger = 0;
uint32_t timer1_count = 0;
void reset_blink(volatile uint8_t count)
{
pinMode(led, OUTPUT);
while (count--) {
digitalWrite(led, digitalRead(led) ^ 1);
delay(20);
}
digitalWrite(led, LOW);
delay(1000);
}
void enter_sleep(void)
{
cbi(ADCSRA, ADEN); // switch Analog to Digitalconverter OFF
/* Disable all peripherals but TIMER1. */
PRR = (1 << PRTWI) | (1 << PRUSART1) | (1 << PRUSART0) | (1 << PRSPI) | (1 << PRTIM2) | (0 << PRTIM1) | (1 << PRTIM0) | (1 << PRADC);
/* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
sbi(ADCSRA, ADEN); // switch Analog to Digitalconverter ON
}
void setup_timer1(void)
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: CTC top=OCR1A
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 2 s
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0) | (0 << WGM11) | (0 << WGM10);
TCCR1B = (0 << ICNC1) | (0 << ICES1) | (0 << WGM13) | (1 << WGM12) | (1 << CS12) | (0 << CS11) | (0 << CS10);
TCNT1H = 0x00;
TCNT1L = 0x00;
ICR1H = 0x00;
ICR1L = 0x00;
OCR1AH = 0xF4;
OCR1AL = 0x23;
OCR1BH = 0x00;
OCR1BL = 0x00;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1 = (0 << ICIE1) | (0 << OCIE1B) | (1 << OCIE1A) | (0 << TOIE1);
sei();
}
void setup()
{
Serial.begin(38400);
reset_blink(30);
Serial.println("-------------");
Serial.println("--- START ---");
Serial.println("-------------");
delay(500);
setup_timer1();
}
void loop()
{
if (timer1_trigger) {
timer1_trigger = 0;
digitalWrite(led, HIGH);
delay(20);
digitalWrite(led, LOW);
uint16_t adc_result = analogRead(A0);
Serial.print("[ADC0] = ");
Serial.println(adc_result);
delay(2);
}
if (timer1_count > 7)
enter_sleep();
}
ISR(TIMER1_COMPA_vect)
{
timer1_trigger = 1;
timer1_count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment