1 | int main(void)
|
2 | {
|
3 | unsigned char ret;
|
4 |
|
5 |
|
6 | DDRB = 0xff; // use all pins on port B for output
|
7 | PORTB = 0xff; // (active low LED's )
|
8 |
|
9 | i2c_init(); // init I2C interface
|
10 |
|
11 | /* write 0x75 to eeprom address 0x05 (Byte Write) */
|
12 | ret = i2c_start(Dev24C02+I2C_WRITE); // set device address and write mode
|
13 | if ( ret ) {
|
14 | /* failed to issue start condition, possibly no device found */
|
15 | i2c_stop();
|
16 | PORTB=0x00; // activate all 8 LED to show error */
|
17 | }else {
|
18 | /* issuing start condition ok, device accessible */
|
19 | i2c_write(0x05); // write address = 5
|
20 | i2c_write(0x75); // ret=0 -> Ok, ret=1 -> no ACK
|
21 | i2c_stop(); // set stop conditon = release bus
|
22 |
|
23 | /* write ok, read value back from eeprom address 0x05, wait until
|
24 | the device is no longer busy from the previous write operation */
|
25 | i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
|
26 | i2c_write(0x05); // write address = 5
|
27 | i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
|
28 | ret = i2c_readNak(); // read one byte
|
29 | i2c_stop();
|
30 |
|
31 | PORTB = ~ret; // output byte on the LED's
|
32 |
|
33 | /* write 0x70,0x71,072,073 to eeprom address 0x00..0x03 (Page Write),
|
34 | wait until the device is no longer busy from the previous write operation */
|
35 | i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
|
36 | i2c_write(0x00); // write start address = 0
|
37 | i2c_write(0x70); // write data to address 0
|
38 | i2c_write(0x71); // " " " " 1
|
39 | i2c_write(0x72); // " " " " 2
|
40 | i2c_write(0x74); // " " " " 3
|
41 | i2c_stop(); // set stop conditon = release bus
|
42 |
|
43 | /* write ok, read value back from eeprom address 0..3 (Sequencial Read),
|
44 | wait until the device is no longer busy from the previous write operation */
|
45 | i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
|
46 | i2c_write(0x00); // write address = 0
|
47 | i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
|
48 | ret = i2c_readAck(); // read one byte form address 0
|
49 | ret = i2c_readAck(); // " " " " " 1
|
50 | ret = i2c_readAck(); // " " " " " 2
|
51 | ret = i2c_readNak(); // " " " " " 3
|
52 | i2c_stop(); // set stop condition = release bus
|
53 |
|
54 | PORTB = ~ret; // output byte on the LED's
|
55 | }
|
56 |
|
57 | for(;;);
|
58 | }
|