px-fwlib
0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers
generated with
Doxygen
1.9.2
gpio.c
1
/* =============================================================================
2
____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3
| _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4
| |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5
| __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6
|_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8
Copyright (c) 2006-2014 Pieter Conradie <https://piconomix.com>
9
10
License: MIT
11
https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13
============================================================================= */
14
15
/**
16
* @ingroup AVR_TUTORIALS
17
* @defgroup AVR_TUT01_GPIO 01_gpio
18
*
19
* How to configure and use general purpose (digital) I/O pins.
20
*
21
* File(s):
22
* - arch/avr/tutorials/01_gpio/gpio.c
23
*
24
* The LED shines while the push button is being pressed.
25
*
26
* Compact C techniques are used to manipulate individual bits of a Peripheral
27
* Control Register. It is good programming practice to encapsulate these
28
* often used and error prone pieces of code in macros, as has been
29
* demonstrated in @ref AVR_EX_FLASHING_LED.
30
*
31
* The following piece of code:
32
*
33
* @code{.c}
34
* PORTB = PORTB | 0x01; // Set bit 0 of PORTB register to enable LED
35
* PORTB = PORTB & 0xfe; // Clear bit 0 of PORTB to disable LED
36
* @endcode
37
*
38
* can be compacted to:
39
*
40
* @code{.c}
41
* PORTB |= (1 << 0); // Set bit 0 of PORTB register to enable LED
42
* PORTB &= ~(1 << 0); // Clear bit 0 of PORTB to disable LED
43
* @endcode
44
*
45
* and better yet, be replaced with macros:
46
*
47
* @code{.c}
48
* // General purpose bit manipulation macros
49
* #define BIT_SET_HI(var, bit) var |= (1 << bit)
50
* #define BIT_SET_LO(var, bit) var &= ~(1 << bit)
51
*
52
* // Define LED GPIO pin
53
* #define GPIO_LED_REG_PORT PORTB
54
* #define GPIO_LED_BIT 0
55
*
56
* // Define macros to turn LED on and off
57
* #define LED_ON() BIT_SET_HI(GPIO_LED_REG_PORT, GPIO_LED_BIT)
58
* #define LED_OFF() BIT_SET_LO(GPIO_LED_REG_PORT, GPIO_LED_BIT)
59
*
60
* void example_fn(void)
61
* {
62
* LED_ON(); // Enable LED
63
* LED_OFF(); // Disable LED
64
* }
65
* @endcode
66
*
67
* or best yet, use @ref AVR_GPIO. Here is an example:
68
*
69
* @include "avr/test/px_gpio_test.c"
70
*
71
* @tip_s
72
* Assuming Atmel Studio 7 is installed in it's default location, The
73
* peripheral registers and bit names are defined in:
74
* @tip_e
75
*
76
* C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\iom328p.h
77
*/
78
79
#include <avr/io.h>
80
#include <stdbool.h>
81
82
// LED is an output on PORTB, pin 0
83
// PB is an input (with pull-up enabled) on PORTD, pin 7
84
85
int
main(
void
)
86
{
87
// Configure LED as output pin (set bit 0 of DDRB register)
88
DDRB |= (1 << 0);
89
// Configure PB as input pin (clear bit 7 of DDRD register)
90
DDRD &= ~(1 << 7);
91
// Enable pull-up on PB (set bit 7 of PORTD register)
92
PORTD |= (1 << 7);
93
94
// Loop forever
95
while
(
true
)
96
{
97
// PB is pressed? (bit 7 of PIND register cleared?)
98
if
((PIND & (1 << 7)) == 0)
99
{
100
// Enable LED (set bit 0 of PORTB register)
101
PORTB |= (1 << 0);
102
}
103
else
104
{
105
// Disable LED (clear bit 0 of PORTB register)
106
PORTB &= ~(1 << 0);
107
}
108
}
109
}