[an error occurred while processing this directive] [an error occurred while processing this directive]
I2C - obrazets coda na HT-PICC. Esli zdes' ne vlezet - poshlu na milo.
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)
[an error occurred while processing this directive] [an error occurred while processing this directive]

Отправлено Ivanich 02 мая 2002 г. 20:27
В ответ на: микроконтроллеры PIC отправлено vovan1 02 мая 2002 г. 13:13

#define i2c_ack() ACKDT=0; ACKEN=1 // send acknowledge sequence
#define i2c_noack() ACKDT=1; ACKEN=1 // send noacknowledge sequence
#define Wd() asm("\tCLRWDT")

void i2c_WaitForIdle(void)
{
do
{
Wd(); // wait for idle and not writing
} while(((SSPCON2 & 0x1F) != 0) || (STAT_RW));
}

void i2c_start(void)
{
i2c_WaitForIdle();
RSEN = 1;
}

void i2c_stop(void)
{
i2c_WaitForIdle();
PEN=1;
}

unsigned char i2c_read(void)
{
unsigned char c;

i2c_WaitForIdle();
RCEN=1;
i2c_WaitForIdle();
c = SSPBUF;
i2c_WaitForIdle();
return c;
}

void i2c_write(unsigned char c)
{
i2c_WaitForIdle();
do
{
Wd();
WCOL = 0;
SSPBUF = c;
} while(WCOL);
}

unsigned char read_ext_eeprom(unsigned int address)
{
unsigned char data;

i2c_start();
i2c_write(0xa0);
i2c_write((unsigned char)(address >> 8));
i2c_write((unsigned char)(address & 0xFF));
i2c_WaitForIdle();
RSEN=1;
i2c_write(0xa1);
data=i2c_read();
i2c_noack();
i2c_stop();
return data;
}

void write_ext_eeprom(unsigned int address, unsigned char data)
{
i2c_start();
i2c_write(0xa0);
i2c_write((unsigned char)(address >> 8));
i2c_write((unsigned char)(address & 0xFF));
i2c_WaitForIdle();
RSEN = 1;
i2c_write(0xa1);
if(i2c_read() != data)
{
i2c_noack();
i2c_stop();
i2c_start();
i2c_write(0xa0);
i2c_write((unsigned char)(address >> 8));
i2c_write((unsigned char)(address & 0xFF));
i2c_write(data);
WrProt = 0;
i2c_stop();
do
{
i2c_start();
WrProt = 1;
i2c_write(0xA0);
i2c_WaitForIdle();
} while(ACKSTAT);
}
else
{
i2c_noack();
i2c_stop();
}
}

void write_ext_eeprom_page(unsigned int AddressPage, volatile unsigned char *data, unsigned char CountPage)
{
do
{
i2c_start();
i2c_write(0xa0);
i2c_write(byte1(AddressPage));
i2c_write(byte0(AddressPage));
i2c_WaitForIdle();
RSEN = 1;
i2c_write(0xa1);
while(i2c_read() == *data)
{
CountPage --;
if(!CountPage)
{
// Everything checked and matches, return
i2c_noack();
i2c_stop();
return;
}
i2c_ack();
data ++;
AddressPage ++;
}
// Mismatch foud, start writing from here
i2c_noack();
i2c_stop();

i2c_start();
i2c_write(0xa0);
i2c_write(byte1(AddressPage));
i2c_write(byte0(AddressPage));
while(CountPage != 0)
{
CountPage--;
i2c_write(*data++);
AddressPage ++;
if(!(byte0(AddressPage) & 0x3F))
break;
}
WrProt = 0;
i2c_stop();
do
{
i2c_start();
WrProt = 1;
i2c_write(0xA0);
i2c_WaitForIdle();
} while(ACKSTAT);
}
while(CountPage != 0);
}

void WipeExtEeprom(void)
{
unsigned int Addr = 0;
unsigned char c;

do
{
i2c_start();
i2c_write(0xa0);
i2c_write(byte1(Addr));
i2c_write(byte0(Addr));
c = 64;
do
{
i2c_write(EepromBlank);
} while(--c != 0);
WrProt = 0;
i2c_stop();
do
{
i2c_start();
WrProt = 1;
i2c_write(0xA0);
i2c_WaitForIdle();
} while(ACKSTAT);
Addr += 64;
}
while(Addr <= MaxExtEeprom);
}

Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru