how to buffer serial data on pic?


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Location
    LECCE - ITALY
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Hi Languer,
    do you have some sample of program to write byte byte on the follow memory by SPI? I ordered 23A256 (2 bank)
    I receive data at 57600 I have about 170 us of time to write data in memory
    thankyou


    Quote Originally Posted by languer View Post
    SRAM Options
    Microchip maxes out at 32 Kbytes, so you could try two of these (23A256 or 23K256). You would have to "bank select" between the two ICs using the CS lines.

    OnSemi also seems to max out at 32 Kbytes (N25S830HAS22I).

    You would most likely operate both these options in "Sequential/Burst Mode".

    FRAM Options
    RAMTRON has the FM25V05 which would allow 64 Kbytes. These do have a limit on the amount of write operations (since they do retain data after power is lost), although it is quite high.

  2. #2
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default pseudo code

    This is by no means complete code, but a starting point would be something like shown below...

    The SERIN/SEROUT are defined by you application.
    Code:
    getData:
    	'[SRAM BANK 0]
    
    	'BANK SELECT
    	SRAM1_CS = 0                'SRAM CHIP #1 ENABLED
    	SRAM2_CS = 1                'SRAM CHIP #2 DISABLED
    
    	'BANK WRITE ADDRESS
    	spi_data = WRITE_INSTR
    	WriteSPI
    	tx_spi_data = addr.HIGHBYTE
    	WriteSPI
    	tx_spi_data = addr.LOWHBYTE
    	WriteSPI
    
    	'GET SERIN INTO FIRST BANK
    	For i = 0 to DATA_LENGTH
    		SERIN tx_spi_data
    		WriteSPI
    	Next i
    
    	'[SRAM BANK 1]
    
    	'BANK SELECT
    	SRAM1_CS = 1                'SRAM CHIP #2 DISABLED
    	SRAM2_CS = 0                'SRAM CHIP #1 ENABLED
    
    	'BANK WRITE ADDRESS
    	tx_spi_data = WRITE_INST
    	WriteSPI
    	tx_spi_data = addr.HIGHBYTE
    	WriteSPI
    	tx_spi_data = addr.LOWHBYTE
    	WriteSPI
    
    	'GET SERIN INTO SECOND BANK
    	For i = 0 to DATA_LENGTH
    		SERIN tx_spi_data
    		WriteSPI
    	Next i
    
    
    sendData:
    	'[SRAM BANK 0]
    
    	'BANK SELECT
    	SRAM1_CS = 0                'SRAM CHIP #1 ENABLED
    	SRAM2_CS = 1                'SRAM CHIP #2 DISABLED
    
    	'BANK WRITE ADDRESS
    	spi_data = WRITE_INSTR
    	WriteSPI
    	tx_spi_data = addr.HIGHBYTE
    	WriteSPI
    	tx_spi_data = addr.LOWHBYTE
    	WriteSPI
    
    	'SEND SEROUT FROM FIRST BANK
    	For i = 0 to DATA_LENGTH
    		ReadSPI
    		SEROUT rx_spi_data
    	Next i
    
    	'[SRAM BANK 1]
    
    	'BANK SELECT
    	SRAM1_CS = 1                'SRAM CHIP #2 DISABLED
    	SRAM2_CS = 0                'SRAM CHIP #1 ENABLED
    
    	'BANK WRITE ADDRESS
    	spi_data = WRITE_INSTR
    	WriteSPI
    	tx_spi_data = addr.HIGHBYTE
    	WriteSPI
    	tx_spi_data = addr.LOWHBYTE
    	WriteSPI
    								                                  
    	'SEND SEROUT FROM SECOND BANK
    	For i = 0 to DATA_LENGTH
    		ReadSPI
    		SEROUT rx_spi_data
    	Next i
    Necessary defines (copied from PRSTEIN's code - http://www.picbasic.co.uk/forum/showthread.php?t=12766):
    Code:
    ' Definations for the SPI communication protocal 
    '------------------------------------------------------------------
    SRAM1_CS        VAR PORTC.0  ' SPI SRAM CHIP #1 CS PIN
    SRAM1_CS_TRIS   VAR TRISC.0  ' SPI SRAM CHIP #1 CS PIN DIRECTION
    SRAM2_CS        VAR PORTC.1  ' SPI SRAM CHIP #2 CS PIN
    SRAM2_CS_TRIS   VAR TRISC.1  ' SPI SRAM CHIP #2 CS PIN DIRECTION
    
    SCK            VAR PORTC.3  ' SPI CLOCK
    SCK_TRIS       VAR TRISC.3  ' SPI CLOCK PIN DIRECTION CONTROL
    SDI            VAR PORTC.4  ' SPI DATA IN
    SDI_TRIS       VAR TRISC.4  ' SPI DATA IN PIN DIRECTION 
    SDO            VAR PORTC.5  ' SPI DATA OUT PIN
    SDO_TRIS       VAR TRISC.5  ' SPI DATA OUT PIN
    
    WCOL           VAR SSPCON.7 'SSP WRITE COLLISION
    SSPEN          VAR SSPCON.5 'SSP ENABLE
    SSPIF          VAR PIR1.3   'SSP INTERRUPT FLAG
    '---------------------------------------------------------------------
    
    '-------------SPI PORT REGISTERS SETUP----------------------------------------
    SSPSTAT = %01000000         ' SAMPLE AT THE MIDDLE OF DATA OUTPUT TIME, TRANSMIT ON IDLE RISING EDGE OF SCK
    SSPCON = %00100000          ' SPI MASTER MODE, CLOCK=Fosc/4  ENABLE HARDWARE SPI PORT
    SSPEN = 1                   ' ENABLE HARDWARE SPI PORT
    '-------------------------------------------------------------------------------
    
    '-------------INITIALIZATION--------------------------------------------------
    SRAM1_CS = 1                'SRAM CHIP #1 DISABLED
    SRAM2_CS = 1                'SRAM CHIP #2 DISABLED
    SRAM1_CS_TRIS = 0           'OUTPUT 
    SRAM2_CS_TRIS = 0           'OUTPUT 
    SDO = 1                     'START SPI PIN WITH HIGH
    SDO_TRIS = 0                'OUTPUT 
    SDI_TRIS = 1                'INPUT 
    
    
    '-------------VARIABLES---------------------------------------------------------
    i VAR WORD
    addr VAR WORD
    tx_spi_data VAR BYTE
    rx_spi_data VAR BYTE
    DATA_LENGTH CON 30716
    '-------------------------------------------------------------------------------
    The WriteSPI and ReadSPI functions below can be GOSUBs, or INLINED (i.e. pasted directly where the functions appear) for faster performance.
    Code:
    WriteSPI:
    	SSPIF = 0                 'CLEAR INTERRUP FLAG
    	WCOL = 0                  'CLEAR COLLISION BIT BEFORE WRITING TO SPI
    	SSPBUF = tx_spi_data      'SEND BYTE
    	WHILE(!SSPIF)             'WAIT FOR BYTE TO BE CLOCKED-OUT/CLOCKED-IN - THIS HAPPENS SIMULTANEOUSLY
    	WEND
    	rx_spi_data = SSPBUF      'STORE RECEIVED DATA
    	SSPIF = 0                 'CLEAR INTERRUP FLAG
    RETURN
    
    ReadSPI:
    	SSPIF = 0                 'CLEAR INTERRUP FLAG
    	WCOL = 0                  'CLEAR COLLISION BIT BEFORE WRITING TO SPI
    	SSPBUF = 0                'SEND/CLOCK-OUT DUMMY BYTE TO CLOCK-IN RECEIVED BYTE
    	WHILE(!SSPIF)             'WAIT FOR 8-BITS TO BE CLOCKED-OUT/CLOCKED-IN - THIS HAPPENS SIMULTANEOUSLY
    	WEND
    	rx_spi_data = SSPBUF      'STORE RECEIVED DATA
    	SSPIF = 0                 'CLEAR INTERRUP FLAG
    RETURN
    Note that there is quite a bit of optimization possible. In the places where you will be accessing the RS232 port right after the SPI commands; you could remove the wait states on the SPI commands (i.e. WHILE-WEND). All you need to make sure is that you allow sufficient time for the 8-bits to clock out; which the RS232 processing takes care of.

    Also note that during this whole process you are buffering data into SRAM (or out of SRAM) sequentially (e.g. you receive an RS232 byte and store it, and again). This means that if you have qualifiers required to be read (or transmitted) at the beginning or end of your packet, then you have to handle this separately (e.g. receive-process-validate first, then enter the buffering routine).

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts