mod01 update headers and descriptions
This commit is contained in:
47
module01/ex00/bitmanip.h
Normal file
47
module01/ex00/bitmanip.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef BITMANIP_H
|
||||
#define BITMANIP_H
|
||||
|
||||
#include "utils.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)))
|
||||
|
||||
// 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 // BITMANIP_H
|
||||
@@ -1,54 +1,9 @@
|
||||
#include <avr/io.h>
|
||||
|
||||
// stringify
|
||||
#define STRINGIFY_HELPER(x) #x
|
||||
#define STRINGIFY(x) STRINGIFY_HELPER(x)
|
||||
#include "utils.h"
|
||||
#include "bitmanip.h"
|
||||
|
||||
// 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)
|
||||
|
||||
// TIME
|
||||
// TIMER
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
@@ -62,6 +17,7 @@
|
||||
|
||||
// END MACROS
|
||||
|
||||
// write a program that blinks an LED every 500ms using a software timer
|
||||
int main() {
|
||||
MODE_OUTPUT(LED2);
|
||||
CLEAR_ELEM(LED2);
|
||||
|
||||
12
module01/ex00/utils.h
Normal file
12
module01/ex00/utils.h
Normal 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
|
||||
47
module01/ex01/bitmanip.h
Normal file
47
module01/ex01/bitmanip.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef BITMANIP_H
|
||||
#define BITMANIP_H
|
||||
|
||||
#include "utils.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)))
|
||||
|
||||
// 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 // BITMANIP_H
|
||||
@@ -1,77 +1,20 @@
|
||||
#include <avr/io.h>
|
||||
|
||||
// stringify
|
||||
#define STRINGIFY_HELPER(x) #x
|
||||
#define STRINGIFY(x) STRINGIFY_HELPER(x)
|
||||
#include "utils.h"
|
||||
#include "bitmanip.h"
|
||||
#include "timer.h"
|
||||
|
||||
// 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)
|
||||
|
||||
// TIME
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
#define PERIOD 500
|
||||
|
||||
// END MACROS
|
||||
|
||||
// led turns on and off every PERIOD ms
|
||||
// led turns on and off every PERIOD ms using CTC timer
|
||||
int main() {
|
||||
MODE_OUTPUT(LED2);
|
||||
|
||||
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)
|
||||
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); // 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)
|
||||
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(PERIOD); // Table 16-4 : set CTC compare value on channel A, the counter is cleared to zero when the counter value (TCNT1) matches the OCR1A register
|
||||
OCR1A = TIME_MS(PERIOD); // Table 16-4 : set CTC compare value on channel A, the counter is cleared to zero when the counter value (TCNT1) matches the OCR1A register
|
||||
|
||||
TCCR1B |= (PRESCALE_SET(PRESCALE_VALUE));
|
||||
|
||||
|
||||
15
module01/ex01/timer.h
Normal file
15
module01/ex01/timer.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
|
||||
#endif // TIMER_H
|
||||
12
module01/ex01/utils.h
Normal file
12
module01/ex01/utils.h
Normal 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
|
||||
47
module01/ex02/bitmanip.h
Normal file
47
module01/ex02/bitmanip.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef BITMANIP_H
|
||||
#define BITMANIP_H
|
||||
|
||||
#include "utils.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)))
|
||||
|
||||
// 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 // BITMANIP_H
|
||||
@@ -1,67 +0,0 @@
|
||||
#ifndef MYHEADER_H
|
||||
#define MYHEADER_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)
|
||||
|
||||
// TIME
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
#define PERIOD 1000
|
||||
#define DUTY_CYCLE 10
|
||||
#define PERCENT(percent, period) (((float)percent / 100) * period)
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,11 @@
|
||||
#include <avr/io.h>
|
||||
#include "headers.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "bitmanip.h"
|
||||
#include "timer.h"
|
||||
|
||||
#define PERIOD 1000
|
||||
#define DUTY_CYCLE 10
|
||||
|
||||
// turns on led2 at 1Hz and duty cycle of 10%, not using PORTx, with empty infinite loop
|
||||
int main() {
|
||||
|
||||
15
module01/ex02/timer.h
Normal file
15
module01/ex02/timer.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
|
||||
#endif // TIMER_H
|
||||
15
module01/ex02/utils.h
Normal file
15
module01/ex02/utils.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#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)
|
||||
|
||||
// mathematics
|
||||
#define PERCENT(percent, total) (((float)percent / 100) * total)
|
||||
|
||||
#endif // UTILS_H
|
||||
47
module01/ex03/bitmanip.h
Normal file
47
module01/ex03/bitmanip.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef BITMANIP_H
|
||||
#define BITMANIP_H
|
||||
|
||||
#include "utils.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)))
|
||||
|
||||
// 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 // BITMANIP_H
|
||||
@@ -1,68 +1,13 @@
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
// stringify
|
||||
#define STRINGIFY_HELPER(x) #x
|
||||
#define STRINGIFY(x) STRINGIFY_HELPER(x)
|
||||
#include "utils.h"
|
||||
#include "bitmanip.h"
|
||||
#include "timer.h"
|
||||
|
||||
// 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)
|
||||
|
||||
// TIME
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
#define PERIOD 1000
|
||||
#define DUTY_CYCLE 10
|
||||
#define PERCENT(percent, period) (((float)percent / 100) * period)
|
||||
|
||||
// Table 16-4 : Waveform Generation Mode Bit Description
|
||||
#define CTC_TOP_OCR1A_IN_TCCR1B (0<<WGM13 | 1<<WGM12)
|
||||
#define CTC_TOP_OCR1A_IN_TCCR1A (0<<WGM11 | 0<<WGM10)
|
||||
@@ -108,6 +53,7 @@ void on_press(int bit, void (*action)(void*), void *params) {
|
||||
}
|
||||
}
|
||||
|
||||
// led turns on and off every PERIOD ms using Fast PWM timer, with duty cycle controlled by SW1 and SW2
|
||||
int main() {
|
||||
int duty_cycle = DUTY_CYCLE;
|
||||
int max = 100;
|
||||
@@ -116,16 +62,16 @@ int main() {
|
||||
|
||||
MODE_OUTPUT(LED2);
|
||||
|
||||
TCCR1A |= FAST_PWM_TOP_ICR1_IN_TCCR1A; // Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14)
|
||||
TCCR1A |= FAST_PWM_TOP_ICR1_IN_TCCR1A; // Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14)
|
||||
TCCR1B |= FAST_PWM_TOP_ICR1_IN_TCCR1B;
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
ICR1 = TIME_MS(PERIOD); // Table 16-4 : set the period (compare TOP value)
|
||||
ICR1 = TIME_MS(PERIOD); // Table 16-4 : set the period (compare TOP value)
|
||||
|
||||
OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD)); // 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time in channel A -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A
|
||||
OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD)); // 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time in channel A -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A
|
||||
|
||||
TCCR1B |= (PRESCALE_SET(PRESCALE_VALUE)); // start the timer with the prescaler
|
||||
TCCR1B |= (PRESCALE_SET(PRESCALE_VALUE)); // start the timer with the prescaler
|
||||
|
||||
while(1) {
|
||||
on_press(SW1, increment_duty_cycle, ¶ms);
|
||||
|
||||
15
module01/ex03/timer.h
Normal file
15
module01/ex03/timer.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
|
||||
#endif // TIMER_H
|
||||
15
module01/ex03/utils.h
Normal file
15
module01/ex03/utils.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#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)
|
||||
|
||||
// mathematics
|
||||
#define PERCENT(percent, total) (((float)percent / 100) * total)
|
||||
|
||||
#endif // UTILS_H
|
||||
47
module02/ex00/bitmanip.h
Normal file
47
module02/ex00/bitmanip.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef BITMANIP_H
|
||||
#define BITMANIP_H
|
||||
|
||||
#include "utils.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)))
|
||||
|
||||
// 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 // BITMANIP_H
|
||||
15
module02/ex00/timer.h
Normal file
15
module02/ex00/timer.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
|
||||
// table 16-5 : prescale sets
|
||||
#define PRESCALE_SET(value) \
|
||||
((value) == 1 ? (0<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(value) == 8 ? (0<<CS12 | 1<<CS11 | 0<<CS10) : \
|
||||
(value) == 64 ? (0<<CS12 | 1<<CS11 | 1<<CS10) : \
|
||||
(value) == 256 ? (1<<CS12 | 0<<CS11 | 0<<CS10) : \
|
||||
(value) == 1024? (1<<CS12 | 0<<CS11 | 1<<CS10) : \
|
||||
(0<<CS12 | 0<<CS11 | 0<<CS10))
|
||||
#define TIME_MS(ms) (((F_CPU / PRESCALE_VALUE) * ms) / 1000)
|
||||
|
||||
#endif // TIMER_H
|
||||
15
module02/ex00/utils.h
Normal file
15
module02/ex00/utils.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#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)
|
||||
|
||||
// mathematics
|
||||
#define PERCENT(percent, total) (((float)percent / 100) * total)
|
||||
|
||||
#endif // UTILS_H
|
||||
Reference in New Issue
Block a user