Zbernica i2c: Rozdiel medzi revíziami
Zo stránky SensorWiki
Bez shrnutí editace |
Bez shrnutí editace |
||
Riadok 51: | Riadok 51: | ||
* Mitchell Kahn: ''[http://ap.urpi.fei.stuba.sk/mmp/doc/prog_i2c.pdf Programming the i2c interface].'' Dr. Dobb's Journal, June 1992. | * Mitchell Kahn: ''[http://ap.urpi.fei.stuba.sk/mmp/doc/prog_i2c.pdf Programming the i2c interface].'' Dr. Dobb's Journal, June 1992. | ||
=== Datasheets === | |||
* [http://www.atmel.com/images/doc0180.pdf AT24C02 EEPROM memory] | |||
* [http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf BlinkM Smart RGB LED] | |||
Verzia z 13:57, 16. marec 2016
I²C | |
![]() | |
Type | Bus |
Production history | |
---|---|
Designer | Philips Semiconductor, known
|
Designed | 1982; 34 years ago |
Data | |
Signal | Open drain |
Width | 1 bit (SDA) + clock (SCL) |
Bitrate | 0.1 / 0.4 / 1.0 / 3.4 / 5.0 Mbit/s (depending on mode) |
Protocol | Serial, Half Duplex |
I²C (Inter-Integrated Circuit), pronounced I-squared-C, is a multi-master, multi-slave, single-ended, serial computer bus invented by Philips Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C).
Since October 10, 2006, no licensing fees are required to implement the I²C protocol. However, fees are still required to obtain I²C slave addresses allocated by NXP.[1]
Several competitors, such as Siemens AG (later Infineon Technologies AG, now Intel mobile communications), NEC, Texas Instruments, STMicroelectronics (formerly SGS-Thomson), Motorola (later Freescale, now merged with NXP[2]), Nordic Semiconductor and Intersil, have introduced compatible I²C products to the market since the mid-1990s.
SMBus, defined by Intel in 1995, is a subset of I²C that defines the protocol use more strictly. One purpose of SMBus is to promote robustness and interoperability. Accordingly, modern I²C systems incorporate some policies and rules from SMBus, sometimes supporting both I²C and SMBus, requiring only minimal reconfiguration either by commanding or output pin use.
Literatúra
- Official I2C Specification Version 6 http://www.nxp.com/documents/user_manual/UM10204.pdf
- Official List of assigned NXP / Philips I2C addresses http://www.diolan.com/downloads/i2c-address-allocation-table.pdf
- Podrobný článok na Wikipedii: https://en.wikipedia.org/wiki/I%C2%B2C
- Pseudokod bitbang i2c https://en.wikipedia.org/wiki/I%C2%B2C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol
- Procyon SW library http://www.procyonengineering.com/embedded/avr/avrlib/docs/html/i2csw_8c-source.html
- Bitbanging by hand http://hackaday.com/2013/08/11/bitbanging-i2c-by-hand/
- http://hackaday.com/2015/06/25/embed-with-elliot-i2c-bus-scanning/
- http://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/
- Arduino TWI Library https://www.arduino.cc/en/Reference/Wire
- M24C02 Datasheet
- Mitchell Kahn: Programming the i2c interface. Dr. Dobb's Journal, June 1992.
Datasheets
Vsetko mozne k zbernici i2c (mozno presunut na stranku Category):
- http://www.i2c-bus.org/
- Analyzator: Bus Pirate + Logic Sniffer OLS:
Ukazkove programy (pre Arduino):
- Jednoduchy zapis
- Jednoduche citanie (vyzaduje aj zapis)
- Jednoduchy scan zbernice http://playground.arduino.cc/Main/I2cScanner
- Komplexny i2c scanner https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner
K. Zbernica i2c: EEPROM
Prečítajte obsah predloženej pamäti EEPROM a zobrazte na PC.
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/twi.h>
#include <stdio.h>
//#include "lcd.h"
#include "Uart_ch.h"
unsigned char RD_EEprom(unsigned char adr_RAM);
void WR_EEprom(unsigned char adr_IC,unsigned char adr_pocitadlo);
void obsluha_Stav_Aut_I2C(void);
#define ADR_IC_WR 0xA0 // EEPROM Adresa IC+WR
#define ADR_IC_RD 0xA1 // EEPROM Adresa IC+RD
volatile unsigned char buffer_I2C[10]; // buffer pre obsluhu I2C
FILE mystdout_Uart = FDEV_SETUP_STREAM(sendchar, NULL, _FDEV_SETUP_WRITE);
void WR_EEprom(unsigned char adr_IC,unsigned char adr_pocitadlo)
{ // nastavenie globalnych premennych
buffer_I2C[0]=adr_IC; // Adr obvodu + wr
buffer_I2C[1]=adr_pocitadlo; // Nastavenie Adresneho citaca 24LC04 , Adresa (z ktorej citam)/(do ktorej zapisujem)
// buffer_I2C[1]=<obsah>; // zapisovane data, byte,-ty
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); // send start condition
// a odovzdanie riadenia "preruseniu od I2C"
obsluha_Stav_Aut_I2C();
}
unsigned char RD_EEprom(unsigned char adr_RAM)
{
// nastavenie globalnych premennych
buffer_I2C[0]=ADR_IC_RD; // Adr obvodu + rd
buffer_I2C[1]=adr_RAM; // Sem vrati obsah z nastavenej adresy 24LC04
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); // send start condition
// a odovzdanie riadenia "preruseniu od I2C"
obsluha_Stav_Aut_I2C();
return(buffer_I2C[1]);
}
// tato funkcia vyuziva globalne premenne
// buffer_I2C[x]
// Wr_data - tato premenna nadobuda vyznam az pri komunikacii s konkretnou premennou
// vid. "instrukcny subor" I2C periferie
// implementovany instrukcny subor pre seriovu EEPROM max 256 byte
// S|adr_IO+wr|<AC>|P -- nastavenie adresneho citaca pamate EEPROM
// {nie je implementova} S|adr_IO+wr|<AC>|data|P -- zapisanie dat na nastavenu adresu pamate EEPROM
// S|adr_IO+rd|data|P -- precitanie jedneho bytu z aktualne nastavenej adresy pamate EEPROM
// obsah adresneho citaca sa inkrementne
void obsluha_Stav_Aut_I2C(void)
{ unsigned char pom_St_Aut=0;
nav:while ((TWCR & _BV(TWINT)) == 0); /* wait for transmission */
{ pom_St_Aut=TWSR & 0xf8; // precitaj stav
printf(" %02x",pom_St_Aut);
switch(pom_St_Aut)
{ case TW_START:// = 0x08 // odvysielany start
case TW_REP_START:// = 0x10 // odvysielany repeated start
TWDR = buffer_I2C[0];// Adr+wr/rd, adresa IO
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
break;
case TW_MT_SLA_ACK: // 0x18 Data byte has been tramsmitted and ACK received
TWDR = buffer_I2C[1]; // tu konkretne, nastavenie Adreseho citaca pamate.
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
break;
case TW_MT_DATA_ACK: // 0x28 Data byte has been tramsmitted and ACK received
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
return; // tento riadok v preruseni vypadne
break;
case TW_MR_SLA_ACK: // 0x40 // Data byte has been tramsmitted and NACK received
TWCR = _BV(TWINT) | _BV(TWEN) ; // clear interrupt to start transmission
break;
case TW_MR_DATA_NACK: // 0x58 // Data byte has been tramsmitted and ACK received
buffer_I2C[1]= TWDR; // Vycitaj jeden byte z 24LC04. Adresny citac sa posunie
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); // send stop condition
return; // tento riadok v preruseni vypadne
break;
default:
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
return; // tento riadok v preruseni vypadne
break;
}
}
goto nav; // toto v preruseni vypadne
}
int main(void)
{ unsigned char znak;
unsigned int i=0;
unsigned char poc_adresa_EEPROM=0;
inituart(); // inicializacia UARTU // inicializacia v 4-bitovom rezime
// sei(); // Enable ALL interrupts
// ini I2C
{
TWCR = _BV(TWEN); /* send start condition */
TWBR = 72; // fi2c= fosc/(16 +2(preddelic=00=> 1)(<TWBR>)) = 100kHz
TWSR=1;
}
// TODO: to USART
/* ************************************************************ */
stdout = &mystdout_Uart;
printf("Nastavenie adresy EEPROM = %02x \n\r",poc_adresa_EEPROM);
WR_EEprom(ADR_IC_WR,poc_adresa_EEPROM);
printf("\n\r\rVypis EEPROM: \n\r");
// vypisme prvych 10 znakov
while (i<10) //256)// kapacita pamate 256 znakov, bytov,
{
znak = RD_EEprom(ADR_IC_RD); // obsah adresneho citaca EEPROM sa automaticky inkrementuje
if(znak==0xff)i=256; // vymazane pametove miesto
{
printf(" %02d %02x %c\r",i++,znak,znak);
}
}
for(;;); // a cakam napr. na "RESET"
}