Skip to content

Instantly share code, notes, and snippets.

@ivoras
Last active April 21, 2017 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivoras/8038f91e11e6f65c511605da661a1ad0 to your computer and use it in GitHub Desktop.
Save ivoras/8038f91e11e6f65c511605da661a1ad0 to your computer and use it in GitHub Desktop.
/*
* Tiny13Blinky.c
*
* Created: 21.04.2017. 11:21:09
* Author : ivoras
*/
#define F_CPU 1000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
ISR(WDT_vect) {
}
unsigned int measure_adc() {
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADSC); // Enable ADC, at 1/8 prescaler, enable conversion
while (ADCSRA & (1 << ADSC)) {}
unsigned int lsbyte = ADCL; // read the low byte first
unsigned int msbyte = ADCH;
ADCSRA = 0;
return lsbyte | (msbyte << 8);
}
int main(void)
{
DDRB = 1 << DDB4;
ADMUX = (1 << REFS0) | (1 << MUX1) | (1 << MUX0); // Internal Vref, ADC3
// prescale timer to 0.5s
WDTCR |= (1<<WDP2) | (1<<WDP0);
// Enable watchdog timer interrupts
WDTCR |= (1<<WDTIE);
sei();
// Use the Power Down sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_bod_disable();
while (1)
{
sleep_cpu();
if (measure_adc() < 512) {
PORTB ^= 1 << PINB4; // blink light
} else {
PORTB &= ~(1 << PINB4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment