Skip to content

Commit 35b7b49

Browse files
committedFeb 24, 2025
add GPIO (digital)
1 parent 0b66402 commit 35b7b49

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed
 

‎.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/.vscode
2-
2+
/build
3+
/hex
4+
main.c
5+
makefile

‎defs.h

+17-7
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@
5454
#ifndef DEFS_H
5555
#define DEFS_H
5656

57-
#define __AVR_ATmega328P__
58-
#define __AVR_ATmega328__
57+
#include <avr/io.h>
5958

60-
#include <avr/iom328p.h>
59+
#define INPUT 0
60+
#define OUTPUT 1
61+
62+
#define LOW 0
63+
#define HIGH 1
6164

62-
#define F_CPU 16000000UL
63-
#define BAUD 9600
6465

6566
// ANALOG
6667
// PORTC (PC0-PC6)
@@ -70,7 +71,7 @@
7071
#define PIN_A3 PC3
7172
#define PIN_A4 PC4
7273
#define PIN_A5 PC5
73-
#define NUM_ANALOG_PINS 8
74+
#define NUM_ANALOG_PINS 6
7475

7576

7677
// DIGITAL
@@ -95,7 +96,9 @@
9596
#define PIN_D6 PD6
9697
#define PIN_D7 PD7
9798

98-
// Data
99+
#define NUM_DIGITAL_PINS 14
100+
101+
// Data (UART)
99102
#define PIN_RX D0
100103
#define PIN_TX D1
101104

@@ -115,5 +118,12 @@
115118
// SYS
116119
#define RESET PC6
117120

121+
// PWM
122+
#define PIN_D11_PWM PIN_D11
123+
#define PIN_D10_PWM PIN_D10
124+
#define PIN_D9_PWM PIN_D9
125+
#define PIN_D5_PWM PIN_D5
126+
#define PIN_D3_PWM PIN_D3
127+
118128
#endif
119129

‎gpio.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef GPIO_H
2+
#define GPIO_H
3+
4+
#include "defs.h"
5+
6+
#define GPIO_BIT(_b) (1 << (_b))
7+
#define GPIO_BITUL(_b) (1UL << (_b))
8+
9+
#define GPIO_SET_BIT(port, bit) ((port) |= GPIO_BIT(bit))
10+
#define GPIO_CLEAR_BIT(port, bit) ((port) &= ~(GPIO_BIT(bit)))
11+
#define GPIO_TOGGLE_BIT(statePort, bit) ((statePort) ^= GPIO_BIT(bit))
12+
#define GPIO_READ_BIT(port, bit) ((port) & GPIO_BIT(bit))
13+
14+
#define GPIO_IS_VALID(pin) ((pin) >= PIN_D0 && (pin) <= PIN_D13) || ((pin) >= PIN_A0 && (pin) <= PIN_A5)
15+
16+
#define GPIO_GET_DDR_PORT(pin) \
17+
((pin) >= PIN_D8 && (pin) <= PIN_D13) ? (&DDRB) : \
18+
((pin) >= PIN_D0 && (pin) <= PIN_D7) ? (&DDRD) : \
19+
(&DDRC)
20+
21+
22+
#define GPIO_PIN_TO_BIT(pin) \
23+
( ((pin) >= PIN_D8 && (pin) <= PIN_D13) ? (pin - PIN_D8) : \
24+
((pin) >= PIN_D0 && (pin) <= PIN_D7) ? (pin - PIN_D0) : \
25+
((pin) - PIN_A0) )
26+
27+
28+
#define GPIO_GET_STATE_PORT(_DDRport) ((_DDRport) == &DDRB ? (&PORTB) : (_DDRport) == (&DDRD) ? (&PORTD) : (&PORTC))
29+
30+
#define _SetOutput(pin) GPIO_SET_BIT(*(volatile uint8_t*)(GPIO_GET_DDR_PORT(pin)), GPIO_PIN_TO_BIT(pin))
31+
#define _SetInput(pin) GPIO_CLEAR_BIT(*(volatile uint8_t*)(GPIO_GET_DDR_PORT(pin)), GPIO_PIN_TO_BIT(pin))
32+
33+
#define _SetPinHigh(pin) GPIO_SET_BIT(*(volatile uint8_t*)(GPIO_GET_STATE_PORT(GPIO_GET_DDR_PORT(pin))), GPIO_PIN_TO_BIT(pin))
34+
#define _SetPinLow(pin) GPIO_CLEAR_BIT(*(volatile uint8_t*)(GPIO_GET_STATE_PORT(GPIO_GET_DDR_PORT(pin))), GPIO_PIN_TO_BIT(pin))
35+
36+
37+
#define DigitalWritePin(pin, state) ((state) ? _SetPinHigh(pin) : _SetPinLow(pin))
38+
39+
#define PinMode(pin, mode) ((mode) ? (_SetOutput(pin)) : (_SetInput(pin)))
40+
41+
#define TogglePin(pin) GPIO_TOGGLE_BIT(*(volatile uint8_t*)(GPIO_GET_STATE_PORT(GPIO_GET_DDR_PORT(pin))), GPIO_PIN_TO_BIT(pin))
42+
43+
44+
45+
#endif /*PORT_MANIPULATION_H*/

0 commit comments

Comments
 (0)
Please sign in to comment.