use button to toggle output
This commit is contained in:
parent
4e1d77b4b1
commit
e3803c678d
83
Makefile
83
Makefile
@ -1,62 +1,53 @@
|
|||||||
TARGET = rc5switch
|
CC := avr-gcc
|
||||||
TARGET_MCU = attiny24
|
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
|
MCU = attiny24
|
||||||
NM = avr-nm
|
AVRDUDE_MCU=t24
|
||||||
SIZE = avr-size
|
# no self programming, 2.7V BOD, 8MHz internal RC Osz.
|
||||||
OBJCOPY = avr-objcopy
|
AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdd:m efuse:w:0xff:m
|
||||||
OBJDUMP = avr-objdump
|
|
||||||
|
|
||||||
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)
|
$(TARGET): $(BUILD_DIR)/$(TARGET).elf
|
||||||
# @$(SIZE) -x -C --mcu=$(TARGET_MCU) $<
|
@$(SIZE) -B -x --mcu=$(MCU) $<
|
||||||
|
|
||||||
%.lss: %.elf
|
$(BUILD_DIR)/$(TARGET).elf: $(patsubst %,$(BUILD_DIR)/%,$(SOURCE:.c=.o))
|
||||||
@$(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))
|
|
||||||
@echo " Linking file: $@"
|
@echo " Linking file: $@"
|
||||||
@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) > /dev/null
|
@$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
|
||||||
@$(SIZE) -x -B $^ $@
|
@$(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: $<"
|
@echo " Building file: $<"
|
||||||
@$(shell mkdir -p $(BUILD)/$(*D))
|
@$(shell test -d $(BUILD_DIR)/$(*D) || mkdir -p $(BUILD_DIR)/$(*D))
|
||||||
@$(CC) -c $(CFLAGS) $< -o $@
|
@$(CC) $(CFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
|
include $(shell find $(BUILD_DIR) -name \*.d 2> /dev/null)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD)
|
rm -rf $(BUILD_DIR)
|
||||||
|
|
||||||
install: $(BUILD)/$(TARGET).hex $(BUILD)/$(TARGET)_eeprom.hex
|
install: $(BUILD_DIR)/$(TARGET).elf
|
||||||
avrdude -c dragon_isp -P usb -p t24 -U flash:w:$(BUILD)/$(TARGET).hex -U eeprom:w:$(BUILD)/$(TARGET)_eeprom.hex
|
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:
|
fuses:
|
||||||
avrdude -c dragon_isp -P usb -p t24 -U lfuse:w:0xc2:m -U hfuse:w:0xdd:m -U efuse:w:0xff:m
|
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(patsubst %,-U %, $(AVRDUDE_FUSES))
|
||||||
|
|
||||||
-include $(shell find $(BUILD) -name *.d 2> /dev/null)
|
|
||||||
|
69
main.c
69
main.c
@ -41,7 +41,6 @@
|
|||||||
#define RC5_CMD_MASK 0x37FF
|
#define RC5_CMD_MASK 0x37FF
|
||||||
#define RC5_COMPLETE 0x8000
|
#define RC5_COMPLETE 0x8000
|
||||||
|
|
||||||
#define PROGMODE_CHECK() (!(PINB & (1<<PINB1)))
|
|
||||||
#define OUTPUT_ON() { PORTB &= ~(1<<PORTB0); }
|
#define OUTPUT_ON() { PORTB &= ~(1<<PORTB0); }
|
||||||
#define OUTPUT_OFF() { 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 uint8_t bitcnt;
|
||||||
static volatile uint16_t value;
|
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)
|
ISR(EXT_INT0_vect)
|
||||||
{
|
{
|
||||||
@ -109,8 +140,10 @@ int main(int argc, char *argv[])
|
|||||||
PORTB = (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
|
PORTB = (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
|
||||||
|
|
||||||
/* INT0: both edges generate interrupt */
|
/* INT0: both edges generate interrupt */
|
||||||
|
/* PB1: enable pin change interrupt */
|
||||||
MCUCR = (1<<ISC00);
|
MCUCR = (1<<ISC00);
|
||||||
GIMSK = (1<<INT0);
|
GIMSK = (1<<INT0) | (1<<PCIE1);
|
||||||
|
PCMSK1 = (1<<PCINT9);
|
||||||
|
|
||||||
/* Timer0: 8Mhz/64 */
|
/* Timer0: 8Mhz/64 */
|
||||||
TCCR0B = (1<<CS01) | (1<<CS00);
|
TCCR0B = (1<<CS01) | (1<<CS00);
|
||||||
@ -128,6 +161,7 @@ int main(int argc, char *argv[])
|
|||||||
sei();
|
sei();
|
||||||
|
|
||||||
uint16_t old_value = 0x0000;
|
uint16_t old_value = 0x0000;
|
||||||
|
uint8_t toggle = 0x00;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* wait for next interrupt */
|
/* wait for next interrupt */
|
||||||
@ -135,26 +169,37 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (value & RC5_COMPLETE) {
|
if (value & RC5_COMPLETE) {
|
||||||
/* PROGMODE jumper set? */
|
/* PROGMODE jumper set? */
|
||||||
if (PROGMODE_CHECK()) {
|
if (!(PINB & (1<<PINB1))) {
|
||||||
params.rc5cmd = (value & RC5_CMD_MASK);
|
params.rc5cmd = (value & RC5_CMD_MASK);
|
||||||
eeprom_write_block(¶ms, ¶ms_in_eeprom, sizeof(struct ee_param));
|
eeprom_write_block(¶ms, ¶ms_in_eeprom, sizeof(struct ee_param));
|
||||||
|
|
||||||
/* current command matches stored one */
|
/* current command matches stored one */
|
||||||
} else if (params.rc5cmd == (value & RC5_CMD_MASK) && value != old_value) {
|
} else if (params.rc5cmd == (value & RC5_CMD_MASK) && value != old_value) {
|
||||||
if (params.state) {
|
toggle = 1;
|
||||||
params.state = 0;
|
|
||||||
OUTPUT_OFF();
|
|
||||||
} else {
|
|
||||||
params.state = 1;
|
|
||||||
OUTPUT_ON();
|
|
||||||
}
|
|
||||||
|
|
||||||
eeprom_write_block(¶ms, ¶ms_in_eeprom, sizeof(struct ee_param));
|
|
||||||
old_value = value;
|
old_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
value = 0x0000;
|
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(¶ms, ¶ms_in_eeprom, sizeof(struct ee_param));
|
||||||
|
toggle = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user