Embedded C Firmware Library : Arduino / Atmega328p  1
Register Level Embedded C Hardware Abstraction Library for AVR ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI
avr_gpio.h
Go to the documentation of this file.
1 
33 #ifndef AVR_GPIO_H_
34 #define AVR_GPIO_H_
35 
36 #include <avr/io.h>
37 
38 #pragma message ( "avr_gpio.h included" )
39 
44 #define GPIO_MODE_OUTPUT 0x00
45 #define GPIO_MODE_TRI_STATE 0x02
46 #define GPIO_MODE_INPUT_WITH_PULL_UP 0x01
47 #define GPIO_MODE_INPUT_WITHOUT_PULL_UP 0x02
54 #define LOW 0x00
55 #define HIGH 0x01
56 #define TOGGLE 0x02
63 #define PB &PORTB
64 #define PC &PORTC
65 #define PD &PORTD
66 
72 #define P0 (1<<0)
73 #define P1 (1<<1)
74 #define P2 (1<<2)
75 #define P3 (1<<3)
76 #define P4 (1<<4)
77 #define P5 (1<<5)
78 #define P6 (1<<6)
79 #define P7 (1<<7)
80 #define ALL_PINS 0xFF
81 
87 #define D0 PD,P0
88 #define D1 PD,P1
89 #define D2 PD,P2
90 #define D3 PD,P3
91 #define D4 PD,P4
92 #define D5 PD,P5
93 #define D6 PD,P6
94 #define D7 PD,P7
95 #define D8 PB,P0
96 #define D9 PB,P1
97 #define D10 PB,P2
98 #define D11 PB,P3
99 #define D12 PB,P4
100 #define D13 PB,P5
101 #define D14 PC,P0
102 #define D15 PC,P1
103 #define D16 PC,P2
104 #define D17 PC,P3
105 #define D18 PC,P4
106 #define D19 PC,P5
107 #define A0 PC,P0
108 #define A1 PC,P1
109 #define A2 PC,P2
110 #define A3 PC,P3
111 #define A4 PC,P4
112 #define A5 PC,P5
113 
119 #ifdef PD0
120 #undef PD0
121 #define PD0 PD,P0
122 #endif
123 #ifdef PD1
124 #undef PD1
125 #define PD1 PD,P1
126 #endif
127 #ifdef PD2
128 #undef PD2
129 #define PD2 PD,P2
130 #endif
131 #ifdef PD3
132 #undef PD3
133 #define PD3 PD,P3
134 #endif
135 #ifdef PD4
136 #undef PD4
137 #define PD4 PD,P4
138 #endif
139 #ifdef PD5
140 #undef PD5
141 #define PD5 PD,P5
142 #endif
143 #ifdef PD6
144 #undef PD6
145 #define PD6 PD,P6
146 #endif
147 #ifdef PD7
148 #undef PD7
149 #define PD7 PD,P7
150 #endif
151 #ifdef PB0
152 #undef PB0
153 #define PB0 PB,P0
154 #endif
155 #ifdef PB1
156 #undef PB1
157 #define PB1 PB,P1
158 #endif
159 #ifdef PB2
160 #undef PB2
161 #define PB2 PB,P2
162 #endif
163 #ifdef PB3
164 #undef PB3
165 #define PB3 PB,P3
166 #endif
167 #ifdef PB4
168 #undef PB4
169 #define PB4 PB,P4
170 #endif
171 #ifdef PB5
172 #undef PB5
173 #define PB5 PB,P5
174 #endif
175 #ifdef PC0
176 #undef PC0
177 #define PC0 PC,P0
178 #endif
179 #ifdef PC1
180 #undef PC1
181 #define PC1 PC,P1
182 #endif
183 #ifdef PC2
184 #undef PC2
185 #define PC2 PC,P2
186 #endif
187 #ifdef PC3
188 #undef PC3
189 #define PC3 PC,P3
190 #endif
191 #ifdef PC4
192 #undef PC4
193 #define PC4 PC,P4
194 #endif
195 #ifdef PC5
196 #undef PC5
197 #define PC5 PC,P5
198 #endif
199 
205 inline void GPIO_DisableGlobalPullUp();
206 inline void GPIO_EnableGlobalPullUp();
213 inline void GPIO_Init(volatile uint8_t* Port,uint8_t Pin,uint8_t PinMode);
214 inline void GPIO_InitPin(uint8_t Pin,uint8_t PinMode);
215 inline void GPIO_DeInit(volatile uint8_t* Port,uint8_t Pin);
216 inline void GPIO_DeInitPin(uint8_t Pin);
223 inline uint8_t GPIO_Read(volatile uint8_t* Port, uint8_t Pin);
224 inline uint8_t GPIO_ReadPin(uint8_t Pin);
225 inline void GPIO_Write(volatile uint8_t* Port, uint8_t Pin, uint8_t PinState);
226 inline void GPIO_Toggle(volatile uint8_t* Port, uint8_t Pin);
227 inline void GPIO_WritePinLow(uint8_t Pin);
228 inline void GPIO_WritePinHigh(uint8_t Pin);
229 inline void GPIO_WritePinToggle(uint8_t Pin);
238 {
239  MCUCR |= 1<<PUD;
240 }
241 
248 {
249  MCUCR &= ~(1<<PUD);
250 }
251 
260 inline void GPIO_Init(volatile uint8_t* Port, uint8_t Pin, uint8_t PinMode)
261 {
262  if (PinMode == GPIO_MODE_OUTPUT)
263  {
264  *(Port-0x01) |= Pin;
265  }
266  else if(PinMode == GPIO_MODE_INPUT_WITH_PULL_UP)
267  {
268  *(Port-0x01) &= ~(Pin);
269  *Port |= Pin;
271  }
272  else if(PinMode == GPIO_MODE_INPUT_WITHOUT_PULL_UP)
273  {
274  *(Port-0x01) &= ~(Pin);
275  *Port |= ~(Pin);
276  }
277 }
278 
286 inline void GPIO_InitPin(uint8_t Pin, uint8_t PinMode)
287 {
288  if (Pin < 8) /* pin 0 = PD0, ..., 7 = PD7 */
289  {
290  GPIO_Init(&PORTD,(1<<Pin),PinMode);
291  }
292  else if (Pin < 14) /* pin 8 = PB0, ..., 13 = PB5 (PB6 and PB7 reserved for External Clock) */
293  {
294  GPIO_Init(&PORTB,(1<<(Pin - 8)),PinMode);
295  }
296  else if (Pin < 21) /* pin 14 = PC0, ..., 19 = PC5 (PC6 is Reset, PC7 doesn't exist) */
297  {
298  GPIO_Init(&PORTC,(1<<(Pin - 14)),PinMode);
299  }
300 }
301 
309 inline void GPIO_DeInit(volatile uint8_t* Port, uint8_t Pin)
310 {
311  *(Port-0x01) &= ~(Pin);
312  *(Port-0x02) &= ~(Pin);
313  *(Port) &= ~(Pin);
314 }
315 
316 
323 inline void GPIO_DeInitPin(uint8_t Pin)
324 {
325  if (Pin < 8) /* pin 0 = PD0, ..., 7 = PD7 */
326  {
327  GPIO_DeInit(&PORTD,(1<<Pin));
328  }
329  else if (Pin < 14) /* pin 8 = PB0, ..., 13 = PB5 (PB6 and PB7 reserved for External Clock) */
330  {
331  GPIO_DeInit(&PORTB,(1<<(Pin - 8)));
332  }
333  else if (Pin < 21) /* pin 14 = PC0, ..., 19 = PC5 (PC6 is Reset, PC7 doesn't exist) */
334  {
335  GPIO_DeInit(&PORTC,(1<<(Pin - 14)));
336  }
337 }
338 
346 inline uint8_t GPIO_Read(volatile uint8_t* Port, uint8_t Pin)
347 {
348  return *(Port - 0x02) & (Pin);
349 }
350 
359 inline void GPIO_Write(volatile uint8_t* Port, uint8_t Pin, uint8_t PinState)
360 {
361  if (PinState == HIGH)
362  {
363  *Port |= Pin;
364  }
365  else if (PinState == LOW)
366  {
367  *Port &= ~(Pin);
368  }
369  else if (PinState == TOGGLE)
370  {
371  *(Port-0x02) = Pin;
372  }
373 }
374 
382 inline void GPIO_Toggle(volatile uint8_t* Port, uint8_t Pin)
383 {
384  *(Port-0x02) |= Pin; /* Uses a Single Instruction to Toggle a Bit */
385 
386  //*Port ^= Pin; /* Uses 4 Instructions to Toggle a Bit */
387 }
388 
395 inline uint8_t GPIO_ReadPin(uint8_t Pin)
396 {
397  if (Pin < 8) /* pin 0 = PD0, ..., 7 = PD7 */
398  {
399  return GPIO_Read(&PORTD,(1<<Pin));
400  }
401  else if (Pin < 14) /* pin 8 = PB0, ..., 13 = PB5 (PB6 and PB7 reserved for External Clock) */
402  {
403  return GPIO_Read(&PORTB,(1<<(Pin - 8)));
404  }
405  else if (Pin < 21) /* pin 14 = PC0, ..., 19 = PC5 (PC6 is Reset, PC7 doesn't exist) */
406  {
407  return GPIO_Read(&PORTC,(1<<(Pin - 14)));
408  }
409 }
410 
417 inline void GPIO_WritePinHigh(uint8_t Pin)
418 {
419  if (Pin < 8) /* pin 0 = PD0, ..., 7 = PD7 */
420  {
421  GPIO_Write(&PORTD,(1<<Pin),HIGH);
422  }
423  else if (Pin < 14) /* pin 8 = PB0, ..., 13 = PB5 (PB6 and PB7 reserved for External Clock) */
424  {
425  GPIO_Write(&PORTB,(1<<(Pin - 8)),HIGH);
426  }
427  else if (Pin < 21) /* pin 14 = PC0, ..., 19 = PC5 (PC6 is Reset, PC7 doesn't exist) */
428  {
429  GPIO_Write(&PORTC,(1<<(Pin - 14)),HIGH);
430  }
431 }
432 
439 inline void GPIO_WritePinLow(uint8_t Pin)
440 {
441  if (Pin < 8) /* pin 0 = PD0, ..., 7 = PD7 */
442  {
443  GPIO_Write(&PORTD,(1<<Pin),LOW);
444  }
445  else if (Pin < 14) /* pin 8 = PB0, ..., 13 = PB5 (PB6 and PB7 reserved for External Clock) */
446  {
447  GPIO_Write(&PORTB,(1<<(Pin - 8)),LOW);
448  }
449  else if (Pin < 21) /* pin 14 = PC0, ..., 19 = PC5 (PC6 is Reset, PC7 doesn't exist) */
450  {
451  GPIO_Write(&PORTC,(1<<(Pin - 14)),LOW);
452  }
453 }
454 
461 inline void GPIO_WritePinToggle(uint8_t Pin)
462 {
463  if (Pin < 8) /* pin 0 = PD0, ..., 7 = PD7 */
464  {
465  GPIO_Write(&PORTD,(1<<Pin),TOGGLE);
466  }
467  else if (Pin < 14) /* pin 8 = PB0, ..., 13 = PB5 (PB6 and PB7 reserved for External Clock) */
468  {
469  GPIO_Write(&PORTB,(1<<(Pin - 8)),TOGGLE);
470  }
471  else if (Pin < 21) /* pin 14 = PC0, ..., 19 = PC5 (PC6 is Reset, PC7 doesn't exist) */
472  {
473  GPIO_Write(&PORTC,(1<<(Pin - 14)),TOGGLE);
474  }
475 }
476 
477 #endif /* AVR_GPIO_H_ */
GPIO_Toggle
void GPIO_Toggle(volatile uint8_t *Port, uint8_t Pin)
Public Function to Toggle Output Value of Digital I/O Pin.
Definition: avr_gpio.h:382
GPIO_EnableGlobalPullUp
void GPIO_EnableGlobalPullUp()
Public Function to Enable Global Pull Up.
Definition: avr_gpio.h:247
GPIO_WritePinLow
void GPIO_WritePinLow(uint8_t Pin)
Public Function to Write Output Value of Digital I/O Pin to LOW.
Definition: avr_gpio.h:439
GPIO_InitPin
void GPIO_InitPin(uint8_t Pin, uint8_t PinMode)
Public Function to Configure and Initialize Digital I/O Pin.
Definition: avr_gpio.h:286
GPIO_Write
void GPIO_Write(volatile uint8_t *Port, uint8_t Pin, uint8_t PinState)
Public Function to Write Output Value Digital I/O Pin.
Definition: avr_gpio.h:359
GPIO_Init
void GPIO_Init(volatile uint8_t *Port, uint8_t Pin, uint8_t PinMode)
Public Function to Configure and Initialize Digital I/O Pin.
Definition: avr_gpio.h:260
GPIO_DeInitPin
void GPIO_DeInitPin(uint8_t Pin)
Public Function to De-Initialize Digital I/O Pin.
Definition: avr_gpio.h:323
GPIO_MODE_OUTPUT
#define GPIO_MODE_OUTPUT
Definition: avr_gpio.h:44
GPIO_MODE_INPUT_WITH_PULL_UP
#define GPIO_MODE_INPUT_WITH_PULL_UP
Definition: avr_gpio.h:46
LOW
#define LOW
Definition: avr_gpio.h:54
GPIO_WritePinHigh
void GPIO_WritePinHigh(uint8_t Pin)
Public Function to Write Output Value of Digital I/O Pin to HIGH.
Definition: avr_gpio.h:417
GPIO_ReadPin
uint8_t GPIO_ReadPin(uint8_t Pin)
Public Function to Read Digital I/O Pin.
Definition: avr_gpio.h:395
GPIO_DisableGlobalPullUp
void GPIO_DisableGlobalPullUp()
Public Function to Disable Global Pull Up.
Definition: avr_gpio.h:237
GPIO_MODE_INPUT_WITHOUT_PULL_UP
#define GPIO_MODE_INPUT_WITHOUT_PULL_UP
Definition: avr_gpio.h:47
GPIO_DeInit
void GPIO_DeInit(volatile uint8_t *Port, uint8_t Pin)
Public Function to De-Initialize Digital I/O Pin.
Definition: avr_gpio.h:309
TOGGLE
#define TOGGLE
Definition: avr_gpio.h:56
GPIO_Read
uint8_t GPIO_Read(volatile uint8_t *Port, uint8_t Pin)
Public Function to Read Digital I/O Pin.
Definition: avr_gpio.h:346
GPIO_WritePinToggle
void GPIO_WritePinToggle(uint8_t Pin)
Public Function to Write Output Value of Digital I/O Pin to TOGGLE.
Definition: avr_gpio.h:461
HIGH
#define HIGH
Definition: avr_gpio.h:55