Tuesday, February 13, 2024

PIC16F887 SPI 25AA010A EEPROM XC8 Example

Overview

The 25AA010A is 1Kbit SPI bus serial EEPROM. It could handle up to 10MHz clock frequency with a high endurance of 1M erase/write cycles. Its memory space is organized into 128x8-bit.

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Simulating Program in Proteus

It has various device's package including a DIP-8 package for hobbyists prototyping.

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Device Selection Table

I don't have a physical device of this chip. So I just take a model of this chip to simulate in Proteus.

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Package Types and Pin Function Table

The SPI transfer and receive must have the following data bytes, instruction, address, and data.

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Block Diagram, Instructions, and SPI Read Sequence
 

The read command is 0x03 follows by the EEPROM address. The EEPROM 8-bit data will shift out by the SPI read routine issued by the microprocessor. 

Writing data to the EEPROM requires around 5ms. It requires a write enable instruction before the data writing sequence able to process.

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Write Enable and Disable Commands
 

Write-enable command is 0x06 while the Write-disable command is 0x04. Additionally the Write Protect (WP) pin must pull high to enable data writing.

Byte write sequence instruction is 0x02 follows by the EEPROM address and the 8-bit data. It also has a page write sequence to write a burst of 16 bytes data package. The microprocessor needs to wait around 5ms to complete data writing.
 

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Byte and Page Write Sequences
 

The Read Status Register Instruction is optional you can read the device datasheet for more detail. 

PIC16F887 MPLABX IDE and XC8 Programming

I use a PIC16F887 micro-controller from Microchip Technology. It has the SPI module inside along with the I2C module. The XC8 C compiler is free to use without registering or buy a software license.

PIC16F887 SPI 25AA010A EEPROM XC8 Example
Screen Shot of This Example Program

In this example, the microprocessor write and read the EEPROM data from this SPI chip. It will show on a 20x2 character LCD and PORTB of PIC16F887 micro-controller.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on February 13, 2024, 3:49 PM
  6.  */
  7.  
  8. #include <xc.h>
  9. #include "config.h"
  10. #include "LCD4Bits.h"
  11. #include "spi.h"
  12.  
  13. #define _XTAL_FREQ 8000000UL
  14.  
  15. void write_25AA010(uint8_t address, uint8_t data){
  16. /*Write Enable Command*/
  17. nCS=0;
  18. spi_send(0x06);
  19. nCS=1;
  20. /*Write Process*/
  21. nCS=0;
  22. spi_send(0x02);
  23. spi_send(address);
  24. spi_send(data);
  25. nCS=1;
  26. /*EEPROM Write Time*/
  27. __delay_ms(10);
  28. }
  29.  
  30. void write_text(uint8_t start, uint8_t *txt){
  31. uint8_t addr=0;
  32. while(*txt){
  33. write_25AA010(start+addr,*txt++);
  34. addr++;
  35. }
  36. }
  37.  
  38. uint8_t read_25AA010(uint8_t address){
  39. uint8_t data;
  40. nCS=0;
  41. spi_send(0x03);
  42. spi_send(address);
  43. data=spi_receive();
  44. nCS=1;
  45. return data;
  46. }
  47.  
  48. void main(void) {
  49. OSCCONbits.IRCF=7;
  50. lcdInit();
  51. spi_init();
  52.  
  53. uint8_t data;
  54. uint8_t txt[]="PIC16F887 24AA010A";
  55. write_text(0,txt);
  56. for(uint8_t i=0;i<sizeof(txt)-1;i++){
  57. data=read_25AA010(i);
  58. lcdData(data);
  59. }
  60.  
  61. PORTB=0;
  62. TRISB=0;
  63. ANSELH=0;
  64. while(1){
  65. for(uint8_t i=0;i<8;i++){
  66. write_25AA010(20+i,1<<i);
  67. PORTB=read_25AA010(20+i);
  68. __delay_ms(100);
  69. }
  70. __delay_ms(1000);
  71. for(int8_t i=7;i>=0;i--){
  72. write_25AA010(40-i,1<<i);
  73. PORTB=read_25AA010(40-i);
  74. __delay_ms(100);
  75. }
  76. __delay_ms(1000);
  77. }
  78. return;
  79. }
  80.  

I wrote an SPI driver that it's putted in one package.

The SPI.h header file:

  1. /*
  2.  * File: spi.h
  3.  * Author: Admin
  4.  *
  5.  * Created on February 9, 2024, 9:13 AM
  6.  */
  7.  
  8. #include <xc.h>
  9.  
  10. #define nCS RC2
  11.  
  12. void spi_init(void);
  13. void spi_send(uint8_t data);
  14. uint8_t spi_receive(void);
  15.  
  16.  

The SPI.c source file:

  1.  
  2. #include "spi.h"
  3.  
  4. void spi_init(void){
  5. /*SPI Mode Clock Low To High*/
  6. SSPCONbits.CKP=0;
  7. SSPSTATbits.CKE=1;
  8. SSPSTATbits.SMP=0;
  9. /*SPI Master Mode Clock = Fosc/4*/
  10. SSPCONbits.SSPM=2;
  11. /*Turn On The Module*/
  12. SSPCONbits.SSPEN=1;
  13. SSPSTATbits.BF=1;
  14. PORTC=0;
  15. TRISC=0;
  16. /*SPI SDI Pin Input*/
  17. TRISC4=1;
  18. }
  19.  
  20. void spi_send(uint8_t data){
  21. SSPSTATbits.BF==1;
  22. SSPBUF=data;
  23. while(SSPSTATbits.BF==0);
  24. SSPSTATbits.BF==1;
  25. }
  26.  
  27. uint8_t spi_receive(void){
  28. uint8_t data;
  29. spi_send(0x00);
  30. data=SSPBUF;
  31. return data;
  32. }

If you wonder about using the SPI communication interface of PIC16F887 you can see this post.

See Also:

  1. PIC16F887 Serial Peripheral Interface Example
  2. PIC16F887 SPI and MCP23S17 XC8 Example 
  3. PIC16F887 SPI MCP23S17 and Character LCD XC8 Example 
  4. PIC16F887 SPI MCP23S17 Character LCD and KeyPad XC8 Example 
  5. PIC16F887 SPI SN74HC165 LCD XC8 Example

Click here to download this example from GitHub.





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 (56) 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 (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) 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 (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)