Hi guys,
I'm using a 64k SRAM chip (23a640) with my PIC16f877A and I can't seem to get the bugger working properly. I'm also using an external EEPROM chip and that works flawlessly, so I though that using pretty much the same code to drive the SRAM would be a good idea, but seems like there's more to it than I thought.
Below is a SAVE/LOAD code for the SRAM chip, I'm using the same thing for EEPROM, the only difference being the enable/disable write code for the EEPROM chip.
The OUTPUT function is a short little thing:
SPI set up like this:
The PIC is hooked up to an LCD and I've used it to debug the code a bit, trying to write to a single address within the SRAM and read the value from it to display it on the screen. Seems like it's the SAVE function that fails, as occasionally I get something showing on the screen, which means I have to be putting something there in the first place. Any ideas what might be wrong? The chip is wired like in this tech note:
https://www.electro-tech-online.com/custompdfs/2010/02/01000a.pdf
the |CS pin is pulled up to 5V using a 10k. Again the same hook up is used on the EEPROM chip.
Any ideas would be welcome.
Regards,
dsc.
I'm using a 64k SRAM chip (23a640) with my PIC16f877A and I can't seem to get the bugger working properly. I'm also using an external EEPROM chip and that works flawlessly, so I though that using pretty much the same code to drive the SRAM would be a good idea, but seems like there's more to it than I thought.
Below is a SAVE/LOAD code for the SRAM chip, I'm using the same thing for EEPROM, the only difference being the enable/disable write code for the EEPROM chip.
Code:
SAVE_SRAM
;Send the write sequence (WRITE)
bcf PORTC, 6 ;clear the chip select (active)
movlw 0x02 ;load WRITE sequence (0000 0010 = WRITE)
movwf outbyte ;store in RAM location outbyte
call output ;call the SPI output routine
movlw 0x00 ;load high address byte
movwf outbyte ;store in RAM location outbyte
call output ;call the SPI output routine
movf addr,W ;move the address into w
movwf outbyte ;store in RAM location outbyte
call output ;call the SPI output routine
movf debug, W ;put the save value in W
movwf SSPBUF ;place data in send buffer
bsf STATUS, RP0 ;select bank 1
SR_loop1
btfss SSPSTAT,BF ;has data been received?
goto SR_loop1 ;loop if not received yet
bcf STATUS, RP0 ;select bank 0
bsf PORTC, 6 ;set negCS to 1 which means deselecting the chip when done
;Perform data polling (RDSR bit 0)
SRAM_poll
bcf PORTC, 6 ;clear the chip select (active)
movlw 0x05 ;load RDSR sequence (0000 0101 = RDSR)
movwf outbyte ;store in RAM location outbyte
call output ;read the data in status reg.
movlw 0x00 ;load up zero to send
movwf outbyte ;store in RAM location outbyte
call output ;read the data in status reg.
bsf PORTC, 6 ;set negCS to 1 which means deselecting the chip when done
btfsc rxdata,0 ;test the WIP bit in status reg.
goto SRAM_poll ;WIP set, loop until WIP clear
bsf PORTC, 6 ;set negCS to 1 which means deselecting the chip when done
incf debug, F
bsf PORTA, 2 ;testing
retlw 0x00
LOAD_SRAM
;Send read sequence (READ), read address 0x55
bcf PORTC, 6 ;clear the chip select (active)
movlw 0x03 ;load READ sequence (0000 0011 = READ)
movwf outbyte ;store in RAM location outbyte
call output ;call the SPI output routine
movlw 0x00 ;load high address byte
movwf outbyte ;store in RAM location outbyte
call output ;call the SPI output routine
movf addr,W ;move the address into w
movwf outbyte ;store in RAM location outbyte
call output ;call the SPI output routine
movlw 0x00 ;load high address byte
movwf outbyte ;store in RAM location outbyte
call output ;call output to read 1 byte
bsf PORTC, 6 ;set negCS to 1 which means deselecting the chip when done
movf rxdata, W
movwf test_spi ;copy rxdata to variable
bcf PORTA, 2 ;testing
retlw 0x00
The OUTPUT function is a short little thing:
Code:
output
movf outbyte,W ;move outbyte into w
movwf SSPBUF ;place data in send buffer
bsf STATUS, RP0 ;select bank 1
loop1
btfss SSPSTAT,BF ;has data been received?
goto loop1 ;loop if not received yet
bcf STATUS, RP0 ;select bank 0
movf SSPBUF,W ;empty the receive buffer
movwf rxdata ;put received byte into rxdata
retlw 0x00 ;return from subroutine
SPI set up like this:
Code:
Set_SPI
bsf PORTC, 7 ;set negCS to 1 which means deselecting the chip
bsf PORTC, 6 ;set negCS to 1 which means deselecting the chip
clrf SSPCON ;clear SSP control register
movlw 0x30 ;0011 0000 - Fosc/4, no collison, no overflow,
movwf SSPCON ;SSPI, idle state for clock is high level
bsf STATUS, RP0 ;select bank 1
clrf SSPSTAT ;clear SSPSTAT
movlw 0x80 ;1000 0000 - Input data sampled at end of data output time
movwf SSPSTAT
bcf STATUS, RP0 ;select bank 0
bsf PCLATH, 3
call SET_SRAM &0x7ff
bcf PCLATH, 3
clrf addr ;clear addr variable
The PIC is hooked up to an LCD and I've used it to debug the code a bit, trying to write to a single address within the SRAM and read the value from it to display it on the screen. Seems like it's the SAVE function that fails, as occasionally I get something showing on the screen, which means I have to be putting something there in the first place. Any ideas what might be wrong? The chip is wired like in this tech note:
https://www.electro-tech-online.com/custompdfs/2010/02/01000a.pdf
the |CS pin is pulled up to 5V using a 10k. Again the same hook up is used on the EEPROM chip.
Any ideas would be welcome.
Regards,
dsc.