From 681e0dfa5e8d3c8836b23ff643e2026a5619e6ee Mon Sep 17 00:00:00 2001 From: hugo LAMY Date: Sat, 8 Mar 2025 21:24:17 +0100 Subject: [PATCH] mod00 ready for correction --- module00/ex00/main.c | 1 + module00/ex01/main.c | 85 +++++++++------------------------------- module00/ex02/main.c | 1 + module00/ex03/macros.h | 54 +++++++++++++++++++++++++ module00/ex03/main.c | 53 +------------------------ module00/ex04/bitmanip.h | 10 +++++ module00/ex04/elements.h | 42 ++++++++++++++++++++ module00/ex04/main.c | 55 ++------------------------ module00/ex04/utils.h | 12 ++++++ 9 files changed, 144 insertions(+), 169 deletions(-) create mode 100644 module00/ex03/macros.h create mode 100644 module00/ex04/bitmanip.h create mode 100644 module00/ex04/elements.h create mode 100644 module00/ex04/utils.h diff --git a/module00/ex00/main.c b/module00/ex00/main.c index 0eefc1c..1edd9a7 100644 --- a/module00/ex00/main.c +++ b/module00/ex00/main.c @@ -1,3 +1,4 @@ +// write a makefile that compiles the code and flashes it to the microcontroller int main() { } \ No newline at end of file diff --git a/module00/ex01/main.c b/module00/ex01/main.c index 7ee0059..4871571 100644 --- a/module00/ex01/main.c +++ b/module00/ex01/main.c @@ -1,5 +1,4 @@ #include -// #include #define SET(REGISTER, BIT) REGISTER |= 1 << BIT #define CLEAR(REGISTER, BIT) REGISTER &= ~(1 << BIT) @@ -11,75 +10,27 @@ #define D3 2 #define D4 4 +// 14.2 : registers : +// DDRx : (Data Direction Register) Controls whether each pin is an input or output +// - 1 = output (pull-up off) +// - 0 = input +// PORTx : (Port Data Register) +// if DDRx = 1 (output mode) : Controls the output state of a pin when it's configured as an output +// - 1 = HIGH (5V?) (VCC) +// - 0 = LOW (0V) (GND) +// if DDRx = 0 (input mode) : Controls the internal pull-up resistor when the pin is an input +// - 1 = pull-up on +// - 0 = pull-up off (pin floats) +// PINx : (Pin Input Register) Reads the current logical state of a pin +// Reflects the actual voltage on the pin +// Special feature: Writing 1 to PINxn toggles the corresponding PORTxn bit (regardless of DDRx) + +// turns on led 1 with only AVR registers (DDRX , PORTX, PINX) int main() { - // registers : - // DDRx : (Data Direction Register) Controls whether each pin is an input or output - // - 1 = output (pull-up off) - // - 0 = input - // PORTx : (Port Data Register) - // if DDRx = 1 (output mode) : Controls the output state of a pin when it's configured as an output - // - 1 = HIGH (5V?) (VCC) - // - 0 = LOW (0V) (GND) - // if DDRx = 0 (input mode) : Controls the internal pull-up resistor when the pin is an input - // - 1 = pull-up on - // - 0 = pull-up off (pin floats) - // PINx : (Pin Input Register) Reads the current logical state of a pin - // Reflects the actual voltage on the pin - // Special feature: Writing 1 to PINxn toggles the corresponding PORTxn bit (regardless of DDRx) - // turns on led 1 - SET(DDRB, D1); // make PB0 as OUTPUT (pullup is off) - SET(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 - - //////////////////////////////// - // TEST weird behavior of PIN register : - // in initial tests, using PIN register to toggle ON two leds only worked for the second one - // after some tests, this behavior is not anymore - // - // test 1 : only led 3 turns on - // SET(DDRB, D1); - // CLEAR(PORTB, D1); - // SET(PINB, D1); - // SET(DDRB, D3); - // CLEAR(PORTB, D3); - // SET(PINB, D3); - // - // test 2 : only led 3 turns on - // SET(DDRB, D1); - // CLEAR(PORTB, D1); - // SET(DDRB, D3); - // CLEAR(PORTB, D3); - // SET(PINB, D1); - // SET(PINB, D3); - // - // test 3 : both leds turns on - // SET(DDRB, D1); - // CLEAR(PORTB, D1); - // SET(DDRB, D3); - // CLEAR(PORTB, D3); - // PINB = (1 << D1) | (1 << D3); - // - // test 4 : both leds turns on - // SET(DDRB, D1); - // CLEAR(PORTB, D1); - // SET(DDRB, D3); - // CLEAR(PORTB, D3); - // SET(PINB, D1); - // _delay_us(500); - // SET(PINB, D3); - // - //////////////////////////////// - + SET(DDRB, D1); // make PB0 as OUTPUT (pullup is off) + SET(PORTB, D1); // make PB0 as HIGH (LED turns ON) return 0; } \ No newline at end of file diff --git a/module00/ex02/main.c b/module00/ex02/main.c index 4e03d41..3529569 100644 --- a/module00/ex02/main.c +++ b/module00/ex02/main.c @@ -52,6 +52,7 @@ // END MACROS +// turn on LED1 when BUTTON1 is pressed int main() { MODE_OUTPUT(LED1); diff --git a/module00/ex03/macros.h b/module00/ex03/macros.h new file mode 100644 index 0000000..c788cf3 --- /dev/null +++ b/module00/ex03/macros.h @@ -0,0 +1,54 @@ +#ifndef HEADERS_H +#define HEADERS_H + +// stringify +#define STRINGIFY_HELPER(x) #x +#define STRINGIFY(x) STRINGIFY_HELPER(x) + +// concatenate +#define CONCAT_HELPER(x, y) x ## y +#define CONCAT(x, y) CONCAT_HELPER(x, y) + +// get argument +#define ARG_1(v1, v2) v1 +#define ARG_2(v1, v2) v2 +#define GET_PORT(args) ARG_1 args +#define GET_BIT(args) ARG_2 args + +// actions on registers +#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 + +// actions on elements +#define SET_ELEM(elem) SET(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define CLEAR_ELEM(elem) CLEAR(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define TEST_ELEM(elem) TEST(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define TOGGLE_ELEM(elem) TOGGLE(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define MODE_OUTPUT(elem) SET(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) +#define MODE_INPUT(elem) CLEAR(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) +#define TOGGLE_PIN(elem) SET(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem)) +#define TEST_PIN(elem) (TEST(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem))) +#define IS_PIN_SET(elem) (TEST_PIN(elem) == 0) +#define IS_PIN_CLEAR(elem) (TEST_PIN(elem) == 1) + +// bits +#define D1 0 +#define D2 1 +#define D3 2 +#define D4 4 +#define SW1 2 +#define SW2 4 + +// elements (port, bit) +#define LED1 (B, D1) +#define LED2 (B, D2) +#define LED3 (B, D3) +#define LED4 (B, D4) +#define BUTTON1 (D, SW1) +#define BUTTON2 (D, SW2) + +#define BUTTON(id) + +#endif \ No newline at end of file diff --git a/module00/ex03/main.c b/module00/ex03/main.c index edf82c1..041d0de 100644 --- a/module00/ex03/main.c +++ b/module00/ex03/main.c @@ -1,58 +1,9 @@ #include #include -// stringify -#define STRINGIFY_HELPER(x) #x -#define STRINGIFY(x) STRINGIFY_HELPER(x) - -// concatenate -#define CONCAT_HELPER(x, y) x ## y -#define CONCAT(x, y) CONCAT_HELPER(x, y) - -// get argument -#define ARG_1(v1, v2) v1 -#define ARG_2(v1, v2) v2 -#define GET_PORT(args) ARG_1 args -#define GET_BIT(args) ARG_2 args - -// actions on registers -#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 - -// actions on elements -#define SET_ELEM(elem) SET(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define CLEAR_ELEM(elem) CLEAR(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define TEST_ELEM(elem) TEST(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define TOGGLE_ELEM(elem) TOGGLE(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define MODE_OUTPUT(elem) SET(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) -#define MODE_INPUT(elem) CLEAR(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) -#define TOGGLE_PIN(elem) SET(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem)) -#define TEST_PIN(elem) (TEST(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem))) -#define IS_PIN_SET(elem) (TEST_PIN(elem) == 0) -#define IS_PIN_CLEAR(elem) (TEST_PIN(elem) == 1) - -// bits -#define D1 0 -#define D2 1 -#define D3 2 -#define D4 4 -#define SW1 2 -#define SW2 4 - -// elements (port, bit) -#define LED1 (B, D1) -#define LED2 (B, D2) -#define LED3 (B, D3) -#define LED4 (B, D4) -#define BUTTON1 (D, SW1) -#define BUTTON2 (D, SW2) - -#define BUTTON(id) - -// END MACROS +#include "macros.h" +// toggle LED1 when BUTTON1 is pressed int main() { MODE_OUTPUT(LED1); MODE_INPUT(BUTTON1); diff --git a/module00/ex04/bitmanip.h b/module00/ex04/bitmanip.h new file mode 100644 index 0000000..2c00a8e --- /dev/null +++ b/module00/ex04/bitmanip.h @@ -0,0 +1,10 @@ +#ifndef BITMANIP_H +#define BITMANIP_H + +// Bit operations on registers +#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))) + +#endif // BITMANIP_H \ No newline at end of file diff --git a/module00/ex04/elements.h b/module00/ex04/elements.h new file mode 100644 index 0000000..a68d55b --- /dev/null +++ b/module00/ex04/elements.h @@ -0,0 +1,42 @@ +#ifndef ELEMENTS_H +#define ELEMENTS_H + +#include "utils.h" +#include "bitmanip.h" + +// Get arguments from tuple-like definitions +#define ARG_1(v1, v2) v1 +#define ARG_2(v1, v2) v2 +#define GET_PORT(args) ARG_1 args +#define GET_BIT(args) ARG_2 args + +// Actions on elements +#define SET_ELEM(elem) SET(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define CLEAR_ELEM(elem) CLEAR(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define TEST_ELEM(elem) TEST(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) +#define TOGGLE_ELEM(elem) TOGGLE(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) + +#define MODE_OUTPUT(elem) SET(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) +#define MODE_INPUT(elem) CLEAR(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) +#define TOGGLE_PIN(elem) SET(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem)) +#define TEST_PIN(elem) (TEST(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem))) +#define IS_PIN_SET(elem) (TEST_PIN(elem) == 0) +#define IS_PIN_CLEAR(elem) (TEST_PIN(elem) == 1) + +// Bit definitions +#define D1 0 +#define D2 1 +#define D3 2 +#define D4 4 +#define SW1 2 +#define SW2 4 + +// Elements (port, bit) +#define LED1 (B, D1) +#define LED2 (B, D2) +#define LED3 (B, D3) +#define LED4 (B, D4) +#define BUTTON1 (D, SW1) +#define BUTTON2 (D, SW2) + +#endif \ No newline at end of file diff --git a/module00/ex04/main.c b/module00/ex04/main.c index 200e181..5bb00d7 100644 --- a/module00/ex04/main.c +++ b/module00/ex04/main.c @@ -1,57 +1,9 @@ #include #include -// stringify -#define STRINGIFY_HELPER(x) #x -#define STRINGIFY(x) STRINGIFY_HELPER(x) - -// concatenate -#define CONCAT_HELPER(x, y) x ## y -#define CONCAT(x, y) CONCAT_HELPER(x, y) - -// get argument -#define ARG_1(v1, v2) v1 -#define ARG_2(v1, v2) v2 -#define GET_PORT(args) ARG_1 args -#define GET_BIT(args) ARG_2 args - -// actions on registers -#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 - -// actions on elements -#define SET_ELEM(elem) SET(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define CLEAR_ELEM(elem) CLEAR(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define TEST_ELEM(elem) TEST(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define TOGGLE_ELEM(elem) TOGGLE(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem)) -#define MODE_OUTPUT(elem) SET(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) -#define MODE_INPUT(elem) CLEAR(CONCAT(DDR, GET_PORT(elem)), GET_BIT(elem)) -#define TOGGLE_PIN(elem) SET(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem)) -#define TEST_PIN(elem) (TEST(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem))) -#define IS_PIN_SET(elem) (TEST_PIN(elem) == 0) -#define IS_PIN_CLEAR(elem) (TEST_PIN(elem) == 1) - -// bits -#define D1 0 -#define D2 1 -#define D3 2 -#define D4 4 -#define SW1 2 -#define SW2 4 - -// elements (port, bit) -#define LED1 (B, D1) -#define LED2 (B, D2) -#define LED3 (B, D3) -#define LED4 (B, D4) -#define BUTTON1 (D, SW1) -#define BUTTON2 (D, SW2) - -#define BUTTON(id) - -// END MACROS +#include "utils.h" +#include "bitmanip.h" +#include "elements.h" typedef struct { int *value; @@ -101,6 +53,7 @@ void on_press(int bit, void (*action)(void*), void *params) { } } +// write a program that increments and decrements a binary number using the buttons, and displays the result on the LEDs int main() { MODE_OUTPUT(LED1); MODE_OUTPUT(LED2); diff --git a/module00/ex04/utils.h b/module00/ex04/utils.h new file mode 100644 index 0000000..3a98eb2 --- /dev/null +++ b/module00/ex04/utils.h @@ -0,0 +1,12 @@ +#ifndef UTILS_H +#define UTILS_H + +// Stringify +#define STRINGIFY_HELPER(x) #x +#define STRINGIFY(x) STRINGIFY_HELPER(x) + +// Concatenate +#define CONCAT_HELPER(x, y) x ## y +#define CONCAT(x, y) CONCAT_HELPER(x, y) + +#endif // UTILS_H \ No newline at end of file