Introduction to the I2C Bus

preview_player
Показать описание
Many devices such as accelerometers, temperature sensors, pressure sensors, etc. communicate on the I2C bus. In this video, I give a brief explanation of how the bus works and give an example of an I2C bus driver, written in C for the ATMEGA microcontroller.
Рекомендации по теме
Комментарии
Автор

Excellent work. The most succint, yet comprehensive tutorial I've seen for a very long time.

cybclinic
Автор

your i2c.c file is the most well-structured and easily understandable i2c library i have got on internet so far. thank you so much for your work

kunjan
Автор

I've searched for a good (and detailed) explanation and code examples for i2c, without having to read the entire i2c section in the datasheet of my MCU, the slave device and an i2c manual in general, and maybe it's just me and i failed to find what i was looking for, or there are just not that many good explanations out there ... but then i found this video ... it's the best and most detailed explanation/ code example i found in days ... you really helped me, thank you for sharing your knowledge <3

ohlo
Автор

Excellent video! Just started two days ago learning on Atmega328p how to write on I2C, but found difficult to understand how to read. at about 1:50 you just finished explanation that is MUST issue "repeated start condidtion" with next R/W bite high, then it can send address from where it can read. I tried various combinations, but on my O-scope getting various strange things - all wrong. Now it is more clear, thanks to your video. Atmel document about Atmega328p is not so clear and did not give example for reading, just for writting. Thanks again. Now I can continue watch your video (I have some issue with dementia, so rush to write comment before my brain get lost). :D

MilanKarakas
Автор

At video 22:35, the formula and setting is:
#define F_CPU
#define TWI_FREQ //100 kHz, 400 kHz, 800 kHz
TWBR = ((F_CPU/TWI_FREQ)-16)/2; //TWBR reg is decimal value of 72 for 100 kHz, 12 for 400 kHz, 2 for 800 kHz

MilanKarakas
Автор

Excellent Video!!! Thank you Craig for sharing it!!

embeddedsoftwareengineer
Автор

nice explanation...thank you for sharing this video.

prakashsurvase
Автор

At video 38:45, the only thing you can do to prevent MCU for hanging is to use watchdog. If everything is okay, then before "return(TWDR);" you should to include reset watchdog. If hang, then WDT will reset whole MCU. I am just begin to learning direct addressing and advanced programming and did not yet tried WDT, but I bet you will get the idea.

MilanKarakas
Автор

At 42:00, instead "goto ReadRestart;", does this code will do better job without using "goto"?
while(i2c_start())
{
if(--restarts==0) return(1);
}

MilanKarakas
Автор

Response to WildMania: Thanks for your comments, all are valid. I wrote the I2C code several years ago and based it on an example that came with the WinAVR compiler (same one used by Arduino). Yes, in good programming practice goto should be avoided but in this case it easily solved a problem. I didn't spend the time to work around using a goto, but I'm sure there could be a better solution.

Using the watchdog timer would work but is a rather 'brute force' method. There could be other more elegant solutions.

A quick internet search brought up this Wikipedea page on the I2C protocol which includes a description of the repeated start (under the heading 'Messaging Example: 24c32 EEPROM'):


Good luck with you journeys in the I2C world. Thanks for watching.

craighollinger
Автор

Hello! I am studying how the source code of an arduino-based sine wave inverter is still in the beginning of the study ... I want to learn how to make a small 500w sine wave inverter that is set to master and connect other inverters to it in the same grid of to work with their synchronized nenoidal waves. Could you give me some help or even some reading material? thankful.

hugocamaras
Автор

Hello where can I see the code you wrote?
I would really love to learn it, to know more about working with this form of communication.
Thank you

smilingman
Автор

Again, at 43:20, instead "goto" routine, maybe it will work something like this:

{
if(--restarts==0)
{
i2c_stop();
return(2);
}
}

MilanKarakas
Автор

Heh, now at 43:58 my brain "hang". :D Can't find proper solution for employing both routines without using "goto". Sorry.

MilanKarakas