This shows you the differences between two versions of the page.
| de:examples:pinops [2009/04/08 17:01] – angelegt nierhoff | de:examples:pinops [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Pin Operationen ====== | ||
| + | Der folgende Code vereinfacht Operationen mit den AVR Pins. | ||
| + | |||
| + | * {{examples: | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // AVR IO pin simple operation macros | ||
| + | // | ||
| + | // Mikk Leini | ||
| + | // Department of Mechatronics | ||
| + | // Tallinn University of Technology | ||
| + | // 2009 | ||
| + | // | ||
| + | |||
| + | // | ||
| + | // Gewoehnliche Funktionen zur Bit-Manipulation | ||
| + | // | ||
| + | #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, | ||
| + | |||
| + | // | ||
| + | // Funktionen zur Bit-Manipulation mit einer Bitmaske | ||
| + | // | ||
| + | #define SET_BITMASK(value, | ||
| + | #define CLEAR_BITMASK(value, | ||
| + | #define INVERT_BITMASK(value, | ||
| + | #define SET_BITMASK_TO(value, | ||
| + | #define IS_BITMASK_SET(value, | ||
| + | |||
| + | // | ||
| + | // Einfache Port Operationen | ||
| + | // | ||
| + | // Methode 1 | ||
| + | // | ||
| + | // Wenig Code, aber ermoeglicht nicht die Pin Value als Expression zu bekommen | ||
| + | // | ||
| + | |||
| + | #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 \ | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Einfache Port Operationen | ||
| + | // | ||
| + | // Methode 2 | ||
| + | // | ||
| + | // Ermoeglicht die Pin Value als Expression zu bekommen | ||
| + | // dafür allerdings Hardware-spezifisch | ||
| + | // | ||
| + | |||
| + | /* | ||
| + | #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, | ||
| + | ) | ||
| + | |||
| + | */ | ||
| + | </ | ||
| + | |||
| + | ===== Anwendungsbeispiel ===== | ||
| + | |||
| + | <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, | ||
| + | } | ||
| + | </ | ||