⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.c

📁 一个在PROTEUS 中方真lpcarm的ucosii移植
💻 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 + -