-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtypedefs.h
62 lines (43 loc) · 1.13 KB
/
typedefs.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
#ifndef TYPE_DEFS_H_
#define TYPE_DEFS_H_
#include <stdint.h>
//set bit or PORTB |= (1<<3);
//#define set_bit(port, bit) ((port) |= (uint8_t)(1 << bit))
//clear bit
//#define clr_bit(port, bit) ((port) &= (uint8_t)~(1 << bit))
#define INPUT 0
#define OUTPUT 1
typedef enum _BOOL { FALSE = 0, TRUE } BOOL;
// Code courtesy of: stu_san on AVR Freaks
typedef struct
{
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
} _io_reg;
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->bit##bt
/* Example:
#define BUTTON_PIN REGISTER_BIT(PINB,3)
#define LED_PORT REGISTER_BIT(PORTB,4)
#define BUTTON_DIR REGISTER_BIT(DDRB,3)
#define LED_DIR REGISTER_BIT(DDRB,4)
main()
{
uint8_t is_button = BUTTON_PIN;
// this actually is expanded by the C preprocessor to:
// uint8_t is_button = ((volatile _io_reg*)&PINB)->bit3;
LED_DIR = 1;
// which after the preprocessor looks like:
// ((volatile _io_reg*)&DDRB)->bit4 = 1;
BUTTON_DIR = 0;
while (1) {
LED_PORT = BUTTON_PIN;
}
}
*/
#endif