timer0 ctc
This commit is contained in:
		
							
								
								
									
										20
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								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:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								src/main.c
									
									
									
									
									
								
							@@ -8,35 +8,34 @@
 | 
			
		||||
#include <avr/wdt.h>
 | 
			
		||||
#include <util/delay.h>
 | 
			
		||||
 | 
			
		||||
#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++;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user