From ebb578db5fbbd469e84f444ded87cd6e74df5fcd Mon Sep 17 00:00:00 2001 From: hugo LAMY Date: Thu, 6 Mar 2025 16:23:18 +0100 Subject: [PATCH] setup mod02 ex00 and oneline comments --- README.md | 1 + module01/ex01/main.c | 11 ++----- module01/ex02/main.c | 15 +++------ module01/ex03/main.c | 15 +++------ module02/ex00/Makefile | 1 + module02/ex00/main.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 module02/ex00/Makefile create mode 100644 module02/ex00/main.c diff --git a/README.md b/README.md index 3e3c766..44ba6f5 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ - avrdude 6.3 doc : https://download-mirror.savannah.gnu.org/releases/avrdude/avrdude-doc-6.3.pdf - embedded systems : https://en.wikibooks.org/wiki/Embedded_Systems/Atmel_AVR - timers for newbies : https://qiriro.com/bme6163/static_files/notes/L3/Newbie%27s%20Guide%20to%20AVR%20Timers.pdf +- timer and pwm : https://wolles-elektronikkiste.de/en/timer-and-pwm-part-2-16-bit-timer1 ## m00ex00 diff --git a/module01/ex01/main.c b/module01/ex01/main.c index 7330abc..2fb2a4a 100644 --- a/module01/ex01/main.c +++ b/module01/ex01/main.c @@ -68,16 +68,11 @@ int main() { MODE_OUTPUT(LED2); - TCCR1B |= (1 << WGM12); // set timer in CTC (Clear Time on Compare) mode - // -> Table 16-4 : use bit WGM12 to set mode ctc - // -> 16.11.2 : bit WGM12 is located in register TCCR1B + TCCR1B |= (1 << WGM12); // Table 16-4 : set timer in CTC (Clear Time on Compare) mode, use bit WGM12, located in register TCCR1B (16.11.2) - TCCR1A |= (1 << COM1A0); // enable timer 1 Compare Output channel A in toggle mode - // -> Table 14-3 : alternate functions for PORTB1 is OC1A (Timer/Counter1 Output Compare Match A Output) - // -> 14.3.1 : OC1A/PCINT1 – Port B, Bit 1 + TCCR1A |= (1 << COM1A0); // 14.3.1 : set Compare Output with COM1A0, it toggles OC1A on compare match (Table 16-1), OC1A is alternate function for PORTB1 (Table 14-3) - OCR1A = TIME_MS(500); // set CTC compare value - // -> Table 16-4 : the value to reset the timer is the Output Compare Registers OCR1A + OCR1A = TIME_MS(500); // Table 16-4 : set CTC compare value, the counter is cleared to zero when the counter value (TCNT1) matches the OCR1A register TCCR1B |= (PRESCALE_SET); diff --git a/module01/ex02/main.c b/module01/ex02/main.c index 2ef21a2..9844ad2 100644 --- a/module01/ex02/main.c +++ b/module01/ex02/main.c @@ -72,22 +72,17 @@ int main() { MODE_OUTPUT(LED2); - // Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14) - SET(TCCR1A, WGM11); + SET(TCCR1A, WGM11); // Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14) SET(TCCR1B, WGM12); SET(TCCR1B, WGM13); - // Table 16-2 : non-inverting mode, the LED will be ON for DUTY_CYCLE% of the time (CLEAR OC1A on compare match, SET OC1A at BOTTOM) - SET(TCCR1A, COM1A1); + SET(TCCR1A, COM1A1); // Table 16-2 : non-inverting mode, the LED will be ON for DUTY_CYCLE% of the time (CLEAR OC1A on compare match, SET OC1A at BOTTOM) - // Table 16-4 : set the period (compare TOP value) - ICR1 = TIME_MS(PERIOD); + ICR1 = TIME_MS(PERIOD); // Table 16-4 : set the period (compare TOP value) - // 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A - OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD)); + OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD)); // 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A - // start the timer with the prescaler - TCCR1B |= (PRESCALE_SET); + TCCR1B |= (PRESCALE_SET); // start the timer with the prescaler while(1) { continue; diff --git a/module01/ex03/main.c b/module01/ex03/main.c index ba36f08..af12460 100644 --- a/module01/ex03/main.c +++ b/module01/ex03/main.c @@ -110,22 +110,17 @@ int main() { MODE_OUTPUT(LED2); - // Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14) - SET(TCCR1A, WGM11); + SET(TCCR1A, WGM11); // Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14) SET(TCCR1B, WGM12); SET(TCCR1B, WGM13); - // Table 16-2 : non-inverting mode, the LED will be ON for DUTY_CYCLE% of the time (CLEAR OC1A on compare match, SET OC1A at BOTTOM) - SET(TCCR1A, COM1A1); + SET(TCCR1A, COM1A1); // Table 16-2 : non-inverting mode, the LED will be ON for DUTY_CYCLE% of the time (CLEAR OC1A on compare match, SET OC1A at BOTTOM) - // Table 16-4 : set the period (compare TOP value) - ICR1 = TIME_MS(PERIOD); + ICR1 = TIME_MS(PERIOD); // Table 16-4 : set the period (compare TOP value) - // 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A - OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD)); + OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD)); // 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A - // start the timer with the prescaler - TCCR1B |= (PRESCALE_SET); + TCCR1B |= (PRESCALE_SET); // start the timer with the prescaler while(1) { on_press(SW1, increment_duty_cycle, ¶ms); diff --git a/module02/ex00/Makefile b/module02/ex00/Makefile new file mode 100644 index 0000000..2e40cba --- /dev/null +++ b/module02/ex00/Makefile @@ -0,0 +1 @@ +include ../../Makefile \ No newline at end of file diff --git a/module02/ex00/main.c b/module02/ex00/main.c new file mode 100644 index 0000000..0eb48ef --- /dev/null +++ b/module02/ex00/main.c @@ -0,0 +1,71 @@ +#include +#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) + +// TIMER +#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000) + +// UART +#define UART_BAUDRATE 115200 + +// END MACROS + +void uart_init() {} + +void uart_tx(char c) {} + +int main() { + uart_init(); + while (1) { + uart_tx('Z'); + _delay_ms(TIME_MS(1000)); + } +} \ No newline at end of file