Hi.

I need a code to write data to flash memory AM29F010B with a PIC16F877A.
I did read the data sheet of the memory and I understand that to write to the data I need to follow 4 cycles like this:

First cycle
write to address bus 0x555
write to data bus 0xAA

Second cycle
write to address bus 0x2AA
write to data bus 0x55

Third cycle
write to address bus 0x555
write to data bus 0xA0

Fourth cycle
write to the address bus the address value where I need to save the data
write to the data bus the value I need to save.

I tried to do that with my code but the memory is not saving any data, and I dont know if I am missing something in my code, or if I need to put some delay somewhere or what.

This is the code I have:

;................................................. .................................................. ...
;*-*-*-*-*; Configure the ports to write to the memory

BCF STATUS,RP1 ;SELECCIONA BANCO 1
BSF STATUS,RP0 ;SELECCIONA BANCO 1

MOVLW 0X07
MOVWF ADCON1
CLRF TRISC ;Set PORTC: b(0 - 3) Data. b(4 - 5) 7seg decoder. b6 e/d mem. b7 dont care
CLRF TRISD ;Set PORTD: b(0 - 1) Data. b(2 - 3) 7seg decoder. b(4 - 7) address
CLRF TRISE ;PORTE as output. b(1, 2) Memory data

BCF STATUS,RP1 ;SELECCIONA BANCO 1
BCF STATUS,RP0 ;SELECCIONA BANCO 1

;................................................. .................................................. ...
;*-*-*-*-*; CICLO I

MOVLW 0XAA ;This section puts a 555 in the address bus
MOVWF PORTB ;and a AA in the data bus

MOVFW PORTC
ANDLW B'00110000'
IORLW B'00000101'
MOVWF PORTC

MOVFW PORTD
ANDLW B'00001100'
IORLW B'01010001'
MOVWF PORTD

MOVFW PORTE
ANDLW B'00000001'
IORLW B'00000010'
MOVWF PORTE

BCF PORTE,0 ;Write enable so the memory takes the 555 and AA
BSF PORTE,0

;................................................. .................................................. ...
;*-*-*-*-*; CICLO II

MOVLW 0X55 ;This section puts a 2AA in the addresss bus
MOVWF PORTB ;and a 55 in the data bus

MOVFW PORTC
ANDLW B'00110000'
IORLW B'00001010'
MOVWF PORTC

MOVFW PORTD
ANDLW B'00001100'
IORLW B'10100010'
MOVWF PORTD

MOVFW PORTE
ANDLW B'00000001'
IORLW B'00000100'
MOVWF PORTE

BCF PORTE,0 ;Write enable so the memory takes the 2AA and 55
BSF PORTE,0

;................................................. .................................................. ...
;*-*-*-*-*; CICLO III

MOVLW 0XAA ;This section puts a 555 in the address bus
MOVWF PORTB ;and a A0 in the data bus

MOVFW PORTC
ANDLW B'00110000'
IORLW B'00000001'
MOVWF PORTC

MOVFW PORTD
ANDLW B'00001100'
IORLW B'01010000'
MOVWF PORTD

MOVFW PORTE
ANDLW B'00000001'
IORLW B'00000010'
MOVWF PORTE

BCF PORTE,0 ;Write enable so the memory takes the 555 and A0
BSF PORTE,0

;................................................. .................................................. ...
;*-*-*-*-*; CICLO IV

BSF PORTE,1 ;This section puts the address value in the address bus
BCF PORTE,2 ;and the data we want to save in the data bus
BSF PORTC,0
BCF PORTC,1 ;The data we want to save by now is 10101010 for testing
BSF PORTC,2
BCF PORTC,3
BSF PORTD,0
BCF PORTD,1

MOVFW PORTD
ANDLW B'00001100'
IORWF ALLD
MOVFW ALLD
MOVWF PORTD ;here is the high port of the address value

MOVFW ALLB
MOVWF PORTB ;here is the low port of the address value

BSF PORTD,2

BCF PORTE,0 ;write enable so the memory takes the values
BSF PORTE,0

;................................................. .................................................. ...
;*-*-*-*-*; Reconfigure the ports

BCF STATUS,RP1 ;SELECCIONA BANCO 1
BSF STATUS,RP0 ;SELECCIONA BANCO 1

MOVLW 0X07
MOVWF ADCON1
MOVLW 0X0F
MOVWF TRISC ;Set PORTC: b(0 - 3) Data. b(4 - 5) 7seg deco. b6 e/d mem
MOVLW 0X03
MOVWF TRISD ;Set PORTD: b(0 - 1) Data. b(2 - 3) 7seg deco. b(4 - 7) address
MOVLW 0X06
MOVWF TRISE ;PORTE as output. b(1, 2) Memory data

BCF STATUS,RP1 ;SELECCIONA BANCO 1
BCF STATUS,RP0 ;SELECCIONA BANCO 1

RETURN


Im pretty sure I am presenting the right values for address and data to the memory as I can mesure them, what Im not sure is if there is any delay neede or if Im sending the write enable to the memory in the correct sequence. I output enable in the memory is permantly high and the chip enable is permanently low so ir should be ready to save data.

Please, help. What do I need to change to this code?