/* Chen 24c01 (1 k) memory I2C bus Experiment C language example single-byte and unsigned long integer read/write program sda scl can be any common I/O */
// # Include <intrins. h>
# Define trytime 250
// Number of attempts to perform the operation again when the operation fails
// # Define SDA p42
// Data line
// # Define SCL p43
// Clock line
# Define i2cwrite 0xa0 // A2 A1 A0 is 0
# Define i2cread 0xa1
Unsigned char trytime;
// Function declaration
Void i2c_write4byte (unsigned long J, unsigned char position); // write four bytes of num at the position in the Add area, position = 1 corresponds to 4
Unsigned long i2c_read4byte (unsigned char position); // read four bytes at position, position = 1 corresponds to 4
Unsigned char i2c_read (unsigned char );
Void i2c_write (unsigned char, unsigned char );
Void i2c_send8bit (unsigned char );
Unsigned char i2c_receive8bit (void );
Void i2c_start (void );
Void i2c_stop (void );
Bit i2c_ack (void );
Void delay () // latency
{
Kmg ();
// Watchdog
}
Void i2c_start (void) // start signal for sending: SDA changes from high to low in high-power mode of the SCL.
{
SDA = 1;
Delay ();
SCL = 1;
Delay (); // Tsu: Start Time of the sta signal> = 0.6us
SDA = 0;
Delay (); // thd: Stas start signal holding time> = 0.6us
SCL = 0 ;//
Delay ();
}
Void i2c_stop (void) // sending end signal: SDA changes from low to high in high-power mode of the SCL.
{
SDA = 0;
Delay ();
SCL = 1;
Delay ();
SDA = 1;
Delay (); // Tsu: sto stop signal build time> = 0.6 us
}
Bit i2c_ack (void) // sends the receiving confirmation signal
{
Bit ack;
SDA = 1;
Delay ();
SCL = 1;
Delay ();
ACK = SDA;
SCL = 0;
Delay ();
Return (ACK );
}
Void i2c_send8bit (unsigned char B) // send eight-digit data: MSB value of SDA when the SCL changes from high to low
{
Unsigned char;
For (A = 0; A <8; A ++)
{
SDA = (BIT) (B <A) & 0x80 );
// B shifts left a bit to the highest bit
Delay ();
SCL = 1;
Delay (); // thigh clock High-Level Cycle> = 0.6us
SCL = 0;
Delay ();
}
}
Unsigned char i2c_receive8bit (void) // receives eight-digit data
// MSB
{
Unsigned char;
Unsigned char B = 0;
For (A = 0; A <8; A ++)
{
SCL = 1;
Delay ();
B = B <1;
// Shift B to the left
B = B | (unsigned char) (SDA); // The second bit of data stored in SDA B
SCL = 0;
Delay ();
}
Return (B );
}
Void i2c_write (unsigned char address, unsigned char data) // optional write
// I2c_write (address, data), write a byte
{
Trytime = trytime;
// Initialization
Do {
I2c_start (); // start command for sending the master device
I2c_send8bit (i2cwrite );
// The master device sends the address (R/W) of the slave device to the slave device.
If (i2c_ack ())
// = 1, indicating no confirmation, send again
{
If (0 = (trytime --))
{
Errno = error_i2c;
Return;
}
}
Else
Break;
} While (1 );
I2c_send8bit (Address );
// The master device sends the cat24wc01/02/04/08/16 byte address
I2c_ack ();
// The master device receives a response from another device.
I2c_send8bit (data );
// Send data to the addressable storage unit
I2c_ack ();
// Cat24wc01/02/04/08/16 reply again
I2c_stop ();
// Start Data erasure after the master device generates a stop signal
// Yans (10 );
// Cat24wc01/02/04/08/16 no longer responds to any request from the master device
Return;
}
Unsigned char i2c_read (unsigned char address)
// Selective read // data = i2c_read (address), read one byte
{
Unsigned char C;
Trytime = trytime;
// Initialization
Do {
I2c_start (); // start command for sending the master device
I2c_send8bit (i2cwrite );
// The master device sends the address (R/W) of the slave device to the slave device.
If (i2c_ack ())
// = 1, indicating no confirmation, send again
{
If (0 = (trytime --))
{
Errno = error_i2c;
Return 0;
}
}
Else
Break;
} While (1 );
I2c_send8bit (Address );
// Send data to the addressable storage unit
I2c_ack ();
// The master device receives a response from another device.
Do {
I2c_start ();
// Start command for sending the master device
I2c_send8bit (i2cread );
// The master device sends the slave device address (R/W position 1) to the slave device
} While (i2c_ack ());
C = i2c_receive8bit ();
I2c_ack ();
I2c_stop ();
// Yans (10 );
Return (C );
}
// Chen extended 4-byte read/write
Unsigned long i2c_read4byte (unsigned char position) // read 4 bytes at position, position = 1 corresponds to 4
{
Unsigned long J = 0;
Unsigned char I;
For (I = 0; I <4; I ++)
{
J = J * 256 + i2c_read (I + position * 4 );
// Read [12345678] [0] 12 [1] 34 with 0x0123
[2] 56 [3] 78
}
Return J;
}
Void i2c_write4byte (unsigned long J, unsigned char position) // write four bytes of num at the position in the Add area position, position = 1 corresponds to 4
{
Unsigned char I = 3;
Do
{
I2c_write (position * 4 + I, j );
// 0x12345678 inverted write [3210] [0] 12
[1] 34 [2] 56
[3] 78
J/= 256;
} While (I --);
}
/*
Void i2c_writesz (unsigned char * wdata, unsigned char address, unsigned char number) // batch write
// OK
{
Trytime = trytime;
// Initialization
Do {
I2c_start (); // start command for sending the master device
I2c_send8bit (i2cwrite );
// The master device sends the address (R/W) of the slave device to the slave device.
If (i2c_ack ())
// = 1, indicating no confirmation, send again
{
If (0 = (trytime --))
{
Errno = error_i2c;
Return;
}
}
Else
Break;
} While (1 );
I2c_send8bit (Address );
I2c_ack ();
For (; number! = 0; number --)
{
I2c_send8bit (* wdata );
I2c_ack ();
Wdata ++;
}
I2c_stop ();
}
// Read error
Void i2c_readsz (unsigned char * ramaddress, unsigned char address, unsigned char number) // batch read
{
Trytime = trytime;
// Initialization
Do {
I2c_start (); // start command for sending the master device
I2c_send8bit (i2cwrite );
// The master device sends the address (R/W) of the slave device to the slave device.
If (i2c_ack ())
// = 1, indicating no confirmation, send again
{
If (0 = (trytime --))
{
Errno = error_i2c;
Return;
}
}
Else
Break;
} While (1 );
I2c_send8bit (Address );
// Send data to the addressable storage unit
I2c_ack ();
// The master device receives a response from another device.
Do {
I2c_start ();
// Start command for sending the master device
I2c_send8bit (i2cread );
// The master device sends the slave device address (R/W position 1) to the slave device
} While (i2c_ack ());
For (; number! = 1; number --)
{
* Ramaddress = i2c_receive8bit ();
I2c_ack ();
Ramaddress ++;
}
* Ramaddress = i2c_receive8bit ();
I2c_ack ();
I2c_stop ();
}
// Chen extended 4-byte read/write OK
Unsigned long i2c_read4byte (unsigned char position) // read 4 bytes at position, position = 1 corresponds to 4
{
Unsigned long J = 0;
Unsigned char I;
I2c_readsz (SZ, position * 4, 4); // batch read
// Xssztest ();
For (I = 0; I <4; I ++)
{
J = J * 256 + SZ [I];
// Read [12345678] [0] 12 [1] 34 with 0x0123
[2] 56 [3] 78
}
Return J;
}
Void i2c_write4byte (unsigned long J, unsigned char position) // write four bytes of num at the position in the Add area position, position = 1 corresponds to 4
{
Unsigned char I = 3;
Do
{
SZ [I] = J;
// I2c_write (position * 4 + I, j );
// 0x12345678 inverted write [3210] [0] 12
[1] 34 [2] 56
[3] 78
J/= 256;
} While (I --);
I2c_writesz (SZ, position * 4, 4); // batch write
}
*/