diff --git a/module00/ex02/Makefile b/module00/ex02/Makefile new file mode 100644 index 0000000..40e9cb4 --- /dev/null +++ b/module00/ex02/Makefile @@ -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 -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 \ No newline at end of file diff --git a/module00/ex02/main.c b/module00/ex02/main.c new file mode 100644 index 0000000..fe04856 --- /dev/null +++ b/module00/ex02/main.c @@ -0,0 +1,41 @@ +#include + +#define SET(REGISTER, BIT) REGISTER |= 1 << BIT +#define CLEAR(REGISTER, BIT) REGISTER &= ~(1 << BIT) +#define TEST(REGISTER, BIT) REGISTER & 1 << BIT +#define TOGGLE(REGISTER, BIT) REGISTER ^= 1 << BIT + +#define D1 0 +#define D2 1 +#define D3 2 +#define D4 4 + +int main() +{ + // DDRx : select direction of the pin + // - 1 = output (pull-up off) + // - 0 = input + // PORTx : + // if DDRx = 1 (output mode) : + // - 1 = HIGH (5V) + // - 0 = LOW (0V) + // if DDRx = 0 (input mode) : + // - 1 = pull-up on + // - 0 = pull-up off + // PINx : + + + SET(DDRB, D1); // make PB0 as OUTPUT (pullup is off) + CLEAR(PORTB, D1); // make PB0 as HIGH (LED turns ON) + + // SET(DDRB, D2); // make PB1 as OUTPUT (pullup is off) + // CLEAR(PORTB, D2); // make PB1 as LOW (LED turns OFF) + + // CLEAR(DDRB, D3); // make PB2 as INPUT + // SET(PORTB, D3); // make PB2 pullup on + + // CLEAR(DDRB, D4); // make PB4 as INPUT + // CLEAR(PORTB, D4); // make PB4 pullup off + + return 0; +} \ No newline at end of file