update ex02 comments
This commit is contained in:
@@ -1,36 +1,36 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
|
||||||
// stringify
|
// stringify
|
||||||
#define STRINGIFY_HELPER(x) #x
|
#define STRINGIFY_HELPER(x) #x
|
||||||
#define STRINGIFY(x) STRINGIFY_HELPER(x)
|
#define STRINGIFY(x) STRINGIFY_HELPER(x)
|
||||||
|
|
||||||
// concatenate
|
// concatenate
|
||||||
#define CONCAT_HELPER(x, y) x ## y
|
#define CONCAT_HELPER(x, y) x ## y
|
||||||
#define CONCAT(x, y) CONCAT_HELPER(x, y)
|
#define CONCAT(x, y) CONCAT_HELPER(x, y)
|
||||||
|
|
||||||
// get argument
|
// get argument
|
||||||
#define ARG_1(v1, v2) v1
|
#define ARG_1(v1, v2) v1
|
||||||
#define ARG_2(v1, v2) v2
|
#define ARG_2(v1, v2) v2
|
||||||
#define GET_PORT(args) ARG_1 args
|
#define GET_PORT(args) ARG_1 args
|
||||||
#define GET_BIT(args) ARG_2 args
|
#define GET_BIT(args) ARG_2 args
|
||||||
|
|
||||||
// actions on registers
|
// actions on registers
|
||||||
#define SET(register, bit) register |= 1 << bit
|
#define SET(register, bit) register |= 1 << bit
|
||||||
#define CLEAR(register, bit) register &= ~(1 << bit)
|
#define CLEAR(register, bit) register &= ~(1 << bit)
|
||||||
#define TEST(register, bit) register & 1 << bit
|
#define TEST(register, bit) register & 1 << bit
|
||||||
#define TOGGLE(register, bit) register ^= 1 << bit
|
#define TOGGLE(register, bit) register ^= 1 << bit
|
||||||
|
|
||||||
// actions on elements
|
// actions on elements
|
||||||
#define SET_ELEM(elem) SET(CONCAT(PORT, GET_PORT(elem)), GET_BIT(elem))
|
#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 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 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 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_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 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 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 TEST_PIN(elem) (TEST(CONCAT(PIN, GET_PORT(elem)), GET_BIT(elem)))
|
||||||
#define IS_PIN_SET(elem) (TEST_PIN(elem) == 0)
|
#define IS_PIN_SET(elem) (TEST_PIN(elem) == 0)
|
||||||
#define IS_PIN_CLEAR(elem) (TEST_PIN(elem) == 1)
|
#define IS_PIN_CLEAR(elem) (TEST_PIN(elem) == 1)
|
||||||
|
|
||||||
// bits
|
// bits
|
||||||
#define D1 0
|
#define D1 0
|
||||||
@@ -76,13 +76,13 @@ int main() {
|
|||||||
SET(TCCR1B, WGM12);
|
SET(TCCR1B, WGM12);
|
||||||
SET(TCCR1B, WGM13);
|
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)
|
// 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);
|
||||||
|
|
||||||
// set the period (compare TOP value)
|
// Table 16-4 : set the period (compare TOP value)
|
||||||
ICR1 = TIME_MS(PERIOD);
|
ICR1 = TIME_MS(PERIOD);
|
||||||
|
|
||||||
// set the duty cycle to DUTY_CYCLE% of the time
|
// 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(DUTY_CYCLE);
|
OCR1A = TIME_MS(DUTY_CYCLE);
|
||||||
|
|
||||||
// start the timer with the prescaler
|
// start the timer with the prescaler
|
||||||
|
|||||||
Reference in New Issue
Block a user