Skip to content

Instantly share code, notes, and snippets.

@lukas2511
Created February 13, 2018 14:19
Show Gist options
  • Save lukas2511/d36011b3f320f533bfa1ef79d5945388 to your computer and use it in GitHub Desktop.
Save lukas2511/d36011b3f320f533bfa1ef79d5945388 to your computer and use it in GitHub Desktop.
attiny13 toggle pin deepsleep foo
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#define _UV(x) (1 << (x))
#define _SETBIT(x, y) ((x) |= _UV(y))
#define _CLRBIT(x, y) ((x) &= ~_UV(y))
#define _GETBIT(d, x) (d & ( 1 << x )) >> x
#include <avr/interrupt.h>
#include <avr/sleep.h>
int state = 0;
void sleep()
{
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT2); // Use PB2 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
MCUCR |= _BV(SM1); MCUCR &= ~_BV(SM0); // Select "Power-down" sleep mode
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // SLEEP
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT2); // Turn off PB2 as interrupt pin
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
}
ISR(INT0_vect) {
asm("nop");
}
ISR(PCINT0_vect){
asm("nop");
}
int main(void) {
// Set up Port B pin 4 mode to output
DDRB = _UV(DDB4);
sei(); // Enable global interrupts
while(1){
if(_GETBIT(PINB,2)){
if(state){
state=0;
_CLRBIT(PORTB,4);
}else{
state=1;
_SETBIT(PORTB,4);
}
_delay_ms(500);
}
sleep();
}
}
MCU=attiny13
MCUFREQ=-DF_CPU=1000000L
PROGTYPE=stk500v1
SOURCES=main.c
TARGET=main
.PHONY: build upload
build: $(TARGET).hex
upload: build
avrdude -p $(MCU) -c $(PROGTYPE) -b 19200 -P /dev/tty.usbserial-A600epyy -U flash:w:$(TARGET).hex -U lfuse:w:0x6a:m -U hfuse:w:0xff:m
$(TARGET).elf: $(SOURCES) Makefile
avr-gcc $(MCUFREQ) -Os -o $@ -mmcu=$(MCU) $(SOURCES)
$(TARGET).hex: $(TARGET).elf Makefile
avr-objcopy -O ihex -R .eeprom $< $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment