timer0 ctc

This commit is contained in:
Thomas Klaehn 2019-12-22 20:27:17 +00:00
parent f5f29be68a
commit a8d7b7a282
2 changed files with 32 additions and 31 deletions

View File

@ -3,19 +3,21 @@ CROSS_COMPILE ?= avr-
TARGET_FILE ?= avr.elf TARGET_FILE ?= avr.elf
MCU = attiny85 MCU = attiny85
DUDE_MCU = t85 # MCU = atmega8
C_FLAGS += -Wall -Werror # C_FLAGS += -Wall -Werror
C_FLAGS += -Os C_FLAGS += -Os
C_FLAGS += -mmcu=$(MCU) C_FLAGS += -mmcu=$(MCU)
C_FLAGS += -ffunction-sections # C_FLAGS += -ffunction-sections
C_FLAGS += -fdata-sections # C_FLAGS += -fdata-sections
C_FLAGS += -std=c11 # C_FLAGS += -std=c11
C_FLAGS += -pedantic # C_FLAGS += -pedantic
C_FLAGS += -pedantic-errors # C_FLAGS += -pedantic-errors
C_FLAGS += -Iinclude C_FLAGS += -Iinclude
DUDE_FLAGS = -p $(DUDE_MCU) LD_FLAGS += -mmcu=$(MCU)
DUDE_FLAGS = -p $(MCU)
DUDE_FLAGS += -P /dev/spidev0.0 DUDE_FLAGS += -P /dev/spidev0.0
DUDE_FLAGS += -c linuxspi DUDE_FLAGS += -c linuxspi
DUDE_FLAGS += -b 10000 DUDE_FLAGS += -b 10000
@ -49,7 +51,7 @@ $(TARGET): $(OBJS) Makefile
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@) @mkdir -p $(dir $@)
@$(call makedep,$<,$@,$(patsubst %.c,%.d,$(OBJ_DIR)/$(notdir $<)),$(C_FLAGS)) @$(call makedep,$<,$@,$(patsubst %.c,%.d,$(OBJ_DIR)/$(notdir $<)),$(C_FLAGS))
$(CC) -c $(C_FLAGS) $(CCFLAGS) -o $@ $< $(CC) -c $(C_FLAGS) -o $@ $<
.PHONY: clean .PHONY: clean
clean: clean:

View File

@ -8,35 +8,34 @@
#include <avr/wdt.h> #include <avr/wdt.h>
#include <util/delay.h> #include <util/delay.h>
#define IGNITION_PIN 4 volatile int tick;
#define POWER_PIN 3
int main(void) int main(void)
{ {
cli();
tick = 0;
DDRB = 0xff; DDRB = 0xff;
DDRB &= ~(1 << IGNITION_PIN);
PORTB |= (1 << PB0) | (1 << PB1) | (1 << PB2);
wdt_enable(WDTO_8S); TCCR0A = (1 << WGM01);
TCCR0B = (1 << CS01);
TIMSK |= (1 << OCIE0A); // enable compare match interrupt
// ((1000000 / 8) / 1000) = 125
OCR0A = 125 - 1;
// TIMSK |= (1 << TOIE0);
sei();
while(1) { while(1) {
wdt_reset(); if(tick >= 1000) {
if(PINB & (1 << IGNITION_PIN)) { PORTB ^= (1 << PB4);
PORTB |= (1 << POWER_PIN); tick = 0;
} else {
if(PINB & (1 << POWER_PIN)) {
for(uint8_t i = 0; i < 100; ++i) {
wdt_reset();
_delay_ms(100);
}
PORTB &= ~(1 << POWER_PIN);
}
wdt_enable(WDTO_8S);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
sleep_disable();
} }
} }
return 0; return 0;
} }
ISR(TIM0_COMPA_vect)
{
tick++;
}