test_i2cmaster.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /****************************************************************************
  2. Title: Access serial EEPROM 24C02 using I2C interace
  3. Author: Peter Fleury <pfleury@gmx.ch>
  4. File: $Id: test_i2cmaster.c,v 1.3 2015/09/16 09:29:24 peter Exp $
  5. Software: AVR-GCC 4.x
  6. Hardware: any AVR device can be used when using i2cmaster.S or any
  7. AVR device with hardware TWI interface when using twimaster.c
  8. Description:
  9. This example shows how the I2C/TWI library i2cmaster.S or twimaster.c
  10. can be used to access a serial eeprom.
  11. *****************************************************************************/
  12. #include <avr/io.h>
  13. #include "i2cmaster.h"
  14. #define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet
  15. int main(void)
  16. {
  17. unsigned char ret;
  18. DDRB = 0xff; // use all pins on port B for output
  19. PORTB = 0xff; // (active low LED's )
  20. i2c_init(); // init I2C interface
  21. /* write 0x75 to eeprom address 0x05 (Byte Write) */
  22. ret = i2c_start(Dev24C02+I2C_WRITE); // set device address and write mode
  23. if ( ret ) {
  24. /* failed to issue start condition, possibly no device found */
  25. i2c_stop();
  26. PORTB=0x00; // activate all 8 LED to show error */
  27. }else {
  28. /* issuing start condition ok, device accessible */
  29. i2c_write(0x05); // write address = 5
  30. i2c_write(0x75); // ret=0 -> Ok, ret=1 -> no ACK
  31. i2c_stop(); // set stop conditon = release bus
  32. /* write ok, read value back from eeprom address 0x05, wait until
  33. the device is no longer busy from the previous write operation */
  34. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  35. i2c_write(0x05); // write address = 5
  36. i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
  37. ret = i2c_readNak(); // read one byte
  38. i2c_stop();
  39. PORTB = ~ret; // output byte on the LED's
  40. /* write 0x70,0x71,072,073 to eeprom address 0x00..0x03 (Page Write),
  41. wait until the device is no longer busy from the previous write operation */
  42. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  43. i2c_write(0x00); // write start address = 0
  44. i2c_write(0x70); // write data to address 0
  45. i2c_write(0x71); // " " " " 1
  46. i2c_write(0x72); // " " " " 2
  47. i2c_write(0x74); // " " " " 3
  48. i2c_stop(); // set stop conditon = release bus
  49. /* write ok, read value back from eeprom address 0..3 (Sequencial Read),
  50. wait until the device is no longer busy from the previous write operation */
  51. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  52. i2c_write(0x00); // write address = 0
  53. i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
  54. ret = i2c_readAck(); // read one byte form address 0
  55. ret = i2c_readAck(); // " " " " " 1
  56. ret = i2c_readAck(); // " " " " " 2
  57. ret = i2c_readNak(); // " " " " " 3
  58. i2c_stop(); // set stop condition = release bus
  59. PORTB = ~ret; // output byte on the LED's
  60. }
  61. for(;;);
  62. }