ex04 almost works
This commit is contained in:
43
module00/ex04/Makefile
Normal file
43
module00/ex04/Makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
# frequency of the CPU (in Hz)
|
||||
F_CPU = 16000000UL
|
||||
TARGET = main
|
||||
CC = avr-gcc
|
||||
|
||||
AVRGCC_MCU_TYPE = atmega328p
|
||||
AVRDUDE_MCU_TYPE = m328p
|
||||
BAUD_RATE = 115200
|
||||
SERIAL_PORT = /dev/ttyUSB0
|
||||
PROGRAMMER_ID = arduino
|
||||
DUMP_ORI = ../../ressources/dump_atmega328p_ori.hex
|
||||
|
||||
|
||||
all: hex flash
|
||||
|
||||
|
||||
hex: $(TARGET).hex
|
||||
|
||||
|
||||
# https://gcc.gnu.org/onlinedocs/gcc/AVR-Options.html
|
||||
$(TARGET).bin: $(TARGET).c
|
||||
$(CC) -mmcu=$(AVRGCC_MCU_TYPE) -D F_CPU=$(F_CPU) $(TARGET).c -Os -o $(TARGET).bin
|
||||
|
||||
|
||||
# https://linux.die.net/man/1/avr-objcopy
|
||||
$(TARGET).hex: $(TARGET).bin
|
||||
avr-objcopy -O ihex $(TARGET).bin $(TARGET).hex
|
||||
|
||||
|
||||
# AVR Downloader/UploaDEr https://avrdudes.github.io/avrdude (v6.3 https://download-mirror.savannah.gnu.org/releases/avrdude/avrdude-doc-6.3.pdf)
|
||||
flash:
|
||||
avrdude -p $(AVRDUDE_MCU_TYPE) -c $(PROGRAMMER_ID) -b $(BAUD_RATE) -P $(SERIAL_PORT) -U flash:w:$(TARGET).hex
|
||||
|
||||
|
||||
restore:
|
||||
avrdude -p $(AVRDUDE_MCU_TYPE) -c $(PROGRAMMER_ID) -b $(BAUD_RATE) -P $(SERIAL_PORT) -U flash:w:$(DUMP_ORI)
|
||||
|
||||
|
||||
clean:
|
||||
rm -f main.hex main.bin
|
||||
|
||||
|
||||
.PHONY : all clean hex flash restore
|
||||
85
module00/ex04/main.c
Normal file
85
module00/ex04/main.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#define SET(REGISTER, BIT) REGISTER |= 1 << BIT
|
||||
#define CLEAR(REGISTER, BIT) REGISTER &= ~(1 << BIT)
|
||||
#define TEST(REGISTER, BIT) REGISTER & 1 << BIT
|
||||
#define TEST_PIN(PORT, BIT) PIN ## PORT & 1 << BIT
|
||||
#define TOGGLE(REGISTER, BIT) REGISTER ^= 1 << BIT
|
||||
#define TOGGLE_PIN(PORT, BIT) PIN ## PORT |= 1 << BIT
|
||||
#define MODE_INPUT(PORT, BIT) CLEAR(DDR ## PORT, BIT)
|
||||
#define MODE_OUTPUT(PORT, BIT) SET(DDR ## PORT, BIT)
|
||||
#define IS_PIN_SET(PORT, BIT) (TEST_PIN(PORT, BIT)) == 0
|
||||
#define IS_PIN_CLEAR(PORT, BIT) (TEST_PIN(PORT, BIT)) == 1
|
||||
|
||||
#define D1 0
|
||||
#define D2 1
|
||||
#define D3 2
|
||||
#define D4 4
|
||||
#define SW1 2
|
||||
#define SW2 4
|
||||
|
||||
typedef struct {
|
||||
int *value;
|
||||
int max;
|
||||
int min;
|
||||
} IncrementParams;
|
||||
|
||||
void print_binary(int value) {
|
||||
int mask_3 = 0b1000;
|
||||
int bit_at_3 = value & mask_3;
|
||||
int bit_at_4 = bit_at_3 << 1;
|
||||
PORTB = (value & 0b111) + bit_at_4;
|
||||
}
|
||||
|
||||
void increment_int(IncrementParams *params) {
|
||||
int *value = params->value;
|
||||
int max = params->max;
|
||||
*value = (*value >= max) ? max : ++(*value);
|
||||
}
|
||||
|
||||
void decrement_int(IncrementParams *params) {
|
||||
int *value = params->value;
|
||||
int min = params->min;
|
||||
*value = (*value <= min) ? min : --(*value);
|
||||
}
|
||||
|
||||
void increment_led(IncrementParams *params) {
|
||||
increment_int(params);
|
||||
print_binary(*(params->value));
|
||||
}
|
||||
|
||||
void decrement_led(IncrementParams *params) {
|
||||
decrement_int(params);
|
||||
print_binary(*(params->value));
|
||||
}
|
||||
|
||||
void on_press(int bit, void (*action)(void*), void *params) {
|
||||
if (IS_PIN_SET(D, bit)) {
|
||||
action(params);
|
||||
_delay_ms(20);
|
||||
while (IS_PIN_SET(D, bit)) {
|
||||
continue;
|
||||
}
|
||||
_delay_ms(20);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
MODE_OUTPUT(B, D1);
|
||||
MODE_OUTPUT(B, D2);
|
||||
MODE_OUTPUT(B, D3);
|
||||
MODE_OUTPUT(B, D4);
|
||||
MODE_INPUT(D, SW1);
|
||||
MODE_INPUT(D, SW2);
|
||||
|
||||
int value = 0;
|
||||
int max = 16;
|
||||
int min = 0;
|
||||
IncrementParams params = {&value, max};
|
||||
|
||||
while(1) {
|
||||
on_press(SW1, increment_led, ¶ms);
|
||||
on_press(SW2, decrement_led, ¶ms);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user