sample code for M25P32


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2006
    Posts
    60

    Default sample code for M25P32

    Hello

    Have anyone sucessfull used the M25P32 serial memory device?
    Perhaps a sample code to share?

    Thank You

    Best regards

    pedro

  2. #2


    Did you find this post helpful? Yes | No

    Default Code snip for M25P32

    Hi Pedro,
    I have used the M25P32 with good success. Here are some code snips as pointers.

    In essence I build an array of 4 x 64 characters and then burst them into the memory. I do this 4 times per record so I store 1024 bytes each logging sample period.

    Heavily cut to fit the 10k character limit but I am sure you can nut it out.

    HTH
    Brian



    DEFINE osc 4 '20
    'DEFINE NO_CLRWDT 1
    DEFINE Loader_Used 1
    DEFINE DEBUG_REG PORTC 'Debug pin port
    DEFINE DEBUG_BIT 6 'Debug pin bit
    DEFINE DEBUG_BAUD 19200 'Debug baud rate
    DEFINE DEBUG_MODE 0 'Debug mode: 0 = True, 1 = Inverted
    'DEFINE DEBUG_PACING 100 'Debug character pacing in us

    '****************** Variables **************************
    ' RAM Assignments and Variables
    ' -----------------------------
    MEMchip var byte ' either 0 or 1 to
    ADval var word 'Create adval to store result
    DB var byte ' Single Data Byte to be passed to memory
    DB2 var byte
    DS0 var byte[64] ' String of data bytes to be passed to memory
    DS1 var byte[64] ' String of data bytes to be passed to memory
    DS2 var byte[64] ' String of data bytes to be passed to memory
    DS3 var byte[64] ' String of data bytes to be passed to memory
    UZ var word
    Addr0 var byte ' LSB of 24 bit address
    Addr1 var byte
    Addr2 var byte ' MSB of 24 bit address

    '*************************** Hardware assigns

    'PortD
    CLK var portd.0 ' Clock to both M25P32
    MD var portd.1 ' Data to both M25P32
    MQ var portd.2 ' Data from both M25P32
    MS0 var portd.3 ' Chip 0 Select - active LOW
    MS1 var portd.4 ' Chip 1 Select - active LOW - idle HIGH
    SpareD5 var portd.5 ' spare
    SpareD6 var portd.6 ' spare
    SpareD7 var portd.7 ' spare
    TRISD = %00000100
    PortD = %00011110


    '**************************** Subroutines *****************************
    goto endsubroutines ' jump subroutines at startup

    WakeUpMemory: 'M25P32
    ' Release from deep power down
    TRISA = %00000001
    TRISB = %11101111 'SDA, SCL, Thermistor, MicPwr, nc, nc, nc, nc
    TRISC = %10000000 'Rx232, Tx232 & LED, MW0, MS1, MD, MC, MemPwr1, MemPwr0
    TRISD = %11100001 'SDA2, SCL2, Alarm, MW1, MS0, MH1, MH0, MQ
    ADCON0 = %11000001 'ADC enabled
    ' ADCON1 = %10000010 ' 10 bit resolution - use ADRESH & ADRESL
    ADCON1 = %00000010 ' 8 bit resolution IF ADRESH is used as adVal
    high clk
    high md
    input mq
    high sda
    high scl
    low micpwr
    high tx232
    high ms0
    high ms1
    ' Mem 0 - Release from Deep Power Down
    low ms0
    high ms1
    shiftout md, clk, 1, [$AB, $AA, $AA, $AA ] 'wake needs 3 dummy bytes
    shiftin mq, clk, 0, [db] '
    high ms0
    high ms1
    pause 1
    ' Mem 1 - Release from Deep Power Down
    high ms0
    low ms1
    shiftout md, clk, 1, [$AB, $AA, $AA, $AA ] 'wake needs 3 dummy bytes
    shiftin mq, clk, 0, [db] '
    high ms0
    high ms1
    pause 1
    return


    '****************** end of subroutines ****************************
    EndSubRoutines:
    '********************** Initialise ********************************

    ''ZZZZZZZZZZZZZ Run from here ZZZZZZZZZZZZZZZZZZ
    ReadActionFlag:
    read 0,af
    if af = 0 then erasemem ' followed by log data AF = 2
    if af = 1 then memdump ' followed by end
    if af = 2 then logdata ' followed by dump memory AF = 1
    if af = 3 then waitfornewcode ' followed by end

    '********************************* Erase Memory ***************************
    EraseMem:
    gosub wakeupmemory 'set up TRIS etc & release from deep power down

    'Remove write protection by writing to Status Register
    low ms0
    low ms1
    'Issue WRite ENable before status register write
    shiftout Md, clk, 1, [$06 ] ' WREN issued to BOTH chips
    high ms0
    high ms1
    pauseus 10
    ' Write to both Mem0 & Mem1 Status Register
    low ms0
    low ms1
    shiftout Md, clk, 1, [$01,$00 ]' Write Status Reg, clear protection bits
    high ms0
    high ms1
    pause 16 ' tw is 15

    ' Issue Bulk erase command
    low ms0
    low ms1
    'need a WRite ENable before the bulk erase command
    shiftout Md, clk, 1, [$06 ] ' WREN issued to BOTH chips
    high ms0
    high ms1
    pauseus 10
    low ms0
    low ms1
    shiftout Md, clk, 1, [$C7]' Bulk Erase command to BOTH chips
    high ms0
    high ms1
    c = 0
    EraseCheck: 'loop at 1/sec until erase complete
    c = c + 1
    low ms0 'select Mem0
    high ms1
    shiftout md, clk, 1, [$05] 'Read Status Register
    shiftin mq, clk, 0, [a]
    a = a & %00000001 ' extract bit 0
    high ms0
    high ms1
    pauseus 10
    high ms0
    low ms1 'select Mem1
    shiftout md, clk, 1, [$05]
    shiftin mq, clk, 0, [b]
    b = b & %00000001
    high ms0
    high ms1
    pause 1000 'nominal 1 second loop
    debug #c, ",", #a, ",", #b ," "
    if c//15 = 0 then debug $0D, $0A
    if c > 250 then end
    if (a = 0) or (b = 0) and c < 15 then
    debug $0D, $0A, "Premature WIP flag clear - retrying", $0D, $0A
    pause 1000
    goto erasemem
    endif
    if (a = 1) or (b = 1 ) then erasecheck 'not finished yet
    debug $0D, $0A, $0D, $0A, " Erase times VALID - PASS", $0D, $0A

    ''*************** Memory Dump ***********************
    MemDump:
    addr0 = 0 'LSB byte address - 256 entries
    addr1 = 0 ' page address - 256 entries
    addr2 = 0 'MSB sector address - 64 entries in M25P32 128 in M25P64
    high ms0
    high ms1
    for memchip = 0 to 1 ' do it for both memory chips
    for addr2 = 0 to 63 ' 64 sectors per chip
    for addr1 = 0 to 255 ' 256 pages per sector each with 256 bytes
    ' Wake from deep power down
    if memchip = 0 then
    low ms0
    high ms1
    pauseus 10
    else
    high ms0
    low ms1
    pauseus 10
    endif
    shiftout Md, clk, 1, [$AB, $AA, $AA, $AA ] 'wake from deep power down
    shiftin mq, clk, 0, [db] ' DB is chip identifier - discarded
    high ms0
    high ms1
    pause 1
    ' Select Memory Dump mode
    if memchip = 0 then
    low ms0
    high ms1
    pause 1
    else
    low ms1
    high ms0
    pause 1
    endif
    shiftout Md, clk, 1, [$03, addr2, addr1, addr0 ] ' Select MemDump mode
    xxxxxxxx snip xxxxxxxxxxxx

    RecordingBlock:
    ' WakeUpMemory already called so should be ready to go
    ' select the correct memory chip
    if addr2 < 64 then
    low ms0
    high ms1
    else
    high ms0
    low ms1
    endif
    'Prepare to clear write protection bits
    shiftout Md, clk, 1, [$06 ] ' WREN issued
    high ms0
    high ms1
    if addr2 < 64 then
    low ms0
    high ms1
    else
    high ms0
    low ms1
    endif
    shiftout Md, clk, 1, [$01,$00 ]' Write Status Reg, clear protection bits
    high ms0
    high ms1
    pause 15 ' this is tw from datasheet

    ' Write a 4x1024 character blocks with data from various sources
    CollectDataForPage0:
    xxxxxxxxxxxxxxxxxxxxx snip xxxxxxxxxxxxxxxxx
    'Now write the 256 bytes just collected to memory
    'Program the page
    'Prepare memory to accept data - issue WREN
    if addr2 < 64 then
    low ms0
    high ms1
    else
    high ms0
    low ms1
    endif
    shiftout Md, clk, 1, [$06 ] ' issue WREN
    ms0 = 1 ' deselect both memory chips at end
    ms1 = 1

    if addr2 < 64 then
    MS0 = 0 ' select memory 0
    MS1 = 1
    else
    ms0 = 1
    ms1 = 0
    endif
    shiftout md, clk, 1, [$02, addr2, addr1, addr0] ' issue Page Program command
    ' Write the first full page of 256 samples including time & date
    for a = 0 to 63
    shiftout md, clk, 1, [ds0[a]] ' write the date, time & heart sample
    next a
    for a = 0 to 63
    shiftout md, clk, 1, [ds1[a]] ' write the heart sample
    next a
    for a = 0 to 63
    shiftout md, clk, 1, [ds2[a]] ' write the heart sample
    next a
    for a = 0 to 63
    shiftout md, clk, 1, [ds3[a]] ' write the heart sample
    next a
    MS0 = 1 'deselect both memory chips
    MS1 = 1
    ' Page Program now running - increment pointers and
    ' buffer some heart samples into RAM
    addr0 = 0 'byte address - set zero for justin
    addr1 = addr1 + 1 'increment page address
    if addr1 = 0 then addr2 = addr2 + 1 'increment sector address
    'Stop completely at end of memory
    if addr2 > 127 then end ' avoids overwriting old data

Similar Threads

  1. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. 18F8722 sample code
    By George in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th June 2008, 13:42
  5. DS2438 onewire code sample
    By Captain Stern in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st January 2007, 02:47

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