use button to toggle output

This commit is contained in:
Olaf Rempel 2015-06-03 21:46:53 +02:00
parent 4e1d77b4b1
commit e3803c678d
2 changed files with 94 additions and 58 deletions

View File

@ -1,62 +1,53 @@
TARGET = rc5switch
TARGET_MCU = attiny24
CC := avr-gcc
LD := avr-ld
OBJCOPY := avr-objcopy
OBJDUMP := avr-objdump
SIZE := avr-size
# ---------------------------------------------------------
TARGET = rc5switch
SOURCE = $(wildcard *.c)
BUILD_DIR = build
BUILD = build
AVRDUDE_PROG := -c avr910 -b 115200 -P /dev/ispprog
#AVRDUDE_PROG := -c dragon_isp -P usb
CC = avr-gcc
NM = avr-nm
SIZE = avr-size
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
MCU = attiny24
AVRDUDE_MCU=t24
# no self programming, 2.7V BOD, 8MHz internal RC Osz.
AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdd:m efuse:w:0xff:m
override CFLAGS = -g -Wall -Os -mmcu=$(TARGET_MCU) -Wa,-gstabs,-adhlns=$(BUILD)/$(*D)/$(*F).lst -MMD -MP -MF $(BUILD)/$(*D)/$(*F).d -pipe
override LDFLAGS = -Wl,-Map=$(BUILD)/$(TARGET).map,--cref
# ---------------------------------------------------------------------------
# ---------------------------------------------------------
CFLAGS = -pipe -g -Os -mmcu=$(MCU) -Wall -fdata-sections -ffunction-sections
CFLAGS += -Wa,-adhlns=$(BUILD_DIR)/$(*D)/$(*F).lst -MMD -MP -MF $(BUILD_DIR)/$(*D)/$(*F).d
LDFLAGS = -Wl,-Map,$(@:.elf=.map),--cref,--relax,--gc-sections
SRC := $(wildcard *.c)
# ---------------------------------------------------------------------------
all: $(addprefix $(BUILD)/$(TARGET), .elf .lss .sym .bin .hex _eeprom.bin _eeprom.hex)
# @$(SIZE) -x -C --mcu=$(TARGET_MCU) $<
$(TARGET): $(BUILD_DIR)/$(TARGET).elf
@$(SIZE) -B -x --mcu=$(MCU) $<
%.lss: %.elf
@$(OBJDUMP) -h -S -C $< > $@
%.sym: %.elf
@$(NM) -n $< > $@
%.bin: %.elf
@$(OBJCOPY) -j .text -j .data -O binary $< $@
%.hex: %.elf
@$(OBJCOPY) -j .text -j .data -O ihex $< $@
%_eeprom.bin: %.elf
@$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ 2> /dev/null
%_eeprom.hex: %.elf
@$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ 2> /dev/null
$(BUILD)/$(TARGET).elf: $(patsubst %,$(BUILD)/%,$(SRC:.c=.o))
$(BUILD_DIR)/$(TARGET).elf: $(patsubst %,$(BUILD_DIR)/%,$(SOURCE:.c=.o))
@echo " Linking file: $@"
@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) > /dev/null
@$(SIZE) -x -B $^ $@
@$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
@$(OBJDUMP) -h -S $@ > $(@:.elf=.lss)
@$(OBJCOPY) -j .text -j .data -O ihex $@ $(@:.elf=.hex)
@$(OBJCOPY) -j .text -j .data -O binary $@ $(@:.elf=.bin)
@$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $@ $(@:.elf=_eeprom.hex)
@$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $@ $(@:.elf=_eeprom.bin)
$(BUILD)/%.o: %.c $(MAKEFILE_LIST)
$(BUILD_DIR)/%.o: %.c $(MAKEFILE_LIST)
@echo " Building file: $<"
@$(shell mkdir -p $(BUILD)/$(*D))
@$(CC) -c $(CFLAGS) $< -o $@
@$(shell test -d $(BUILD_DIR)/$(*D) || mkdir -p $(BUILD_DIR)/$(*D))
@$(CC) $(CFLAGS) -o $@ -c $<
include $(shell find $(BUILD_DIR) -name \*.d 2> /dev/null)
clean:
rm -rf $(BUILD)
rm -rf $(BUILD_DIR)
install: $(BUILD)/$(TARGET).hex $(BUILD)/$(TARGET)_eeprom.hex
avrdude -c dragon_isp -P usb -p t24 -U flash:w:$(BUILD)/$(TARGET).hex -U eeprom:w:$(BUILD)/$(TARGET)_eeprom.hex
install: $(BUILD_DIR)/$(TARGET).elf
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U flash:w:$(<:.elf=.hex) -U eeprom:w:$(<:.elf=_eeprom.hex)
# no self programming, 2.7V BOD, 8MHz internal RC Osz.
fuses:
avrdude -c dragon_isp -P usb -p t24 -U lfuse:w:0xc2:m -U hfuse:w:0xdd:m -U efuse:w:0xff:m
-include $(shell find $(BUILD) -name *.d 2> /dev/null)
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(patsubst %,-U %, $(AVRDUDE_FUSES))

