From a8d7b7a282075cd0e29bfb7b94b2a120f82bbf7c Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Sun, 22 Dec 2019 20:27:17 +0000 Subject: [PATCH] timer0 ctc --- Makefile | 20 +++++++++++--------- src/main.c | 43 +++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 06c930f..dcfc7c3 100644 --- a/Makefile +++ b/Makefile @@ -3,19 +3,21 @@ CROSS_COMPILE ?= avr- TARGET_FILE ?= avr.elf MCU = attiny85 -DUDE_MCU = t85 +# MCU = atmega8 -C_FLAGS += -Wall -Werror +# C_FLAGS += -Wall -Werror C_FLAGS += -Os C_FLAGS += -mmcu=$(MCU) -C_FLAGS += -ffunction-sections -C_FLAGS += -fdata-sections -C_FLAGS += -std=c11 -C_FLAGS += -pedantic -C_FLAGS += -pedantic-errors +# C_FLAGS += -ffunction-sections +# C_FLAGS += -fdata-sections +# C_FLAGS += -std=c11 +# C_FLAGS += -pedantic +# C_FLAGS += -pedantic-errors C_FLAGS += -Iinclude -DUDE_FLAGS = -p $(DUDE_MCU) +LD_FLAGS += -mmcu=$(MCU) + +DUDE_FLAGS = -p $(MCU) DUDE_FLAGS += -P /dev/spidev0.0 DUDE_FLAGS += -c linuxspi DUDE_FLAGS += -b 10000 @@ -49,7 +51,7 @@ $(TARGET): $(OBJS) Makefile $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(dir $@) @$(call makedep,$<,$@,$(patsubst %.c,%.d,$(OBJ_DIR)/$(notdir $<)),$(C_FLAGS)) - $(CC) -c $(C_FLAGS) $(CCFLAGS) -o $@ $< + $(CC) -c $(C_FLAGS) -o $@ $< .PHONY: clean clean: diff --git a/src/main.c b/src/main.c index 0c9545b..d8623da 100644 --- a/src/main.c +++ b/src/main.c @@ -8,35 +8,34 @@ #include #include -#define IGNITION_PIN 4 -#define POWER_PIN 3 +volatile int tick; int main(void) { + cli(); + tick = 0; 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) { - wdt_reset(); - if(PINB & (1 << IGNITION_PIN)) { - PORTB |= (1 << POWER_PIN); - } 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(); + if(tick >= 1000) { + PORTB ^= (1 << PB4); + tick = 0; } } - return 0; } + +ISR(TIM0_COMPA_vect) +{ + tick++; +}