Skip to content

Instantly share code, notes, and snippets.

@hmms
Last active August 22, 2017 03:41
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 hmms/d42c97ee4585b187938684a720b075c1 to your computer and use it in GitHub Desktop.
Save hmms/d42c97ee4585b187938684a720b075c1 to your computer and use it in GitHub Desktop.
firmware for ATtiny85 capacitive touch activated LED thing, with optimizations for extremely low power consumption
/*
* Capsense-LED-thing.c
*
* Created: 9/30/2016 10:25:57 PM
* Author : firmware for Cap Sense LED thing
* basic flow of the program is as follows
* wait for Pin change interrupt on PB2 -> wake up the MCU -> PWM cycle LEDs on PB3 and PB0 go back to sleep and wait for pin change interrupt
* interesting link: https://thewanderingengineer.com/2014/08/11/pin-change-interrupts-on-attiny85/
* Firmware for project at: http://murli-shenoy.com/?p=606
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
uint16_t i;
volatile short ActivateHeartbeat; //gotcha! - compiler optimizes this out if not volatile
ISR(INT0_vect){
ActivateHeartbeat = 1;
}
void heart_beat(){
_delay_ms(10);
DDRB |= (1 << DDB0)|(1 << DDB3)|(1 << DDB4);
for(i=0; i<254; i++){
OCR0A=i;
OCR1B = i;
_delay_ms(1);
}
for(i=0; i<254; i++){
OCR0A=~i;
OCR1B =~i;
_delay_ms(1);
}
for(i=0; i<254; i++){
OCR0A=i;
OCR1B = i;
_delay_ms(1);
}
DDRB &= ~((1 << DDB0)|(1 << DDB3)|(1 << DDB4));
sleep_mode();
}
void init(){
DDRB |= (1 << DDB0)|(1 << DDB3)|(1 << DDB4)|(1<< DDB1)|(1<<DDB5);
PORTB |= (1<<PB4)|(1<<PB1)|(1<<PB5); //enable pull up on pb4
DDRB &= ~(1 << DDB2);
GIMSK |= (1<<INT0); //enable int0 request
MCUCR |= (0<<ISC00); //configure INT0 to be triggered on the falling edge
//init timer counter for PWM output on PB0 and PB3
TCCR0A = 3<<COM0A0 | 3<<WGM00;
TCCR0B = 0<<WGM02 | 3<<CS00;
GTCCR = 1<<PWM1B | 1<<COM1B0;
TCCR1 = 3<<COM1A0 | 7<<CS10;
ADCSRA &= ~(1<<ADEN); //disable ADC
ActivateHeartbeat = 0;
}
int main(void) {
init();
//heart_beat(); //test
sei();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
OCR0A=255;
OCR1B = 255;
while(ActivateHeartbeat){
heart_beat();
ActivateHeartbeat = 0;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment