-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIO_MACROS.h
executable file
·59 lines (53 loc) · 1.94 KB
/
IO_MACROS.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef IO_MACROS_H_INCLUDED
#define IO_MACROS_H_INCLUDED
/*
||
|| Filename: IO_MACROS.h
|| Title: IO manipulation macros
|| Author: Efthymios Koktsidis
|| Email: [email protected]
|| Compiler: AVR-GCC
|| Description: This library contains macros for
|| easy port manipulation (similar
|| to Arduino).
||
|| Demo:
|| 1. #define LED A, 0 || 6. pinModeToggle(BUTTON);
|| 2. #define BUTTON A, 1 || 7. digitalWrite(LED, LOW);
|| 3. || 8. digitalWrite(LED, HIGH);
|| 4. pinMode(BUTTON, OUTPUT); || 9. digitalLevelToggle(LED);
|| 5. pinMode(LED, OUTPUT); ||10. int a = digitalRead(BUTTON);
||
*/
//----- I/O Macros -----
//Macros to edit PORT, DDR and PIN
#define pinMode( x, y) ( y ? _SET(DDR, x) : _CLEAR(DDR, x) )
#define digitalWrite( x, y) ( y ? _SET(PORT, x) : _CLEAR(PORT, x) )
#define digitalRead( x) ( _GET(PIN, x) )
#define pinModeToggle( x) ( _TOGGLE(DDR, x) )
#define digitalLevelToggle( x) ( _TOGGLE(PORT, x) )
//General use bit manipulating commands
#define bitSet( x, y) ( x |= (1UL<<y) )
#define bitClear( x, y) ( x &= (~(1UL<<y)) )
#define bitToggle( x, y) ( x ^= (1UL<<y) )
#define bitCheck( x, y) ( x & (1UL<<y) ? 1 : 0 )
//Access PORT, DDR and PIN
#define PORT( port) (_PORT( port))
#define DDR( port) (_DDR( port))
#define PIN( port) (_PIN( port))
#define _PORT( port) (PORT## port)
#define _DDR( port) (DDR## port)
#define _PIN( port) (PIN## port)
#define _SET( type, port, bit) ( bitSet( (type##port), bit) )
#define _CLEAR( type, port, bit) ( bitClear( (type##port), bit) )
#define _TOGGLE(type, port, bit) ( bitToggle( (type##port), bit) )
#define _GET( type, port, bit) ( bitCheck( (type##port), bit) )
//Definitions
#define INPUT 0
#define OUTPUT !INPUT
#define LOW 0
#define HIGH !LOW
#define FALSE 0
#define TRUE !FALSE
//------------------
#endif