-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin_macros.h
executable file
·114 lines (98 loc) · 4.07 KB
/
pin_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*----------------------------------------------------------------------------------------------
* pin_macros.h (AVR/avr-gcc)
*----------------------------------------------------------------------------------------------
*
* Author: Oleksandr Redchuk aka ReAl ([email protected])
*
* Description: port bits access macros for AVR uC family (avr-gcc compiler)
*
* Based on macros by Ascold Volkov, Andy Mozzevilov, Aleksey Musin
*----------------------------------------------------------------------------------------------
*/
/*
examples
#define Pin1 A,2,H
#define Pin2 B,3,L
^ ^ ^
port | |
bit \active level
void main(void)
{
DRIVER(Pin1,OUT); //as output
DRIVER(Pin2,IN); //as input
DRIVER(Pin2,PULLUP); // with pullup
ON(Pin1);
if (ACTIVE(Pin2)) OFF(Pin1);
if (LATCH(Pin1)) { //if active level presents on Pin1 for drive something åñëè àêòèâíûé óðîâåíü â PORT! ïðèñóòñòâóåò òî ÷òî òî äåëàåì
//do something
}
}
*/
#ifndef PIN_MACROS_H
#define PIN_MACROS_H
// assume chip has PORT_TOGGLE_BY_PIN_WRITE feature by default (all new AVR chips)
// check only old-style chips
#if defined(__AVR_AT94K__) \
|| defined(__AVR_AT43USB320__) || defined(__AVR_AT43USB355__) \
|| defined(__AVR_AT76C711__) || defined(__AVR_AT86RF401__) \
|| defined(__AVR_AT90C8534__) || defined(__AVR_AT90S4434__) \
|| defined(__AVR_AT90S1200__) || defined(__AVR_AT90S2343__) \
|| defined(__AVR_AT90S2333__) || defined(__AVR_AT90S2323__) \
|| defined(__AVR_AT90S2313__) || defined(__AVR_AT90S4433__) \
|| defined(__AVR_AT90S4414__) \
|| defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \
|| defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \
|| defined(__AVR_ATmega103__) \
|| defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) \
|| defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) \
|| defined(__AVR_ATmega32__) \
|| defined(__AVR_ATtiny11__) || defined(__AVR_ATtiny12__) \
|| defined(__AVR_ATtiny15__) || defined(__AVR_ATtiny28__) \
|| defined(__AVR_ATtiny22__) || defined(__AVR_ATtiny26__) \
|| defined(__AVR_ATmega161__) || defined(__AVR_ATmega162__) \
|| defined(__AVR_ATmega163__) || defined(__AVR_ATmega323__) \
|| defined(__AVR_ATmega8HVA__) || defined(__AVR_ATmega16HVA__)
# define PORT_TOGGLE_BY_PIN_WRITE 0
#else
# define PORT_TOGGLE_BY_PIN_WRITE 1
#endif
#define PM_BITNUM(port,bit,val) (bit)
#define BITNUM(x) PM_BITNUM(x)
#define BITMASK(x) (1<<PM_BITNUM(x))
#define PM_SETOUT(port,bit) (DDR##port |= (1<<(bit)))
#define PM_SETIN(port,bit) (DDR##port &= ~(1<<(bit)))
#define PM_SETPULLUP(port,bit) (PORT##port |= (1<<(bit)))
#define PM_SETHIZ(port,bit) (PORT##port &= ~(1<<(bit)))
#define PM_DRIVER(port,bit,val,mode) PM_SET##mode(port,bit)
/* dmode = OUT or IN, PULLUP or HIZ */
#define DRIVER(x,mode) PM_DRIVER(x,mode)// #define DB7 B,3,L, OUT; PM_SETOUT(B,3); DDRB |= (1<<(3));
#define PM_SETL(port,bit,dummy) (PORT##port &= ~(1<<(bit)))
#define PM_SETH(port,bit,dummy) (PORT##port |= (1<<(bit)))
#define PM_SET(port,bit,val) PM_SET##val(port,bit,dummy)
#define ON(x) PM_SET(x)
#define SET(x) PM_SETH(x)
#define CLR(x) PM_SETL(x)
#define PM_CLRL(port,bit,dummy) PM_SETH(port,bit,dummy)
#define PM_CLRH(port,bit,dummy) PM_SETL(port,bit,dummy)
#define PM_CLR(port,bit,val) PM_CLR##val(port,bit,dummy)
#define OFF(x) PM_CLR(x) //#define DB7 B,3,L; PM_CLR(B,3,L); B |= (1<<(3));
#define PM_PINH(port,bit,dummy) (PIN##port & (1<<(bit)))
#define PM_PINL(port,bit,dummy) !PM_PINH(port,bit,dummy)
#define PM_PIN(port,bit,val) PM_PIN##val(port,bit,dummy)
#define ACTIVE(x) PM_PIN(x)
#define PIN_H(x) PM_PINH(x)
#define PIN_L(x) PM_PINL(x)
#define PM_LATCHH(port,bit,dummy) (PORT##port & (1<<(bit)))
#define PM_LATCHL(port,bit,dummy) !PM_LATCHH(port,bit,dummy)
#define PM_LATCH(port,bit,val) PM_LATCH##val(port,bit,dummy)
#define LATCH(x) PM_LATCH(x)
#define LATCH_H(x) PM_LATCHH(x)
#define LATCH_L(x) PM_LATCHL(x)
#if PORT_TOGGLE_BY_PIN_WRITE
# define PM_CPL(port,bit,val) (PIN##port |= (1 << (bit)))
#else
# define PM_CPL(port,bit,val) (PORT##port ^= (1 << (bit)))
#endif
#define CPL(x) PM_CPL(x)
#define TOGGLE(x) PM_CPL(x)
#endif // PIN_MACROS_H