📄 i2c_demo.c.bak
字号:
// *************** I2C driver Demo V 1.0 *************
// 通用性的 IO模拟方式的I2C接口操作
// 最大400KCLK@1.8V
// *******************************************************
#include <iom1281v.h>
#include <MACROS.h>
#include <DC_Defines.h>
#include "I2C.h"
void EEPROM_Write(unsigned char, unsigned int, unsigned char);
unsigned char EEPROM_Read(unsigned char, unsigned int);
// **************************************************************** //
// *** Init_I2C(); *** //
// *** This routine will setup the I2C port direction registers *** //
// **************************************************************** //
// ***************************************************************** //
// *** I2C_Start(); *** //
// *** This routine will set the I2C start condition on the bus, *** //
// *** All commands must be preceded by a START condition *** //
// ***************************************************************** //
// ***************************************************************** //
// *** I2C_Stop(); *** //
// *** This routine will set the I2C stop condition on the bus, *** //
// *** All commands must end with a STOP condition *** //
// ***************************************************************** //
// *********************************************************************** //
// *** Write_I2C_Control(0x0A,0,0); *** //
// *** This routine will write the I2C device code, the device address *** //
// *** setup on the hardware pins A0,A1 & A2, and also the W/R bit *** //
// *** So for an external EEPROM, such as the 24LC04B you would need *** //
// *** a device code of 1010 (0x0A), *** //
// *** hardware address 0 (if pins A0,A1 & A2 are left unconnected, *** //
// *** and the last parameter is R/W. Write is active low *** //
// *********************************************************************** //
// *********************************************************************** //
// *** I2C_Ackn(); *** //
// *** This routine will clock the ACK bit from the I2C slave device *** //
// *** it will return TRUE for a fail, and FALSE for a correct ACK bit *** //
// *********************************************************************** //
// ************************************************************** //
// *** Write_I2C_Byte(); *** //
// *** This routine will clock a byte to the slave I2C device *** //
// ************************************************************** //
// ************************************************************************** //
// *** Read_I2C_Byte(); *** //
// *** This routine will read and return a byte from the I2C slave device *** //
// ************************************************************************** //
// *********************************************************************** //
// *** Example of using Imagecraft I2C driver *** //
// *** to write to an external 8 bit address EEPROM *** //
// *** H_ADD is the hardware address set on the device A0,A1 & A2 pins *** //
// *** M_ADD is the devices internal memory address *** //
// *** Data is user data to be writen *** //
// *********************************************************************** //
void EEPROM_Write(unsigned char H_ADD, unsigned int M_ADD, unsigned char Data)
{
I2C_Start(); // Set I2C start condition
Write_I2C_Control(0x0A,H_ADD,0); // Send the EEPROM control Byte
Write_I2C_Byte(M_ADD>>8); // Send the EEPROM internal Address higher byte.
Write_I2C_Byte(M_ADD); // Send the EEPROM internal Address lower byte.
Write_I2C_Byte(Data); // Send the EEPROM Data
I2C_Stop(); // Set I2C Stop condition
}
// *********************************************************************** //
// *** Example of using Imagecraft I2C driver *** //
// *** to Read an external 8 bit address EEPROM *** //
// *** H_ADD is the hardware address set on the device A0,A1 & A2 pins *** //
// *** M_ADD is the devices internal memory address *** //
// *** Data is user data to be writen *** //
// *********************************************************************** //
unsigned char EEPROM_Read(unsigned char H_ADD, unsigned int M_ADD)
{
unsigned char Temp; // Temp RAM for EEPROM Read
I2C_Start(); // Set I2C start condition
Write_I2C_Control(0x0A,H_ADD,0); // Send the EEPROM control Byte
// Dummy write to set address
Write_I2C_Byte(M_ADD>>8); // Send the EEPROM internal Address Higher byte.
Write_I2C_Byte(M_ADD); // Send the EEPROM internal Address lower byte.
I2C_Start(); // Set I2C start condition
Write_I2C_Control(0x0A,H_ADD,1); // Send the EEPROM control Byte
Temp = Read_I2C_Byte(); // Read data from EEPROM
I2C_Stop(); // Set I2C Stop condition
return Temp; // Return data from EEPROM
}
//AT24C512B使用2BYTE片内地址。
unsigned char bf[26];
void main (void)
{
unsigned int i;
unsigned char j, k;
unsigned int c; // Temp Ram used for write delay
volatile unsigned char data;
DDRG |=BIT(PG1)|BIT(PG0);
PORTG |=BIT(PG1)|BIT(PG0); //VCC =high. Pull up=1.
I2C_DIR |= BIT(PD0)|BIT(PD1); // Set Port B to Outputs
Init_I2C(); // Setup the hardware port
while (true)
{
for (k=0,i=0x1050, j='A'; 1; i++,j++ )
{
EEPROM_Write( 1 , i , j );
for(c=0;c<5000;c++); // Delay for EEPROM Write
data = EEPROM_Read( 1, i ); // Read device 1, memory address 1
bf[k++] =data;
if (j=='Z') break;
}
for(c=0;c<5000;c++); // Delay for EEPROM Write
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -