mod00 ready for correction

This commit is contained in:
hugo LAMY
2025-03-08 21:24:17 +01:00
parent fe0e7fa223
commit 681e0dfa5e
9 changed files with 144 additions and 169 deletions

View File

@@ -1,3 +1,4 @@
// write a makefile that compiles the code and flashes it to the microcontroller
int main()
{
}

View File

@@ -1,5 +1,4 @@
#include <avr/io.h>
// #include <util/delay.h>
#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;
}

View File

@@ -52,6 +52,7 @@
// END MACROS
// turn on LED1 when BUTTON1 is pressed
int main()
{
MODE_OUTPUT(LED1);

54
module00/ex03/macros.h Normal file
View File

@@ -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

View File

@@ -1,58 +1,9 @@
#include <avr/io.h>
#include <util/delay.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)
// END MACROS
#include "macros.h"
// toggle LED1 when BUTTON1 is pressed
int main() {
MODE_OUTPUT(LED1);
MODE_INPUT(BUTTON1);

10
module00/ex04/bitmanip.h Normal file
View File

@@ -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

42
module00/ex04/elements.h Normal file
View File

@@ -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

View File

@@ -1,57 +1,9 @@
#include <avr/io.h>
#include <util/delay.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)
// 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);

12
module00/ex04/utils.h Normal file
View File

@@ -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