diff --git a/Makefile b/Makefile index 10e3e36..0707a16 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,10 @@ LDFLAGS += -Wl,--gc-sections LDFLAGS += -Wl,-u,vfprintf -lprintf_flt MAIN_OBJS += main.o -MAIN_OBJS += contr_main.o +MAIN_OBJS += contr.o MAIN_OBJS += relay.o -MAIN_OBJS += contr_init.o +MAIN_OBJS += timer.o +MAIN_OBJS += adc.o MAIN_OBJS += eeprom.o MAIN_OBJS += i2c.o MAIN_OBJS += disp.o @@ -36,9 +37,12 @@ $(MAIN_ELF): $(MAIN_OBJS) $(CC) $(LDFLAGS) -o $(MAIN_ELF) $(MAIN_OBJS) $(SIZE) --format=berkeley $@ +DEPENDS_DIR = .dep + %.o: %.c + @mkdir -p $(DEPENDS_DIR) $(CC) $(CFLAGS) -c $*.c -o $*.o - $(CC) -MM $(CFLAGS) $*.c > $*.d + @$(CC) -MM $(CFLAGS) $*.c > $(DEPENDS_DIR)/$*.d $(MAIN_HEX): $(MAIN_ELF) @@ -57,9 +61,10 @@ download: clean: rm -f *.i *.o *.s - rm -f *.elf *.bin *~ *.hex - rm -f *.d + rm -f *.hex *.elf *.bin + rm -f *~ *.d + rm -rf $(DEPENDS_DIR) --include $(MAIN_OBJS:.o=.d) +-include $(DEPENDS_DIR)/$(MAIN_OBJS:.o=.d) #EOF diff --git a/contr_init.c b/adc.c similarity index 53% rename from contr_init.c rename to adc.c index 12b8c22..6bf7a06 100644 --- a/contr_init.c +++ b/adc.c @@ -8,7 +8,7 @@ #include -void contr_adc_init() { +void adc_init() { /* Disable ADC */ REG_SETDOWN_BIT(ADCSRA, ADEN); /* Set reference*/ @@ -28,21 +28,16 @@ void contr_adc_init() { REG_SETUP_BIT(ADCSRA, ADEN); } - -void contr_timer_init(void) { - /* Disable comparators */ - REG_SETDOWN_BIT(TCCR0A, COM0A1); - REG_SETDOWN_BIT(TCCR0A, COM0A0); - REG_SETDOWN_BIT(TCCR0A, COM0B1); - REG_SETDOWN_BIT(TCCR0A, COM0B0); - /* Set normal mode */ - REG_SETDOWN_BIT(TCCR0A, WGM01); - REG_SETDOWN_BIT(TCCR0A, WGM00); - /* Set clock to 1/64 */ - REG_SETDOWN_BIT(TCCR0B, CS02); - REG_SETUP_BIT(TCCR0B, CS01); - REG_SETUP_BIT(TCCR0B, CS00); - /* Enable timer interrupt */ - REG_SETUP_BIT(TIMSK0, TOIE0); - //REG_SETUP(TCNT0, TIMER_INITVAL); +uint16_t adc_read(uint8_t channel) { + uint8_t reg = ADMUX; + ADMUX = (reg & 0xF0) | channel; + REG_SETUP_BIT(ADCSRA, ADSC); + while (ADCSRA & BIT(ADSC)); + uint16_t lval = (uint16_t)ADCL; + uint16_t hval = (uint16_t)ADCH; + hval = (hval << 8) & 0xFF00; + lval = lval & 0x00FF; + return hval | lval; } + + diff --git a/adc.h b/adc.h new file mode 100644 index 0000000..6834a27 --- /dev/null +++ b/adc.h @@ -0,0 +1,16 @@ +#ifndef ADC_H_QWERTY +#define ADC_H_QWERTY + +#define ACD_CHANELL0 0x00 +#define ACD_CHANELL2 0x01 +#define ACD_CHANELL3 0x02 +#define ACD_CHANELL3 0x03 +#define ACD_CHANELL4 0x04 +#define ACD_CHANELL5 0x05 +#define ACD_CHANELL6 0x06 +#define ACD_CHANELL7 0x07 + +void adc_init(void); +uint16_t adc_read(uint8_t channel); + +#endif diff --git a/contr_main.c b/contr.c similarity index 99% rename from contr_main.c rename to contr.c index b9ffdf7..3005c3c 100644 --- a/contr_main.c +++ b/contr.c @@ -17,9 +17,9 @@ #include #include #include +#include -#include -#include +#include #define CONTR_BAND_OFF 0x01 #define CONTR_BAND_10M 0x02 @@ -91,8 +91,8 @@ void contr_setup(void) { contr_key_init(); - contr_adc_init(); - contr_timer_init(); + adc_init(); + timer0_init(); } diff --git a/contr_main.h b/contr.h similarity index 100% rename from contr_main.h rename to contr.h diff --git a/contr_init.h b/contr_init.h deleted file mode 100644 index deaf913..0000000 --- a/contr_init.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef CONTR_INIT_H_QWERTY -#define CONTR_INIT_H_QWERTY - -void contr_timer_init(void); -void contr_adc_init(void); - -#endif diff --git a/main.c b/main.c index 26ddc9d..bc410a2 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,7 @@ #include #include -#include +#include int main(void) { uart_init(); diff --git a/timer.c b/timer.c new file mode 100644 index 0000000..95659a8 --- /dev/null +++ b/timer.c @@ -0,0 +1,27 @@ + +/* + * Copyright 2017 Oleg Borodin + */ + +#include +#include + +#include + +void timer0_init(void) { + /* Disable comparators */ + REG_SETDOWN_BIT(TCCR0A, COM0A1); + REG_SETDOWN_BIT(TCCR0A, COM0A0); + REG_SETDOWN_BIT(TCCR0A, COM0B1); + REG_SETDOWN_BIT(TCCR0A, COM0B0); + /* Set normal mode */ + REG_SETDOWN_BIT(TCCR0A, WGM01); + REG_SETDOWN_BIT(TCCR0A, WGM00); + /* Set clock to 1/64 */ + REG_SETDOWN_BIT(TCCR0B, CS02); + REG_SETUP_BIT(TCCR0B, CS01); + REG_SETUP_BIT(TCCR0B, CS00); + /* Enable timer interrupt */ + REG_SETUP_BIT(TIMSK0, TOIE0); + //REG_SETUP(TCNT0, TIMER_INITVAL); +} diff --git a/timer.h b/timer.h new file mode 100644 index 0000000..a75b9ec --- /dev/null +++ b/timer.h @@ -0,0 +1,18 @@ +#ifndef CONTR_INIT_H_QWERTY +#define CONTR_INIT_H_QWERTY + +#define ACD_CHANELL0 0x00 +#define ACD_CHANELL1 0x01 +#define ACD_CHANELL2 0x02 +#define ACD_CHANELL3 0x03 +#define ACD_CHANELL4 0x04 +#define ACD_CHANELL5 0x05 +#define ACD_CHANELL6 0x06 +#define ACD_CHANELL7 0x07 + +void adc_init(void); +uint16_t adc_read(uint8_t channel); + +void timer0_init(void); + +#endif