?? i2c.c
字號:
/********************************************************************/
/********************************************************************/
/***** *****/
/***** L A B C E N T E R E L E C T R O N I C S *****/
/***** *****/
/***** PROTEUS VSM GNU CHESS SAMPLE *****/
/***** *****/
/***** LPC2000 I2C EEPROM Module *****/
/***** *****/
/********************************************************************/
/********************************************************************/
#include "config.h"
#define AA 0x04
#define SI 0x08
#define STO 0x10
#define STA 0x20
#define I2CEN 0x40
void eeprom_init ()
// Initialize the I2C Interface
{ PINSEL0 |= 0x00000050;
I2SCLL = 15; // I2C bus will run at 100kHz with 12MHz clock
I2SCLH = 15;
I2CONSET = I2CEN;
}
uint8 eeprom_read (uint8 addr)
// Read single byte of the EEPROM.
{ uint8 data;
i2c_start(0xA0); // Select write mode
i2c_write(addr);
i2c_start(0xA1); // Select read mode
data = i2c_read(FALSE);
i2c_stop();
return data;
}
void eeprom_write (uint8 addr, uint8 data)
// Write single byte of the EEPROM.
{ i2c_start(0xA0); // Select write mode
i2c_write(addr);
i2c_write(data);
i2c_stop();
}
uint8 i2c_start (uint8 addr)
{ // Send the start condition:
I2DAT = addr;
I2CONSET = STA;
while (!(I2CONSET & SI))
;
if ((I2STAT & 0xF8) != 0x08 && (I2STAT & 0xF8) != 0x10) // Start or restart
return FALSE;
// Send the slave address and read/write bit
//I2CONCLR |= STA;
I2CONCLR = SI;
while (!(I2CONSET & SI))
;
if (addr & 1)
{ if (((I2STAT & 0xF8) != 0x40))
return FALSE;
}
else
{ if ((I2STAT & 0xF8) != 0x18)
return FALSE;
}
// start condition and slave address are acknowledged:
return TRUE;
}
uint8 i2c_stop ()
{ // Send a stop:
I2CONSET = STO;
I2CONCLR = SI;
return TRUE;
}
uint8 i2c_read (uint8 ack)
{ if (ack)
I2CONSET = AA;
I2CONCLR = SI;
while (!(I2CONSET & SI))
;
return (I2STAT & 0xF0) == 0x50 ? I2DAT : -1;
}
uint8 i2c_write (uint8 data)
{ I2DAT = data;
I2CONCLR = SI;
while (!(I2CONSET & SI))
;
return (I2STAT & 0xF8) == 0x28 ? data : -1;
}
void I2C_Exception(void)
{}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -