Picproje Elektronik Sitesi

MİKRODENETLEYİCİLER => Picproje Kütüphane (Library) => Konuyu başlatan: XX_CİHAN_XX - 18 Eylül 2009, 14:49:56

Başlık: Fast I2C
Gönderen: XX_CİHAN_XX - 18 Eylül 2009, 14:49:56
16 Bitlik adresleme yapan seri eepromlar için minik bir kütüphane sunuyorum.

Kullanılan Derleyici:

HI-TECH C PRO for the PIC10/12/16 MCU family  V9.65PL1


Kütüphanenin desteklediği fonksiyon seçenekleri:

unsigned char data8bit;
unsigned int data16bit;
unsigned int adres16bit;

Write_Char_Eeprom(adres16bit, data8bit);     //8 bitlik data yazar
Write_Int_Eeprom(adres16bit, data16bit);      //16 bitlik data yazar
data8bit = Read_Char_Eeprom(adres16bit);   //8 bitlik data okur
data16bit = Read_Int_Eeprom(adres16bit);    //16 bitlik data okur


Kütüphanenin desteklediği sistem gereksinimleri:

Pic12 ve Pic16 serilerine tam uyumlu olarak çalışır.
20Mhz osilatör frekansına kadar destekler.


i2c.c Dosyası:

#define ACK 0
#define NACK 1
#define I2C_MORE 0
#define I2C_LAST 1
#define BitSet(var,bitno)   ((var) |= 1UL << (bitno))
#define BitClear(var,bitno)   ((var) &= ~(1UL << (bitno)))
#define SCL RC3
#define SCL_DIR     TRISC3
#define SDA     RC4
#define SDA_DIR     TRISC4
#define FALSE 0
#define TRUE !FALSE
#define _XTAL_FREQ 20000000

void i2c_Start (void)
{
SCL = 0;
SDA = 0;
SDA_DIR = 1;
SCL_DIR = 1;
__delay_us(1);
SDA = 0;
SDA_DIR = 0;
__delay_us(1);
SCL = 0;
SCL_DIR = 0;
__delay_us(1);
}

void i2c_Stop (void)
{
SDA = 0;
SDA_DIR = 0;
SCL_DIR = 1;
__delay_us(1);
SDA_DIR = 1;
}

void i2c_SendBit (void)
{
SDA = 0;
SDA_DIR = CARRY;
SCL_DIR = 1;
__delay_us(1);
SCL = 0;
SCL_DIR = 0;
}

signed char i2c_ReadBit (void)
{
unsigned char a = 0;
SDA_DIR = 1;
SCL_DIR = 1;
__delay_us(1);
a = SDA;
SCL = 0;
SCL_DIR = 0;
return a;
}

unsigned char i2c_SendByte (unsigned char byte)
{
unsigned char i;
for(i=8; i!=0; i--)
{
byte = byte << 1;
i2c_SendBit();
}
i2c_ReadBit();
i = byte; //idle for byte
return ACK; //hata yok "0"
}

unsigned char i2c_GetByte (unsigned char more)
{
unsigned char byte = 0, i;
for(i=8; i!=0; i--)
{
byte = byte << 1;
byte |= i2c_ReadBit();
}
CARRY = more;
i2c_SendBit();
return byte;
}

unsigned char Read_Char_Eeprom (unsigned int adr)
{
unsigned char byte;
i2c_Start();
i2c_SendByte(0xA0);
i2c_SendByte(adr>>8);
i2c_SendByte(adr);
i2c_Start();
i2c_SendByte(0xA1);
byte = i2c_GetByte(I2C_LAST);
i2c_Stop();
return byte;
}

unsigned int Read_Int_Eeprom (unsigned int adr)
{
unsigned int byte;
i2c_Start();
i2c_SendByte(0xA0);
i2c_SendByte(adr>>8);
i2c_SendByte(adr);
i2c_Start();
i2c_SendByte(0xA1);
byte = i2c_GetByte(I2C_MORE);
byte = byte << 8;
byte |= i2c_GetByte(I2C_LAST);
i2c_Stop();
return byte;
}

void Write_Char_Eeprom (unsigned int adr, unsigned char byte)
{
i2c_Start();
i2c_SendByte(0xA0);
i2c_SendByte(adr>>8);
i2c_SendByte(adr);
i2c_SendByte(byte);
i2c_Stop();
}

void Write_Int_Eeprom (unsigned int adr, unsigned int byte)
{
i2c_Start();
i2c_SendByte(0xA0);
i2c_SendByte(adr>>8);
i2c_SendByte(adr);
i2c_SendByte(byte>>8);
i2c_SendByte(byte);
i2c_Stop();
}


Kodlar test edilmiştir. İki yazma periyodu arasına en az 6mS koymak gerekir. Isis simulasyonundada 400Khz@6ms lik olan memorylerden kullanılmalıdır.

Sağlıcakla kalın.
Başlık: Ynt: Fast I2C
Gönderen: izwirlee_35 - 26 Temmuz 2011, 00:55:18
teşekkür ediyorum çalışmanız için
Başlık: Ynt: Fast I2C
Gönderen: re3ii - 26 Temmuz 2011, 08:50:42
teşekkürler aradığım buydu.