Skip to content

This repository provides explanations on USART for the AVR microcontroller, including its setup, and introduces the relevant libraries and APIs for configuration and usage.

License

Notifications You must be signed in to change notification settings

aKaReZa75/AVR_I2C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

I2C (Inter-Integrated Circuit) Communication Protocol

The I2C (Inter-Integrated Circuit) protocol is a widely used communication interface in the ATmega328 microcontroller, enabling multiple devices to communicate using just two wires: SDA (data) and SCL (clock). It supports master and slave modes, allowing the microcontroller to connect with various peripherals such as sensors, EEPROMs, and other microcontrollers. This repository covers the register-level configuration, practical usage examples, and important considerations for implementing I2C 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,
everything you need is clearly explained in this video:
aKaReZa 95 - Programming, Git - PART B
Make sure to check it out!

aKaReZa 95 - Programming, Git - PART B Thumbnail

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.

πŸ”— Resources

Here you'll find a collection of useful links and videos related to the topic of AVR microcontrollers.

IΒ²C / TWI Overview

I2C (TWI) Overview
β”œβ”€β”€ Documentation
β”‚   β”œβ”€ [TWI Bit Rate]
β”‚   β”‚   β”œβ”€ Registers β€” TWBR, TWSR (TWPS bits)
β”‚   β”‚   β”œβ”€ Formula β€” SCL frequency calculation
β”‚   β”‚   └─ Effects β€” Impact on communication speed and bus stability
β”‚   β”‚
β”‚   β”œβ”€ [TWI Control Operations]
β”‚   β”‚   β”œβ”€ Register β€” TWCR (Control Register)
β”‚   β”‚   β”œβ”€ Operations β€” START, STOP, ACK, NACK
β”‚   β”‚   └─ Bus control sequences
β”‚   β”‚
β”‚   └─ [TWI Status Codes]
β”‚       β”œβ”€ Register β€” TWSR (Status bits)
β”‚       β”œβ”€ Master & Slave status ranges
β”‚       └─ Usage β€” State machine control
β”‚    
β”œβ”€β”€ [API_Reference]
β”‚    β”œβ”€ File β€” i2c.h / i2c.c
β”‚    β”œβ”€ Functions β€” Init, Start, Stop, Write, writeAddress, Read, readAdress, readSequential
β”‚    └─ Purpose β€” High-level I2C master interface for communicating with slave devices, including sequential register access
|
└── Registers
    β”œβ”€ [TWBR] β€” TWI Bit Rate Register β€” Sets SCL clock frequency
    β”œβ”€ [TWSR] β€” TWI Status Register β€” Status codes and prescaler bits
    β”œβ”€ [TWAR] β€” TWI Address Register β€” Slave address and general call enable
    β”œβ”€ [TWDR] β€” TWI Data Register β€” Holds data for transmission/reception
    β”œβ”€ [TWCR] β€” TWI Control Register β€” Controls TWI operations and interrupts
    └─ [TWAMR] β€” TWI Address Mask Register β€” Address masking for multi-slave setups

Tip

The resources are detailed in the sections below.
To access any of them, simply click on the corresponding blue link.

  • Describes how to set the IΒ²C clock speed using TWBR and TWPS bits in TWSR, along with the formula for calculating SCL frequency. Includes examples for 100 kHz (Standard Mode) and 400 kHz (Fast Mode).

  • Details how to use TWCR to issue START and STOP conditions, send ACK/NACK, and manage TWI bus control sequences.

  • Provides a full table of TWI status codes from TWSR for both master and slave modes, with explanations of their meaning and how to use them in software-controlled state machines.

  • This section provides detailed descriptions of the functions in the i2c.h and i2c.c files for handling I2C master operations on the ATmega328 microcontroller. Each function is designed to manage communication with slave devices via the I2C bus, including sending and receiving data, controlling start/stop conditions, and performing sequential register-based reads. These APIs simplify I2C initialization and data exchange, making it easier to implement sensor interfacing and device control in your projects.

Videos Overview

AVR, Communication, I2C
   └── [aKaReZa 114 - AVR, I2C - PART A]
         β”œβ”€ Intro β€” Protocol overview and use cases.
         β”œβ”€ Registers β€” Block diagram and bit-level control.
         β”œβ”€ Safety β€” Atomic operations for reliable comms.
         └─ Functions β€” Init, Start, Write, Read, Sequential ops. 

Learn I2C communication in AVR microcontrollers from the ground up. This video covers protocol fundamentals, register-level analysis, atomic operations, and implementation of key functions like i2c_Init(), i2c_Start(), i2c_Write(), and moreβ€”perfect for building reliable sensor and module interfaces.

aKaReZa 114 – AVR, I2C - PART A Thumbnail

πŸ’» How to Use Git and GitHub

To access the repository files and save them on your computer, there are two methods available:

  1. 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.
  2. 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.

Clone using the URL.

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.

open Git Bash

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_I2C.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.

Clone the Repository

  • Then, press Enter to start the cloning operation and wait for the success message to appear.

Open the Library File

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.

Download Zip

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:

  1. 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.
  2. Download the repository:

    • Click the Code button to open a dropdown menu.
    • Select Download ZIP from the menu.

Download Zip

  1. Save the ZIP file:

    • Choose a location on your computer to save the ZIP file and click Save.
  2. 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.
  3. 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.

πŸ“ How to Ask Questions

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:

  1. Navigate to the "Issues" tab at the top of the repository page.

Issues

  1. Click on the "New Issue" button.

New Issue

  1. In the Title field, write a short summary of your issue or question.

  2. 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.

  3. Optionally, you can add labels, type, projects, or milestones to your issue for better categorization.

  4. Click on the "Submit new issue" button to post your question or issue.

Submeet New 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.
    Closed section
  • 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.

🀝 Contributing to the Repository

To contribute to this repository, please follow these steps:

  1. Fork the Repository
  2. Clone the Forked Repository
  3. Create a New Branch
  4. Make Your Changes
  5. Commit Your Changes
  6. Push Your Changes to Your Forked Repository
  7. 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.

🌟 Support Me

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.

πŸ“œ License

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.

βœ‰οΈ Contact Me

Feel free to reach out to me through any of the following platforms:

About

This repository provides explanations on USART for the AVR microcontroller, including its setup, and introduces the relevant libraries and APIs for configuration and usage.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages