The General Purpose Input/Output (GPIO) peripheral is a fundamental feature in microcontrollers like the AVR series. It allows the microcontroller to interact with external devices such as LEDs, switches, sensors, and more. GPIO pins can be configured as either inputs or outputs, and their states can be read or written using specific registers. These pins offer a versatile way to communicate with the outside world, enabling a wide range of applications in embedded systems.
Tip If you're looking to better understand how to navigate and use my GitHub repositories β including exploring their structure, downloading or cloning projects, submitting issues, and asking questions, |
|
Caution It is absolutely critical that you carefully read every single word of this document, line by line, to ensure you don't miss any details. Nothing can be overlooked. |
In AVR microcontrollers, GPIO functionality is controlled through several registers:
The DDRx register controls the direction of the GPIO pins. Each bit corresponds to a specific pin, and setting a bit to 1
configures the corresponding pin as an output. Setting the bit to 0
configures it as an input.
Bit | Pin | Description |
---|---|---|
0 | PD0 | 1 = Output, 0 = Input |
1 | PD1 | 1 = Output, 0 = Input |
2 | PD2 | 1 = Output, 0 = Input |
3 | PD3 | 1 = Output, 0 = Input |
4 | PD4 | 1 = Output, 0 = Input |
5 | PD5 | 1 = Output, 0 = Input |
6 | PD6 | 1 = Output, 0 = Input |
7 | PD7 | 1 = Output, 0 = Input |
The PORTx register controls the output state of pins configured as outputs. Writing a 1
to a bit sets the corresponding pin to a high voltage level, while writing a 0
sets the pin to a low voltage level.
Bit | Pin | Description |
---|---|---|
0 | PD0 | 1 = High, 0 = Low |
1 | PD1 | 1 = High, 0 = Low |
2 | PD2 | 1 = High, 0 = Low |
3 | PD3 | 1 = High, 0 = Low |
4 | PD4 | 1 = High, 0 = Low |
5 | PD5 | 1 = High, 0 = Low |
6 | PD6 | 1 = High, 0 = Low |
7 | PD7 | 1 = High, 0 = Low |
The PINx register reads the current state of pins configured as inputs. If a bit is set to 1
, the corresponding pin is at a high voltage level. If the bit is 0
, the pin is at a low voltage level.
Bit | Pin | Description |
---|---|---|
0 | PD0 | 1 = High, 0 = Low |
1 | PD1 | 1 = High, 0 = Low |
2 | PD2 | 1 = High, 0 = Low |
3 | PD3 | 1 = High, 0 = Low |
4 | PD4 | 1 = High, 0 = Low |
5 | PD5 | 1 = High, 0 = Low |
6 | PD6 | 1 = High, 0 = Low |
7 | PD7 | 1 = High, 0 = Low |
Note
The macros used in the code below are all defined in the aKaReZa.h
header file, and detailed descriptions of these macros can be found at the following link:
https://github.com/aKaReZa75/AVR/blob/main/Macros.md
#include "aKaReZa.h"
/* Configure PD0 as output and PD1 as input */
GPIO_Config_OUTPUT(DDRD, 0); /**< Set PD0 as output */
GPIO_Config_INPUT(DDRD, 1); /**< Set PD1 as input */
bitSet(PORTD, 0); /**< Set PD0 to high */
/* Check if the bit 1 of PIND is high */
if (bitCheckHigh(PIND, 1))
{
bitWaitLow(PIND, 1); /**< Wait until bit 1 of PIND goes low */
bitToggle(PORTD, 0); /**< Toggle the bit 0 of PORTD (change the state of pin 0 of PORTD) */
}
-
Input Configuration: When configuring a pin as an input, ensure that the corresponding bit in the
DDRx
register is set to0
. If you accidentally set the pin to output and try to read from it, the value returned will not reflect the actual pin state. -
Internal Pull-Up Resistor: If you need to use a pin as an input and you want to avoid floating states, you can enable the internal pull-up resistor by writing a
1
to the corresponding bit in thePORTx
register. This is useful when no external resistor is connected to the input pin./* Enable internal pull-up resistor on PD1 */ bitSet(PORTD, 1); // Enable pull-up on PD1
-
Avoid Writing to Input Pins: Writing to a pin configured as input will have no effect, but it may cause confusion if you mistakenly attempt to do so. Ensure that you only write to pins configured as outputs.
-
Debouncing Buttons: When reading inputs from mechanical switches, be aware of button bounce. Consider adding a delay or implementing a software debouncing mechanism to ensure accurate reading of the button state.
-
PINx for Input Only: The
PINx
register should only be used to read the state of input pins. Reading from output pins withPINx
may give incorrect results as it returns the last value written to thePORTx
register. -
Port Pin Conflict: Be mindful of pins that may serve multiple purposes (e.g., a pin that is both an SPI pin and a regular GPIO). Check the datasheet to ensure proper pin configuration to avoid conflicts between functions.
Here you'll find a collection of useful links and videos related to the topic of AVR microcontrollers.
Tip
The resources are detailed in the sections below.
To access any of them, simply click on the corresponding blue link.
- This repository contains comprehensive resources for AVR microcontrollers, including hardware schematics, software libraries, and educational projects.
AVR, GPIO
βββ [aKaReZa 20 - AVR, GPIO - Part A]
β ββ Output Config β GPIO registers and pin setup.
β ββ Simulation β Proteus + LED control in PlatformIO/VSCode.
β ββ Timing Sync β Fuse bits vs compiler clock.
β ββ Macros β Improving code readability.
β
βββ [aKaReZa 23 - AVR, GPIO - Part B]
ββ Input Config β GPIO registers and pin reading.
ββ Simulation β Input handling in Proteus + PlatformIO/VSCode.
ββ Pull-Up β Enabling and using internal resistor.
ββ Logic Control β Handling input with `if` statements.
To access the repository files and save them on your computer, there are two methods available:
-
Using Git Bash and Cloning the Repository
- This method is more suitable for advanced users and those familiar with command-line tools.
- By using this method, you can easily receive updates for the repository.
-
Downloading the Repository as a ZIP file
- This method is simpler and suitable for users who are not comfortable with command-line tools.
- Note that with this method, you will not automatically receive updates for the repository and will need to manually download any new updates.
First, open Git Bash :
- Open the folder in File Explorer where you want the library to be stored.
- Right-click inside the folder and select the option "Open Git Bash here" to open Git Bash in that directory.
Note
If you do not see the "Open Git Bash here" option, it means that Git is not installed on your system.
You can download and install Git from this link.
For a tutorial on how to install and use Git, check out this video.
- Once Git Bash is open, run the following command to clone the repository:
git clone https://github.com/aKaReZa75/AVR_GPIO.git
- You can copy the above command by either:
- Clicking on the Copy button on the right of the command.
- Or select the command text manually and press Ctrl + C to copy.
- To paste the command into your Git Bash terminal, use Shift + Insert.
- Then, press Enter to start the cloning operation and wait for the success message to appear.
Important
Please keep in mind that the numbers displayed in the image might vary when you perform the same actions.
This is because repositories are continuously being updated and expanded. Nevertheless, the overall process remains unchanged.
Note
Advantage of Cloning the Repository:
- Receiving Updates: By cloning the repository, you can easily and automatically receive new updates.
- Version Control: Using Git allows you to track changes and revert to previous versions.
- Team Collaboration: If you are working on a project with a team, you can easily sync changes from team members and collaborate more efficiently.
If you prefer not to use Git Bash or the command line, you can download the repository directly from GitHub as a ZIP file.
Follow these steps:
-
Navigate to the GitHub repository page and Locate the Code button:
- On the main page of the repository, you will see a green Code button near the top right corner.
-
Download the repository:
- Click the Code button to open a dropdown menu.
- Select Download ZIP from the menu.
-
Save the ZIP file:
- Choose a location on your computer to save the ZIP file and click Save.
-
Extract the ZIP file:
- Navigate to the folder where you saved the ZIP file.
- Right-click on the ZIP file and select Extract All... (Windows) or use your preferred extraction tool.
- Choose a destination folder and extract the contents.
-
Access the repository:
- Once extracted, you can access the repository files in the destination folder.
Important
- No Updates: Keep in mind that downloading the repository as a ZIP file does not allow you to receive updates.
If the repository is updated, you will need to download it again manually. - Ease of Use: This method is simpler and suitable for users who are not comfortable with Git or command-line tools.
If you have any questions or issues, you can raise them through the "Issues" section of this repository. Here's how you can do it:
- Navigate to the "Issues" tab at the top of the repository page.
- Click on the "New Issue" button.
-
In the Title field, write a short summary of your issue or question.
-
In the "Description" field, detail your question or issue as thoroughly as possible. You can use text formatting, attach files, and assign the issue to someone if needed. You can also use text formatting (like bullet points or code snippets) for better readability.
-
Optionally, you can add labels, type, projects, or milestones to your issue for better categorization.
-
Click on the "Submit new issue" button to post your question or issue.
I will review and respond to your issue as soon as possible. Your participation helps improve the repository for everyone!
Tip
- Before creating a new issue, please check the "Closed" section to see if your question has already been answered.
- Write your question clearly and respectfully to ensure a faster and better response.
- While the examples provided above are in English, feel free to ask your questions in Persian (ΩΨ§Ψ±Ψ³Ϋ) as well.
- There is no difference in how they will be handled!
Note
Pages and interfaces may change over time, but the steps to create an issue generally remain the same.
To contribute to this repository, please follow these steps:
- Fork the Repository
- Clone the Forked Repository
- Create a New Branch
- Make Your Changes
- Commit Your Changes
- Push Your Changes to Your Forked Repository
- Submit a Pull Request (PR)
Note
Please ensure your pull request includes a clear description of the changes youβve made. Once submitted, I will review your contribution and provide feedback if necessary.
If you found this repository useful:
- Subscribe to my YouTube Channel.
- Share this repository with others.
- Give this repository and my other repositories a star.
- Follow my GitHub account.
This project is licensed under the GPL-3.0 License. This license grants you the freedom to use, modify, and distribute the project as long as you:
- Credit the original authors: Give proper attribution to the original creators.
- Disclose source code: If you distribute a modified version, you must make the source code available under the same GPL license.
- Maintain the same license: When you distribute derivative works, they must be licensed under the GPL-3.0 too.
- Feel free to use it in your projects, but make sure to comply with the terms of this license.
Feel free to reach out to me through any of the following platforms:
- π§ Email: aKaReZa75@gmail.com
- π₯ YouTube: @aKaReZa75
- πΌ LinkedIn: @akareza75