Friday, January 5, 2024

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8

Overview

The AT24C16B is a serial EEPROM with a storage capacity of 16K. It uses I2C interface to communicate with microprocessor. The controller can write data to this memory up to 1 million cycles, with a 100 year data retention. 

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8

 

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8
AT24C16B DIP 8

For more details about this chip you can check its datasheet.

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8
Features

Write Protect (WP) pin must be wired to GND to allow data read and write. The operating voltage is between 1.8V to 5.5V DC. 







PIC16F887 I2C AT24C16B EEPROM LCD Using XC8
Block Diagram

This device is internally organized with 128 pages of 16 bytes each, the 16K requires an 11-bit word address for random word addressing.

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8
Device Address

With page 0 addressing we can use 0xA0 for I2c Write and 0xA1 for I2C Read.

PIC16F887 XC8 Programming

In this example, I use a PIC16F887 micro-controller to write and read data to and from this EEPROM chip. Those ASCII data will show on a 16x4 character LCD.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on January 5, 2024, 9:50 AM
  6.  */
  7.  
  8. #include <xc.h>
  9. #include "config.h"
  10. #include "i2c.h"
  11. #include "lcd.h"
  12.  
  13. const write_address=0xA0;
  14. const read_address=0xA1;
  15.  
  16. uint16_t write_24c16(uint16_t address,uint8_t *data){
  17. uint16_t write_size=0;
  18. i2c_start();
  19. i2c_write(write_address);
  20. i2c_write(0);
  21. while(*data){
  22. i2c_write(*data++);
  23. write_size++;
  24. while(WCOL);
  25. }
  26. i2c_stop();
  27. return write_size;
  28. }
  29.  
  30. uint8_t *read_24c16(uint16_t address, uint16_t size){
  31. uint8_t temp[16];
  32. for(uint16_t i=0;i<sizeof(temp);i++) temp[i]='\0';
  33. for(uint8_t i=0;i<size;i++){
  34. i2c_start();
  35. i2c_write(write_address);
  36. i2c_write(i);
  37. i2c_stop();
  38.  
  39. i2c_start();
  40. i2c_write(read_address);
  41. temp[i]=i2c_read(0);
  42. i2c_stop();
  43. }
  44. return temp;
  45. }
  46.  
  47. void main(void) {
  48. OSCCONbits.IRCF=7;
  49. lcdInit();
  50. i2c_init(1000);
  51. uint16_t length=0;
  52. lcdCommand(0x0C);
  53. __delay_ms(100);
  54.  
  55. length=write_24c16(0,"PIC16F887 24C16B");
  56. __delay_ms(10);
  57. lcdString(read_24c16(0,length));
  58.  
  59. lcdXY(1,2);
  60. length=write_24c16(20,"I2C Programming");
  61. __delay_ms(10);
  62. lcdString(read_24c16(20,length));
  63.  
  64. lcdXY(1,3);
  65. length=write_24c16(40,"With MPLABX IDE");
  66. __delay_ms(10);
  67. lcdString(read_24c16(40,length));
  68.  
  69. lcdXY(1,4);
  70. length=write_24c16(60,"XC8 C Compiler");
  71. __delay_ms(10);
  72. lcdString(read_24c16(60,length));
  73.  
  74. while(1);
  75. return;
  76. }
  77.  

This I2C EEPROM chip requires a read and write time between 5ms and 10ms. On start up, It will need to wait for a little time. I use 100ms delay at start up.

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8
Proteus Simulation

Proteus simulation creates a good result. However using a real hardware we need to fix all related issues.

PIC16F887 I2C AT24C16B EEPROM LCD Using XC8
PIC16F887 DIY PCB

Click here to download its source file.


No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)