This shows you the differences between two versions of the page.
| et:examples:pinops [2009/02/26 13:48] – tekitatud mikk.leini | et:examples:pinops [2009/04/13 17:27] (current) – eemaldatud mikk.leini | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Viikude operatsioonid ====== | ||
| - | Järgnevalt on toodud päisefail {{examples: | ||
| - | |||
| - | <code c> | ||
| - | // | ||
| - | // AVR IO pin simple operation macros | ||
| - | // | ||
| - | // Mikk Leini | ||
| - | // Department of Mechatronics | ||
| - | // Tallinn University of Technology | ||
| - | // 2009 | ||
| - | // | ||
| - | |||
| - | // | ||
| - | // Common bit manipulation functions | ||
| - | // | ||
| - | #define BIT(x) | ||
| - | #define SET_BIT(value, | ||
| - | #define CLEAR_BIT(value, | ||
| - | #define INVERT_BIT(value, | ||
| - | #define SET_BIT_TO(value, | ||
| - | #define IS_BIT_SET(value, | ||
| - | |||
| - | // | ||
| - | // Bit manipulation with bitmask | ||
| - | // | ||
| - | #define SET_BITMASK(value, | ||
| - | #define CLEAR_BITMASK(value, | ||
| - | #define INVERT_BITMASK(value, | ||
| - | #define SET_BITMASK_TO(value, | ||
| - | #define IS_BITMASK_SET(value, | ||
| - | |||
| - | // | ||
| - | // Simple port operations | ||
| - | // | ||
| - | // Method 1 | ||
| - | // | ||
| - | // Short code, but doesn' | ||
| - | // | ||
| - | |||
| - | #define PORTPIN(portChar, | ||
| - | DDR ## portChar = simple_dir_op(DDR ## portChar, bitIndex); \ | ||
| - | PORT ## portChar = simple_port_op(PORT ## portChar, bitIndex); \ | ||
| - | simple_pin_op(PIN ## portChar, bitIndex); | ||
| - | |||
| - | #define setup_output_pin(simplePin) \ | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value | BIT(bit); } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define setup_input_pin(simplePin) \ | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value & ~BIT(bit); } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value | BIT(bit); } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define setup_tristate_input_pin(simplePin) \ | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value & ~BIT(bit); } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define set_pin(simplePin) \ | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value | BIT(bit); } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define clear_pin(simplePin) \ | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value & ~BIT(bit); } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define set_pin_to(simplePin, | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) \ | ||
| - | { \ | ||
| - | return ((newState) ? (value | BIT(bit)) : (value & ~BIT(bit))); | ||
| - | } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define toggle_pin(simplePin) \ | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value ^ BIT(bit); } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { } \ | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | #define get_pin_value(simplePin, | ||
| - | { \ | ||
| - | inline unsigned char simple_dir_op(unsigned char value, unsigned char bit) { return value; } \ | ||
| - | inline unsigned char simple_port_op(unsigned char value, unsigned char bit) { return value | BIT(bit); } \ | ||
| - | inline void simple_pin_op(unsigned char value, unsigned char bit) { variable = IS_BIT_SET(value, | ||
| - | simplePin \ | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // Simple port operations | ||
| - | // | ||
| - | // Method 2 | ||
| - | // | ||
| - | // Support getting pin value in expression | ||
| - | // Downside is it's dependence of hardware | ||
| - | // | ||
| - | |||
| - | /* | ||
| - | #define SIMPLEPORT_A 0x0100 | ||
| - | #define SIMPLEPORT_B 0x0200 | ||
| - | #define SIMPLEPORT_C 0x0400 | ||
| - | #define SIMPLEPORT_D 0x0800 | ||
| - | #define SIMPLEPORT_E 0x1000 | ||
| - | #define SIMPLEPORT_F 0x2000 | ||
| - | #define SIMPLEPORT_G 0x4000 | ||
| - | #define SIMPLEPORT_H 0x8000 | ||
| - | |||
| - | #define PORTPIN(portChar, | ||
| - | |||
| - | #define SIMPLE_PORT_CHECK(simplePin, | ||
| - | #define SIMPLE_PIN_VALUE(simplePin) (simplePin) & 0x00FF | ||
| - | |||
| - | #define set_pin(simplePin) \ | ||
| - | { \ | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | } | ||
| - | |||
| - | #define clear_pin(simplePin) \ | ||
| - | { \ | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | } | ||
| - | |||
| - | #define set_pin_to(simplePin, | ||
| - | { \ | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | if SIMPLE_PORT_CHECK(simplePin, | ||
| - | } | ||
| - | |||
| - | #define is_pin_set(simplePin) \ | ||
| - | ( \ | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | (SIMPLE_PORT_CHECK(simplePin, | ||
| - | ) | ||
| - | |||
| - | */ | ||
| - | </ | ||
| - | |||
| - | ===== Näidis ===== | ||
| - | |||
| - | <code c> | ||
| - | #include < | ||
| - | #include " | ||
| - | |||
| - | #define LED1 PORTPIN(C, 1) | ||
| - | |||
| - | int main(void) | ||
| - | { | ||
| - | unsigned char x; | ||
| - | |||
| - | setup_output_pin(LED1); | ||
| - | set_pin(LED1); | ||
| - | clear_pin(LED1); | ||
| - | set_pin_to(LED1, | ||
| - | toggle_pin(LED1); | ||
| - | |||
| - | get_pin_value(LED1, | ||
| - | } | ||
| - | </ | ||