69
main.c
View File

@ -41,7 +41,6 @@
#define RC5_CMD_MASK 0x37FF
#define RC5_COMPLETE 0x8000
#define PROGMODE_CHECK() (!(PINB & (1<<PINB1)))
#define OUTPUT_ON() { PORTB &= ~(1<<PORTB0); }
#define OUTPUT_OFF() { PORTB |= (1<<PORTB0); }
@ -58,6 +57,38 @@ struct ee_param params_in_eeprom EEMEM = {
static uint8_t bitcnt;
static volatile uint16_t value;
static volatile uint8_t button_debounce;
static volatile uint8_t button;
ISR(PCINT1_vect)
{
/* get button state */
button = !(PINB & (1<<PINB1));
/* 20 * 2ms = 40ms */
button_debounce = 20;
/* clear and enable overflow interrupt */
TIFR0 = (1<<TOV0);
TIMSK0 |= (1<<TOIE0);
/* disable Pinchange interrupt */
GIMSK &= ~(1<<PCIE1);
}
ISR(TIM0_OVF_vect)
{
button_debounce--;
if (button_debounce == 0x00)
{
/* disable overflow interrupt */
TIMSK0 &= ~(1<<TOIE0);
/* clear & enable Pinchange interrupt */
GIFR = (1<<PCIF1);
GIMSK |= (1<<PCIE1);
}
}
ISR(EXT_INT0_vect)
{
@ -109,8 +140,10 @@ int main(int argc, char *argv[])
PORTB = (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
/* INT0: both edges generate interrupt */
/* PB1: enable pin change interrupt */
MCUCR = (1<<ISC00);
GIMSK = (1<<INT0);
GIMSK = (1<<INT0) | (1<<PCIE1);
PCMSK1 = (1<<PCINT9);
/* Timer0: 8Mhz/64 */
TCCR0B = (1<<CS01) | (1<<CS00);
@ -128,6 +161,7 @@ int main(int argc, char *argv[])
sei();
uint16_t old_value = 0x0000;
uint8_t toggle = 0x00;
while (1) {
/* wait for next interrupt */
@ -135,26 +169,37 @@ int main(int argc, char *argv[])
if (value & RC5_COMPLETE) {
/* PROGMODE jumper set? */
if (PROGMODE_CHECK()) {
if (!(PINB & (1<<PINB1))) {
params.rc5cmd = (value & RC5_CMD_MASK);
eeprom_write_block(&params, &params_in_eeprom, sizeof(struct ee_param));
/* current command matches stored one */
} else if (params.rc5cmd == (value & RC5_CMD_MASK) && value != old_value) {
if (params.state) {
params.state = 0;
OUTPUT_OFF();
} else {
params.state = 1;
OUTPUT_ON();
}
eeprom_write_block(&params, &params_in_eeprom, sizeof(struct ee_param));
toggle = 1;
old_value = value;
}
value = 0x0000;
}
else if (button)
{
toggle = 1;
button = 0;
}
if (toggle)
{
if (params.state) {
params.state = 0;
OUTPUT_OFF();
} else {
params.state = 1;
OUTPUT_ON();
}
eeprom_write_block(&params, &params_in_eeprom, sizeof(struct ee_param));
toggle = 0;
}
}
return 0;